Skip to content

Optionless endpoints still require 2nd param #1127

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

Closed
drwpow opened this issue Mar 14, 2023 · 2 comments
Closed

Optionless endpoints still require 2nd param #1127

drwpow opened this issue Mar 14, 2023 · 2 comments
Labels
bug Something isn't working openapi-fetch Relevant to the openapi-fetch library PRs welcome PRs are welcome to solve this issue!

Comments

@drwpow
Copy link
Contributor

drwpow commented Mar 14, 2023

For simple endpoints that don’t require any parameters or requestBodies, the 2nd param is still required:

import { paths } from './v1';

const { get } = createClient<paths>();
get('/self');
// ~~~~~~~~~~
// ^ Expected 2 parameters, but got 1.

The solution is adding an empty {} to the call, like so:

- get('/self');
+ get('/self', {});

This is because it’s an intersection of { params: … } & { body: … } & RequestInit, which are all required options, but in some scenarios end up having all keys optional which results in an empty object ({}). However, if we make the root types always optional, then the types break, and this library would be pointless.

If there’s a way to solve this without jeopardizing the types, PRs are welcome!

Though note that this is only a cosmetic issue. There are no actual performance or runtime bugs with providing an empty object as the 2nd param.

Counterpoint to fixing: Many APIs won’t have any endpoints with absolutely zero parameters. For the schemas that do, only a few endpoints realistically will deal with this. Further, simply having the 2nd param there can reduce Git diff noise if/when options are ever needed to be passed to that endpoint.

@drwpow drwpow added bug Something isn't working PRs welcome PRs are welcome to solve this issue! labels Mar 14, 2023
@yinzhenyu-su
Copy link

Hello, I have found that using openapi-typescript is a fantastic library that has greatly assisted me in my work. I have also experienced the similar issue you mentioned earlier. Hopefully, my solution can be helpful to you. Please refer to this link for my solution: https://github.com/yinzhenyu-su/api-typing/blob/3ba2c3e7e3ee278d54df46c36de6c501fc943bfa/src/core-type.ts#L131

@drwpow
Copy link
Contributor Author

drwpow commented May 1, 2023

@yinzhenyu-su yes thank you! That helped me fix some type bugs in the 0.1.0 release. There are still a few instances where this happens, but I think we can fix this easily in an upcoming release.

@drwpow drwpow transferred this issue from drwpow/openapi-fetch May 22, 2023
@drwpow drwpow added the openapi-fetch Relevant to the openapi-fetch library label May 22, 2023
drwpow added a commit that referenced this issue Oct 5, 2023
drwpow added a commit that referenced this issue Oct 5, 2023
drwpow added a commit that referenced this issue Oct 5, 2023
@drwpow drwpow closed this as completed in 04dbd6d Oct 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working openapi-fetch Relevant to the openapi-fetch library PRs welcome PRs are welcome to solve this issue!
Projects
None yet
Development

No branches or pull requests

2 participants