1.0.1 • Published 4 months ago

@optimum-partners/node-api-gateway v1.0.1

Weekly downloads
-
License
ISC
Repository
github
Last release
4 months ago

node-api-gateway

installation

  npm i @optimum-partners/node-api-gateway

usage

import { gateway } from '@optimum-partners/node-api-gateway';
...
gateway(config);

or

import gateway from '@optimum-partners/node-api-gateway';
...
gateway(config);

config

keydefault
cors'*'
port3000
maxFileSize'2gb'
cluster''
jwt.secret
jwt.algorithms['HS256']
securedBodyTokens[...]
services[]
  • cors : CORS settings
  • port : server port
  • maxFileSize : max allowed file size for upload
  • cluster : environment identifier
  • jwt : jwt settings
  • securedBodyTokens : secure sensitive data from logs (when key is found in the request body it will be replaced with '*')
  • services : microservices config

required config

jwt.token must be provided to the library

default config

{
  port: 3000,
  maxFileSize: '2gb',
  cluster: '',
  cors: '*',
  jwt: {
    algorithms: ['HS256'],
  },
  securedBodyTokens: [
    { key: 'number', regex: /.(?=.{4,}$)/g },
    { key: 'cvc', regex: /[0-9]/g },
    { key: 'password', regex: /.*/ },
  ],
  services: [],
}

services config

{
      "name": SERVICE_NAME,
      "server": SERVICE_SERVER_HOST,
      "port": SERVICE_PORT,
      "prefix": SERVICE_PREFIX,
      "requestPaths": [
        { "path": WILD_CARD_ROUTE, "publicRoutes": [PUBLIC_ROUTES_LIST] },
      ]
}
  • name : service name, used to identify the services
  • server: the hostname of the hosting server
  • port: the hosting server port
  • prefix: the service prefix, used in /healthcheck/all endpoint
  • requestPaths : routes mapping
    • path : route
    • publicRoutes : list of string/regex to exclude public routes from JWT validation

multiple environments support

the server name can include {CLUSTER} token that will be replaced with the cluster config key provided in the config object, this will allow to target an environment-based cluster

sample

    {
      name: 'service1',
      server: 'service1-{CLUSTER}-svc',
      port: 8080,
      prefix: 'service1',
      requestPaths: [
        { path: '/service1/healthcheck', publicRoutes: ['/service1/healthcheck'] },
        { path: '/api/public_routes/*', publicRoutes: [/\/api\/public_routes\/*/i] },
        { path: '/api/private_routes/*', publicRoutes: [] },
      ],
    },
    {
      name: 'service2',
      server: 'service2-{CLUSTER}-svc',
      port: 8080,
      prefix: 'service2',
      requestPaths: [
        { path: '/service2/healthcheck', publicRoutes: ['/service2/healthcheck'] },
        { path: '/api/mix_routes/*', publicRoutes: [/\/api\/mix_routes\/public_routes\/*/i] },
      ],
    },

Keep in mind !

The order of the routes in the services config matters

sample

    {
      name: 'service1',
      server: 'service1-{CLUSTER}-svc',
      port: 8080,
      prefix: 'service1',
      requestPaths: [
        { path: '/api/entity/:id/specific/*', publicRoutes: [] },
      ],
    },
    {
      name: 'service2',
      server: 'service2-{CLUSTER}-svc',
      port: 8080,
      prefix: 'service2',
      requestPaths: [
        { path: '/api/entity/*', publicRoutes: [] },
      ],
    },

in the above example route /api/entity/:id/specific/* will be forwarded to "service1" and all other routes related to /api/entity will be forwarded to "service2"