Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve impossible body typing #1242

Merged
merged 1 commit into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading