
Security News
Potemkin Understanding in LLMs: New Study Reveals Flaws in AI Benchmarks
New research reveals that LLMs often fake understanding, passing benchmarks but failing to apply concepts or stay internally consistent.
micro-api-router
Advanced tools
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:
X-Correlation-ID
provided by micro-correlation-id)/health
)npm install --save micro-api-router
Or even better
yarn add micro-api-router
const { createRouter } = require('micro-api-router')
const request = require('some-request-lib')
const handler = () => 'ok!'
// service.js
module.exports = createRouter().get('/', handler)
// test.js
const response = await request('/')
console.log(response) // 'ok!'
createRouter([options])
Creates a new router. Adds a default /health
endpoint which will return all information under options.application
.
Returns an object with the following route methods (each method returns the router object to allow chaining):
get(path = String, handler = Function)
post(path = String, handler = Function)
put(path = String, handler = Function)
patch(path = String, handler = Function)
del(path = String, handler = Function)
head(path = String, handler = Function)
options(path = String, handler = Function)
The URL pattern to define your path. Parameters are specified using :
notation and they, along with query parameters, will be returned as part of the req
object passed to handler
.
For more information about defining paths, see url-pattern. This package is used to match paths.
A simple function that will make some action based on your path. The format of this function is (req, res) => {}
req.params
As shown below, the parameters specified in the path
will be present as part of the req
parameter:
const { createRouter } = require('micro-api-router')
const request = require('some-request-lib')
// service.js
module.exports = createRouter().get('/hello/:who', (req, res) => req.params)
// test.js
const response = await request('/hello/World')
console.log(response) // { who: 'World' }
req.query
As shown below, the query parameters used in the request will also be present as part of the req
parameter:
const { createRouter } = require('micro-api-router')
const request = require('some-request-lib')
// service.js
module.exports = createRouter().get('/hello', (req, res) => req.query)
// test.js
const response = await request('/hello?who=World')
console.log(response) // { who: 'World' }
Defaults:
{
application: { // Application-specific properties. Returned via `/health`
name: process.env.API_NAME || 'Unknown', // Name of the micro service
description: process.env.API_DESCRIPTION || '', // Desceription of the micro service
host: process.env.API_HOST || 'unknown', // Host url/name of the micro service
dependencies: [], // Dependencies that the micro service relies on
version: process.env.API_VERSION || 'N/A', // Version of the micro service
},
cors: {
allowMethods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
allowHeaders: [
'Accept',
'Authorization',
'Access-Control-Allow-Origin',
'Content-Type',
'X-Correlation-ID',
'X-HTTP-Method-Override',
'X-Requested-With',
],
exposeHeaders: [],
maxAge: 86400,
origin: '*',
},
}
Example:
const { createRouter } = require('micro-api-router')
const request = require('some-request-lib')
// service.js
module.exports = createRouter({ application: { name: 'Micro API' } })
// test.js
const response = await request('/health')
console.log(response) // { name: 'Micro API', description: '' host: 'unknown', dependencies: [], version: 'N/A' }
createError(statusCode, message[, data])
Creates a Boom Error instance with the supplied statusCode
, message
and data
. Use with throw
to return an error.
Returned errors include the request's correlation ID.
const { createRouter, createError } = require('micro-api-router')
const request = require('some-request-lib')
const error = () => throw createError(500, 'Internal Server Error')
const errorWithData = () => throw createError(500, 'Internal Server Error', { some: 'data' })
// service.js
module.exports = createRouter()
.get('/error', error)
.get('/errorWithData', errorWithData)
// test.js
let response = await request('/error')
console.log(response) // { statusCode: 500, error: 'Internal Server Error', message: 'An internal server error occurred', correlationId: '123' }
response = await request('/errorWithData')
console.log(response) // { statusCode: 500, error: 'Internal Server Error', message: 'An internal server error occurred', correlationId: '123', data: { some: 'data' } }
getId()
Returns the correlation ID of the current request. Must be used inside a handler function.
The current correlation ID is either a generated UUIDv4 or the value passed in via the request's x-correlation-id
header.
log
The log object used in Micro API Router. By default, all logs are output to the console in JSON format. For more information on customising the logging or adding new loggers, see: Bristol.
All logs output using this logger will automatically include the Correlation ID assigned to each request. Micro API Router also logs all incoming requests to the console by default.
yarn install
You can run the Jest test by using yarn test
and the XO metrics by using: yarn metrics
MIT © James Carr
FAQs
Micro middleware to standardise API microservices
The npm package micro-api-router receives a total of 0 weekly downloads. As such, micro-api-router popularity was classified as not popular.
We found that micro-api-router demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
New research reveals that LLMs often fake understanding, passing benchmarks but failing to apply concepts or stay internally consistent.
Security News
Django has updated its security policies to reject AI-generated vulnerability reports that include fabricated or unverifiable content.
Security News
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.