Skip to content

Commit 4891ae7

Browse files
committed
Load remote schemas
1 parent 3112b4d commit 4891ae7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+5476
-6279
lines changed

README.md

+12-9
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,15 @@
1010

1111
🚀 Convert [OpenAPI 3.0][openapi3] and [2.0 (Swagger)][openapi2] schemas to TypeScript interfaces using Node.js.
1212

13-
💅 The output is prettified with [Prettier][prettier] (and can be customized!).
13+
**Features**
1414

15-
👉 Works for both local and remote resources (filesystem and HTTP).
15+
- Convert [Open API 3.x][openapi3] and [Swagger 2.x][openapi2] to TypeScript types
16+
- Load schemas either from local `.yaml` or `.json` files, or from a remote URL (simple authentication supported with the `--auth` flag)
17+
- Supports remote `$ref`s using [json-schema-ref-parser][json-schema-ref-parser]
18+
- Formats output using [Prettier][prettier]
19+
- Uses the latest TypeScript 4.0 syntax
1620

17-
View examples:
21+
**Examples**
1822

1923
- [Stripe, OpenAPI 2.0](./examples/stripe-openapi2.ts)
2024
- [Stripe, OpenAPI 3.0](./examples/stripe-openapi3.ts)
@@ -125,24 +129,22 @@ npm i --save-dev openapi-typescript
125129
const fs = require("fs");
126130
const openapiTS = require("openapi-typescript").default;
127131

128-
// option 1: load JS object, write to local file
132+
// option 1: load [object] as schema (JSON only)
129133
const schema = await fs.promises.readFile("spec.json", "utf8") // must be OpenAPI JSON
130134
const output = await openapiTS(JSON.parse(schema));
131135

132-
// option 2 (new in v3.3): load local path
136+
// option 2: load [string] as local file (YAML or JSON; released in v3.3)
133137
const localPath = path.join(__dirname, 'spec.yaml'); // may be YAML or JSON format
134138
const output = await openapiTS(localPath);
135139

136-
// option 3 (new in v3.3): load remote URL
140+
// option 3: load [string] as remote URL (YAML or JSON; released in v3.3)
137141
const output = await openapiTS('https://myurl.com/v1/openapi.yaml');
138142
```
139143

140-
The Node API may be useful if dealing with dynamically-created schemas, or you’re using within context of a larger application. It
144+
The Node API may be useful if dealing with dynamically-created schemas, or you’re using within context of a larger application. Pass in either a JSON-friendly object to load a schema from memory, or a string to load a schema from a local file or remote URL (it will load the file quickly using built-in Node methods). Note that a YAML string isn’t supported in the Node.js API; either use the CLI or convert to JSON using [js-yaml][js-yaml] first.
141145

142146
⚠️ As of `v3.3`, this is an async function.
143147

144-
It’s important to note that options 2 and 3 are triggered by passing in a `string` rather than an `object`.
145-
146148
#### Custom Formatter
147149

148150
If using the Node.js API, you can optionally pass a **formatter** to openapi-typescript. This is useful if you want to override the default types and substitute your own.
@@ -187,6 +189,7 @@ encouraged but not required.
187189

188190
[glob]: https://www.npmjs.com/package/glob
189191
[js-yaml]: https://www.npmjs.com/package/js-yaml
192+
[json-schema-ref-parser]: https://github.com/APIDevTools/json-schema-ref-parser
190193
[namespace]: https://www.typescriptlang.org/docs/handbook/namespaces.html
191194
[npm-run-all]: https://www.npmjs.com/package/npm-run-all
192195
[openapi-format]: https://swagger.io/specification/#data-types

bin/cli.js

+1-19
Original file line numberDiff line numberDiff line change
@@ -67,28 +67,10 @@ async function main() {
6767
throw new Error(`--raw-schema requires --version flag`);
6868
}
6969

70-
<<<<<<< HEAD
71-
// 1. input
72-
let spec = undefined;
73-
try {
74-
spec = await loadSpec(pathToSpec, {
75-
auth: cli.flags.auth,
76-
log: output !== "STDOUT",
77-
});
78-
} catch (err) {
79-
process.exitCode = 1; // needed for async functions
80-
throw new Error(red(`❌ ${err}`));
81-
}
82-
83-
// 2. generate schema (the main part!)
84-
const result = openapiTS(spec, {
85-
auth: cli.flags.auth,
86-
additionalProperties: cli.flags.additionalProperties,
87-
=======
8870
const result = await openapiTS(pathToSpec, {
8971
auth: cli.flags.auth,
72+
additionalProperties: cli.flags.additionalProperties,
9073
silent: output === "STDOUT",
91-
>>>>>>> 3ea8d8f (Absorb schema loading/parsing into Node API)
9274
immutableTypes: cli.flags.immutableTypes,
9375
prettierConfig: cli.flags.prettierConfig,
9476
rawSchema: cli.flags.rawSchema,

0 commit comments

Comments
 (0)