├── .editorconfig ├── .gitattributes ├── .github ├── dependabot.yml └── workflows │ └── main.yml ├── .gitignore ├── .npmrc ├── index.js ├── license ├── package.json ├── readme.md ├── test.js └── types.d.ts /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: "npm" 4 | directory: "/" 5 | schedule: 6 | interval: "daily" 7 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} 8 | runs-on: ubuntu-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 16 14 | - 14 15 | - 12 16 | steps: 17 | - uses: actions/checkout@v2 18 | - uses: actions/setup-node@v2 19 | with: 20 | node-version: ${{ matrix.node-version }} 21 | - run: npm install 22 | - run: npm test 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | import {v4 as uuidv4} from 'uuid'; 2 | 3 | function generateV4UUID(_request) { 4 | return uuidv4(); 5 | } 6 | 7 | const ATTRIBUTE_NAME = 'id'; 8 | 9 | export default function requestID({ 10 | generator = generateV4UUID, 11 | headerName = 'X-Request-Id', 12 | setHeader = true, 13 | } = {}) { 14 | return function (request, response, next) { 15 | const oldValue = request.get(headerName); 16 | const id = oldValue === undefined ? generator(request) : oldValue; 17 | 18 | if (setHeader) { 19 | response.set(headerName, id); 20 | } 21 | 22 | request[ATTRIBUTE_NAME] = id; 23 | 24 | next(); 25 | }; 26 | } 27 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Vsevolod Strukchinsky (https://floatdrop.me) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "express-request-id", 3 | "version": "3.0.0", 4 | "description": "Generate UUID for request", 5 | "license": "MIT", 6 | "repository": "floatdrop/express-request-id", 7 | "author": { 8 | "name": "Vsevolod Strukchinsky", 9 | "email": "floatdrop@gmail.com", 10 | "url": "https://floatdrop.me/" 11 | }, 12 | "type": "module", 13 | "exports": "./index.js", 14 | "types": "./types.d.ts", 15 | "engines": { 16 | "node": "^14.20.1 || >=16.0.0" 17 | }, 18 | "scripts": { 19 | "test": "xo && ava" 20 | }, 21 | "files": [ 22 | "index.js", 23 | "types.d.ts" 24 | ], 25 | "keywords": [ 26 | "express", 27 | "middleware", 28 | "request", 29 | "id", 30 | "uuid" 31 | ], 32 | "dependencies": { 33 | "uuid": "^9.0.0" 34 | }, 35 | "devDependencies": { 36 | "ava": "^4.0.1", 37 | "express": "^4.17.2", 38 | "express-serve-static-core": "^0.1.1", 39 | "supertest": "^6.2.1", 40 | "xo": "^0.52.4" 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # express-request-id 2 | 3 | [![Tests](https://github.com/floatdrop/express-request-id/workflows/CI/badge.svg)](https://github.com/floatdrop/express-request-id/actions) 4 | [![npm version](https://img.shields.io/npm/v/express-request-id.svg)](https://npmjs.org/package/express-request-id 'View this project on NPM') 5 | [![npm downloads](https://img.shields.io/npm/dm/express-request-id)](https://www.npmjs.com/package/express-request-id) 6 | 7 | > Generates UUID for request and add it to header. 8 | 9 | ## Install 10 | 11 | ```sh 12 | npm install express-request-id 13 | ``` 14 | 15 | ## Usage 16 | 17 | ```js 18 | import express from 'express'; 19 | import requestID from 'express-request-id'; 20 | 21 | app.use(requestID()); 22 | 23 | app.get('/', function (req, res, next) { 24 | res.send(req.id); 25 | next(); 26 | }); 27 | 28 | app.listen(3000, function() { 29 | console.log('Listening on port %d', server.address().port); 30 | }); 31 | 32 | // curl localhost:3000 33 | // d7c32387-3feb-452b-8df1-2d8338b3ea22 34 | ``` 35 | 36 | ## API 37 | 38 | ### requestID(options?) 39 | 40 | #### options 41 | 42 | Type: `object` 43 | 44 | ##### generator 45 | 46 | Type: `function` 47 | Default: `func(req) { return uuidv4(); }` 48 | 49 | Defines function, that generated ID from request. By default used `uuid` module, that generated UUID V4 for every request. 50 | 51 | ##### headerName 52 | 53 | Type: `string` 54 | Default: `X-Request-Id` 55 | 56 | Defines name of header, that should be used for request ID checking and setting. 57 | 58 | ##### setHeader 59 | 60 | Type: `bool` 61 | Default: `true` 62 | 63 | If `false` – header will not be set. 64 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import express from 'express'; 3 | import request from 'supertest'; 4 | import {validate} from 'uuid'; 5 | import requestID from './index.js'; 6 | 7 | function errorHandler(t) { 8 | return (error, _request, _response, next) => { 9 | t.fail(error.message); 10 | next(); 11 | }; 12 | } 13 | 14 | test('sets request id', async t => { 15 | const app = express(); 16 | app.use(requestID()); 17 | app.get('/', (request, response, _next) => { 18 | t.true(validate(request.id)); 19 | response.send('OK'); 20 | }); 21 | app.use(errorHandler(t)); 22 | 23 | const response = await request(app).get('/').expect(200, 'OK'); 24 | 25 | t.true(validate(response.get('X-Request-Id'))); 26 | }); 27 | 28 | test('preserves old request id', async t => { 29 | const app = express(); 30 | app.use(requestID()); 31 | app.get('/', (request, response, _next) => { 32 | t.is(request.id, 'MyID'); 33 | response.send('OK'); 34 | }); 35 | app.use(errorHandler(t)); 36 | 37 | await request(app).get('/').set('X-Request-Id', 'MyID').expect(200, 'OK'); 38 | }); 39 | 40 | test('setHeader option', async t => { 41 | const app = express(); 42 | app.use(requestID({setHeader: false})); 43 | app.get('/', (_request, response, _next) => { 44 | response.send('OK'); 45 | }); 46 | app.use(errorHandler(t)); 47 | 48 | const response = await request(app).get('/').set('X-Request-Id', 'MyID').expect(200, 'OK'); 49 | 50 | t.is(response.get('X-Request-Id'), undefined); 51 | }); 52 | 53 | test('headerName option', async t => { 54 | const app = express(); 55 | app.use(requestID({headerName: 'X-My-Request-Id'})); 56 | app.get('/', (_request, response, _next) => { 57 | response.send('OK'); 58 | }); 59 | app.use(errorHandler(t)); 60 | 61 | const response = await request(app).get('/').set('X-My-Request-Id', 'MyID').expect(200, 'OK'); 62 | 63 | t.is(response.get('X-My-Request-Id'), 'MyID'); 64 | }); 65 | 66 | test('generator option', async t => { 67 | const app = express(); 68 | app.use(requestID({generator: _request => 'ID'})); 69 | app.get('/', (request, response, _next) => { 70 | t.is(request.id, 'ID'); 71 | response.send('OK'); 72 | }); 73 | app.use(errorHandler(t)); 74 | 75 | await request(app).get('/').expect(200, 'OK'); 76 | }); 77 | -------------------------------------------------------------------------------- /types.d.ts: -------------------------------------------------------------------------------- 1 | import express from 'express'; 2 | import type {Request, RequestHandler} from 'express-serve-static-core'; 3 | 4 | declare global { 5 | namespace Express { 6 | // Inject additional properties on express.Request 7 | type Request = { 8 | id: string; 9 | }; 10 | } 11 | } 12 | 13 | declare namespace expressRequestId { 14 | type Options = { 15 | setHeader?: boolean | undefined; 16 | headerName?: string | undefined; 17 | generator?: ((request: Request) => string) | undefined; 18 | }; 19 | } 20 | 21 | declare function expressRequestId(options?: expressRequestId.Options): RequestHandler; 22 | export = expressRequestId; 23 | --------------------------------------------------------------------------------