Skip to content

Commit 8e0eabc

Browse files
committed
chore: README fixes (#634)
1 parent bfe51e3 commit 8e0eabc

File tree

3 files changed

+1619
-1097
lines changed

3 files changed

+1619
-1097
lines changed

README.md

+18-14
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@
1212

1313
**Features**
1414

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
15+
-[OpenAPI 3.0][openapi3]
16+
-[Swagger 2.0][openapi2]
17+
- ✅ Supports YAML and JSON schema formats
18+
- ✅ Supports loading via remote URL (simple authentication supported with the `--auth` flag)
19+
- ✅ Supports remote references: `$ref: "external.yaml#components/schemas/User"`
20+
- ✅ Formats using [Prettier][prettier]
21+
- ✅ TypeScript 4.0 features
2022

2123
**Examples**
2224

@@ -36,6 +38,7 @@ npx openapi-typescript schema.yaml --output schema.ts
3638
# 🚀 schema.yaml -> schema.ts [250ms]
3739

3840
npx openapi-typescript "specs/**/*.yaml" --output schemas/
41+
3942
# 🔭 Loading spec from specs/one.yaml…
4043
# 🔭 Loading spec from specs/two.yaml…
4144
# 🔭 Loading spec from specs/three.yaml…
@@ -71,7 +74,7 @@ import { components } from './generated-schema.ts';
7174
type APIResponse = components["schemas"]["APIResponse"];
7275
```
7376

74-
The reason for all the `["…"]` everywhere is because OpenAPI lets you use more characters than are valid TypeScript identifiers. The goal of this project is to generate _all_ of your schema, not merely the parts that are “TypeScript-safe.”
77+
Because OpenAPI schemas may have invalid TypeScript characters as names, the square brackets are a safe way to access every property.
7578

7679
Also note that there’s a special `operations` interface that you can import `OperationObjects` by their [operationId][openapi-operationid]:
7780

@@ -81,11 +84,13 @@ import { operations } from './generated-schema.ts';
8184
type getUsersById = operations["getUsersById"];
8285
```
8386

84-
This is the only place where our generation differs from your schema as-written, but it’s done so as a convenience and shouldn’t cause any issues (you can still use deep references as-needed).
87+
Even though `operations` isn’t present in your original schema, it’s a simple convenience and won’t disrupt any of your other types.
88+
89+
_Thanks to [@gr2m](https://github.com/gr2m) for the operations feature!_
8590

86-
_Thanks to @gr2m for the operations feature!_
91+
#### Outputting to stdout
8792

88-
#### Outputting to `stdout`
93+
Simply omit the `--output` flag to return to stdout:
8994

9095
```bash
9196
npx openapi-typescript schema.yaml
@@ -113,21 +118,21 @@ npm i --save-dev openapi-typescript
113118
const fs = require("fs");
114119
const openapiTS = require("openapi-typescript").default;
115120

116-
// option 1: load [object] as schema (JSON only)
121+
// example 1: load [object] as schema (JSON only)
117122
const schema = await fs.promises.readFile("spec.json", "utf8") // must be OpenAPI JSON
118123
const output = await openapiTS(JSON.parse(schema));
119124

120-
// option 2: load [string] as local file (YAML or JSON; released in v3.3)
125+
// example 2: load [string] as local file (YAML or JSON; released in v4.0)
121126
const localPath = path.join(__dirname, 'spec.yaml'); // may be YAML or JSON format
122127
const output = await openapiTS(localPath);
123128

124-
// option 3: load [string] as remote URL (YAML or JSON; released in v3.3)
129+
// example 3: load [string] as remote URL (YAML or JSON; released in v4.0)
125130
const output = await openapiTS('https://myurl.com/v1/openapi.yaml');
126131
```
127132

128133
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.
129134

130-
⚠️ As of `v3.3`, this is an async function.
135+
⚠️ As of `v4.0`, `openapiTS()` is an async function.
131136

132137
#### Custom Formatter
133138

@@ -173,7 +178,6 @@ encouraged but not required.
173178

174179
[glob]: https://www.npmjs.com/package/glob
175180
[js-yaml]: https://www.npmjs.com/package/js-yaml
176-
[json-schema-ref-parser]: https://github.com/APIDevTools/json-schema-ref-parser
177181
[namespace]: https://www.typescriptlang.org/docs/handbook/namespaces.html
178182
[npm-run-all]: https://www.npmjs.com/package/npm-run-all
179183
[openapi-format]: https://swagger.io/specification/#data-types

0 commit comments

Comments
 (0)