openapi-typescript-fetch icon indicating copy to clipboard operation
openapi-typescript-fetch copied to clipboard

fix request body type detection when using headers, cookies

Open fedorg opened this issue 3 years ago • 3 comments
trafficstars

The library had trouble detecting the POST body payload types of my API. The argument was being typed as an empty object, and preventing me from sending any payloads. I had to use hacks such as these to make things work:

type CreateStationArg = components['schemas']['CreateStation'] // type hack
export const CreateStation = (arg: CreateStationArg, init?: RequestInit) => fetcher.path("/stations").method("post").create()(arg as any, init)

Instead of this:

export const CreateStation2 = fetcher.path("/stations").method("post").create()
// const CreateStation2: (arg: Record<string, never>, init?: RequestInit | undefined) => ...

This PR fixes this by changing the inference of OpArgType slightly. This allows for these types of POST payloads to be sent. I have also added a test for a slice of my problematic API.

fedorg avatar Apr 20 '22 22:04 fedorg

Since the merge request has stalled, I have published my version on NPM: @fedorg/openapi-typescript-fetch You can use if you have the same issue with Record<string, never> showing up in arguments. Use at least version ^1.1.3

fedorg avatar Apr 25 '22 13:04 fedorg

HI @fedorg there are a couple of commits in master that has not yet been released (my bad) and a fix addressing this exact issue is one of them https://github.com/ajaishankar/openapi-typescript-fetch/commit/b62ee714e645c14b8e07c98aaaa53880e7d6c847. Will release the pending changes today, sorry about the confusion and your time...

ajaishankar avatar Apr 26 '22 13:04 ajaishankar

These changes were released in 1.1.3. I think this PR should be closed.

duncanbeevers avatar Sep 13 '22 12:09 duncanbeevers