Skip to content

Commit 1a1f851

Browse files
openapi-ts-botgithub-actions[bot]
authored andcommitted
[ci] release (openapi-ts#2087)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent f1edc97 commit 1a1f851

File tree

8 files changed

+55
-10
lines changed

8 files changed

+55
-10
lines changed

.changeset/funny-beans-film.md

Lines changed: 0 additions & 5 deletions
This file was deleted.

docs/openapi-fetch/api.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,22 @@ or when instantiating the client.
192192

193193
:::
194194

195+
### URL-encoded body
196+
197+
To send a body request in `application/x-www-form-urlencoded` format, which is commonly used to transmit key-value pairs in APIs like OAuth 2.0, pass the appropriate header and body as an object. `openapi-fetch` will automatically encode the body to the correct format.
198+
199+
```ts
200+
const { data, error } = await client.POST("/submit", {
201+
body: {
202+
clientId: "someClientId",
203+
clientSecret: "someClientSecret",
204+
},
205+
headers: {
206+
Accept: "application/x-www-form-encoded",
207+
},
208+
});
209+
```
210+
195211
## Path serialization
196212

197213
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:

packages/openapi-fetch/src/index.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export default function createClient(clientOptions) {
7272
});
7373
}
7474

75-
const serializedBody = body === undefined ? undefined : bodySerializer(body);
75+
const serializedBody = body === undefined ? undefined : bodySerializer(body, headers);
7676

7777
const defaultHeaders =
7878
// with no body, we should not to set Content-Type
@@ -568,10 +568,17 @@ export function defaultPathSerializer(pathname, pathParams) {
568568
* Serialize body object to string
569569
* @type {import("./index.js").defaultBodySerializer}
570570
*/
571-
export function defaultBodySerializer(body) {
571+
export function defaultBodySerializer(body, headers) {
572572
if (body instanceof FormData) {
573573
return body;
574574
}
575+
if (headers) {
576+
const contentType = headers.get instanceof Function ? headers.get("Content-Type") : (headers["Content-Type"] ?? headers["content-type"]);
577+
console.log("Content-Type", contentType);
578+
if (contentType === "application/x-www-form-urlencoded") {
579+
return new URLSearchParams(body).toString();
580+
}
581+
}
575582
return JSON.stringify(body);
576583
}
577584

packages/openapi-fetch/test/common/request.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,19 @@ describe("request", () => {
265265
expect(bodyUsed).toBe(true);
266266
expect(bodyText).toBe("0");
267267
});
268+
269+
test("`application/x-www-form-urlencoded` body", async () => {
270+
const { bodyUsed, bodyText } = await fireRequestAndGetBodyInformation({
271+
method: "POST",
272+
fetchOptions: {
273+
body: { key1: "value1", key2: "value2" },
274+
headers: { "Content-Type": "application/x-www-form-urlencoded" },
275+
},
276+
});
277+
278+
expect(bodyUsed).toBe(true);
279+
expect(bodyText).toBe("key1=value1&key2=value2");
280+
});
268281
});
269282

270283
test("cookie header is preserved", async () => {

packages/openapi-typescript/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# openapi-typescript
22

3+
## 7.5.2
4+
5+
### Patch Changes
6+
7+
- [#2085](https://github.com/openapi-ts/openapi-typescript/pull/2085) [`2bffe2a`](https://github.com/openapi-ts/openapi-typescript/commit/2bffe2a652864a54c8dc969327e4a8eb4081eb25) Thanks [@drwpow](https://github.com/drwpow)! - Update @redocly/openapi-core to 1.27.0
8+
39
## 7.5.1
410

511
### Patch Changes

packages/openapi-typescript/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "openapi-typescript",
33
"description": "Convert OpenAPI 3.0 & 3.1 schemas to TypeScript",
4-
"version": "7.5.1",
4+
"version": "7.5.2",
55
"author": {
66
"name": "Drew Powers",
77
"email": "drew@pow.rs"

packages/swr-openapi/CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# swr-openapi
22

3+
## 5.1.3
4+
5+
### Patch Changes
6+
7+
- Updated dependencies [[`2bffe2a`](https://github.com/openapi-ts/openapi-typescript/commit/2bffe2a652864a54c8dc969327e4a8eb4081eb25)]:
8+
- openapi-typescript@7.5.2
9+
- openapi-fetch@0.13.4
10+
311
## 5.1.2
412

513
### Patch Changes

packages/swr-openapi/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "swr-openapi",
33
"description": "Generate SWR hooks from OpenAPI schemas",
4-
"version": "5.1.2",
4+
"version": "5.1.3",
55
"author": {
66
"name": "Hunter Tunnicliff",
77
"email": "hunter@tunnicliff.co"
@@ -57,7 +57,7 @@
5757
},
5858
"peerDependencies": {
5959
"openapi-fetch": "0.13.4",
60-
"openapi-typescript": "7.5.1",
60+
"openapi-typescript": "7.5.2",
6161
"react": "18 || 19",
6262
"swr": "2",
6363
"typescript": "^5.x"

0 commit comments

Comments
 (0)