0.3.0 • Published 12 months ago

node-http-interceptor v0.3.0

Weekly downloads
-
License
MIT
Repository
github
Last release
12 months ago

Sponsor NPM version TypeScript version Node.js version Build Status - GitHub Actions

node-http-interceptor

⚡ Intercept the low-level http requests, which is helpful when you need to do logging, monitoring or instrumentation etc.

Features

  • transparent to the normal http flow without inference
    • all errors in the interceptor are silently suppressed, only logged but not thrown
    • never consume the original stream (ClientRequest or IncomingMessage) when downstream consumer is not going to read or write
  • capability of enabling or disabling the interception
  • RequestContext is a request scope container to hold any data during the request-response round-trip, which is like the concept of HttpServletRequest.getAttributes() in Java.
  • generate a requestId and request timings by default in RequestContext

Timings

timings

Usage

nodejs preload modules

npm i -g node-http-interceptor
ln -vs "$(npm root -g)" "$HOME"/.node_modules
node -r node-http-interceptor/register

hooks

import { HttpInterceptor, RequestContext } from './http-interceptor';

const interceptor = new HttpInterceptor();
interceptor.on('request.initiated', (request: ClientRequest, context: RequestContext) => {
  // do somethong to mutate request
})

interceptor.on('request.sent', (request: Request, context: RequestContext) => {
  // log the request
})

interceptor.on('response.received', (request: Request, response: Response, context: RequestContext) => {
  // log the response
})

interceptor.on('response.error', (request: Request, error: any, context: RequestContext) => {
  // log the error
})

interceptor.on('socket.error', (request: Request, error: any, context: RequestContext) => {
  // log the error
})
interceptor.enable()

stubbing

const interceptor = new HttpInterceptor();

interceptor.stub((req: Request) => ({
  statusCode: 200,
  statusMessage: 'OK',
  headers: {
    'content-type': 'text/plain',
    'x-custom-header': 'blabla'
  },
  body: Buffer.from('test')
}));

interceptor.on('response.received', (response: Response, context: RequestContext) => {
  expect(response.body.toString()).toEqual('test')
})
interceptor.enabled();

// later on
interceptor.unstub()
0.4.0-beta.1

12 months ago

0.4.0-beta.2

12 months ago

0.4.0-beta.3

12 months ago

0.4.0-beta.4

12 months ago

0.4.0-beta.5

12 months ago

0.4.0-beta.6

12 months ago

0.2.12

1 year ago

0.3.0

1 year ago

0.2.11

2 years ago

0.2.10

2 years ago

0.2.6

2 years ago

0.2.9

2 years ago

0.2.8

2 years ago

0.2.5

3 years ago

0.2.3

3 years ago

0.2.4

3 years ago

0.2.2

3 years ago

0.2.1

3 years ago

0.2.0

3 years ago

0.1.9

3 years ago

0.1.8

3 years ago

0.1.7

3 years ago

0.1.6

3 years ago

0.1.5

3 years ago

0.1.4

3 years ago

0.1.3

3 years ago

0.1.2

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago