Skip to content

Ability to access response object in onError function #2257

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
1 task done
deweve opened this issue Apr 18, 2025 · 0 comments
Open
1 task done

Ability to access response object in onError function #2257

deweve opened this issue Apr 18, 2025 · 0 comments
Labels
enhancement New feature or request openapi-react-query Relevant to openapi-react-query

Comments

@deweve
Copy link

deweve commented Apr 18, 2025

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

@deweve deweve added enhancement New feature or request openapi-react-query Relevant to openapi-react-query labels Apr 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request openapi-react-query Relevant to openapi-react-query
Projects
None yet
Development

No branches or pull requests

1 participant