UNPKG

@nexica/nestjs-trpc

Version:
138 lines 4.97 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ContextUtils = void 0; exports.isWebSocketContext = isWebSocketContext; exports.isFastifyContext = isFastifyContext; exports.isExpressContext = isExpressContext; exports.isEnhancedExpressContext = isEnhancedExpressContext; exports.isEnhancedFastifyContext = isEnhancedFastifyContext; exports.isEnhancedWebSocketContext = isEnhancedWebSocketContext; function hasConnectionParams(obj) { if (typeof obj !== 'object' || obj === null) { return false; } if (!('info' in obj)) { return false; } const objWithInfo = obj; if (typeof objWithInfo.info !== 'object' || objWithInfo.info === null) { return false; } const info = objWithInfo.info; if (!('connectionParams' in info)) { return false; } return typeof info.connectionParams === 'object' && info.connectionParams !== null; } function isWebSocketContext(ctx) { return 'info' in ctx; } function isFastifyContext(ctx) { return 'reply' in ctx && 'req' in ctx && !('info' in ctx); } function isExpressContext(ctx) { return 'req' in ctx && 'res' in ctx && !('reply' in ctx) && !('info' in ctx); } function isEnhancedExpressContext(ctx) { return isExpressContext(ctx); } function isEnhancedFastifyContext(ctx) { return isFastifyContext(ctx); } function isEnhancedWebSocketContext(ctx) { return isWebSocketContext(ctx); } class ContextUtils { static getAuthorizationHeader(ctx) { if (isWebSocketContext(ctx)) { if (hasConnectionParams(ctx)) { const connectionParams = ctx.info.connectionParams; const token = connectionParams.token || connectionParams.authorization; if (typeof token === 'string') { if (token.startsWith('Bearer ')) { return token; } return `Bearer ${token}`; } } return null; } if (isFastifyContext(ctx)) { return ctx.req.headers.authorization || null; } if (isExpressContext(ctx)) { return ctx.req.headers.authorization || null; } return null; } static getBearerToken(ctx) { const authHeader = this.getAuthorizationHeader(ctx); if (!authHeader || !authHeader.startsWith('Bearer ')) { return null; } return authHeader.slice(7); } static getHeader(ctx, headerName) { const lowerHeaderName = headerName.toLowerCase(); if (isWebSocketContext(ctx)) { if (hasConnectionParams(ctx)) { const connectionParams = ctx.info.connectionParams; if (connectionParams && typeof connectionParams === 'object' && connectionParams !== null) { const headerValue = connectionParams[lowerHeaderName]; return Array.isArray(headerValue) || typeof headerValue === 'string' ? headerValue : null; } } return null; } if (isFastifyContext(ctx)) { return ctx.req.headers[lowerHeaderName] || null; } if (isExpressContext(ctx)) { return ctx.req.headers[lowerHeaderName] || null; } return null; } static getClientIP(ctx) { var _a, _b; if (isWebSocketContext(ctx)) { if (hasConnectionParams(ctx)) { const connectionParams = ctx.info.connectionParams; const ip = connectionParams.ip; return typeof ip === 'string' ? ip : null; } return null; } if (isFastifyContext(ctx)) { return ctx.req.ip || ((_a = ctx.req.socket) === null || _a === void 0 ? void 0 : _a.remoteAddress) || null; } if (isExpressContext(ctx)) { const xForwardedFor = ctx.req.headers['x-forwarded-for']; if (xForwardedFor) { const ips = Array.isArray(xForwardedFor) ? xForwardedFor[0] : xForwardedFor; return ips.split(',')[0].trim(); } return ctx.req.headers['x-real-ip'] || ((_b = ctx.req.socket) === null || _b === void 0 ? void 0 : _b.remoteAddress) || null; } return null; } static getUserAgent(ctx) { return this.getHeader(ctx, 'user-agent') || null; } static isWebSocketRequest(ctx) { return isWebSocketContext(ctx); } static isHttpRequest(ctx) { return isExpressContext(ctx) || isFastifyContext(ctx); } static getConnectionType(ctx) { if (isWebSocketContext(ctx)) return 'websocket'; if (isFastifyContext(ctx)) return 'fastify'; if (isExpressContext(ctx)) return 'express'; return 'unknown'; } } exports.ContextUtils = ContextUtils; //# sourceMappingURL=context-utils.js.map