Generate openapi scheme for moleculer
Installations
npm install @spailybot/moleculer-auto-openapi
Developer Guide
Typescript
Yes
Module System
ESM
Min. Node Version
>=14.0.0
Node Version
18.20.2
NPM Version
10.5.0
Score
64.1
Supply Chain
97.2
Quality
75.5
Maintenance
100
Vulnerability
98.2
License
Releases
Contributors
Unable to fetch Contributors
Languages
TypeScript (98.39%)
JavaScript (1.61%)
validate.email 🚀
Verify real, reachable, and deliverable emails with instant MX records, SMTP checks, and disposable email detection.
Developer
Download Statistics
Total Downloads
4,126
Last Day
4
Last Week
22
Last Month
153
Last Year
2,873
GitHub Statistics
MIT License
4 Stars
135 Commits
2 Forks
1 Watchers
3 Branches
7 Contributors
Updated on Jan 11, 2025
Bundle Size
33.63 kB
Minified
11.38 kB
Minified + Gzipped
Package Meta Information
Latest Version
1.3.1
Package Id
@spailybot/moleculer-auto-openapi@1.3.1
Unpacked Size
146.14 kB
Size
45.16 kB
File Count
38
NPM Version
10.5.0
Node Version
18.20.2
Published on
May 09, 2024
Total Downloads
Cumulative downloads
Total Downloads
4,126
Last Day
33.3%
4
Compared to previous day
Last Week
-8.3%
22
Compared to previous week
Last Month
-22.7%
153
Compared to previous month
Last Year
129.3%
2,873
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
1
Peer Dependencies
1
Dev Dependencies
25
Optional Dependencies
1
Why Use OpenAPI:
OpenAPI standardizes and documents RESTful APIs, streamlines development, improves team communication, and automates testing. Moreover, it can be used to generate client SDKs. It allows for a focus on business logic, making it a valuable tool in a microservices environment.
This project is a fork of moleculer-auto-openapi by grinat.
Big thanks to grinat for the original work, and also to everyone who has contributed to it!
🌟 Features
- Supports multiple Moleculer-Web servers, allowing API separation
Fastest-Validator
support for direct OpenAPI generation from parameters, complete with examples- OpenAPI 3.1 compatibility
- Cached OpenAPI with efficient regeneration when needed
- Granular and reusable configuration
- TypeScript exports of mixin settings and OpenAPI parameters
- Get your first openapi in less a minute
🚀 Getting Started
📦 Prerequisites
To use this library, you must have the Moleculer framework installed along with the Moleculer-Web module. Additionally, the listAliases
action must be available (which is the default setting).
🔧 Installation
Install the package using your preferred package manager:
npm install @spailybot/moleculer-auto-openapi
Optional
If you wish to use a local instance of Swagger UI, install it like this:
npm install swagger-ui-dist@^5
For the full TypeScript autocompletion experience, install the openapi-types
package in addition to the above:
npm install --save-dev openapi-types
📁 Setting Up Your Service
Note: The following examples use the ESM syntax.
Depending on your environment and requirements, you may need to adapt these examples, possibly to the CommonJS (CJS) syntax, or to your specific coding standard.
Create an OpenApi service
Typescript
1import { OpenApiMixin, type OpenApiMixinSettings, type MoleculerWebTypes } from '@spailybot/moleculer-auto-openapi'; 2import { Service, type ServiceBroker } from 'moleculer'; 3 4/** 5 * MoleculerWebTypes are typings created from moleculer-web to enhance included typings; their use is totally optional. 6 */ 7 8export default class OpenApiService extends Service<OpenApiMixinSettings & MoleculerWebTypes.RestServiceSettings> { 9 public constructor(public broker: ServiceBroker) { 10 super(broker); 11 12 this.parseServiceSchema({ 13 // Choose your preferred name 14 name: 'openapi', 15 mixins: [mixin], 16 settings: { 17 // Set the path as you prefer 18 rest: '/openapi', 19 // Path to the endpoint that returns the JSON 20 // With autoalias, it's exposed on /openapi.json 21 schemaPath: '/openapi/openapi.json', 22 // This will be the root of your document 23 // use it to define some default informations 24 openapi: { 25 info: { 26 title: "My API", 27 version: "0.0.1" 28 } 29 } 30 } 31 }); 32 } 33}
Typescript without using class
1import { OpenApiMixin, type OpenApiMixinSettings, type MoleculerWebTypes } from '@spailybot/moleculer-auto-openapi'; 2import { Service, type ServiceBroker } from 'moleculer'; 3 4const OpenApiService: ServiceSchema<OpenApiMixinSettings & MoleculerWebTypes.RestServiceSettings> = { 5 // Choose your preferred name 6 name: 'openapi', 7 mixins: [mixin], 8 settings: { 9 // Set the path as you prefer 10 rest: '/openapi', 11 // Path to the endpoint that returns the JSON 12 // With autoalias, it's exposed on /openapi.json 13 schemaPath: '/openapi/openapi.json', 14 // This will be the root of your document 15 // use it to define some default informations 16 openapi: { 17 info: { 18 title: "My API", 19 version: "0.0.1" 20 } 21 } 22 } 23}; 24export default OpenApiService;
Javascript
1import { OpenApiMixin } from '@spailybot/moleculer-auto-openapi'; 2import { Service } from 'moleculer'; 3 4export default class OpenApiService extends Service { 5 public constructor(broker) { 6 super(broker); 7 8 this.parseServiceSchema({ 9 // Choose your preferred name 10 name: 'openapi', 11 mixins: [OpenApiMixin], 12 settings: { 13 // Set the path as you prefer 14 rest: '/openapi', 15 // Path to the endpoint that returns the JSON 16 // With autoalias, it's exposed on /openapi.json 17 schemaPath: '/openapi/openapi.json', 18 // This will be the root of your document 19 // use it to define some default informations 20 openapi: { 21 info: { 22 title: "My API", 23 version: "0.0.1" 24 } 25 } 26 } 27 }); 28 } 29}
Javascript without using class
1import { OpenApiMixin } from '@spailybot/moleculer-auto-openapi'; 2import { Service } from 'moleculer'; 3 4const OpenApiService = { 5 // Choose your preferred name 6 name: 'openapi', 7 mixins: [mixin], 8 settings: { 9 // Set the path as you prefer 10 rest: '/openapi', 11 // Path to the endpoint that returns the JSON 12 // With autoalias, it's exposed on /openapi.json 13 schemaPath: '/openapi/openapi.json', 14 // This will be the root of your document 15 // use it to define some default informations 16 openapi: { 17 info: { 18 title: "My API", 19 version: "0.0.1" 20 } 21 } 22 } 23}; 24export default OpenApiService;
Javascript using CJS
1const OpenApiMixin = require('@spailybot/moleculer-auto-openapi'); 2// or 3// const { OpenApiMixin } = require('@spailybot/moleculer-auto-openapi'); 4import { Service } from 'moleculer'; 5 6module.exports = { 7 // Choose your preferred name 8 name: 'openapi', 9 mixins: [mixin], 10 settings: { 11 // Set the path as you prefer 12 rest: '/openapi', 13 // Path to the endpoint that returns the JSON 14 // With autoalias, it's exposed on /openapi.json 15 schemaPath: '/openapi/openapi.json', 16 // This will be the root of your document 17 // use it to define some default informations 18 openapi: { 19 info: { 20 title: "My API", 21 version: "0.0.1" 22 } 23 } 24 } 25};
You can find detailed information about all the settings of the mixin in the technical documentation.
Update your moleculer-web service
Typescript
1import type { ApiSettingsSchemaOpenApi } from '@spailybot/moleculer-auto-openapi'; 2import ApiGateway from "moleculer-web"; 3import { Service, type ServiceBroker } from 'moleculer'; 4 5/** 6 * Note that ApiSettingsSchemaOpenApi is a re-export of ApiSettingsSchema because moleculer-web doesn't allow to extend it. 7 */ 8 9export default class WebApiService extends Service<ApiSettingsSchemaOpenApi> { 10 public constructor(public broker: ServiceBroker) { 11 super(broker); 12 13 this.parseServiceSchema({ 14 name: "api", 15 mixins: [mixin], 16 settings: { 17 // Place other settings here 18 openapi: { 19 // Define an OpenAPI specification that will be applied to all routes of this api 20 }, 21 routes: [ 22 // Place additional route configurations here 23 { 24 openapi: { 25 // Define an OpenAPI specification that will apply to all aliases within this route 26 }, 27 path: '/openapi', 28 aliases: { 29 'GET /openapi.json': 'openapi.generateDocs', 30 'GET /ui': 'openapi.ui', 31 'GET /assets/:file': 'openapi.assets', 32 'GET /oauth2-redirect': 'openapi.oauth2Redirect', 33 }, 34 }, 35 // To use autoAliases, refer to the following configuration 36 // { 37 // path: '/openapi', 38 // whitelist: ['openapi.*'], 39 // autoAliases: true 40 // } 41 42 ] 43 // Insert other settings here 44 } 45 }); 46 } 47}
Javascript
1import ApiGateway from "moleculer-web"; 2import { Service } from 'moleculer'; 3 4export default class WebApiService extends Service { 5 public constructor(broker) { 6 super(broker); 7 8 this.parseServiceSchema({ 9 name: "api", 10 mixins: [mixin], 11 settings: { 12 // Place other settings here 13 openapi: { 14 // Define an OpenAPI specification that will be applied to all routes of this api 15 }, 16 routes: [ 17 // Place additional route configurations here 18 { 19 openapi: { 20 // Define an OpenAPI specification that will apply to all aliases within this route 21 }, 22 path: '/openapi', 23 aliases: { 24 'GET /openapi.json': 'openapi.generateDocs', 25 'GET /ui': 'openapi.ui', 26 'GET /assets/:file': 'openapi.assets', 27 'GET /oauth2-redirect': 'openapi.oauth2Redirect', 28 }, 29 }, 30 // To use autoAliases, refer to the following configuration 31 // { 32 // path: '/openapi', 33 // whitelist: ['openapi.*'], 34 // autoAliases: true 35 // } 36 37 ] 38 // Insert other settings here 39 } 40 }); 41 } 42}
Launch Your Project
Your setup is now complete.
To view your API documentation via Swagger UI, you can navigate to http://127.0.0.1/openapi/ui
in your web browser (adjust the URL according to your configuration).
What's Next?
With your project now up and running, there are several resources available to help you develop further:
- Examples: Check out the examples in the examples folder. These provide practical code snippets and usage scenarios that can help you understand how to leverage this tool in various situations.
- Wiki: Visit our Wiki for a comprehensive guide on different features, advanced topics, and best practices.
- FAQs: The Frequently Asked Questions section can provide quick answers to common queries and issues others have encountered.
Remember, the journey of mastering any tool involves experimentation, learning from examples, reading documentation, and continuous practice. Happy coding!
📝 TODO
- $$oa
- allow to define a ref, and use the ref instead of creating a new one
- allow to define a "to ref", and create the ref with this name
- allow to define examples
- investigate the needs of requestBodyAndResponseBodyAreSameOnMethods / requestBodyAndResponseBodyAreSameDescription
- support multiple openapi version on generator ? (will need converters)
📄 License
This project is protected under the MIT License. For more details, refer to the LICENSE file.

No vulnerabilities found.

No security vulnerabilities found.