Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Discussion options

Hi, I really like this project
By the way, how can we get the status code of error with openapi-react-query ?
Let's say we have some code like this, and the it seems that the error object does not contain status code

 const { mutate, error, isPending } = api.useMutation('post', '/login')

Thanks in advance

You must be logged in to vote

Replies: 2 comments · 1 reply

Comment options

@tn-m did you figure something out?

I have the following types:

        400: {
            headers: {
                [name: string]: unknown;
            };
            content: {
                "application/json": components["schemas"]["BadRequestErrorResponse"];
            };
        };
        /** @description Validation Error */
        422: {
            headers: {
                [name: string]: unknown;
            };
            content: {
                "application/json": components["schemas"]["HTTPValidationError"];
            };
        };

I would love to be able to differentiate the error types by the code.

You must be logged in to vote
1 reply
@bluenote10
Comment options

I also just ran into that and haven't found a solution so far, but it looks like there are a few feature requests for it: #2383 (comment)

Comment options

Instead of using openapi-react-query, use react-query itself
Then you can write something like this

export function useGetQuery<P extends Paths<'get'>>(
  path: P,
  params: Params<'get', P> & { rq?: UseQueryOptions; staleTime?: number; enabled?: boolean }
) {
  return useQuery({
    queryKey: [path, params],
    queryFn: async () => {
      const { data, error, response } = await client.GET(path, params)
      if (error) throw error
      return { data, status: response.status }
    },
    staleTime: params.staleTime ?? 1000 * 60 * 5,
    gcTime: 1000 * 60 * 10,
    retry: 0,
    refetchOnWindowFocus: false,
    enabled: params.enabled ?? true,
    ...params?.rq
  })
}
You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
🙏
Q&A
Labels
None yet
4 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.