JSPM

micro-api-router

1.0.1
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • 0
  • Score
    100M100P100Q9503F
  • License MIT

Micro middleware to standardise API microservices

Package Exports

  • micro-api-router

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (micro-api-router) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Micro API Router

CircleCI XO code style Prettier code style Greenkeeper badge

Summary

Micro API Router helps to standardise API microservices. It's middleware for ZEIT's Micro and provides a level of base functionality out of the box such as:

  • CORS headers
  • Correlation ID header (X-Correlation-ID)
  • Health Check endpoint (/health)
  • JSON error handling

Installation

npm install --save micro-api-router

Or even better

yarn add micro-api-router

Usage

Basic

const { createRouter } = require('micro-api-router')
const handler = () => 'ok!'

module.exports = createRouter().get('/', handler)

With options

const { createRouter } = require('micro-api-router')
const router = createRouter({ application: { name: 'Micro API' } })
const handler = () => 'ok!'

module.exports = router.get('/', handler)

Error Handling

const { createRouter, createError } = require('micro-api-router')
const error = () => throw createError(500, 'Internal Server Error')
const errorWithData = () => throw createError(500, 'Internal Server Error', { some: 'data' })

module.exports = createRouter()
  .get('/error', error)
  .get('/errorWithData', errorWithData)

Options

application

Application-specific properties. All properties under application will be returned as JSON via the /health endpoint.

application.name

default: process.env.API_NAME or Unknown if not set

Name of the microservice

application.description

default: process.env.API_DESCRIPTION or empty string if not set

Short description of the microservice

application.host

default: process.env.API_HOST or unknown if not set

Host url/name of the microservice

application.dependecies

default: []

Array of external dependencies that the microservice relies on

application.version

default: process.env.API_VERSION or N/A if not set

Version of the microservice

cors

cors.allowMethods

default: ['POST','GET','PUT','DELETE','OPTIONS']

cors.allowHeaders

default: ['X-Requested-With','Access-Control-Allow-Origin','X-HTTP-Method-Override','Content-Type','Authorization','Accept']

cors.exposeHeaders

default: []

cors.maxAge

default: 86400

cors.origin

default: *

Contributing

  1. Fork this repository to your own GitHub account and then clone it to your local device
  2. Install the dependencies with yarn install
  3. Create a pull request with your changes when ready

You can run the Jest test by using yarn test and the XO metrics by using: yarn metrics

License

MIT © James Carr