Skip to content

Commit 8d11701

Browse files
authored
Fix impossible body (#1242)
1 parent eb894cb commit 8d11701

File tree

5 files changed

+26
-4
lines changed

5 files changed

+26
-4
lines changed

.changeset/cold-pets-sit.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"openapi-fetch": patch
3+
---
4+
5+
Fix impossible body typing

packages/openapi-fetch/src/index.test.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ describe("client", () => {
235235

236236
// expect error on missing `body`
237237
// @ts-expect-error
238-
await client.GET("/blogposts", {});
238+
await client.PUT("/blogposts", {});
239239

240240
// expect error on missing fields
241241
// @ts-expect-error
@@ -401,9 +401,8 @@ describe("client", () => {
401401
it("returns empty object on 204", async () => {
402402
const client = createClient<paths>();
403403
mockFetchOnce({ status: 204, body: "" });
404-
const { data, error, response } = await client.PUT("/tag/{name}", {
404+
const { data, error, response } = await client.DELETE("/tag/{name}", {
405405
params: { path: { name: "New Tag" } },
406-
body: { description: "This is a new tag" },
407406
});
408407

409408
// assert correct data was returned

packages/openapi-fetch/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export type Params<T> = T extends { parameters: any } ? { params: NonNullable<T[
5151
export type RequestBodyObj<T> = T extends { requestBody?: any } ? T["requestBody"] : never;
5252
export type RequestBodyContent<T> = undefined extends RequestBodyObj<T> ? FilterKeys<NonNullable<RequestBodyObj<T>>, "content"> | undefined : FilterKeys<RequestBodyObj<T>, "content">;
5353
export type RequestBodyMedia<T> = FilterKeys<RequestBodyContent<T>, MediaType> extends never ? FilterKeys<NonNullable<RequestBodyContent<T>>, MediaType> | undefined : FilterKeys<RequestBodyContent<T>, MediaType>;
54-
export type RequestBody<T> = undefined extends RequestBodyMedia<T> ? { body?: RequestBodyMedia<T> } : { body: RequestBodyMedia<T> };
54+
export type RequestBody<T> = RequestBodyMedia<T> extends never ? { body?: never } : undefined extends RequestBodyMedia<T> ? { body?: RequestBodyMedia<T> } : { body: RequestBodyMedia<T> };
5555
export type QuerySerializer<T> = (query: T extends { parameters: any } ? NonNullable<T["parameters"]["query"]> : Record<string, unknown>) => string;
5656
export type BodySerializer<T> = (body: RequestBodyMedia<T>) => any;
5757
export type RequestOptions<T> = Params<T> &

packages/openapi-fetch/test/v1.d.ts

+12
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,18 @@ export interface paths {
176176
500: components["responses"]["Error"];
177177
};
178178
};
179+
delete: {
180+
parameters: {
181+
path: {
182+
name: string;
183+
};
184+
};
185+
responses: {
186+
/** @description No Content */
187+
204: never;
188+
500: components["responses"]["Error"];
189+
};
190+
};
179191
parameters: {
180192
path: {
181193
name: string;

packages/openapi-fetch/test/v1.yaml

+6
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,12 @@ paths:
190190
$ref: '#/components/responses/CreateTag'
191191
500:
192192
$ref: '#/components/responses/Error'
193+
delete:
194+
responses:
195+
204:
196+
description: No Content
197+
500:
198+
$ref: '#/components/responses/Error'
193199
/default-as-error:
194200
get:
195201
responses:

0 commit comments

Comments
 (0)