express-openapi-json v3.3.3
ExpressJS + OpenAPI + JSON = <3
Builds an express-compatible router using OpenAPI with request and response validation.
Assumptions
- Your
ExpressJSserver includesbody-parserandcookie-parserwhen applicable. - Your
OpenAPIdocument is version3.0and valid (See https://editor.swagger.io/). - Your
OpenAPIdocument consumesapplication/jsonand producesapplication/json. - Your
OpenAPIdocument operations declare anoperationId.
Installation
Install express-openapi-json:
npm install express-openapi-jsonQuick Start
Create a router with one operation for the specified operationId:
const router = api.createCore(require('./openapi.json'))
.operation('getUserById', ctx => api.json({id: ctx.query.id})))
.router();Create a router with one controller (using decorators):
class PersonController {
@api.createOperation('getUser')
get(context) {
return api.json({id: context.query.id});
}
}
const router = api.createCore(require('./openapi.json'))
.controller(new PersonController())
.router();Using the router in express:
app.use(router.express());Using the router in express mounted under /api:
app.use('/api', router.express());Full Example
import api from 'express-openapi-json';
import bodyParser from 'body-parser';
import cookieParser from 'cookie-parser';
import express from 'express';
class UserController {
@api.createOperation('getUser')
get(context) {
return api.json({id: context.query.id});
}
@api.createOperation('postUser')
post(context) {
return api.status(200);
}
}
const router = api.createCore(require('./openapi.json'))
.controller(new UserController())
.router();
const server = express();
server.use(bodyParser.json());
server.use(cookieParser());
server.use(router.express());
server.listen(3000);Scripts
Certain scripts are made available after package installation.
openapi2ts
Converts an openapi document to TypeScript definitions. Limitations:
- Your
OpenAPIdocument must be stored asjson.
Installation:
npm install json-schema-to-typescript --save-devUsage:
openapi2js your_openapi.json > your_typescript.tsOpenAPI
This section describes the supported features and limitations.
Paths
{
paths: {
[Path]: {
[method]: Operation = {
parameters?: Parameter[],
requestBody?: RequestBody
responses: {
[responseKey]: Response
}
}
}
}
}Path
Supported.
Parameter (Operation)
Supported with validation (including required). Limitations:
- Parameter MUST have a
schemaor$ref. See Schema. - Parameter MUST have a
schema.typethat is a primitive:boolean(/^(1|0|true|false|yes|no)$/i)integer(/^[0-9]+$/)number(/^[0-9]+(\.[0-9]+)?$/)string
RequestBody (Operation)
Supported with validation. Limitations:
- Content MUST have a
contentType=application/json. - Content MUST have a
schemaforapplication/json. See Schema.
Response (Operation)
Supported. See Components.
Components
{
components: {
responses?: {
[responseName]: Response = {
headers?: Headers
content?: Content
}
}
schemas?: {
[schemaName]: Schema
}
}
}Headers (Response)
Unsupported.
Content (Response)
Supported with validation. Limitations:
- Content SHOULD have a
contentType=application/json. - Content MUST have a
schemaforapplication/json. See Schema.
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago