Skip to content

Commit 41439de

Browse files
authored
docs: Documents are consistent with the website (openapi-ts#1755)
* fix(docs): Document inconsistency * docs: format * docs: Document consistency * docs: update
1 parent 436dfd6 commit 41439de

File tree

1 file changed

+39
-34
lines changed

1 file changed

+39
-34
lines changed

β€ŽREADME.md

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,70 @@
11
<img src="../../docs/public/assets/openapi-ts.svg" alt="openapi-typescript" width="200" height="40" />
22

3-
openapi-typescript generates TypeScript types from static <a href="https://spec.openapis.org/oas/latest.html" target="_blank" rel="noopener noreferrer">OpenAPI</a> schemas quickly using only Node.js. It is fast, lightweight, (almost) dependency-free, and no Java/node-gyp/running OpenAPI servers necessary.
3+
openapi-typescript turns [OpenAPI 3.0 & 3.1](https://spec.openapis.org/oas/latest.html) schemas into TypeScript quickly using Node.js. No Java/node-gyp/running OpenAPI servers necessary.
44

55
The code is [MIT-licensed](./LICENSE) and free for use.
66

7+
> **Tip:**
8+
> New to OpenAPI? Speakeasy’s [Intro to OpenAPI](https://www.speakeasyapi.dev/openapi) is an accessible guide to newcomers that explains the β€œwhy” and β€œhow” of OpenAPI.
9+
710
## Features
811

9-
- βœ… Supports OpenAPI 3.0 and 3.1 (including advanced features like <a href="https://spec.openapis.org/oas/v3.1.0#discriminator-object" target="_blank" rel="noopener noreferrer">discriminators</a>)
10-
- βœ… Generate **runtime-free types** that outperform old-school codegen
12+
- βœ… Supports OpenAPI 3.0 and 3.1 (including advanced features like [discriminators](https://spec.openapis.org/oas/v3.1.0#discriminator-object))
13+
- βœ… Generate **runtime-free types** that outperform old school codegen
1114
- βœ… Load schemas from YAML or JSON, locally or remotely
12-
- βœ… Native Node.js code is fast and generates types within milliseconds
15+
- βœ… Generate types for even huge schemas within milliseconds
16+
17+
_Note: OpenAPI 2.x is supported with versions `5.x` and previous_
1318

1419
## Examples
1520

1621
πŸ‘€ [See examples](./examples/)
1722

1823
## Setup
1924

20-
This library requires the latest version of <a href="https://nodejs.org/en" target="_blank" rel="noopener noreferrer">Node.js</a> installed (20.x or higher recommended). With that present, run the following in your project:
25+
This library requires the latest version of [Node.js](https://nodejs.org) installed (20.x or higher recommended). With that present, run the following in your project:
2126

2227
```bash
2328
npm i -D openapi-typescript typescript
2429
```
2530

26-
> ✨ **Tip**
27-
>
28-
> Enabling [noUncheckedIndexedAccess](https://www.typescriptlang.org/tsconfig#noUncheckedIndexedAccess) in `tsconfig.json` can go along way to improve type safety ([read more](../../docs/src/content/docs/advanced.md#enable-nouncheckedindexedaccess-in-your-tsconfigjson))
31+
And in your `tsconfig.json`, to load the types properly:
32+
33+
```diff
34+
{
35+
"compilerOptions": {
36+
+ "module": "ESNext", // or "NodeNext"
37+
+ "moduleResolution": "Bundler" // or "NodeNext"
38+
}
39+
}
40+
```
41+
42+
> **Highly recommended**
43+
>
44+
> Also adding the following can boost type safety:
45+
> ```diff
46+
> {
47+
> "compilerOptions": {
48+
> + "noUncheckedIndexedAccess": true
49+
> }
50+
> }
51+
> ```
2952
3053
## Basic usage
3154
32-
First, generate a local type file by running `npx openapi-typescript`:
55+
First, generate a local type file by running `npx openapi-typescript`, first specifying your input schema (JSON or YAML), and where you’d like the `--output` (`-o`) to be saved:
3356
3457
```bash
3558
# Local schema
3659
npx openapi-typescript ./path/to/my/schema.yaml -o ./path/to/my/schema.d.ts
3760
# πŸš€ ./path/to/my/schema.yaml -> ./path/to/my/schema.d.ts [7ms]
38-
```
3961
40-
```bash
4162
# Remote schema
4263
npx openapi-typescript https://myapi.dev/api/v1/openapi.yaml -o ./path/to/my/schema.d.ts
4364
# πŸš€ https://myapi.dev/api/v1/openapi.yaml -> ./path/to/my/schema.d.ts [250ms]
4465
```
4566
46-
> ⚠️ Be sure to <a href="https://redocly.com/docs/cli/commands/lint/" target="_blank" rel="noopener noreferrer">validate your schemas</a>! openapi-typescript will err on invalid schemas.
47-
48-
Then, import schemas from the generated file like so:
67+
Then in your TypeScript project, import types where needed:
4968

5069
```ts
5170
import type { paths, components } from "./my-openapi-3-schema"; // generated by openapi-typescript
@@ -63,27 +82,13 @@ type ErrorResponse =
6382
paths["/my/endpoint"]["get"]["responses"][500]["content"]["application/json"]["schema"];
6483
```
6584

66-
#### 🦠 Globbing local schemas
67-
68-
```bash
69-
npx openapi-typescript "specs/**/*.yaml" --output schemas/
70-
71-
# πŸš€ specs/one.yaml -> schemas/specs/one.ts [7ms]
72-
# πŸš€ specs/two.yaml -> schemas/specs/two.ts [7ms]
73-
# πŸš€ specs/three.yaml -> schemas/specs/three.ts [7ms]
74-
```
75-
76-
_Thanks, [@sharmarajdaksh](https://github.com/sharmarajdaksh)!_
77-
78-
#### ☁️ Remote schemas
79-
80-
```bash
81-
npx openapi-typescript https://petstore3.swagger.io/api/v3/openapi.yaml --output petstore.d.ts
82-
83-
# πŸš€ https://petstore3.swagger.io/api/v3/openapi.yaml -> petstore.d.ts [250ms]
84-
```
85+
From here, you can use these types for any of the following (but not limited to):
8586

86-
_Thanks, [@psmyrdek](https://github.com/psmyrdek)!_
87+
- Using an OpenAPI-supported fetch client (like [openapi-fetch](https://openapi-ts.dev/openapi-fetch/))
88+
- Asserting types for other API requestBodies and responses
89+
- Building core business logic based on API types
90+
- Validating mock test data is up-to-date with the current schema
91+
- Packaging API types into any npm packages you publish (such as client SDKs, etc.)
8792

8893
## πŸ““ Docs
8994

0 commit comments

Comments
Β (0)