qapinterface
Version:
Comprehensive API utilities for Node.js applications including authentication, security, request processing, and response handling with zero external dependencies
30 lines (25 loc) • 791 B
JavaScript
/**
* Request ID Generator
* Single Responsibility: Generate unique identifiers for HTTP requests
*/
const { generateId } = require('../id/generator');
/**
* Generates a request ID from headers or creates a new one.
* @param {object} req - Express request object.
* @param {string} [prefix='req_'] - Prefix for generated IDs.
* @returns {string} - Request ID.
*/
function generateRequestId(req, prefix = 'req_') {
if (req.headers) {
const existingId = req.headers['x-request-id'] ||
req.headers['x-correlation-id'] ||
req.headers['request-id'];
if (existingId && typeof existingId === 'string') {
return existingId.trim();
}
}
return `${prefix}${generateId()}`;
}
module.exports = {
generateRequestId
};