Skip to content

Commit 3112b4d

Browse files
committed
Absorb schema loading/parsing into Node API
1 parent c0458ac commit 3112b4d

13 files changed

+350
-358
lines changed

README.md

+15-8
Original file line numberDiff line numberDiff line change
@@ -122,19 +122,26 @@ npm i --save-dev openapi-typescript
122122
```
123123

124124
```js
125-
const { readFileSync } = require("fs");
125+
const fs = require("fs");
126126
const openapiTS = require("openapi-typescript").default;
127127

128-
const input = JSON.parse(readFileSync("spec.json", "utf8")); // Input can be any JS object (OpenAPI format)
129-
const output = openapiTS(input); // Outputs TypeScript defs as a string (to be parsed, or written to a file)
128+
// option 1: load JS object, write to local file
129+
const schema = await fs.promises.readFile("spec.json", "utf8") // must be OpenAPI JSON
130+
const output = await openapiTS(JSON.parse(schema));
131+
132+
// option 2 (new in v3.3): load local path
133+
const localPath = path.join(__dirname, 'spec.yaml'); // may be YAML or JSON format
134+
const output = await openapiTS(localPath);
135+
136+
// option 3 (new in v3.3): load remote URL
137+
const output = await openapiTS('https://myurl.com/v1/openapi.yaml');
130138
```
131139

132-
The Node API is a bit more flexible: it will only take a JS object as input (OpenAPI format), and return a string of TS
133-
definitions. This lets you pull from any source (a Swagger server, local files, etc.), and similarly lets you parse,
134-
post-process, and save the output anywhere.
140+
The Node API may be useful if dealing with dynamically-created schemas, or you’re using within context of a larger application. It
141+
142+
⚠️ As of `v3.3`, this is an async function.
135143

136-
If your specs are in YAML, you’ll have to convert them to JS objects using a library such as [js-yaml][js-yaml]. If
137-
you’re batching large folders of specs, [glob][glob] may also come in handy.
144+
It’s important to note that options 2 and 3 are triggered by passing in a `string` rather than an `object`.
138145

139146
#### Custom Formatter
140147

bin/cli.js

+10-14
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ const { bold, green, red } = require("kleur");
55
const path = require("path");
66
const meow = require("meow");
77
const { default: openapiTS } = require("../dist/cjs/index.js");
8-
const { loadSpec } = require("./loaders");
98

109
const cli = meow(
1110
`Usage
@@ -58,17 +57,17 @@ async function main() {
5857
let output = "FILE"; // FILE or STDOUT
5958
const pathToSpec = cli.input[0];
6059

61-
// 0. setup
6260
if (!cli.flags.output) {
6361
output = "STDOUT"; // if --output not specified, fall back to stdout
6462
}
6563
if (output === "FILE") {
66-
console.info(bold(`✨ openapi-typescript ${require("../package.json").version}`)); // only log if we’re NOT writing to stdout
64+
console.info(bold(`✨ openapi-typescript ${require("../package.json").version}`)); // don’t log anything to console!
6765
}
6866
if (cli.flags.rawSchema && !cli.flags.version) {
6967
throw new Error(`--raw-schema requires --version flag`);
7068
}
7169

70+
<<<<<<< HEAD
7271
// 1. input
7372
let spec = undefined;
7473
try {
@@ -85,34 +84,31 @@ async function main() {
8584
const result = openapiTS(spec, {
8685
auth: cli.flags.auth,
8786
additionalProperties: cli.flags.additionalProperties,
87+
=======
88+
const result = await openapiTS(pathToSpec, {
89+
auth: cli.flags.auth,
90+
silent: output === "STDOUT",
91+
>>>>>>> 3ea8d8f (Absorb schema loading/parsing into Node API)
8892
immutableTypes: cli.flags.immutableTypes,
8993
prettierConfig: cli.flags.prettierConfig,
9094
rawSchema: cli.flags.rawSchema,
9195
version: cli.flags.version,
9296
});
9397

94-
// 3. output
9598
if (output === "FILE") {
9699
// output option 1: file
97100
const outputFile = path.resolve(process.cwd(), cli.flags.output);
98101

99-
// recursively create parent directories if they don’t exist
100-
const parentDirs = cli.flags.output.split(path.sep);
101-
for (var i = 1; i < parentDirs.length; i++) {
102-
const dir = path.resolve(process.cwd(), ...parentDirs.slice(0, i));
103-
if (!fs.existsSync(dir)) {
104-
fs.mkdirSync(dir);
105-
}
106-
}
107-
108-
fs.writeFileSync(outputFile, result, "utf8");
102+
await fs.promises.mkdir(path.dirname(outputFile), { recursive: true });
103+
await fs.promises.writeFile(outputFile, result, "utf8");
109104

110105
const timeEnd = process.hrtime(timeStart);
111106
const time = timeEnd[0] + Math.round(timeEnd[1] / 1e6);
112107
console.log(green(`🚀 ${pathToSpec} -> ${bold(cli.flags.output)} [${time}ms]`));
113108
} else {
114109
// output option 2: stdout
115110
process.stdout.write(result);
111+
// (still) don’t log anything to console!
116112
}
117113

118114
return result;

bin/loaders/index.js

-69
This file was deleted.

bin/loaders/loadFromFs.js

-14
This file was deleted.

bin/loaders/loadFromHttp.js

-57
This file was deleted.

0 commit comments

Comments
 (0)