0.2.2 • Published 4 years ago

open-express v0.2.2

Weekly downloads
1
License
ISC
Repository
github
Last release
4 years ago

Quickly spin up express APIs using OpenAPI 3.0 or Swagger 2.0 definitions.

This module uses express, express-openapi-validator and swagger-routes-express.

Install

$ npm install open-express

Example

example.js

const openExpress = require('open-express');

// Define all your operations here
const api = {
  alive: async (req, res) => {
    res.json({
      status: 'alive',
    });
  },
};

// Define all configs
const config = {
  routerPath: path.join(__dirname, './router.yaml'),
  operations: api,
};

// Call open-express with config
openExpress(config)
  .then((app) => {
    app.listen(3000);
    console.log('App running on port 3000');
  });
 

router.yaml

openapi: 3.0.0
info:
  description: Something about the API
  version: 1.0.0
  title: Example API

servers:
  - url: /api/v1

paths:
  /alive:
    get:
      tags:
        - alive
      summary: Get API Status
      description: Get API Status
      operationId: alive
      responses:
        "200":
          description: success
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/ServerInfo"

  /alive:
    get:
      tags:
        - alive
      summary: Get API Status
      description: Get API Status
      operationId: alive
      security:
        - BearerAuth: []
      responses:
        "200":
          description: success
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/ServerInfo"

components:
  schemas:
    ServerInfo:
      type: object
      properties:
        name:
          type: string
        description:
          type: string
        version:
          type: string
        uptime:
          type: number
          

Usage

config:

  • routerPath (string) * Path to your OpenAPI 3.0 or OpenAPI 2.0 .yaml file
  • operations (object) * All your endpoint operations
  • errorHandler (func) - Custom error handler
  • apiOptions (object) - security (object) - All your endpoint operations middleware (object) - Any custom middleware

Show your support

Give a ⭐️ if this project helped you!