mongoose-to-openapi
v1.0.1
Published
Generate OpenAPI Schema Component Definitions based on Mongoose Schemas
Downloads
2
Maintainers
Readme
mongoose-to-openapi
A TypeScript library for generating OpenAPI Schema Component Definitions from Mongoose Schemas.
Installation
npm install mongoose-to-openapi
Usage
Initializing the OpenAPI Factory
import createOpenAPIFactory from 'mongoose-to-openapi';
const openAPIFactory = createOpenAPIFactory({
info: {
title: 'My API',
version: '1.0.0'
},
schemaPattern: './src/models/**/*.ts',
});
openAPIFactory.init();
Adding Routes
Adding a Single Route
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' }
}
}
}
}
}
});
Adding Multiple Routes
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' }
}
}
}
}
}
}
});
Exporting the OpenAPI Object
const openAPI = openAPIFactory.getOpenAPI();
console.log(JSON.stringify(openAPI, null, 2));
Configuration Options
info
: Information about the API (title, version, etc.).schemaPattern
: A glob pattern to locate Mongoose schema files.