Skip to content

Commit cf18ebe

Browse files
phk422kerwanp
authored andcommitted
docs: Documents are consistent with the website (openapi-ts#1755)
* fix(docs): Document inconsistency * docs: format * docs: Document consistency * docs: update
1 parent 649f4bc commit cf18ebe

File tree

3 files changed

+53
-46
lines changed

3 files changed

+53
-46
lines changed

β€Ždocs/openapi-fetch/index.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ await client.PUT("/blogposts", {
4444

4545
:::
4646

47-
`data` and `error` are typechecked and expose their shapes to Intellisence in VS Code (and any other IDE with TypeScript support). Likewise, the request `body` will also typecheck its fields, erring if any required params are missing, or if there’s a type mismatch.
47+
`data` and `error` are typechecked and expose their shapes to Intellisense in VS Code (and any other IDE with TypeScript support). Likewise, the request `body` will also typecheck its fields, erring if any required params are missing, or if there’s a type mismatch.
4848

4949
`GET()`, `PUT()`, `POST()`, etc. are thin wrappers around the native [fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) (which you can [swap for anyΒ call](/openapi-fetch/api#create-client)).
5050

β€Žpackages/openapi-fetch/README.md

+13-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<img src="../../docs/public/assets/openapi-fetch.svg" alt="openapi-fetch" width="216" height="40" />
22

3-
openapi-fetch is a type-safe fetch client that pulls in your OpenAPI schema. Weighs **4 kB** and has virtually zero runtime. Works with React, Vue, Svelte, or vanilla JS.
3+
openapi-fetch is a type-safe fetch client that pulls in your OpenAPI schema. Weighs **5 kb** and has virtually zero runtime. Works with React, Vue, Svelte, or vanilla JS.
44

55
| Library | Size (min) | β€œGET” request\* |
66
| :------------------------- | ---------: | :------------------------- |
@@ -12,7 +12,7 @@ openapi-fetch is a type-safe fetch client that pulls in your OpenAPI schema. Wei
1212

1313
_\* [Benchmarks are approximate](https://github.com/openapi-ts/openapi-typescript/blob/main/packages/openapi-fetch/test/index.bench.js) to just show rough baseline and will differ among machines and browsers. The relative performance between libraries is more reliable._
1414

15-
The syntax is inspired by popular libraries like react-query or Apollo client, but without all the bells and whistles and in a 2 kB package.
15+
The syntax is inspired by popular libraries like react-query or Apollo client, but without all the bells and whistles and in a 5 kb package.
1616

1717
```ts
1818
import createClient from "openapi-fetch";
@@ -38,7 +38,7 @@ await client.PUT("/blogposts", {
3838

3939
`data` and `error` are typechecked and expose their shapes to Intellisense in VS Code (and any other IDE with TypeScript support). Likewise, the request `body` will also typecheck its fields, erring if any required params are missing, or if there’s a type mismatch.
4040

41-
`GET`, `PUT`, `POST`, etc. are only thin wrappers around the native [fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) (which you can [swap for any call](https://openapi-ts.dev/openapi-fetch/api/#create-client)).
41+
`GET()`, `PUT()`, `POST()`, etc. are thin wrappers around the native [fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) (which you can [swap for anyΒ call](https://openapi-ts.dev/openapi-fetch/api/#create-client)).
4242

4343
Notice there are no generics, and no manual typing. Your endpoint’s request and response were inferred automatically. This is a huge improvement in the type safety of your endpoints because **every manual assertion could lead to a bug**! This eliminates all of the following:
4444

@@ -49,7 +49,7 @@ Notice there are no generics, and no manual typing. Your endpoint’s request an
4949
- βœ… Also eliminates `as` type overrides that can also hide bugs
5050
- βœ… All of this in a **5 kb** client package πŸŽ‰
5151

52-
## πŸ”§ Setup
52+
## Setup
5353

5454
Install this library along with [openapi-typescript](../openapi-typescript):
5555

@@ -58,17 +58,17 @@ npm i openapi-fetch
5858
npm i -D openapi-typescript typescript
5959
```
6060

61-
> **Highly recommended**: enable [noUncheckedIndexedAccess](https://www.typescriptlang.org/tsconfig#noUncheckedIndexedAccess) in your `tsconfig.json` ([docs](/advanced#enable-nouncheckedindexaccess-in-your-tsconfigjson))
61+
> **Highly recommended**
62+
>
63+
> Enable [noUncheckedIndexedAccess](https://www.typescriptlang.org/tsconfig#noUncheckedIndexedAccess) in your `tsconfig.json` ([docs](/advanced#enable-nouncheckedindexaccess-in-your-tsconfigjson))
6264
6365
Next, generate TypeScript types from your OpenAPI schema using openapi-typescript:
6466

6567
```bash
6668
npx openapi-typescript ./path/to/api/v1.yaml -o ./src/lib/api/v1.d.ts
6769
```
6870

69-
> ⚠️ 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.
70-
71-
Lastly, be sure to **run typechecking** in your project. This can be done by adding `tsc --noEmit` to your <a href="https://docs.npmjs.com/cli/v9/using-npm/scripts" target="_blank" rel="noopener noreferrer">npm scripts</a> likeΒ so:
71+
Lastly, be sure to **run typechecking** in your project. This can be done by adding `tsc --noEmit` to your [npm scripts](https://docs.npmjs.com/cli/v9/using-npm/scripts) likeΒ so:
7272

7373
```json
7474
{
@@ -80,9 +80,11 @@ Lastly, be sure to **run typechecking** in your project. This can be done by add
8080

8181
And run `npm run test:ts` in your CI to catch typeΒ errors.
8282

83-
> **Tip**: use `tsc --noEmit` to check for type errors rather than relying on your linter or your build command. Nothing will typecheck as accurately as the TypeScript compiler itself.
83+
> **TIP:**
84+
>
85+
> Use `tsc --noEmit` to check for type errors rather than relying on your linter or your build command. Nothing will typecheck as accurately as the TypeScript compiler itself.
8486
85-
## Usage
87+
## Basic usage
8688

8789
The best part about using openapi-fetch over oldschool codegen is no documentation needed. openapi-fetch encourages using your existing OpenAPI documentation rather than trying to find what function to import, or what parameters that function wants:
8890

@@ -116,4 +118,4 @@ const { data, error } = await client.PUT("/blogposts", {
116118

117119
## πŸ““ Docs
118120

119-
[View Docs](https://openapi-ts.dev/openapi-fetch/)
121+
[View Docs](https://openapi-ts.dev/openapi-fetch/)

β€Žpackages/openapi-typescript/README.md

+39-34
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)