Skip to content

Commit

Permalink
Fix impossible body (#1242)
Browse files Browse the repository at this point in the history
  • Loading branch information
drwpow authored Aug 9, 2023
1 parent eb894cb commit 8d11701
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/cold-pets-sit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"openapi-fetch": patch
---

Fix impossible body typing
5 changes: 2 additions & 3 deletions packages/openapi-fetch/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -401,9 +401,8 @@ describe("client", () => {
it("returns empty object on 204", async () => {
const client = createClient<paths>();
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
Expand Down
2 changes: 1 addition & 1 deletion packages/openapi-fetch/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export type Params<T> = T extends { parameters: any } ? { params: NonNullable<T[
export type RequestBodyObj<T> = T extends { requestBody?: any } ? T["requestBody"] : never;
export type RequestBodyContent<T> = undefined extends RequestBodyObj<T> ? FilterKeys<NonNullable<RequestBodyObj<T>>, "content"> | undefined : FilterKeys<RequestBodyObj<T>, "content">;
export type RequestBodyMedia<T> = FilterKeys<RequestBodyContent<T>, MediaType> extends never ? FilterKeys<NonNullable<RequestBodyContent<T>>, MediaType> | undefined : FilterKeys<RequestBodyContent<T>, MediaType>;
export type RequestBody<T> = undefined extends RequestBodyMedia<T> ? { body?: RequestBodyMedia<T> } : { body: RequestBodyMedia<T> };
export type RequestBody<T> = RequestBodyMedia<T> extends never ? { body?: never } : undefined extends RequestBodyMedia<T> ? { body?: RequestBodyMedia<T> } : { body: RequestBodyMedia<T> };
export type QuerySerializer<T> = (query: T extends { parameters: any } ? NonNullable<T["parameters"]["query"]> : Record<string, unknown>) => string;
export type BodySerializer<T> = (body: RequestBodyMedia<T>) => any;
export type RequestOptions<T> = Params<T> &
Expand Down
12 changes: 12 additions & 0 deletions packages/openapi-fetch/test/v1.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions packages/openapi-fetch/test/v1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 8d11701

Please sign in to comment.