|
10 | 10 |
|
11 | 11 | 🚀 Convert [OpenAPI 3.0][openapi3] and [2.0 (Swagger)][openapi2] schemas to TypeScript interfaces using Node.js.
|
12 | 12 |
|
13 |
| -💅 The output is prettified with [Prettier][prettier] (and can be customized!). |
| 13 | +**Features** |
14 | 14 |
|
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 |
16 | 20 |
|
17 |
| -View examples: |
| 21 | +**Examples** |
18 | 22 |
|
19 | 23 | - [Stripe, OpenAPI 2.0](./examples/stripe-openapi2.ts)
|
20 | 24 | - [Stripe, OpenAPI 3.0](./examples/stripe-openapi3.ts)
|
@@ -106,19 +110,24 @@ npm i --save-dev openapi-typescript
|
106 | 110 | ```
|
107 | 111 |
|
108 | 112 | ```js
|
109 |
| -const { readFileSync } = require("fs"); |
| 113 | +const fs = require("fs"); |
110 | 114 | const openapiTS = require("openapi-typescript").default;
|
111 | 115 |
|
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) |
| 116 | +// option 1: load [object] as schema (JSON only) |
| 117 | +const schema = await fs.promises.readFile("spec.json", "utf8") // must be OpenAPI JSON |
| 118 | +const output = await openapiTS(JSON.parse(schema)); |
| 119 | + |
| 120 | +// option 2: load [string] as local file (YAML or JSON; released in v3.3) |
| 121 | +const localPath = path.join(__dirname, 'spec.yaml'); // may be YAML or JSON format |
| 122 | +const output = await openapiTS(localPath); |
| 123 | + |
| 124 | +// option 3: load [string] as remote URL (YAML or JSON; released in v3.3) |
| 125 | +const output = await openapiTS('https://myurl.com/v1/openapi.yaml'); |
114 | 126 | ```
|
115 | 127 |
|
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. |
| 128 | +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. |
119 | 129 |
|
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. |
| 130 | +⚠️ As of `v3.3`, this is an async function. |
122 | 131 |
|
123 | 132 | #### Custom Formatter
|
124 | 133 |
|
@@ -164,6 +173,7 @@ encouraged but not required.
|
164 | 173 |
|
165 | 174 | [glob]: https://www.npmjs.com/package/glob
|
166 | 175 | [js-yaml]: https://www.npmjs.com/package/js-yaml
|
| 176 | +[json-schema-ref-parser]: https://github.com/APIDevTools/json-schema-ref-parser |
167 | 177 | [namespace]: https://www.typescriptlang.org/docs/handbook/namespaces.html
|
168 | 178 | [npm-run-all]: https://www.npmjs.com/package/npm-run-all
|
169 | 179 | [openapi-format]: https://swagger.io/specification/#data-types
|
|
0 commit comments