Open
Description
Description
For now, the onError receive as argument Response["error”], it content the body of the response, but you are not able to access the status code of the response which could be interesting if you have different body between with code 400 or 404.
Proposal
Maybe it needs to change too much things. I presume what needs to be change is GetResponseContent from openapi-typescript-helpers
type GetResponseContent<
T extends Record<string | number, any>,
Media extends MediaType = MediaType,
ResponseCode extends keyof T = keyof T,
> = ResponseCode extends keyof T
? {
[K in ResponseCode]: T[K]["content"] extends Record<string, any>
? FilterKeys<T[K]["content"], Media> extends never
? T[K]["content"]
: FilterKeys<T[K]["content"], Media>
: K extends keyof T
? T[K]["content"]
: never;
}[ResponseCode]
: never;
/**
* Return all 5XX and 4XX responses (in that order) from a Response Object Map
*/
export type ErrorResponse<
T extends Record<string | number, any>,
Media extends MediaType = MediaType,
> = GetResponseContent<T, Media, ErrorStatus>;
Or maybe the query function (openapi-react-query) could return the response object instead of only the error.
const queryFn = async <Method extends HttpMethod, Path extends PathsWithMethod<Paths, Method>>({
queryKey: [method, path, init],
signal,
}: QueryFunctionContext<QueryKey<Paths, Method, Path>>) => {
const mth = method.toUpperCase() as Uppercase<typeof method>;
const fn = client[mth] as ClientMethod<Paths, typeof method, Media>;
const { data, error, response } = await fn(path, { signal, ...(init as any) }); // TODO: find a way to avoid as any
if (error) {
throw error; // Could throw {response, error}
}
return data;
};
I can contribute but tell me if there already a solution
Extra
- I’m willing to open a PR (see CONTRIBUTING.md)