Skip to content

Commit 4c55285

Browse files
committed
Load remote schemas
1 parent 3ea8d8f commit 4c55285

Some content is hidden

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

53 files changed

+1402
-855
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)
@@ -124,24 +128,22 @@ npm i --save-dev openapi-typescript
124128
const fs = require("fs");
125129
const openapiTS = require("openapi-typescript").default;
126130

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

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)
132136
const localPath = path.join(__dirname, 'spec.yaml'); // may be YAML or JSON format
133137
const output = await openapiTS(localPath);
134138

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)
136140
const output = await openapiTS('https://myurl.com/v1/openapi.yaml');
137141
```
138142

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.
140144

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

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

147149
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.
186188

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

0 commit comments

Comments
 (0)