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
|`params`|ParamsObject|[path](https://swagger.io/specification/#parameter-locations) and [query](https://swagger.io/specification/#parameter-locations) params for the endpoint |
35
-
|`body`|`{ [name]:value }`|[requestBody](https://spec.openapis.org/oas/latest.html#request-body-object) data for the endpoint |
36
-
|`querySerializer`|QuerySerializer| (optional) Provide a [querySerializer](#queryserializer)|
37
-
|`bodySerializer`|BodySerializer| (optional) Provide a [bodySerializer](#bodyserializer)|
|`params`|ParamsObject|[path](https://swagger.io/specification/#parameter-locations) and [query](https://swagger.io/specification/#parameter-locations) params for the endpoint |
35
+
|`body`|`{ [name]:value }`|[requestBody](https://spec.openapis.org/oas/latest.html#request-body-object) data for the endpoint |
36
+
|`querySerializer`|QuerySerializer| (optional) Provide a [querySerializer](#queryserializer)|
37
+
|`bodySerializer`|BodySerializer| (optional) Provide a [bodySerializer](#bodyserializer)|
38
38
|`parseAs`|`"json"`\|`"text"`\|`"arrayBuffer"`\|`"blob"`\|`"stream"`| (optional) Parse the response using [a built-in instance method](https://developer.mozilla.org/en-US/docs/Web/API/Response#instance_methods) (default: `"json"`). `"stream"` skips parsing altogether and returns the raw stream. |
39
-
|`fetch`|`fetch`| Fetch instance used for requests (default: fetch from `createClient`) |
39
+
|`fetch`|`fetch`| Fetch instance used for requests (default: fetch from `createClient`) |
By default, this library serializes query parameters using `style: form` and `explode: true`[according to the OpenAPI specification](https://swagger.io/docs/specification/serialization/#query). To change the default behavior, you can supply your own `querySerializer()` function either on the root `createClient()` as well as optionally on an individual request. This is useful if your backend expects modifications like the addition of `[]` for array params:
44
+
OpenAPI supports [different ways of serializing objects and arrays](https://swagger.io/docs/specification/serialization/#query) for parameters (strings, numbers, and booleans—primitives—always behave the same way). By default, this library serializes arrays using `style: "form", explode: true`, and objects using `style: "deepObject", explode: true`, but you can customize that behavior with the `querySerializer` option (either on `createClient()` to control every request, or on individual requests for just one).
45
+
46
+
### Object syntax
47
+
48
+
openapi-fetch ships the common serialization methods out-of-the-box:
|`array`| SerializerOptions | Set `style` and `explode` for arrays ([docs](https://swagger.io/docs/specification/serialization/#query)). Default: `{ style: "form", explode: true }`. |
53
+
|`object`| SerializerOptions | Set `style` and `explode` for objects ([docs](https://swagger.io/docs/specification/serialization/#query)). Default: `{ style: "deepObject", explode: true }`. |
54
+
|`allowReserved`|`boolean`| Set to `true` to skip URL encoding (⚠️ may break the request) ([docs](https://swagger.io/docs/specification/serialization/#query)). Default: `false`. |
> **deepObject** is always exploded, so it doesn’t matter if you set `explode: true` or `explode: false`—it’ll generate the same output.
93
+
94
+
### Alternate function syntax
95
+
96
+
Sometimes your backend doesn’t use one of the standard serialization methods, in which case you can pass a function to `querySerializer` to serialize the entire string yourself. You’ll also need to use this if you’re handling deeply-nested objects and arrays in your params:
> When serializing yourself, the string will be kept exactly as-authored, so you’ll have to call [encodeURI](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURI) or [encodeURIComponent](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent) to escape special characters.
68
120
69
-
Similar to [querySerializer](#querySerializer), bodySerializer allows you to customize how the requestBody is serialized if you don’t want the default [JSON.stringify()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify) behavior. You probably only need this when using `multipart/form-data`:
121
+
## bodySerializer
122
+
123
+
Similar to [querySerializer](#queryserializer), bodySerializer allows you to customize how the requestBody is serialized if you don’t want the default [JSON.stringify()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify) behavior. You probably only need this when using `multipart/form-data`:
openapi-fetch supports path serialization as [outlined in the 3.1 spec](https://swagger.io/docs/specification/serialization/#path). This happens automatically, based on the specific format in your OpenAPI schema:
Copy file name to clipboardExpand all lines: docs/openapi-fetch/examples.md
+5-1
Original file line number
Diff line number
Diff line change
@@ -130,4 +130,8 @@ _Note: if you’re using Svelte without SvelteKit, the root example in `src/rout
130
130
131
131
### Vue
132
132
133
-
TODO
133
+
There isn’t an example app in Vue yet. Are you using it in Vue? Please [open a PR to add it!](https://github.com/drwpow/openapi-typescript/pulls)
134
+
135
+
---
136
+
137
+
Additional examples are always welcome! Please [open a PR](https://github.com/drwpow/openapi-typescript/pulls) with your examples.
openapi-fetch is a typesafe fetch client that pulls in your OpenAPI schema. Weighs **2 kb** and has virtually zero runtime. Works with React, Vue, Svelte, or vanilla JS.
7
+
openapi-fetch is a typesafe fetch client that pulls in your OpenAPI schema. Weighs **4 kb** and has virtually zero runtime. Works with React, Vue, Svelte, or vanilla JS.
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.
17
+
The syntax is inspired by popular libraries like react-query or Apollo client, but without all the bells and whistles and in a 4 kb package.
18
18
19
19
```ts
20
20
importcreateClientfrom"openapi-fetch";
@@ -49,7 +49,7 @@ Notice there are no generics, and no manual typing. Your endpoint’s request an
49
49
- ✅ No manual typing of your API
50
50
- ✅ Eliminates `any` types that hide bugs
51
51
- ✅ Also eliminates `as` type overrides that can also hide bugs
52
-
- ✅ All of this in a **2 kB** client package 🎉
52
+
- ✅ All of this in a **4 kb** client package 🎉
53
53
54
54
## Setup
55
55
@@ -139,6 +139,8 @@ openapi-fetch infers types from the URL. Prefer static string values over dynami
139
139
140
140
:::
141
141
142
+
This library also supports the **label** and **matrix** serialization styles as well ([docs](https://swagger.io/docs/specification/serialization/#path)) automatically.
143
+
142
144
### Request
143
145
144
146
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.
0 commit comments