redux-toolkit icon indicating copy to clipboard operation
redux-toolkit copied to clipboard

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

Open mikewheaton opened this issue 3 years ago • 3 comments

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.

mikewheaton avatar Mar 21 '22 21:03 mikewheaton

Any update on this?

gavishSpirent avatar Jun 08 '22 13:06 gavishSpirent

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?

riyaadh-abrahams avatar Jun 19 '22 20:06 riyaadh-abrahams

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

anisometropie avatar Jul 07 '23 09:07 anisometropie