|
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)
|
@@ -124,24 +128,22 @@ npm i --save-dev openapi-typescript
|
124 | 128 | const fs = require("fs");
|
125 | 129 | const openapiTS = require("openapi-typescript").default;
|
126 | 130 |
|
127 |
| -// option 1: load JS object, write to local file |
| 131 | +// option 1: load [object] as schema (JSON only) |
128 | 132 | const schema = await fs.promises.readFile("spec.json", "utf8") // must be OpenAPI JSON
|
129 | 133 | const output = await openapiTS(JSON.parse(schema));
|
130 | 134 |
|
131 |
| -// option 2 (new in v3.3): load local path |
| 135 | +// option 2: load [string] as local file (YAML or JSON; released in v3.3) |
132 | 136 | const localPath = path.join(__dirname, 'spec.yaml'); // may be YAML or JSON format
|
133 | 137 | const output = await openapiTS(localPath);
|
134 | 138 |
|
135 |
| -// option 3 (new in v3.3): load remote URL |
| 139 | +// option 3: load [string] as remote URL (YAML or JSON; released in v3.3) |
136 | 140 | const output = await openapiTS('https://myurl.com/v1/openapi.yaml');
|
137 | 141 | ```
|
138 | 142 |
|
139 |
| -The Node API may be useful if dealing with dynamically-created schemas, or you’re using within context of a larger application. It |
| 143 | +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. |
140 | 144 |
|
141 | 145 | ⚠️ As of `v3.3`, this is an async function.
|
142 | 146 |
|
143 |
| -It’s important to note that options 2 and 3 are triggered by passing in a `string` rather than an `object`. |
144 |
| - |
145 | 147 | #### Custom Formatter
|
146 | 148 |
|
147 | 149 | 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.
|
@@ -186,6 +188,7 @@ encouraged but not required.
|
186 | 188 |
|
187 | 189 | [glob]: https://www.npmjs.com/package/glob
|
188 | 190 | [js-yaml]: https://www.npmjs.com/package/js-yaml
|
| 191 | +[json-schema-ref-parser]: https://github.com/APIDevTools/json-schema-ref-parser |
189 | 192 | [namespace]: https://www.typescriptlang.org/docs/handbook/namespaces.html
|
190 | 193 | [npm-run-all]: https://www.npmjs.com/package/npm-run-all
|
191 | 194 | [openapi-format]: https://swagger.io/specification/#data-types
|
|
0 commit comments