Skip to content

Generated type definitions should not include the error case #2046

Open
@manuschillernordlb

Description

@manuschillernordlb

Models generated with openapi-typescript-codegen contain the correctly inferred types from our OpenAPI Spec, but also the structure for the error object.

That should probably not be the case adds unnecessary of guarding code when calling the api functions.

Example:

const { data: favorites, isLoading } = useQuery({
    queryKey: ["favorites"],
    queryFn: async () => {
      const result = await api.getFavorites();

      if ("status" in result) throw new Error(result.title, { cause: result });

      return result.favorites;
    },
    initialData: () => [],
  });

It seems like the generated client should throw (and therefore not resolve) in any case when response.ok is not true:

if (!result.ok) {
const errorStatus = result.status ?? 'unknown';
const errorStatusText = result.statusText ?? 'unknown';
const errorBody = (() => {
try {
return JSON.stringify(result.body, null, 2);
} catch (e) {
return undefined;
}
})();
throw new ApiError(options, result,
`Generic Error: status: ${errorStatus}; status text: ${errorStatusText}; body: ${errorBody}`
);

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions