Giter VIP home page Giter VIP logo

express-openapi's Introduction

express-openapi

$ npm install @practio/express-openapi

Usage

api.spec.yml:

openapi: 3.0.0
info:
  title: Vaccines API
  version: 1.0.0
servers:
  - url: http://localhost:8181/api/v1
paths:
  /vaccines:
    get:
      operationId: getVaccines
      summary: Get Vaccines
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/VaccineList'
        '401':
          $ref: '#/components/responses/Unauthorized'
components:
  responses:
    Unauthorized:
      description: Unauthorized
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Error'
  schemas:
    Error:
      type: object
      properties:
        code:
          type: string
        message:
          type: string
    Vaccine:
      type: object
      properties:
        id:
          $ref: '#/components/schemas/VaccineId'
        name:
          $ref: '#/components/schemas/VaccineName'
    VaccineId:
      type: string
      description: Vaccine ID
      example: f9a48b38-4d84-441d-8725-dc8348254a41
    VaccineName:
      type: string
      description: Vaccine Name
      example: Covid 19
    VaccineList:
      type: array
      description: Vaccine List
      items:
        $ref: '#/components/schemas/Vaccine'
  securitySchemes:
    user:
      type: http
      scheme: Bearer
      description: A User-level Authorization Token
security:
  - user: []

main.js:

const { createOpenApiRouter } = require('@practio/express-openapi');
const express = require('express');
const fs = require('fs');
const YAML = require('yaml');

const apiSpecYml = fs.readFileSync('./api.spec.yml', 'utf8');
const apiSpec = YAML.parse(apiSpecYml);

const app = express();

app.use('/api/v1', createOpenApiRouter(apiSpec, {
  securitySchemes: { user },
  operations: { getVaccines }
});

app.listen(8181);

async function getVaccines({ query }) {
  return OK([{
   id: 1, name: 'Covid 19'
  }, {
   id: 2, name: 'Covid 20' // Yikes
  }]);
}

async function user({ credentials }) {
  return credentials === 'jane' ? { id: 1, name: 'jane' } : null;
}

async function OK(content) {
  return {
    content,
    statusCode: 200
  };
}

express-openapi's People

Contributors

jonatanpedersen avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.