fabien0102 / openapi-codegen

A tool for generating code base on an OpenAPI schema.
491 stars 61 forks source link

Enhancement: allow to return a response wrapper from a Fetcher function #97

Open bcucumber opened 2 years ago

bcucumber commented 2 years ago

Hi,

I would like to suggest yet another enhancement to the library.

Currently, both the fetch fn and hook expect to return TData which doesn't allow for returning a wrapper from the fetcher itself. While not always necessary, there are times where you might need to have additional data on the response (such as status code, even when there is no error at all).

I'm no expert on the matter nor do I know that much of the inner workings of the library, but I think this could be somehow solved by the following ways:

1) Exporting a TDataWrapper (or any other name) which would be a generic type that would use TData and where you can add anything you require (if necessary, by default it could be just TDataWrapper = TData) and using it from the components file.

2) Adding a ReturnType<typeof myFetchFn> return type to the hooks and fetch fn of the type of the Fetcher function itself (with TData as a generic type), which would probably involve less changes to current Fetcher codebase, if any. This might sound confusing at a first glance, but the following can be used a source for the same behaviour I'm talking about (taken from Orval, another open api code generator): https://github.com/anymaniax/orval/blob/master/samples/react-query/custom-client/src/api/endpoints/petstoreFromFileSpecWithTransformer.ts#L54

Cheers!

fabien0102 commented 2 years ago

That sounds like a reasonable improvement, even if I'm wondering what kind of use case you have that need to do something regarding the statusCode 😅

needim commented 1 year ago

@bcucumber I am also curious, can you provide some use cases? It looks interesting.

bcucumber2 commented 1 year ago

Hi everyone, I'm sorry for the huge delay. furthermore, I lost my access to my original account and I was not able to recover it unfortunately.

About the issue in hand, one of the reasons why it was required was, for example, if you wanted to build a testing dashboard for the APIs, in which it would be recommended to show the result http status (200, 201, etc.). I know it's not pretty and there are other tools for that purpose, but that's the specific use case for this.

Thanks!

Edit: another reason could pretty well be something like this: https://github.com/fabien0102/openapi-codegen/issues/110. If you need to get a header in a response promise result, you wouldn't be able as you would only get the body, however, if you were to wrap it with an object that has fetch response along with the payload, it would get much easier.