22 lines
546 B
TypeScript
22 lines
546 B
TypeScript
import { ErrorRequestHandler, Request, RequestHandler } from 'express';
|
|
import { randomUUID } from 'crypto';
|
|
|
|
export function getId(req: Request): string {
|
|
return req.get('request-id') || 'unknown';
|
|
}
|
|
|
|
export function BeforeEach(): RequestHandler {
|
|
return (req, res, next) => {
|
|
req.headers['request-id'] = randomUUID();
|
|
next();
|
|
};
|
|
}
|
|
|
|
export function ErrorHandler(): ErrorRequestHandler {
|
|
return (error, req, res, next) => {
|
|
error.status
|
|
? res.status(error.status).send(error)
|
|
: res.status(500).send(error);
|
|
};
|
|
}
|