fabien0102 / openapi-codegen

A tool for generating code base on an OpenAPI schema.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Is it possible to not expose fetch methods?

Richard87 opened this issue · comments

Hi!

We need React Context in our fetch (to get a valid auth token).

If fetchXXX is used directly this will not work. Is it possible to not expose those methods?

or even better, automatically add all as a api client hook:

const api = useApi()
api.fetchXYZ()

Hi,
You can use the context and fetcher files for this. Those files are only generated when they don't exists and meant to be tweak for this exact usecase

You can see an example here:

  1. Retrieve the token from a custom react hook and expose it inside fetcherOptions

  2. Inject it in the fetch (as you can see, the fetcher is using the types from the custom hook)

    } & GithubContext["fetcherOptions"];

To understand "the magic", you can have a look on the generated components:

https://github.com/fabien0102/openapi-codegen/blob/138a808be699cc65383708744e72ffe26b85b9eb/examples/frontend/src/github/githubComponents.ts#L172-L182

Every components is calling this custom hook (that you can tweak) and send the result the fetcher (that you can also tweak)

Hi!

Thanks for the explanation, that helps a lot 🙏