Skip to content

Please rename .del to .delete because of TS #1218

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
1 task
Sawtaytoes opened this issue Jul 9, 2023 · 8 comments
Closed
1 task

Please rename .del to .delete because of TS #1218

Sawtaytoes opened this issue Jul 9, 2023 · 8 comments
Labels
enhancement New feature or request openapi-fetch Relevant to the openapi-fetch library PRs welcome PRs are welcome to solve this issue!

Comments

@Sawtaytoes
Copy link

Description

I'm using TypeScript and had a hell of a time trying to figure out why my types for paths 2nd level didn't match the types for the functions on createClient. I believe it's because of del. Wasted 10 hours on this because it's so hard to catch.

I wrapped createClient, so I could use it in a React context with react-query through a helper hook. But I kept getting errors doing openapiFetchClient[method] where method was paths[Path].

TS was erroring, but the message was so convoluted. Even looking at the types, I couldn't tell what was wrong because I didn't have a delete in my paths only that TS wasn't happy about it.

Proposal

The types created from OpenAPI-TypeScript should 100% match the function names in createClient to allow easier wrapping of createClient with type-safety.

Please rename .del to .delete.

I had to rewrite the type of createClient to get it working (before I knew about the .del function). Because of rewriting that type, I ran into an issue where del didn't work, but now TS didn't error, and react-query ate the runtime error.

Checklist

@Sawtaytoes Sawtaytoes added enhancement New feature or request PRs welcome PRs are welcome to solve this issue! openapi-fetch Relevant to the openapi-fetch library labels Jul 9, 2023
@drwpow
Copy link
Contributor

drwpow commented Jul 9, 2023

delete is a reserved word in JS, which means you couldn’t spread it, the original method couldn’t have that name, etc. It could only be named delete when it’s an object key. Though I agree it would be good to match the OpenAPI method. Let me see if there would be any potential footguns / bugs / confusion from this.

@Sawtaytoes
Copy link
Author

Sawtaytoes commented Jul 10, 2023

I completely forgot about the reserved word. Been forever since I used it.

Confusion from using the keyword delete or naming the OpenAPI-TypeScript generation as del?

Another option is to name it DELETE and GET, etc and update OpenAPI-TypeScript as well. But who would assume an enum-style prop would be a function? Yuck.

@ysulyma
Copy link

ysulyma commented Jul 11, 2023

I encountered this recently and just did

/**
 * Map HTTP method to the corresponding `openapi-fetch` method.
 * @param {string} str
 */
function methodName(str) {
  if (str === "delete") return "del";
  return str;
}

@ptlthg
Copy link

ptlthg commented Jul 18, 2023

Another option is to name it DELETE and GET, etc and update OpenAPI-TypeScript as well. But who would assume an enum-style prop would be a function? Yuck.

Would just like to point out that this is the route SvelteKit took for similar reasons, and you can read the discussion here. Personally, I don't mind the capitalization and agree with the reasoning made there.

@drwpow
Copy link
Contributor

drwpow commented Jul 18, 2023

Would just like to point out that this is the route SvelteKit took for similar reasons, and you can read the discussion here. Personally, I don't mind the capitalization and agree with the reasoning made there.

That’s some fantastic prior art and discussion. Thank you for sharing! I’m strongly inclined to copy this pattern (as a breaking change).

@drwpow
Copy link
Contributor

drwpow commented Jul 25, 2023

v0.7.0 makes the breaking change of renaming all methods to uppercase. Which is annoying for some, but it’s made better by the fact that with pre-1.0 versions, minor can introduce breaking changes (and even utilities like pnpm update won’t bump minor 0.x versions).

@drwpow
Copy link
Contributor

drwpow commented Jul 25, 2023

Hopefully this is an adequate fix to the issue! 🙂 Marking as closed, but still open to feedback here.

@drwpow drwpow closed this as completed Jul 25, 2023
@Sawtaytoes
Copy link
Author

Thanks for fixing this issue ;).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request 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

4 participants