Skip to content

Commit 8c2ada5

Browse files
committed
Absorb schema loading/parsing into Node API
1 parent fd37dd1 commit 8c2ada5

13 files changed

+336
-251
lines changed

README.md

+15-8
Original file line numberDiff line numberDiff line change
@@ -106,19 +106,26 @@ npm i --save-dev openapi-typescript
106106
```
107107

108108
```js
109-
const { readFileSync } = require("fs");
109+
const fs = require("fs");
110110
const openapiTS = require("openapi-typescript").default;
111111

112-
const input = JSON.parse(readFileSync("spec.json", "utf8")); // Input can be any JS object (OpenAPI format)
113-
const output = openapiTS(input); // Outputs TypeScript defs as a string (to be parsed, or written to a file)
112+
// option 1: load JS object, write to local file
113+
const schema = await fs.promises.readFile("spec.json", "utf8") // must be OpenAPI JSON
114+
const output = await openapiTS(JSON.parse(schema));
115+
116+
// option 2 (new in v3.3): load local path
117+
const localPath = path.join(__dirname, 'spec.yaml'); // may be YAML or JSON format
118+
const output = await openapiTS(localPath);
119+
120+
// option 3 (new in v3.3): load remote URL
121+
const output = await openapiTS('https://myurl.com/v1/openapi.yaml');
114122
```
115123

116-
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
117-
definitions. This lets you pull from any source (a Swagger server, local files, etc.), and similarly lets you parse,
118-
post-process, and save the output anywhere.
124+
The Node API may be useful if dealing with dynamically-created schemas, or you’re using within context of a larger application. It
125+
126+
⚠️ As of `v3.3`, this is an async function.
119127

120-
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
121-
you’re batching large folders of specs, [glob][glob] may also come in handy.
128+
It’s important to note that options 2 and 3 are triggered by passing in a `string` rather than an `object`.
122129

123130
#### Custom Formatter
124131

bin/cli.js

+12-11
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ const path = require("path");
66
const meow = require("meow");
77
const glob = require("tiny-glob");
88
const { default: openapiTS } = require("../dist/cjs/index.js");
9-
const { loadSpec } = require("./loaders");
109

1110
const cli = meow(
1211
`Usage
@@ -71,20 +70,21 @@ async function generateSchema(pathToSpec) {
7170
const output = cli.flags.output ? OUTPUT_FILE : OUTPUT_STDOUT; // FILE or STDOUT
7271

7372
// load spec
74-
let spec = undefined;
75-
try {
76-
spec = await loadSpec(pathToSpec, {
77-
auth: cli.flags.auth,
78-
log: output !== OUTPUT_STDOUT,
79-
});
80-
} catch (err) {
81-
errorAndExit(`❌ ${err}`);
73+
if (!cli.flags.output) {
74+
output = "STDOUT"; // if --output not specified, fall back to stdout
75+
}
76+
if (output === "FILE") {
77+
console.info(bold(`✨ openapi-typescript ${require("../package.json").version}`)); // don’t log anything to console!
78+
}
79+
if (cli.flags.rawSchema && !cli.flags.version) {
80+
throw new Error(`--raw-schema requires --version flag`);
8281
}
8382

8483
// generate schema
85-
const result = openapiTS(spec, {
84+
const result = await openapiTS(pathToSpec, {
8685
auth: cli.flags.auth,
8786
additionalProperties: cli.flags.additionalProperties,
87+
silent: output === "STDOUT",
8888
immutableTypes: cli.flags.immutableTypes,
8989
defaultNonNullable: cli.flags.defaultNonNullable,
9090
prettierConfig: cli.flags.prettierConfig,
@@ -108,6 +108,7 @@ async function generateSchema(pathToSpec) {
108108
console.log(green(`🚀 ${pathToSpec} -> ${bold(outputFile)} [${time}ms]`));
109109
} else {
110110
process.stdout.write(result);
111+
// (still) don’t log anything to console!
111112
}
112113

113114
return result;
@@ -148,7 +149,7 @@ async function main() {
148149
errorAndExit(`❌ Expected directory for --output if using glob patterns. Received "${cli.flags.output}".`);
149150
}
150151

151-
// generate schema(s)
152+
// generate schema(s) in parallel
152153
await Promise.all(
153154
inputSpecPaths.map(async (specPath) => {
154155
if (cli.flags.output !== "." && output === OUTPUT_FILE) {

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)