diff --git a/.changeset/cold-pets-sit.md b/.changeset/cold-pets-sit.md new file mode 100644 index 000000000..26403cf38 --- /dev/null +++ b/.changeset/cold-pets-sit.md @@ -0,0 +1,5 @@ +--- +"openapi-fetch": patch +--- + +Fix impossible body typing diff --git a/packages/openapi-fetch/src/index.test.ts b/packages/openapi-fetch/src/index.test.ts index f421f225c..ad89b6ffb 100644 --- a/packages/openapi-fetch/src/index.test.ts +++ b/packages/openapi-fetch/src/index.test.ts @@ -235,7 +235,7 @@ describe("client", () => { // expect error on missing `body` // @ts-expect-error - await client.GET("/blogposts", {}); + await client.PUT("/blogposts", {}); // expect error on missing fields // @ts-expect-error @@ -401,9 +401,8 @@ describe("client", () => { it("returns empty object on 204", async () => { const client = createClient(); mockFetchOnce({ status: 204, body: "" }); - const { data, error, response } = await client.PUT("/tag/{name}", { + const { data, error, response } = await client.DELETE("/tag/{name}", { params: { path: { name: "New Tag" } }, - body: { description: "This is a new tag" }, }); // assert correct data was returned diff --git a/packages/openapi-fetch/src/index.ts b/packages/openapi-fetch/src/index.ts index 2fd36073b..263e435ca 100644 --- a/packages/openapi-fetch/src/index.ts +++ b/packages/openapi-fetch/src/index.ts @@ -51,7 +51,7 @@ export type Params = T extends { parameters: any } ? { params: NonNullable = T extends { requestBody?: any } ? T["requestBody"] : never; export type RequestBodyContent = undefined extends RequestBodyObj ? FilterKeys>, "content"> | undefined : FilterKeys, "content">; export type RequestBodyMedia = FilterKeys, MediaType> extends never ? FilterKeys>, MediaType> | undefined : FilterKeys, MediaType>; -export type RequestBody = undefined extends RequestBodyMedia ? { body?: RequestBodyMedia } : { body: RequestBodyMedia }; +export type RequestBody = RequestBodyMedia extends never ? { body?: never } : undefined extends RequestBodyMedia ? { body?: RequestBodyMedia } : { body: RequestBodyMedia }; export type QuerySerializer = (query: T extends { parameters: any } ? NonNullable : Record) => string; export type BodySerializer = (body: RequestBodyMedia) => any; export type RequestOptions = Params & diff --git a/packages/openapi-fetch/test/v1.d.ts b/packages/openapi-fetch/test/v1.d.ts index 74b13f97d..879f1b3be 100644 --- a/packages/openapi-fetch/test/v1.d.ts +++ b/packages/openapi-fetch/test/v1.d.ts @@ -176,6 +176,18 @@ export interface paths { 500: components["responses"]["Error"]; }; }; + delete: { + parameters: { + path: { + name: string; + }; + }; + responses: { + /** @description No Content */ + 204: never; + 500: components["responses"]["Error"]; + }; + }; parameters: { path: { name: string; diff --git a/packages/openapi-fetch/test/v1.yaml b/packages/openapi-fetch/test/v1.yaml index 6425c1ac2..8ba03d68a 100644 --- a/packages/openapi-fetch/test/v1.yaml +++ b/packages/openapi-fetch/test/v1.yaml @@ -190,6 +190,12 @@ paths: $ref: '#/components/responses/CreateTag' 500: $ref: '#/components/responses/Error' + delete: + responses: + 204: + description: No Content + 500: + $ref: '#/components/responses/Error' /default-as-error: get: responses: