hey-api / openapi-ts

✨ Turn your OpenAPI specification into a beautiful TypeScript client

Home Page:https://heyapi.vercel.app

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Default response types (error) override response types

dudasaus opened this issue · comments

Description

Including a default response type for errors (per Swagger recommendation overrides the type for specified status codes.

Example:

spec

paths:
  /templates/{id}:
    get:
      summary: Retrieve a Template
      description: Returns a given Template by ID
      parameters:
        - in: path
          name: id
          description: ID of Template to fetch
          required: true
          schema:
            type: string
      responses:
        '200':
          description: Template response
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Template'
        default:
          description: Not found error
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'

types.gen.ts

export type $OpenApiTs = {
  '/templates/{id}': {
    get: {
      req: {
        /**
         * ID of Template to fetch
         */
        id: string;
      };
      res: {
        /**
         * Not found error
         */
        200: Error;
      };
    };
  };
}

OpenAPI specification (optional)

paths:
  /templates/{id}:
    get:
      summary: Retrieve a Template
      description: Returns a given Template by ID
      parameters:
        - in: path
          name: id
          description: ID of Template to fetch
          required: true
          schema:
            type: string
      responses:
        '200':
          description: Template response
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Template'
        default:
          description: Not found error
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'

Configuration

// https://heyapi.vercel.app/openapi-ts/configuration.html
import { defineConfig } from '@hey-api/openapi-ts';

export default defineConfig({
  client: 'fetch',
  input: 'backend/backend-api.yaml',
  output: 'frontend/src/services/heyapi',
  format: 'prettier',
  services: {
    export: true,
    name: 'Name',
  },
  dryRun: false,
});

System information (optional)

$ npm -v
10.5.0

$ node -v
v18.18.0

Hey @dudasaus, this will be fixed in the next release #500

Awesome! 🥳

@dudasaus please update to 0.43.0 of the package. The fix has just been released.