reduxjs / redux-toolkit

The official, opinionated, batteries-included toolset for efficient Redux development
https://redux-toolkit.js.org
MIT License
10.76k stars 1.18k forks source link

RTKQ Codegen: Multiple API responses (200, 404, etc.) #2157

Open mikewheaton opened 2 years ago

mikewheaton commented 2 years ago

I'm using rtk-query-codegen-api to generate from an OpenAPI spec. The endpoints have multiple responses (200, 404, etc.) with different values. Here's a simplified example:

  /user/city:
    post:
      operationId: setCity
      responses:
        '200':
          content:
            application/json:
              schema:
                type: string
        '404':
          content:
            application/json:
              schema:
                type: object
                properties:
                  error:
                    type: string
                  friendlyMessage:
                    type: string

The codegen then produces a single type for the response:

export type EnterBonusCodeApiResponse = /** status 200 OK */ string;

Should it also be producing the 404 response? In the case of an error I'd like to be able to display friendlyMessage, but there are no generated types containing this property. It's contained inside the error object that comes back from the useSetCity() hook, but the types are missing friendlyMessage so I can't get it out safely.

Any suggestions on how to best implement this? I'm hoping for full type safety for multiple response types.

gavishSpirent commented 2 years ago

Any update on this?

riyaadh-abrahams commented 2 years ago

Also ran into this issue now. The type itself does get generated but does not get linked to the mutation in any way. Is there a way to add this manually using enhanceEndpoints maybe?

anisometropie commented 1 year ago

Having the same issue. How do you generate error statuses 4xx 5xx ? only 2xx are generated