drwpow / openapi-fetch

Ultra-fast fetching for TypeScript generated automatically from your OpenAPI schema

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add optional query method?

HerrBertling opened this issue · comments

Description

First off: Thanks for this. I’ve tried building exactly this functionality a few days ago. And then found this repo 😅

The API I’ll be querying has a specific way of handling array params in queries (arrayValue[]=foo&arrayValue[]=bar to pass arrayValue: [‘foo, ‘bar’]). It also replaces every empty space with a +. Both of these I’ve extracted into a tiny function to ensure a well written query for this API in my stab at building this.

I’ve seen that you “just” stringify the query from new URLSearchParams.

That’s the reason why I haven’t directly used your library - I already know that the requests would fail due to the query params being passed in the wrong format for that API.

Proposal

I could imagine keeping the current functionality to generate the query. And add an optional parseQuery parameter which would be used if supplied. What do you think?

Checklist

Thanks for suggesting this. This was a question I had in the back of my mind but didn’t solve for yet.

I know there’s no “official” standard for arrayed parameters, but I am used to the exact syntax you proposed—suffixing with []. I think that’s common enough this library should just handle transforming array params in a way that will work for most people. The type assertion is already correct; the runtime transform just needs to be added. I can handle this part.

But also yes I think a parseParam() API would be essential as well. For all the other edge cases people would need, such as people needing arrayed params transformed differently, or other rate edge cases (I’ve actually needed base64 parameter transformation of a long string before). If you’d want to propose an API for this, I’d be open!

We use https://www.npmjs.com/package/qs for serializing our query parameters. I'm forking to add this functionality so I can use our function.

Thanks for the PR! Will check that out later.

And yes I’m sure we can add this without adding 34 kB to this library from qs.

Was not suggesting that, sorry for the confusion. PR only allows passing any function to serialize with, but falls back to existing implementation.

This has landed! Doing a little more testing & a little bit of polish, but this will go out in the next release (refer to the README on the next release; may tweak what’s there a bit)

Awesome, thank you 👍🚀