Socket
Socket
Sign inDemoInstall

@americanairlines/controller-interceptor

Package Overview
Dependencies
12
Maintainers
4
Versions
8
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

@americanairlines/controller-interceptor

A simple interceptor for ExpressJs for logging/tracing purposes.


Version published
Maintainers
4
Weekly downloads
1
Install size
2.11 MB

Weekly downloads

Readme

Source

Controller Interceptor

Written in Typescript

Controller Interceptor is a small Typescript package that allows for the interception of requests made to an ExpressJS service. Controller interceptor also attaches a TraceId to the request headers. The TraceId is implemented synchronously and does not use async-storage. This is to avoid the performance impact caused by calling async storage each request.

Usage

To install

$ npm i @americanairlines/controller-interceptor

Create your interceptor

const interceptor = require("@americanairlines/controller-interceptor");

function beforeController(requestInfo) {
  let { requestId, method, url, params, headers, body } = requestInfo;
  console.log("RequestId " + requestId);
  console.log("Request Type " + method);
  console.log("Request Url " + url);
  console.log("Request Params " + params);
  console.log("Request Headers " + headers);
  console.log("Request Body" + body);
}

function afterController(requestInfo) {
  let { requestId, method, url, params, headers, body } = requestInfo;
  console.log("RequestId " + requestId);
  console.log("Request Type " + method);
  console.log("Request Url " + url);
  console.log("Request Params " + params);
  console.log("Request Headers " + headers);
  console.log("Request Body" + body);
}

module.exports.default = interceptor(beforeController, afterController);

Attach interceptor as middleware in ExpressJS

const express = require("express");
const interceptor = require("../path/to/interceptor");

const app = express();

app.use(interceptor);

app.listen(3000);

API

Tracing

The trace ids are of the format X-Trace-Id. The instanceId is generated when the server starts, and the requestId is generated when the route is called. This allows for tracing calls to specific routes as well as tracing requests to a specific instance of a service.

requestInfo

This is the logging object with the request info that the handler functions have access to.

RequestInfo = {
  requestId: string; // The trace Id of the request
  method: string; // The http method of the request
  url: string; // The url that the request was made to
  params: Object; // The params passed to the request
  headers: Object; // The header object of the incoming/outgoing request
  body: Body; // The body of the incoming/outgoing request
}

beforeController/afterController

These functions are not passed the actual body or header objects, just copies of them. You are not able to mutate these fields in the handler functions.

Options

You can pass options to the interceptor.

const options = {
  instanceIdLength: 6,
  requestIdLength: 16,
};
// This will cause the interceptor to generate a trace id with 6 digits for for the instance id and 16 digits for the request id length

module.exports.default = interceptor(
  beforeController,
  afterController,
  options
);

Author

Chandler Barlow

Collaborators

Steven Paulino Charlie Albright

License

MIT

FAQs

Last updated on 10 May 2022

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc