openapi-react-query-codegen icon indicating copy to clipboard operation
openapi-react-query-codegen copied to clipboard

TError always unknown

Open Alexandre-Cornillon opened this issue 1 year ago • 1 comments

Describe the bug

Hello, When we use the TError type from codegen, the type is always unknown. How to have the payload or message of all the 400s and 500s errors typed ? Thanks

To Reproduce Steps to reproduce the behavior: npx --package @7nohe/openapi-react-query-codegen openapi-rq -i https://petstore.swagger.io/v2/swagger.json -o src/generated

OpenAPI spec file https://petstore.swagger.io/v2/swagger.json

Expected behavior I need to have the content or message of all the 400s and 500s errors.

Screenshots Screenshot 2024-09-05 at 17 45 16

  • OS: macOS
  • Version: Sonoma 14.5

Additional context Add any other context about the problem here.

Alexandre-Cornillon avatar Sep 05 '24 15:09 Alexandre-Cornillon

Facing the same issue, I don't know how to use error message in my returned data with the onError method.

My openapi.yml has definitions for error objects returned on 400 error code and these are picked up in the types.gen.ts file but I don't see how to use them in my mutate hooks.

w00kie avatar Oct 03 '24 13:10 w00kie

From v2, TError is now typed.

export const useFindPets = <
  TData = Common.FindPetsDefaultResponse,
  TError = FindPetsError,
  TQueryKey extends Array<unknown> = unknown[],
>(
  clientOptions: Options<FindPetsData, true> = {},
  queryKey?: TQueryKey,
  options?: Omit<UseQueryOptions<TData, TError>, "queryKey" | "queryFn">,
) =>
  useQuery<TData, TError>({
    queryKey: Common.UseFindPetsKeyFn(clientOptions, queryKey),
    queryFn: () =>
      findPets({ ...clientOptions }).then(
        (response) => response.data as TData,
      ) as TData,
    ...options,
  });

7nohe avatar Oct 14 '24 07:10 7nohe