hey-api / openapi-ts

✨ Turn your OpenAPI specification into a beautiful TypeScript client
https://heyapi.vercel.app
MIT License
634 stars 44 forks source link

Handle different http status like 200 or 204 #676

Closed bielas closed 4 days ago

bielas commented 2 weeks ago

Imagine I have backend api definition

  @Operation(summary = "Get my next training")
    @GetMapping(path = ["/next-training"])
    @ApiResponse(responseCode = "200", description = "Successfully get my next training")
    @ApiResponse(responseCode = "204", description = "Successfully get my next training")
    fun getMyNextTraining(): ResponseEntity<NextTrainingResponse>

when backend reponds with 204 status I want to show/hide component and vice versa on frontent. How that is possible with code generated in typescript when I cant explicilty change any code in request.ts file nor any othere from those that are generated cause they will be overwritten every time new generation performs. On frontend it looks like:

    const getNextTrainingCallback = useCallback((): Observable<NextTraining> => {
        return from(getMyNextTraining()).pipe(map((response) => mapNextTraining(response)));
    }, []);

    export const getMyNextTraining = (
    data: GetMyNextTrainingData = {}
): CancelablePromise<GetMyNextTrainingResponse> => {
    return __request(OpenAPI, {
        method: 'GET',
        url: '/self/trainer/next-training',
        headers: {
            'Content-Language': data.contentLanguage
        }
    });
};

and here what I see to the response only body is passed (this part of generated code which is responsbile of generating request.ts file):

image
mrlubos commented 2 weeks ago

Hey @bielas, are you able to use the new Fetch API client? You have access to the full response object there

bielas commented 2 weeks ago

@mrlubos i see I am missing that. Thanks! Do you have any guide how to add properly into the react project? Are there any specific place where createClient method should be called?

mrlubos commented 2 weeks ago

There's an example with React project in the docs 🚀

bielas commented 2 weeks ago

@mrlubos is there any way to return a typed Response to in further code have body and other props of response that are typed to the body?

    const getNextTrainingCallback2 = useCallback((): Observable<Response<NextTrainingResponse>
    > => {
        return from(getMyNextTraining()).pipe(
            map((response) => response.response)
        );
    }, []);
mrlubos commented 2 weeks ago

Not at the moment @bielas