0.2.2 • Published 4 years ago
open-express v0.2.2
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 fileoperations (object) *
All your endpoint operationserrorHandler (func) -
Custom error handlerapiOptions (object) -
security (object) -
All your endpoint operationsmiddleware (object) -
Any custom middleware
Show your support
Give a ⭐️ if this project helped you!