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 |
36
-
|`body`|`{ [name]:value }`|[requestBody](https://spec.openapis.org/oas/latest.html#request-body-object) data for the endpoint |
37
-
|`querySerializer`| QuerySerializer | (optional) Provide a [querySerializer](#queryserializer)|
38
-
|`pathSerializer`| PathSerializer | (optional) Provide a [pathSerializer](#pathserializer)|
39
-
|`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)|
40
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. |
41
-
|`fetch`|`fetch`| Fetch instance used for requests (default: fetch from `createClient`) |
39
+
|`fetch`|`fetch`| Fetch instance used for requests (default: fetch from `createClient`) |
String, number, and boolean query params are straightforward when forming a request, but arrays and objects not so much. OpenAPI supports [different ways of handling each](https://swagger.io/docs/specification/serialization/#query). By default, this library serializes arrays using `style: "form", explode: true`, and objects using `style: "deepObject", explode: true`.
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).
47
45
48
-
To change that behavior, you can supply `querySerializer` options that control how `object` and `arrays` are serialized for query params. This can either be passed on `createClient()` to control every request, or on individual requests for just one:
46
+
### Object syntax
47
+
48
+
openapi-fetch ships the common serialization methods out-of-the-box:
> **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
72
95
73
-
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 with no restrictions:
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.
97
-
98
-
:::
99
-
100
-
### pathSerializer
101
-
102
-
If your backend doesn’t use the standard `{param_name}` syntax, you can control the behavior of how path params are serialized according to the spec ([docs](https://swagger.io/docs/specification/serialization/#path)):
> 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.
113
120
114
-
The `explode` behavior ([docs](https://swagger.io/docs/specification/serialization/#path)) is determined automatically by the pathname, depending on whether an asterisk suffix is present or not, e.g. `/users/{id}` vs `/users/{id*}`. Globs are **NOT** supported, and the param name must still match exactly; the asterisk is only a suffix.
121
+
## bodySerializer
115
122
116
-
:::
117
-
118
-
### bodySerializer
119
-
120
-
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`:
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.
0 commit comments