43 lines
1.3 KiB
TypeScript
43 lines
1.3 KiB
TypeScript
/**
|
|
* Request-level product context helpers — delegates to @bytelyst/fastify-auth.
|
|
*/
|
|
import { createRequestContext } from '@bytelyst/fastify-auth';
|
|
import type { FastifyRequest } from 'fastify';
|
|
import { randomUUID } from 'node:crypto';
|
|
import { PRODUCT_ID } from './product-config.js';
|
|
|
|
export type { JwtPayload } from '@bytelyst/fastify-auth';
|
|
|
|
const _ctx = createRequestContext({ productId: PRODUCT_ID });
|
|
|
|
export function getRequestProductId(req: FastifyRequest): string {
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-explicit-any
|
|
return _ctx.getRequestProductId(req as any);
|
|
}
|
|
|
|
export function getUserId(req: FastifyRequest): string {
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-explicit-any
|
|
return _ctx.getUserId(req as any);
|
|
}
|
|
|
|
type RequestIdCarrier = {
|
|
headers: Record<string, string | string[] | undefined>;
|
|
id: string;
|
|
};
|
|
|
|
export function getRequestId(req: RequestIdCarrier): string {
|
|
const header = req.headers['x-request-id'];
|
|
if (Array.isArray(header)) {
|
|
return header[0] ?? req.id;
|
|
}
|
|
return header ?? req.id;
|
|
}
|
|
|
|
export function createOutboundRequestId(): string {
|
|
return randomUUID();
|
|
}
|
|
|
|
export function requestIdHeaders(requestId: string | undefined): Record<string, string> {
|
|
return requestId ? { 'x-request-id': requestId } : {};
|
|
}
|