hey-api / openapi-ts

✨ Turn your OpenAPI specification into a beautiful TypeScript client

Home Page:https://heyapi.vercel.app

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Handle different http status like 200 or 204

bielas opened this issue · comments

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

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

@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?

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

@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)
		);
	}, []);

Not at the moment @bielas