ajaishankar / openapi-typescript-fetch

A typed fetch client for openapi-typescript
MIT License
233 stars 54 forks source link

fix request body type detection when using headers, cookies #23

Open fedorg opened 2 years ago

fedorg commented 2 years ago

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 commented 2 years ago

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

ajaishankar commented 2 years ago

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...

duncanbeevers commented 2 years ago

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