npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@dotcom-reliability-kit/serialize-request

v3.1.1

Published

A utility function to serialize a request object in a way that's friendly to loggers, view engines, and converting to JSON

Downloads

12,143

Readme

@dotcom-reliability-kit/serialize-request

A utility function to serialize a request object (Express or IncomingMessage) in a way that's friendly to loggers, view engines, and converting to JSON. This module is part of FT.com Reliability Kit.

Usage

Install @dotcom-reliability-kit/serialize-request as a dependency:

npm install --save @dotcom-reliability-kit/serialize-request

Include in your code:

import serializeRequest from '@dotcom-reliability-kit/serialize-request';
// or
const serializeRequest = require('@dotcom-reliability-kit/serialize-request');

serializeRequest

The serializeRequest function accepts a request-like object (e.g. an instance of Express.Request or an object with method and url properties) and returns a plain JavaScript object (conforming to the SerializedRequest type) which contains the relevant properties. The example below assumes that app is an Express application:

app.get('/fruit/:fruitId', (request, response, next) => {
	console.log(serializeRequest(request));
	next();
});
// {
//     id: 'request137',
//     method: 'GET',
//     url: '/fruit/feijoa',
//     headers: {
//         accept: '*/*'
//     },
//     route: {
//         path: '/fruit/:fruitId',
//         params: { fruitId: 'feijoa' }
//     }
// }

You can also pass in a plain object if you already have one that looks like a request:

serializeRequest({
	method: 'get',
    url: '/hello'
});

Configuration options

Config options can be passed into the serializeRequest function as a second argument. It expects an object with any of the keys below.

serializeRequest(request, {
    // Config options go here
});

options.includeHeaders

An array of request headers to include in the serialized request object. This must be an Array of Strings, with each string being a header name. It's important that you do not include headers which include personally-identifiable-information, API keys, or other privileged information.

This option defaults to:

[
    'accept',
    'accept-encoding',
    'accept-language',
    'content-type',
    'referer',
    'user-agent'
]

Example of usage:

serializeRequest(request, {
    includeHeaders: [
        'accept',
        'content-length',
        'content-type',
        'user-agent'
    ]
});

The default set of headers is also available to use, so that you don't need to repeat them if you want to add new included headers. It can be accessed as serializeRequest.DEFAULT_INCLUDED_HEADERS:

serializeRequest(request, {
    includeHeaders: [
        ...serializeRequest.DEFAULT_INCLUDED_HEADERS,
        'my-custom-header'
    ]
});

SerializedRequest type

The SerializedRequest type documents the return value of the serializeRequest function. It will have the following properties, extracting them from a given request object.

SerializedRequest.id

This is extracted from the request.headers['x-request-id'] property and is always cast to a String. It defaults to null.

SerializedRequest.method

This is extracted from the request.method property and is always cast to a String and switched to uppercase. It defaults to "-".

SerializedRequest.url

This is extracted from the request.url property and is always cast to a String. It defaults to "/".

SerializedRequest.headers

This is extracted from the request.headers property and is filtered to only include the headers specified in the includeHeaders option. It defaults to an empty object.

SerializedRequest.route

This is an object extracted from request.route.path (string) and request.params (object) if they are present and conform to the same properties on an Express Request object. It defaults to undefined.

Migrating

Consult the Migration Guide if you're trying to migrate to a later major version of this package.

Contributing

See the central contributing guide for Reliability Kit.

License

Licensed under the MIT license. Copyright © 2022, The Financial Times Ltd.