Skip to content

Commit 7f76e4b

Browse files
committed
docs: Document consistency
1 parent 3aa0c54 commit 7f76e4b

File tree

1 file changed

+57
-17
lines changed

1 file changed

+57
-17
lines changed

packages/openapi-fetch/README.md

+57-17
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,9 +12,9 @@ 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

17-
```ts
17+
```ts [src/my-project.ts]
1818
import createClient from "openapi-fetch";
1919
import type { paths } from "./my-openapi-3-schema"; // generated by openapi-typescript
2020

@@ -36,9 +36,9 @@ await client.PUT("/blogposts", {
3636
});
3737
```
3838

39-
`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.
39+
`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.
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](/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,28 +49,29 @@ 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

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

5656
```bash
5757
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+
> **TIP:**
62+
> **Highly recommended**
63+
>
64+
> Enable [noUncheckedIndexedAccess](https://www.typescriptlang.org/tsconfig#noUncheckedIndexedAccess) in your `tsconfig.json` ([docs](/advanced#enable-nouncheckedindexaccess-in-your-tsconfigjson))
6265
6366
Next, generate TypeScript types from your OpenAPI schema using openapi-typescript:
6467

6568
```bash
6669
npx openapi-typescript ./path/to/api/v1.yaml -o ./src/lib/api/v1.d.ts
6770
```
6871

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:
72+
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:
7273

73-
```json
74+
```json [package.json]
7475
{
7576
"scripts": {
7677
"test:ts": "tsc --noEmit"
@@ -80,15 +81,17 @@ Lastly, be sure to **run typechecking** in your project. This can be done by add
8081

8182
And run `npm run test:ts` in your CI to catch type errors.
8283

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.
84+
> **TIP:**
85+
>
86+
> 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.
8487
85-
## Usage
88+
## Basic usage
8689

8790
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:
8891

8992
![OpenAPI schema example](../../docs/public/assets/openapi-schema.png)
9093

91-
```ts
94+
```ts [src/my-project.ts]
9295
import createClient from "openapi-fetch";
9396
import type { paths } from "./my-openapi-3-schema"; // generated by openapi-typescript
9497

@@ -114,6 +117,43 @@ const { data, error } = await client.PUT("/blogposts", {
114117
2. You pass in your desired `path` to `GET()`, `PUT()`, etc.
115118
3. TypeScript takes over the rest and returns helpful errors for anything missing or invalid
116119

117-
## 📓 Docs
120+
### Pathname
121+
122+
The pathname of `GET()`, `PUT()`, `POST()`, etc. **must match your schema literally.** Note in the example, the URL is `/blogposts/{post_id}`. This library will quickly replace all `path` params for you (so they can be typechecked).
123+
124+
125+
> **TIP:**
126+
>
127+
> openapi-fetch infers types from the URL. Prefer static string values over dynamic runtime values, e.g.:
128+
> - ✅ `"/blogposts/{post_id}"`
129+
> - ❌ `[...pathParts].join("/") + "{post_id}"`
130+
131+
This library also supports the **label** and **matrix** serialization styles as well ([docs](https://swagger.io/docs/specification/serialization/#path)) automatically.
132+
133+
### Request
134+
135+
The `GET()` request shown needed the `params` object that groups [parameters by type](https://spec.openapis.org/oas/latest.html#parameter-object) (`path` or `query`). If a required param is missing, or the wrong type, a type error will be thrown.
136+
137+
The `POST()` request required a `body` object that provided all necessary [requestBody](https://spec.openapis.org/oas/latest.html#request-body-object) data.
138+
139+
### Response
140+
141+
All methods return an object with **data**, **error**, and **response**.
142+
143+
```ts
144+
const { data, error, response } = await client.GET("/url");
145+
```
146+
147+
| Object | Response |
148+
| :--------- | :-------------------------------------------------------------------------------------------------------------------------- |
149+
| `data` | `2xx` response if OK; otherwise `undefined` |
150+
| `error` | `5xx`, `4xx`, or `default` response if not OK; otherwise `undefined` |
151+
| `response` | [The original Response](https://developer.mozilla.org/en-US/docs/Web/API/Response) which contains `status`, `headers`, etc. |
152+
153+
## Support
118154

119-
[View Docs](https://openapi-ts.dev/openapi-fetch/)
155+
| Platform | Support |
156+
| :------------- | :------------------------------------------------------------------------------------------------------------------------------------------------- |
157+
| **Browsers** | [See fetch API support](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API#browser_compatibility) (widely-available in all major browsers) |
158+
| **Node** | >= 18.0.0 |
159+
| **TypeScript** | >= 4.7 (>= 5.0 recommended) |

0 commit comments

Comments
 (0)