You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
@@ -12,9 +12,9 @@ openapi-fetch is a type-safe fetch client that pulls in your OpenAPI schema. Wei
12
12
13
13
_\*[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._
14
14
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.
16
16
17
-
```ts
17
+
```ts [src/my-project.ts]
18
18
importcreateClientfrom"openapi-fetch";
19
19
importtype { paths } from"./my-openapi-3-schema"; // generated by openapi-typescript
`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.
40
40
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 anycall](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 anycall](/openapi-fetch/api#create-client)).
42
42
43
43
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:
44
44
@@ -49,28 +49,29 @@ Notice there are no generics, and no manual typing. Your endpoint’s request an
49
49
- ✅ Also eliminates `as` type overrides that can also hide bugs
50
50
- ✅ All of this in a **5 kb** client package 🎉
51
51
52
-
## 🔧 Setup
52
+
## Setup
53
53
54
-
Install this library along with [openapi-typescript](../openapi-typescript):
54
+
Install this library along with [openapi-typescript](/introduction):
55
55
56
56
```bash
57
57
npm i openapi-fetch
58
58
npm i -D openapi-typescript typescript
59
59
```
60
60
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))
62
65
63
66
Next, generate TypeScript types from your OpenAPI schema using openapi-typescript:
> ⚠️ Be sure to <ahref="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 <ahref="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:
72
73
73
-
```json
74
+
```json [package.json]
74
75
{
75
76
"scripts": {
76
77
"test:ts": "tsc --noEmit"
@@ -80,15 +81,17 @@ Lastly, be sure to **run typechecking** in your project. This can be done by add
80
81
81
82
And run `npm run test:ts` in your CI to catch type errors.
82
83
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.
84
87
85
-
## Usage
88
+
## Basic usage
86
89
87
90
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:
2. You pass in your desired `path` to `GET()`, `PUT()`, etc.
115
118
3. TypeScript takes over the rest and returns helpful errors for anything missing or invalid
116
119
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**.
|**Browsers**|[See fetch API support](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API#browser_compatibility) (widely-available in all major browsers) |
0 commit comments