Skip to content

Commit e78959e

Browse files
authored
Merge branch 'main' into pr/pass-cache-in-context
2 parents 826616c + 72acb8d commit e78959e

23 files changed

+2174
-4086
lines changed

.all-contributorsrc

+11
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,17 @@
402402
"contributions": [
403403
"code"
404404
]
405+
},
406+
{
407+
"login": "ericzorn93",
408+
"name": "Eric Zorn",
409+
"avatar_url": "https://avatars.githubusercontent.com/u/22532542?v=4",
410+
"profile": "http://www.zornwebdev.com",
411+
"contributions": [
412+
"code",
413+
"test",
414+
"doc"
415+
]
405416
}
406417
],
407418
"contributorsPerLine": 7,

.npmrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
engine-strict=true

CONTRIBUTING.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ It’s also recommended you have [ESLint][eslint] installed and set up correctly
2020
> ```
2121
> git remote add upstream https://github.com/drwpow/openapi-typescript.git
2222
> git fetch upstream
23-
> git branch --set-upstream-to=upstream/master master
23+
> git branch --set-upstream-to=upstream/main main
2424
> ```
2525
>
2626
> This will add the original repository as a "remote" called "upstream," Then fetch the git information from that

README.md

+14-10
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[![codecov](https://codecov.io/gh/drwpow/openapi-typescript/branch/main/graph/badge.svg)](https://codecov.io/gh/drwpow/openapi-typescript)
44

55
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
6-
[![All Contributors](https://img.shields.io/badge/all_contributors-38-orange.svg?style=flat-square)](#contributors-)
6+
[![All Contributors](https://img.shields.io/badge/all_contributors-39-orange.svg?style=flat-square)](#contributors-)
77
<!-- ALL-CONTRIBUTORS-BADGE:END -->
88

99
# 📘️ openapi-typescript
@@ -98,15 +98,18 @@ npx openapi-typescript schema.yaml
9898

9999
#### CLI Options
100100

101-
| Option | Alias | Default | Description |
102-
| :----------------------------- | :---- | :------: | :------------------------------------------------------------------------------------------------------ |
103-
| `--output [location]` | `-o` | (stdout) | Where should the output file be saved? |
104-
| `--auth [token]` | | | (optional) Provide an auth token to be passed along in the request (only if accessing a private schema) |
105-
| `--immutable-types` | `-it` | `false` | (optional) Generates immutable types (readonly properties and readonly array) |
106-
| `--additional-properties` | `-ap` | `false` | (optional) Allow arbitrary properties for all schema objects without `additionalProperties: false` |
107-
| `--default-non-nullable` | | `false` | (optional) Treat schema objects with default values as non-nullable |
108-
| `--prettier-config [location]` | `-c` | | (optional) Path to your custom Prettier configuration for output |
109-
| `--raw-schema` | | `false` | Generate TS types from partial schema (e.g. having `components.schema` at the top level) |
101+
| Option | Alias | Default | Description |
102+
| :----------------------------- | :---- | :------: | :-------------------------------------------------------------------------------------------------------------------------------------- |
103+
| `--output [location]` | `-o` | (stdout) | Where should the output file be saved? |
104+
| `--auth [token]` | | | (optional) Provide an auth token to be passed along in the request (only if accessing a private schema) |
105+
| `--immutable-types` | `-it` | `false` | (optional) Generates immutable types (readonly properties and readonly array) |
106+
| `--additional-properties` | `-ap` | `false` | (optional) Allow arbitrary properties for all schema objects without `additionalProperties: false` |
107+
| `--default-non-nullable` | | `false` | (optional) Treat schema objects with default values as non-nullable |
108+
| `--prettier-config [location]` | `-c` | | (optional) Path to your custom Prettier configuration for output |
109+
| `--raw-schema` | | `false` | Generate TS types from partial schema (e.g. having `components.schema` at the top level) |
110+
| `--httpMethod` | `-m` | `GET` | (optional) Provide the HTTP Verb/Method for fetching a schema from a remote URL |
111+
| `--headersObject` | `-h` | | (optional) Provide a JSON object as string of HTTP headers for remote schema request. This will take priority over `--header` |
112+
| `--header` | `-x` | | (optional) Provide an array of or singular headers as an alternative to a JSON object. Each header must follow the `key: value` pattern |
110113

111114
### 🐢 Node
112115

@@ -243,6 +246,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
243246
<td align="center"><a href="https://github.com/sharmarajdaksh"><img src="https://avatars.githubusercontent.com/u/33689528?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Dakshraj Sharma</b></sub></a><br /><a href="https://github.com/drwpow/openapi-typescript/commits?author=sharmarajdaksh" title="Code">💻</a></td>
244247
<td align="center"><a href="https://github.com/shuluster"><img src="https://avatars.githubusercontent.com/u/1707910?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Shaosu Liu</b></sub></a><br /><a href="https://github.com/drwpow/openapi-typescript/commits?author=shuluster" title="Code">💻</a></td>
245248
<td align="center"><a href="https://vytenis.kuciauskas.lt"><img src="https://avatars.githubusercontent.com/u/468006?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Vytenis</b></sub></a><br /><a href="https://github.com/drwpow/openapi-typescript/commits?author=FDiskas" title="Code">💻</a></td>
249+
<td align="center"><a href="http://www.zornwebdev.com"><img src="https://avatars.githubusercontent.com/u/22532542?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Eric Zorn</b></sub></a><br /><a href="https://github.com/drwpow/openapi-typescript/commits?author=ericzorn93" title="Code">💻</a> <a href="https://github.com/drwpow/openapi-typescript/commits?author=ericzorn93" title="Tests">⚠️</a> <a href="https://github.com/drwpow/openapi-typescript/commits?author=ericzorn93" title="Documentation">📖</a></td>
246250
</tr>
247251
</table>
248252

bin/cli.js

+41-6
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
const fs = require("fs");
44
const { bold, green, red } = require("kleur");
5-
const path = require("path");
65
const meow = require("meow");
6+
const path = require("path");
77
const glob = require("tiny-glob");
88
const { default: openapiTS } = require("../dist/cjs/index.js");
99

@@ -15,6 +15,9 @@ Options
1515
--help display this
1616
--output, -o Specify output file (default: stdout)
1717
--auth (optional) Provide an authentication token for private URL
18+
--headersObject, -h (optional) Provide a JSON object as string of HTTP headers for remote schema request
19+
--header, -x (optional) Provide an array of or singular headers as an alternative to a JSON object. Each header must follow the key: value pattern
20+
--httpMethod, -m (optional) Provide the HTTP Verb/Method for fetching a schema from a remote URL
1821
--immutable-types, -it (optional) Generates immutable types (readonly properties and readonly array)
1922
--additional-properties, -ap (optional) Allow arbitrary properties for all schema objects without "additionalProperties: false"
2023
--default-non-nullable (optional) If a schema object has a default value set, don’t mark it as nullable
@@ -31,6 +34,20 @@ Options
3134
auth: {
3235
type: "string",
3336
},
37+
headersObject: {
38+
type: "string",
39+
alias: "h",
40+
},
41+
header: {
42+
type: "string",
43+
alias: "x",
44+
isMultiple: true,
45+
},
46+
httpMethod: {
47+
type: "string",
48+
alias: "m",
49+
default: "GET",
50+
},
3451
immutableTypes: {
3552
type: "boolean",
3653
alias: "it",
@@ -69,6 +86,22 @@ function errorAndExit(errorMessage) {
6986
async function generateSchema(pathToSpec) {
7087
const output = cli.flags.output ? OUTPUT_FILE : OUTPUT_STDOUT; // FILE or STDOUT
7188

89+
// Parse incoming headers from CLI flags
90+
let httpHeaders = {};
91+
// prefer --headersObject if specified
92+
if (cli.flags.headersObject) {
93+
httpHeaders = JSON.parse(cli.flags.headersObject); // note: this will generate a recognizable error for the user to act on
94+
}
95+
// otherwise, parse --header
96+
else if (Array.isArray(cli.flags.header)) {
97+
cli.flags.header.forEach((header) => {
98+
const firstColon = header.indexOf(":");
99+
const k = header.substring(0, firstColon).trim();
100+
const v = header.substring(firstColon + 1).trim();
101+
httpHeaders[k] = v;
102+
});
103+
}
104+
72105
// generate schema
73106
const result = await openapiTS(pathToSpec, {
74107
additionalProperties: cli.flags.additionalProperties,
@@ -79,22 +112,24 @@ async function generateSchema(pathToSpec) {
79112
rawSchema: cli.flags.rawSchema,
80113
silent: output === OUTPUT_STDOUT,
81114
version: cli.flags.version,
115+
httpHeaders,
116+
httpMethod: cli.flags.httpMethod,
82117
});
83118

84119
// output
85120
if (output === OUTPUT_FILE) {
86-
let outputFile = path.resolve(process.cwd(), cli.flags.output); // note: may be directory
87-
const isDir = fs.existsSync(outputFile) && fs.lstatSync(outputFile).isDirectory();
121+
let outputFilePath = path.resolve(process.cwd(), cli.flags.output); // note: may be directory
122+
const isDir = fs.existsSync(outputFilePath) && fs.lstatSync(outputFilePath).isDirectory();
88123
if (isDir) {
89124
const filename = pathToSpec.replace(new RegExp(`${path.extname(pathToSpec)}$`), ".ts");
90-
outputFile = path.join(outputFile, filename);
125+
outputFilePath = path.join(outputFilePath, filename);
91126
}
92127

93-
await fs.promises.writeFile(outputFile, result, "utf8");
128+
await fs.promises.writeFile(outputFilePath, result, "utf8");
94129

95130
const timeEnd = process.hrtime(timeStart);
96131
const time = timeEnd[0] + Math.round(timeEnd[1] / 1e6);
97-
console.log(green(`🚀 ${pathToSpec} -> ${bold(outputFile)} [${time}ms]`));
132+
console.log(green(`🚀 ${pathToSpec} -> ${bold(outputFilePath)} [${time}ms]`));
98133
} else {
99134
process.stdout.write(result);
100135
// if stdout, (still) don’t log anything to console!

0 commit comments

Comments
 (0)