OpenAPI Backend

Tools for building API backends with the OpenAPI standard
Features
Quick Start
Full example projects included in the repo
npm install --save openapi-backend
import OpenAPIBackend from 'openapi-backend';
const api = new OpenAPIBackend({
definition: {
openapi: '3.0.2',
info: {
title: 'My API',
version: '1.0.0',
},
paths: {
'/pets': {
get: {
operationId: 'getPets',
responses: {
200: { description: 'ok' },
},
},
},
'/pets/{id}': {
get: {
operationId: 'getPetById',
parameters: [
{
name: 'id',
in: 'path',
required: true,
schema: {
type: 'integer',
},
},
],
responses: {
200: { description: 'ok' },
},
},
},
},
},
handlers: {
getPets: async (req) => ({ status: 200, body: 'ok' }),
getPetById: async (req) => ({ status: 200, body: 'ok' }),
notFound: async (req) => ({ status: 404, body: 'not found' }),
validationFail: async (err, req) => ({ status: 400, body: JSON.stringify({ err }) }),
},
});
api.init();
Express
import express from 'express';
const app = express();
app.use((req, res) => api.handleRequest(req, req, res));
app.listen(9000);
Hapi
import Hapi from 'hapi';
const server = new Hapi.Server({ host: '0.0.0.0', port: 9000 });
server.route({
method: ['GET', 'POST', 'PUT', 'PATCH', 'DELETE'],
path: '/{path*}',
handler: (req, h) =>
api.handleRequest(
{
method: req.method,
path: req.path,
body: req.payload,
query: req.query,
headers: req.headers,
},
req,
h,
),
});
server.start();
AWS Serverless (Lambda)
module.exports.handler = (event, context) =>
api.handleRequest(
{
method: event.httpMethod,
path: event.path,
query: event.queryStringParameters,
body: event.body,
headers: event.headers,
},
event,
context,
);
Azure Serverless Function
module.exports = (context, req) =>
api.handleRequest(
{
method: req.httpMethod,
path: req.params.path,
query: req.queryStringParameters,
body: req.body,
headers: req.headers,
},
context,
req,
);