🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

openapi-backend

Package Overview
Dependencies
Maintainers
1
Versions
127
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

openapi-backend

Tools for building API backends with the OpenAPI standard

0.1.0
Source
npm
Version published
Weekly downloads
48K
-1.98%
Maintainers
1
Weekly downloads
 
Created
Source

OpenAPI Backend Tools

Build Status License

Tools for building API backends with the OpenAPI standard

Quick Start

Full example projects included in the repo

npm install --save openapi-backend
import OpenAPIBackend from 'openapi-backend';

const api = new OpenAPIBackend({
  document: {
    openapi: '3.0.0',
    info: {
      title: 'My API',
      version: '1.0.0',
    },
    paths: {
      '/pets': {
        get: {
          operationId: 'getPets',
          responses: {
            200: { description: 'ok' },
          },
        },
        post: {
          operationId: 'createPet',
          responses: {
            201: { description: 'ok' },
          },
        },
      },
      '/pets/{id}': {
        get: {
          operationId: 'getPetById',
          responses: {
            200: { description: 'ok' },
          },
        },
        get: {
          operationId: 'deletePetById',
          responses: {
            200: { description: 'ok' },
          },
        },
      },
    },
  },
  handlers: {
    // your platform specific request handlers here
    getPets: async () => { status: 200, body: 'ok' },
    createPet: async () => { status: 201, body: 'ok' }) },
    getPetById: async () => { status: 200, body: 'ok' }) },
    deletePetById: async () => { status: 200, body: 'ok' }) },
    notFound: async () => { status: 404, body: 'not found' }) },
  },
});

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)

// API Gateway Proxy handler
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,
  );

Keywords

openapi

FAQs

Package last updated on 27 Oct 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts