A TypeScript library for generating OpenAPI Schema Component Definitions from Mongoose Schemas.
npm install mongoose-to-openapi
import createOpenAPIFactory from 'mongoose-to-openapi';
const openAPIFactory = createOpenAPIFactory({
info: {
title: 'My API',
version: '1.0.0'
},
schemaPattern: './src/models/**/*.ts',
});
openAPIFactory.init();
openAPIFactory.addRoute('/users', 'get', {
summary: 'Get all users',
responses: {
'200': {
description: 'A list of users',
content: {
'application/json': {
schema: {
type: 'array',
items: { $ref: '#/components/schemas/User' }
}
}
}
}
}
});
openAPIFactory.addRoutes({
'/users': {
get: {
summary: 'Get all users',
responses: {
'200': {
description: 'A list of users',
content: {
'application/json': {
schema: {
type: 'array',
items: { $ref: '#/components/schemas/User' }
}
}
}
}
}
}
},
'/users/{id}': {
get: {
summary: 'Get a user by ID',
parameters: [
{
name: 'id',
in: 'path',
required: true,
schema: {
type: 'string'
}
}
],
responses: {
'200': {
description: 'A user object',
content: {
'application/json': {
schema: { $ref: '#/components/schemas/User' }
}
}
}
}
}
}
});
const openAPI = openAPIFactory.getOpenAPI();
console.log(JSON.stringify(openAPI, null, 2));
info
: Information about the API (title, version, etc.).schemaPattern
: A glob pattern to locate Mongoose schema files.