Gathering detailed insights and metrics for express-openapi-generator
Gathering detailed insights and metrics for express-openapi-generator
Gathering detailed insights and metrics for express-openapi-generator
Gathering detailed insights and metrics for express-openapi-generator
@openapi-generator-plus/typescript-express-example-server-generator
An OpenAPI Generator Plus template for a TypeScript API server using Express to output example responses
express-jsdoc-swagger
Swagger OpenAPI 3.x generator
express-oas-generator
Module to automatically generate OpenAPI (Swagger) specification for existing ExpressJS 4.x REST API applications
@openapi-generator-plus/typescript-express-passport-server-generator
An OpenAPI Generator+ module for an Express + Passport API server in TypeScript
An Express plugin that can parse an app and generate OpenApiv3 document.
npm install express-openapi-generator
Typescript
Module System
Node Version
NPM Version
57.5
Supply Chain
94.9
Quality
73.2
Maintenance
100
Vulnerability
100
License
TypeScript (92.87%)
JavaScript (7.13%)
Total Downloads
18,661
Last Day
23
Last Week
176
Last Month
936
Last Year
10,173
2 Stars
86 Commits
1 Forks
4 Watching
1 Branches
1 Contributors
Minified
Minified + Gzipped
Latest Version
1.2.0
Package Id
express-openapi-generator@1.2.0
Unpacked Size
92.77 kB
Size
17.04 kB
File Count
23
NPM Version
8.13.2
Node Version
18.5.0
Cumulative downloads
Total Downloads
Last day
-58.2%
23
Compared to previous day
Last week
-36.5%
176
Compared to previous week
Last month
47.6%
936
Compared to previous month
Last year
33.3%
10,173
Compared to previous year
1
5
21
This package analyzes an Express app project, generating OpenApi v3 documentation. The optional middleware attaches additional documentation to routes and provides optional request validation.
This project takes a minimalist, un-opinionated approach with only a single dependency, express-route-parser, a dependency free package developed and maintained by me.
npm i express-openapi-generator
You can use this package to generate quick and dirty valid open api specs for every route on your project.
API Documentation: TS Docs
Warning: OpenApi does not support exotic route matching, such as /p(ab)th
, /p*ath/
, or optional path parameters /:name?
. If these are in your project the generated document won't follow the OpenApi v3 spec. It may still work, since the route parser can handle these special routes, but plugins like swagger ui may fail.
1import express, { Request, Response } from 'express' 2import { DocumentBuilder } from 'express-openapi-generator' 3const app = express(); 4const router = express.Router(); 5// This initializes and creates our document builder interface 6const documentBuilder = DocumentBuilder.initializeDocument({ 7 openapi: '3.0.1', 8 info: { 9 title: 'A example document', 10 version: '1', 11 }, 12 paths: {}, // You don't need to include any path objects, those will be generated later 13}); 14app.use(express.json()) 15app.use('/api/v1', router); 16 17router.get('/user/:id', (req: Request, res: Response) => { 18 res.json({ id: '1', name: 'John Smith' }); 19}); 20router.post('/user', (req: Request, res: Response) => { 21 const save = req.body; 22 res.json(save); 23}); 24 25// Generates our full open api document 26documentBuilder.generatePathsObject(app); 27 28// The final document can be retrieved with the build() method. A new deep copy is created each time. 29console.log(documentBuilder.build()); 30
Output
1const exampleDocumentOutput = { 2 openapi: '3.0.1', 3 info: { title: 'A example document', version: '1' }, 4 paths: { 5 '/api/v1/user': { 6 post: { 7 responses: { 8 default: { description: 'Responses object not provided for this route' }, 9 }, 10 }, 11 }, 12 '/api/v1/user/{id}': { 13 get: { 14 responses: { 15 default: { description: 'Responses object not provided for this route' }, 16 }, 17 parameters: [{ 18 in: 'path', 19 name: 'id', 20 required: true, 21 schema: { 22 type: 'string', 23 }, 24 }], 25 }, 26 }, 27 }, 28}; 29
1import express, { Request, Response } from 'express'; 2import { OpenAPIV3 } from 'openapi-types'; 3import { DocumentBuilder, PathMiddleware, ResponseBuilder, OperationBuilder } from '../index'; 4const app = express(); 5const router = express.Router(); 6// This initializes and creates our document builder interface 7const documentBuilder = DocumentBuilder.initializeDocument({ 8 openapi: '3.0.1', 9 info: { 10 title: 'A example document', 11 version: '1', 12 }, 13 paths: {}, // You don't need to include any path objects, those will be generated later 14}); 15const userSchema: OpenAPIV3.SchemaObject = { 16 title: 'A user object', 17 type: 'object', 18 properties: { 19 id: { type: 'integer' }, 20 name: { type: 'string' }, 21 }, 22}; 23documentBuilder.schema('user', { component: userSchema }); 24app.use(express.json()) 25app.use('/api/v1', router); 26 27// Create our open api operation object following OpenApiv3 specification 28const createUserOperation: OpenAPIV3.OperationObject = { 29 operationId: 'createUser', 30 tags: ['users'], 31 responses: { 32 '200': { 33 description: 'Create a User', 34 content: { 35 'application/json': { 36 schema: documentBuilder.schema('user'), 37 }, 38 }, 39 }, 40 }, 41 requestBody: { content: { 'application/json': { schema: documentBuilder.schema('user') } } } 42}; 43 44// Attach our middleware 45router.post( 46 '/user', 47 PathMiddleware.path('createUser', { operationObject: createUserOperation }), 48 (req: Request, res: Response): void => { 49 const save = req.body; 50 res.json(save); 51 }, 52); 53 54// ** As an alternative to passing the full operation object ** 55// ** there are some builder classes provided, with more planned ** 56 57// Setup re-usable defaults for our ResponseBuilder object, 58// useful if your application sends mostly json 59ResponseBuilder.defaults({ mimeType: 'application/json' }); 60 61// Build our open api operation object for this route, using the builder method 62const getUserOperation: OpenAPIV3.OperationObject = OperationBuilder.new({ 63 '200': ResponseBuilder.new('Get user by id') 64 .schema(documentBuilder.schema('user') as OpenAPIV3.ReferenceObject) 65 .build(), 66}) 67 .operationId('getUsers') 68 .tags(['users']) 69 .build(); 70 71// Attach our operation object to the route with the path middleware 72router.get( 73 '/user/:id', 74 PathMiddleware.path('getUser', { operationObject: getUserOperation }), 75 (req: Request, res: Response) => { 76 res.json({ id: '1', name: 'John Smith' }); 77 }, 78); 79 80// Generates our full open api document 81documentBuilder.generatePathsObject(app); 82 83// The final document can be retrieved with the .build() method. . A new deep copy is created each time. 84console.log(documentBuilder.build()); 85
Output
1const exampleOutputSchema = { 2 openapi: '3.0.1', 3 info: { title: 'An example document', version: '1' }, 4 paths: { 5 '/api/v1/user': { 6 post: { 7 operationId: 'createUser', 8 tags: ['users'], 9 requestBody: { 10 content: { 11 'application/json': { 12 schema: { $ref: '#/components/schemas/user' }, 13 }, 14 }, 15 }, 16 responses: { 17 '200': { 18 description: 'Create a User', 19 content: { 20 'application/json': { schema: { $ref: '#/components/schemas/user' } }, 21 }, 22 }, 23 }, 24 }, 25 }, 26 '/api/v1/user/{id}': { 27 get: { 28 responses: { 29 '200': { 30 description: 'Get user by id', 31 content: { 32 'application/json': { schema: { $ref: '#/components/schemas/user' } }, 33 }, 34 }, 35 }, 36 parameters: [{ 37 in: 'path', 38 name: 'id', 39 required: true, 40 schema: { 41 type: 'string', 42 }, 43 }], 44 operationId: 'getUser', 45 tags: ['users'], 46 }, 47 }, 48 }, 49 components: { 50 schemas: { 51 user: { 52 title: 'A user object', 53 type: 'object', 54 properties: { id: { type: 'integer' }, name: { type: 'string' } }, 55 }, 56 }, 57 }, 58};
This is a incomplete code snippet, with just the changes needed from the prior example to work.
1// ... prior imports here 2import Ajv from 'ajv'; 3import addFormats from 'ajv-formats' 4 5// .... previous example here .... /// 6 7documentBuilder.generatePathsObject(app); // generate open api doc 8 9const ajv = new Ajv({ coerceTypes: true }) // choose any Ajv options 10 11// For example, included coerceTypes: true and it will convert the path params, headers, query and body into the correct types. 12 13addFormats(ajv); // Apply any desired ajv plugins 14 15// Build and provide the document and ajv client to the validation middlewares 16PathMiddleware.initializeValidation(documentBuilder.build(), ajv); 17
Both DocumentBuilder and PathMiddleware use singleton patterns. This allows you to initialize the underlying objects and then import the classes in other modules directly from express-openapi-generator package. This allows the required state to be maintained throughout the project.
These packages may integrate well into this eco-system/philosophy of documentation/validation generated from code. This package has no direct affiliation with any of these packages.
See the TS Docs
Inspired by @wesleytodd/express-openapi. This project seeks to use the same philosophy of documentation from code, but provide a less opinionated interface, improved features and support more complex Express app router structures.
No vulnerabilities found.
No security vulnerabilities found.