Skip to content

Commit 3abf019

Browse files
committed
fix(docs): Document inconsistency
1 parent 3a39b68 commit 3abf019

File tree

1 file changed

+56
-39
lines changed

1 file changed

+56
-39
lines changed

packages/openapi-typescript/README.md

Lines changed: 56 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,86 @@
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

5-
The code is [MIT-licensed](./LICENSE) and free for use.
5+
The code is [MIT-licensed](https://github.com/openapi-ts/openapi-typescript/blob/main/packages/openapi-typescript/LICENSE") and free for use.
6+
7+
::: tip
8+
9+
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.
10+
11+
:::
612

713
## Features
814

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
15+
- ✅ Supports OpenAPI 3.0 and 3.1 (including advanced features like [discriminators](https://spec.openapis.org/oas/v3.1.0#discriminator-object))
16+
- ✅ Generate **runtime-free types** that outperform old school codegen
1117
- ✅ Load schemas from YAML or JSON, locally or remotely
12-
- ✅ Native Node.js code is fast and generates types within milliseconds
18+
- ✅ Generate types for even huge schemas within milliseconds
19+
20+
_Note: OpenAPI 2.x is supported with versions `5.x` and previous_
1321

1422
## Examples
1523

16-
👀 [See examples](./examples/)
24+
👀 [See examples](https://github.com/openapi-ts/openapi-typescript/blob/main/packages/openapi-typescript/examples/)
1725

1826
## Setup
1927

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:
28+
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:
2129

2230
```bash
2331
npm i -D openapi-typescript typescript
2432
```
2533

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))
34+
And in your `tsconfig.json`, to load the types properly:
35+
36+
::: code-group
37+
38+
```json [tsconfig.json]
39+
{
40+
"compilerOptions": {
41+
"module": "ESNext", // or "NodeNext" // [!code ++]
42+
"moduleResolution": "Bundler" // or "NodeNext" // [!code ++]
43+
}
44+
}
45+
```
46+
47+
:::
48+
49+
::: tip Highly recommended
50+
51+
Also adding the following can boost type safety:
52+
53+
::: code-group
54+
55+
```json [tsconfig.json]
56+
{
57+
"compilerOptions": {
58+
"noUncheckedIndexedAccess": true // [!code ++]
59+
}
60+
}
61+
```
62+
63+
:::
2964

3065
## Basic usage
3166

32-
First, generate a local type file by running `npx openapi-typescript`:
67+
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:
3368

3469
```bash
3570
# Local schema
3671
npx openapi-typescript ./path/to/my/schema.yaml -o ./path/to/my/schema.d.ts
3772
# 🚀 ./path/to/my/schema.yaml -> ./path/to/my/schema.d.ts [7ms]
38-
```
3973

40-
```bash
4174
# Remote schema
4275
npx openapi-typescript https://myapi.dev/api/v1/openapi.yaml -o ./path/to/my/schema.d.ts
4376
# 🚀 https://myapi.dev/api/v1/openapi.yaml -> ./path/to/my/schema.d.ts [250ms]
4477
```
4578

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.
79+
Then in your TypeScript project, import types where needed:
4780

48-
Then, import schemas from the generated file like so:
81+
::: code-group
4982

50-
```ts
83+
```ts [src/my-project.ts]
5184
import type { paths, components } from "./my-openapi-3-schema"; // generated by openapi-typescript
5285

5386
// Schema Obj
@@ -63,28 +96,12 @@ type ErrorResponse =
6396
paths["/my/endpoint"]["get"]["responses"][500]["content"]["application/json"]["schema"];
6497
```
6598

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-
86-
_Thanks, [@psmyrdek](https://github.com/psmyrdek)!_
99+
:::
87100

88-
## 📓 Docs
101+
From here, you can use these types for any of the following (but not limited to):
89102

90-
[View Docs](https://openapi-ts.dev/)
103+
- Using an OpenAPI-supported fetch client (like [openapi-fetch](/openapi-fetch/))
104+
- Asserting types for other API requestBodies and responses
105+
- Building core business logic based on API types
106+
- Validating mock test data is up-to-date with the current schema
107+
- Packaging API types into any npm packages you publish (such as client SDKs, etc.)

0 commit comments

Comments
 (0)