7nohe / openapi-react-query-codegen

OpenAPI React Query Codegen is a code generator for creating React Query (also known as TanStack Query) hooks based on your OpenAPI schema.
https://openapi-react-query-codegen.vercel.app
MIT License
298 stars 30 forks source link

TError always unknown #147

Closed Alexandre-Cornillon closed 1 month ago

Alexandre-Cornillon commented 2 months ago

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

Additional context Add any other context about the problem here.

w00kie commented 1 month ago

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.

7nohe commented 1 month ago

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