Skip to content

OpenAPI TypeScript

Directory Structure

.
├── package.json
├── schema.yaml
└── tsconfig.json

Install

sh
pnpm i -D openapi-typescript typescript

schema.yaml

yaml
info:
  title: Hono API
  version: v1
openapi: 3.0.0
tags:
  - name: Hono
    description: Hono API
  - name: Post
    description: Post API
components:
  schemas: {}
  parameters: {}
paths:
  /:
    get:
      tags:
        - Hono
      responses:
        '200':
          description: Hono🔥
          content:
            application/json:
              schema:
                type: object
                properties:
                  message:
                    type: string
                    example: Hono🔥
                required:
                  - message
  /posts:
    post:
      tags:
        - Post
      description: create a new post
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                post:
                  type: string
                  minLength: 1
                  maxLength: 140
              required:
                - post
      responses:
        '201':
          description: Created
        '400':
          description: Bad Request
          content:
            application/json:
              schema:
                type: object
                properties:
                  message:
                    type: string
                    example: Bad Request
                required:
                  - message
        '500':
          description: Internal Server Error
          content:
            application/json:
              schema:
                type: object
                properties:
                  message:
                    type: string
                    example: Internal Server Error
                required:
                  - message
    get:
      tags:
        - Post
      description: get PostList posts with optional pagination
      parameters:
        - schema:
            type: string
          required: true
          name: page
          in: query
        - schema:
            type: string
          required: true
          name: rows
          in: query
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                type: array
                items:
                  type: object
                  properties:
                    id:
                      type: string
                      format: uuid
                    post:
                      type: string
                      minLength: 1
                      maxLength: 140
                    createdAt:
                      type: string
                    updatedAt:
                      type: string
                  required:
                    - id
                    - post
                    - createdAt
                    - updatedAt
        '400':
          description: Bad Request
          content:
            application/json:
              schema:
                type: object
                properties:
                  message:
                    type: string
                    example: Bad Request
                required:
                  - message
        '500':
          description: Internal Server Error
          content:
            application/json:
              schema:
                type: object
                properties:
                  message:
                    type: string
                    example: Internal Server Error
                required:
                  - message
  /posts/{id}:
    put:
      tags:
        - Post
      description: update Post
      parameters:
        - schema:
            type: string
            format: uuid
          required: true
          name: id
          in: path
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                post:
                  type: string
                  minLength: 1
                  maxLength: 140
              required:
                - post
      responses:
        '204':
          description: No Content
        '400':
          description: Bad Request
          content:
            application/json:
              schema:
                type: object
                properties:
                  message:
                    type: string
                    example: Bad Request
                required:
                  - message
        '500':
          description: Internal Server Error
          content:
            application/json:
              schema:
                type: object
                properties:
                  message:
                    type: string
                    example: Internal Server Error
                required:
                  - message
    delete:
      tags:
        - Post
      description: delete post
      parameters:
        - schema:
            type: string
            format: uuid
          required: true
          name: id
          in: path
      responses:
        '204':
          description: No Content
        '400':
          description: Bad Request
          content:
            application/json:
              schema:
                type: object
                properties:
                  message:
                    type: string
                    example: Bad Request
                required:
                  - message
        '500':
          description: Internal Server Error
          content:
            application/json:
              schema:
                type: object
                properties:
                  message:
                    type: string
                    example: Internal Server Error
                required:
                  - message

Generate

sh
pnpx openapi-typescript schema.yaml -o schema.ts

schema.ts

ts
/**
 * This file was auto-generated by openapi-typescript.
 * Do not make direct changes to the file.
 */

export interface paths {
  '/': {
    parameters: {
      query?: never
      header?: never
      path?: never
      cookie?: never
    }
    get: {
      parameters: {
        query?: never
        header?: never
        path?: never
        cookie?: never
      }
      requestBody?: never
      responses: {
        /** @description Hono🔥 */
        200: {
          headers: {
            [name: string]: unknown
          }
          content: {
            'application/json': {
              /** @example Hono🔥 */
              message: string
            }
          }
        }
      }
    }
    put?: never
    post?: never
    delete?: never
    options?: never
    head?: never
    patch?: never
    trace?: never
  }
  '/posts': {
    parameters: {
      query?: never
      header?: never
      path?: never
      cookie?: never
    }
    /** @description get PostList posts with optional pagination */
    get: {
      parameters: {
        query: {
          page: string
          rows: string
        }
        header?: never
        path?: never
        cookie?: never
      }
      requestBody?: never
      responses: {
        /** @description OK */
        200: {
          headers: {
            [name: string]: unknown
          }
          content: {
            'application/json': {
              /** Format: uuid */
              id: string
              post: string
              createdAt: string
              updatedAt: string
            }[]
          }
        }
        /** @description Bad Request */
        400: {
          headers: {
            [name: string]: unknown
          }
          content: {
            'application/json': {
              /** @example Bad Request */
              message: string
            }
          }
        }
        /** @description Internal Server Error */
        500: {
          headers: {
            [name: string]: unknown
          }
          content: {
            'application/json': {
              /** @example Internal Server Error */
              message: string
            }
          }
        }
      }
    }
    put?: never
    /** @description create a new post */
    post: {
      parameters: {
        query?: never
        header?: never
        path?: never
        cookie?: never
      }
      requestBody: {
        content: {
          'application/json': {
            post: string
          }
        }
      }
      responses: {
        /** @description Created */
        201: {
          headers: {
            [name: string]: unknown
          }
          content?: never
        }
        /** @description Bad Request */
        400: {
          headers: {
            [name: string]: unknown
          }
          content: {
            'application/json': {
              /** @example Bad Request */
              message: string
            }
          }
        }
        /** @description Internal Server Error */
        500: {
          headers: {
            [name: string]: unknown
          }
          content: {
            'application/json': {
              /** @example Internal Server Error */
              message: string
            }
          }
        }
      }
    }
    delete?: never
    options?: never
    head?: never
    patch?: never
    trace?: never
  }
  '/posts/{id}': {
    parameters: {
      query?: never
      header?: never
      path?: never
      cookie?: never
    }
    get?: never
    /** @description update Post */
    put: {
      parameters: {
        query?: never
        header?: never
        path: {
          id: string
        }
        cookie?: never
      }
      requestBody: {
        content: {
          'application/json': {
            post: string
          }
        }
      }
      responses: {
        /** @description No Content */
        204: {
          headers: {
            [name: string]: unknown
          }
          content?: never
        }
        /** @description Bad Request */
        400: {
          headers: {
            [name: string]: unknown
          }
          content: {
            'application/json': {
              /** @example Bad Request */
              message: string
            }
          }
        }
        /** @description Internal Server Error */
        500: {
          headers: {
            [name: string]: unknown
          }
          content: {
            'application/json': {
              /** @example Internal Server Error */
              message: string
            }
          }
        }
      }
    }
    post?: never
    /** @description delete post */
    delete: {
      parameters: {
        query?: never
        header?: never
        path: {
          id: string
        }
        cookie?: never
      }
      requestBody?: never
      responses: {
        /** @description No Content */
        204: {
          headers: {
            [name: string]: unknown
          }
          content?: never
        }
        /** @description Bad Request */
        400: {
          headers: {
            [name: string]: unknown
          }
          content: {
            'application/json': {
              /** @example Bad Request */
              message: string
            }
          }
        }
        /** @description Internal Server Error */
        500: {
          headers: {
            [name: string]: unknown
          }
          content: {
            'application/json': {
              /** @example Internal Server Error */
              message: string
            }
          }
        }
      }
    }
    options?: never
    head?: never
    patch?: never
    trace?: never
  }
}
export type webhooks = Record<string, never>
export interface components {
  schemas: never
  responses: never
  parameters: never
  requestBodies: never
  headers: never
  pathItems: never
}
export type $defs = Record<string, never>
export type operations = Record<string, never>