Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/thebumpaster/mongoose-to-openapi
https://github.com/thebumpaster/mongoose-to-openapi
Last synced: 10 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/thebumpaster/mongoose-to-openapi
- Owner: TheBumpaster
- License: mit
- Created: 2024-07-09T10:31:33.000Z (6 months ago)
- Default Branch: master
- Last Pushed: 2024-07-11T08:15:35.000Z (6 months ago)
- Last Synced: 2024-07-12T09:26:04.624Z (6 months ago)
- Language: TypeScript
- Size: 56.6 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# mongoose-to-openapi
A TypeScript library for generating OpenAPI Schema Component Definitions from Mongoose Schemas.
## Installation
```bash
npm install mongoose-to-openapi
```## Usage
### Initializing the OpenAPI Factory
```typescript
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
```typescript
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
```typescript
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
```typescript
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.## License
[MIT](LICENSE)