ts-to-openapi-spec is a TypeScript library that helps you generate OpenAPI (Swagger) specifications for AWS Lambda functions with ease. It provides a simple, composable interface for creating API documentation directly from your TypeScript types.
- Generate OpenAPI 3.0 specifications from TypeScript types
- Project-based configuration with glob pattern support
- Composable route definitions with rich metadata
- Automatic type inference and schema generation
- Support for path parameters with validation
- Query parameter definitions with optional/required flags
- Request and response header management
- Multiple response type definitions
- Security scheme configurations
- Server configurations for different environments
- Type-driven API documentation
- Easy integration with AWS Lambda functions
npm install ts-to-openapi-specimport OpenAPIGenerator from 'ts-to-openapi-spec';
// Create a generator instance with project configuration
const generator = await OpenAPIGenerator.create({
title: 'My Lambda API',
version: '1.0.0',
project: {
rootDir: 'src',
include: ['**/*.ts'], // Include all TypeScript files
exclude: ['**/*.test.ts'] // Exclude test files
}
});
// Add server configurations
generator.addServer({
url: 'https://api.example.com',
description: 'Production server'
});
// Add security schemes
generator.addSecurityScheme('bearerAuth', {
type: 'http',
scheme: 'bearer',
bearerFormat: 'JWT'
});
// Define routes with comprehensive configurations
generator.addRoute({
path: '/users/{userId}',
method: 'get',
description: 'Get user by ID',
parameters: {
userId: {
description: 'The user ID'
}
},
responses: [
{
statusCode: 200,
description: 'User found',
type: 'User' // References your TypeScript type
},
{
statusCode: 404,
description: 'User not found',
type: 'ErrorResponse'
}
]
});
// Generate the OpenAPI specification
const spec = generator.generateSpec();The library now uses a project-based configuration approach, making it easier to work with TypeScript files across your project:
type ProjectConfig = {
rootDir: string; // Root directory for TypeScript files
include: string[]; // Glob patterns for files to include
exclude?: string[]; // Optional glob patterns for files to exclude
};
type GeneratorConfig = {
title?: string; // API title
version?: string; // API version
project: ProjectConfig; // Project configuration
};- Basic configuration:
const config = {
title: 'My API',
version: '1.0.0',
project: {
rootDir: 'src',
include: ['**/*.ts']
}
};- With exclusions:
const config = {
title: 'My API',
version: '1.0.0',
project: {
rootDir: 'src',
include: ['**/*.ts'],
exclude: ['**/*.test.ts', '**/*.spec.ts']
}
};- Multiple source directories:
const config = {
title: 'My API',
version: '1.0.0',
project: {
rootDir: 'src',
include: ['types/**/*.ts', 'models/**/*.ts']
}
};The library will automatically discover and process TypeScript types from all files matching your project configuration. This means you can:
- Organize your types across multiple files
- Use types from any file in your project
- Exclude test files or other non-API types
- Keep your API documentation close to your type definitions
generator.addRoute({
path: '/users',
method: 'get',
description: 'Search users',
queryParameters: {
name: {
required: false,
schema: {
type: 'string'
},
description: 'Filter by name'
}
},
headers: {
request: [
{
name: 'X-API-Key',
required: true,
schema: {
type: 'string'
}
}
]
},
security: ['bearerAuth'],
responses: [
{
statusCode: 200,
description: 'Users found',
type: 'UserList'
}
]
});generator.addRoutes([
{
path: '/users',
method: 'post',
description: 'Create user',
requestType: 'CreateUserRequest',
responses: [
{
statusCode: 201,
description: 'User created',
type: 'User'
}
]
},
{
path: '/users/{userId}',
method: 'delete',
description: 'Delete user',
parameters: {
userId: {
description: 'The user ID'
}
},
responses: [
{
statusCode: 204,
description: 'User deleted'
}
]
}
]);Please see our Contributing Guide for details on how to contribute to this project.
MIT