Skip to content

Commit

Permalink
Fix impossible body
Browse files Browse the repository at this point in the history
  • Loading branch information
drwpow committed Jul 25, 2023
1 parent e90632b commit 5bd37ba
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
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 @@ -233,7 +233,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 @@ -398,9 +398,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.del("/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 @@ -47,7 +47,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 @@ -153,6 +153,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 @@ -160,6 +160,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 5bd37ba

Please sign in to comment.