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
Breaking: openapi-fetch now just takes the first media type it finds rather than preferring JSON. This is because in the case of `multipart/form-data` vs `application/json`, it’s not inherently clear which you’d want. Or if there were multiple JSON-like media types.
|`params`| ParamsObject | Provide `path` and `query` params from the OpenAPI schema |
31
-
|`params.path`|`{ [name]: value }`| Provide all `path` params (params that are part of the URL) |
32
-
|`params.query`|`{ [name]: value }`| Provide all `query params (params that are part of the <ahref="https://developer.mozilla.org/en-US/docs/Web/API/URL/searchParams"target="_blank"rel="noopener noreferrer">searchParams</a> |
33
-
|`body`|`{ [name]:value }`| The <ahref="https://spec.openapis.org/oas/latest.html#request-body-object"target="_blank"rel="noopener noreferrer">requestBody</a> data, if needed (PUT/POST/PATCH/DEL only) |
|`parseAs`|`"json"`\|`"text"`\|`"arrayBuffer"`\|`"blob"`\|`"stream"`| Decide how to parse the <ahref="https://developer.mozilla.org/en-US/docs/Web/API/Response/body"target="_blank"rel="noopener noreferrer">response body</a>, with `"stream"` skipping processing altogether (default: `"json"`) |
|`params`| ParamsObject | Provide `path` and `query` params from the OpenAPI schema |
33
+
|`params.path`|`{ [name]: value }`| Provide all `path` params (params that are part of the URL) |
34
+
|`params.query`|`{ [name]: value }`| Provide all `query params (params that are part of the <ahref="https://developer.mozilla.org/en-US/docs/Web/API/URL/searchParams"target="_blank"rel="noopener noreferrer">searchParams</a> |
35
+
|`body`|`{ [name]:value }`| The <ahref="https://spec.openapis.org/oas/latest.html#request-body-object"target="_blank"rel="noopener noreferrer">requestBody</a> data, if needed (PUT/POST/PATCH/DEL only) |
36
+
|`querySerializer`| QuerySerializer | (optional) Serialize query params for this request only (default: `new URLSearchParams()`) |
37
+
|`bodySerializer`| BodySerializer | (optional) Serialize request body for this request only (default: `JSON.stringify()`) |
38
+
|`parseAs`|`"json"`\|`"text"`\|`"arrayBuffer"`\|`"blob"`\|`"stream"`| Parse the <ahref="https://developer.mozilla.org/en-US/docs/Web/API/Response/body"target="_blank"rel="noopener noreferrer">response body</a>, with `"stream"` skipping processing altogether (default: `"json"`) |
In the spirit of being lightweight, this library only uses <ahref="https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams"target="_blank"rel="noopener noreferrer">URLSearchParams</a> to <ahref="https://swagger.io/docs/specification/serialization/"target="_blank"rel="noopener noreferrer">serialize parameters</a>. So for complex query param types (e.g. arrays) you’ll need to provide your own `querySerializer()` method that transforms query params into a URL-safe string:
43
+
This library uses <ahref="https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams"target="_blank"rel="noopener noreferrer">URLSearchParams</a> to <ahref="https://swagger.io/docs/specification/serialization/"target="_blank"rel="noopener noreferrer">serialize query parameters</a>. For complex query param types (e.g. arrays) you’ll need to provide your own `querySerializer()` method that transforms query params into a URL-safe string:
41
44
42
45
```ts
43
-
importcreateClientfrom"openapi-fetch";
44
-
import { paths } from"./v1"; // generated from openapi-typescript
45
-
46
-
const { get, post } =createClient<paths>({ baseUrl: "https://myapi.dev/v1/" });
openapi-fetch is an ultra-fast fetch client for TypeScript using your OpenAPI schema. Weighs in at **1 kb** and has virtually zero runtime. Works with React, Vue, Svelte, or vanilla JS.
8
+
openapi-fetch applies your OpenAPI types to the native fetch API via TypeScript. Weighs in at **1 kb** and has virtually zero runtime. Works with React, Vue, Svelte, or vanilla JS.
9
9
10
10
| Library | Size (min) |
11
11
| :----------------------------- | ---------: |
@@ -19,7 +19,7 @@ The syntax is inspired by popular libraries like react-query or Apollo client, b
19
19
importcreateClientfrom"openapi-fetch";
20
20
import { paths } from"./v1"; // generated from openapi-typescript
21
21
22
-
const { get, post } =createClient<paths>({ baseUrl: "https://myapi.dev/v1/" });
22
+
const { get, put } =createClient<paths>({ baseUrl: "https://myapi.dev/v1/" });
@@ -134,3 +133,11 @@ All methods return an object with **data**, **error**, and **response**.
134
133
-**error** likewise contains that endpoint’s `4xx`/`5xx` response if the server returned either; otherwise it will be `undefined`
135
134
-_Note: `default` will also be interpreted as `error`, since its intent is handling unexpected HTTP codes_
136
135
-**response** has response info like `status`, `headers`, etc. It is not typechecked.
136
+
137
+
## Version Support
138
+
139
+
openapi-fetch implements the [native fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) which is available in all major browsers.
140
+
141
+
If using in a Node.js environment, version 18 or greater is recommended (newer is better).
142
+
143
+
TypeScript support is pretty far-reaching as this library doesn’t use any cutting-edge features, but using the latest version of TypeScript is always recommended for accuracy.
0 commit comments