This is a wrapper for express that turns an openapi 3.0 document into a working api server. It sets up the endpoints, validates inputs, outputs, authentication and more.
This is pre-release code that is not stable yet and does not fully meet open api 3.0 standards. You're more than welcome to contribute to this repo to increase the velocity of the development effort.
You can install this fork via npm:
npm i openapi-middleware
Sample usage with express:
import openapiMiddleware from 'openapi-middleware';
import express from 'express';
import bodyParser from 'body-parser';
import {resolve} from "path";
const config = {
definition: resolve('./tests/helpers/openapi-sample-v3.yaml'),
controllers: resolve('./tests/helpers/controllers'),
securitySchemes: {
basicAuth: (req, res, next) => {
// sample security callback for basicAuth security scheme
next();
}
},
enforceResponseValidation: false,
};
const app = express();
new openapiMiddleware.ExpressMiddleware(config)
.on('ready', (router) => {
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(router);
app.listen(2020, () => console.log('server is running!'));
})
.on('invalidResponse', (error) => {
console.error('silently failed on invalid response', error);
})
.on('error', (error) => {
console.error('startup error', error);
});