Exciting news!Announcing our $4.6M Series Seed. Learn more
Socket
LoveBlogFAQ
Install
Log in

@americanairlines/controller-interceptor

Package Overview
Dependencies
1
Maintainers
4
Versions
5
Issues
File Explorer

Advanced tools

@americanairlines/controller-interceptor

A simple interceptor for ExpressJs for logging/tracing purposes.

    1.0.4latest

Version published
Maintainers
4
Yearly downloads
233

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

What is @americanairlines/controller-interceptor?

A simple interceptor for ExpressJs for logging/tracing purposes.

Is @americanairlines/controller-interceptor popular?

The npm package @americanairlines/controller-interceptor receives a total of 6 weekly downloads. As such, @americanairlines/controller-interceptor popularity was classified as not popular.

Is @americanairlines/controller-interceptor well maintained?

We found that @americanairlines/controller-interceptor demonstrated a healthy version release cadence and project activity. It has 4 open source maintainers collaborating on the project.

Last updated on 10 May 2022
Socket

Product

Subscribe to our newsletter

Get open source security insights delivered straight into your inbox. Be the first to learn about new features and product updates.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc