The @inversifyjs/open-api-2-typescript package converts OpenAPI documents into TypeScript type declarations. It works by collecting components.schemas and operation parameter schemas, then composing them using @inversifyjs/json-schema-2-typescript.
Naming Conventions
- Schemas: Uses the schema
title as the TypeScript name if present; otherwise, it uses the component key. - Operation Parameters: Types for path, query, querystring, header, and cookie parameters are named using the PascalCase
operationId (or a combination of HTTP method and path) plus a location suffix:PathParamsQueryQuerystringHeadersCookies
- Omissions:
- Locations with no parameters for a specific operation are omitted.
- Header parameters named
Accept, Content-Type, or Authorization (case-insensitive) are ignored per OpenAPI 3.1/3.2 requirements.
- Root Type: The generated
Root type is a union of all collected schemas.
Special Behaviors
- Querystring Unwrapping: If an operation has a single
querystring parameter, the generated type is the schema of that parameter directly. If there are multiple querystring parameters, they are generated as an object keyed by the parameter names. - Reference Resolution:
$ref values (including fragment-only refs like #/components/parameters/Page and OpenAPI 3.2 $self) are resolved against the document URI. Refs that do not resolve to a resource within the document are omitted.
Supported OpenAPI Versions
@inversifyjs/open-api-2-typescript/v3Dot1@inversifyjs/open-api-2-typescript/v3Dot2
import { transformOpenApiToTypeScript } from '@inversifyjs/open-api-2-typescript/v3Dot1';
const typeScript = transformOpenApiToTypeScript({
components: {
schemas: {
User: {
properties: {
id: { type: 'string' },
},
required: ['id'],
type: 'object',
},
},
},
info: { title: 'API', version: '1.0.0' },
openapi: '3.1.0',
});