Skip to content

Commit 620ba5a

Browse files
committed
docs: update openapi-fetch docs
1 parent af533fc commit 620ba5a

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

docs/openapi-fetch/api.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ createClient<paths>(options);
1919
| `fetch` | `fetch` | Fetch instance used for requests (default: `globalThis.fetch`) |
2020
| `querySerializer` | QuerySerializer | (optional) Provide a [querySerializer](#queryserializer) |
2121
| `bodySerializer` | BodySerializer | (optional) Provide a [bodySerializer](#bodyserializer) |
22+
| `pathSerializer` | PathSerializer | (optional) Provide a [pathSerializer](#pathserialization) |
2223
| (Fetch options) | | Any valid fetch option (`headers`, `mode`, `cache`, `signal` …) ([docs](https://developer.mozilla.org/en-US/docs/Web/API/fetch#options) |
2324

2425
## Fetch options
@@ -35,6 +36,7 @@ client.GET("/my-url", options);
3536
| `body` | `{ [name]:value }` | [requestBody](https://spec.openapis.org/oas/latest.html#request-body-object) data for the endpoint |
3637
| `querySerializer` | QuerySerializer | (optional) Provide a [querySerializer](#queryserializer) |
3738
| `bodySerializer` | BodySerializer | (optional) Provide a [bodySerializer](#bodyserializer) |
39+
| `pathSerializer` | PathSerializer | (optional) Provide a [pathSerializer](#pathserialization) |
3840
| `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. |
3941
| `baseUrl` | `string` | Prefix the fetch URL with this option (e.g. `"https://myapi.dev/v1/"`) |
4042
| `fetch` | `fetch` | Fetch instance used for requests (default: fetch from `createClient`) |
@@ -208,7 +210,33 @@ const { data, error } = await client.POST("/tokens", {
208210
});
209211
```
210212

211-
## Path serialization
213+
## Path Serializer
214+
215+
Similar to [querySerializer](#queryserializer) and [bodySerializer](#bodyserializer), `pathSerializer` allows you to customize how path parameters are serialized. This is useful when your API uses a non-standard path serialization format, or you want to change the default behavior.
216+
217+
### Custom Path Serializer
218+
219+
You can provide a custom path serializer when creating the client:
220+
221+
```ts
222+
const client = createClient({
223+
pathSerializer(pathname, pathParams) {
224+
let result = pathname;
225+
for (const [key, value] of Object.entries(pathParams)) {
226+
result = result.replace(`{${key}}`, `[${value}]`);
227+
}
228+
return result;
229+
},
230+
});
231+
232+
const { data, error } = await client.GET("/users/{id}", {
233+
params: { path: { id: 5 } },
234+
});
235+
236+
// URL: `/users/[5]`
237+
```
238+
239+
### Default Path Serializer
212240

213241
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:
214242

0 commit comments

Comments
 (0)