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

Partial query params aren't optional #32

Open
TkDodo opened this issue Mar 19, 2024 · 1 comment
Open

Partial query params aren't optional #32

TkDodo opened this issue Mar 19, 2024 · 1 comment

Comments

@TkDodo
Copy link

TkDodo commented Mar 19, 2024

I've copied the output of the standard PetStore example into a TypeScript playground.

At the end, I've created an api and tried to invoke findByStatus:

const api = createApiClient((method, url, params) =>
 fetch(url, { method, body: JSON.stringify(params) }).then((res) => res.json()),
);

api.get("/pet/findByStatus");

The error you'll see is:

Expected 2 arguments, but got 1.(2554)
input.tsx(316, 5): Arguments for the rest parameter 'params' were not provided.

When looking at the type of that function, config is indeed required:

(method) ApiClient.get<"/pet/findByStatus", Endpoints.get_FindPetsByStatus>(path: "/pet/findByStatus", config: {
    query: Partial<{
        status: "available" | "pending" | "sold";
    }>;
}): Promise<Schemas.Pet[]>

Screenshot 2024-03-19 at 16 44 18

However, running the same code in the typed-openapi playground shows the whole config as optional:

 const api = createApiClient((method, url, params) =>
   fetch(url, { method, body: JSON.stringify(params) }).then((res) => res.json()),
 );

 api.get("/pet/findByStatus");

Screenshot 2024-03-19 at 16 42 40

I'm not sure where that difference is coming from, but it seems that I have to pass { query: {} } to satisfy typescript right now and I don't think that is correct :)

@TkDodo
Copy link
Author

TkDodo commented Mar 25, 2024

as a workaround, I've made the partial query optional, too:

- query: Partial<{
+ query?: Partial<{
    status: "available" | "pending" | "sold";
}>;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant