From 8be8475fa60926a5295c7bf07665f6eb626c1075 Mon Sep 17 00:00:00 2001 From: Brenden Palmer Date: Mon, 17 Jul 2023 02:19:03 -0400 Subject: [PATCH] fix: detect Express/Fastify requests correctly as of Fastify v4.19.0 (#672) --- src/decorator.spec.ts | 1 + src/decorator.ts | 19 ++++++++++++++----- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/decorator.spec.ts b/src/decorator.spec.ts index f22499b..e84ba05 100644 --- a/src/decorator.spec.ts +++ b/src/decorator.spec.ts @@ -41,6 +41,7 @@ function fastifyContextFactory(query: FastifyRequest['query']): Partial { + return data !== null && typeof data === 'object' && !Array.isArray(data) +} + +function isExpressRequest(request: unknown): request is ExpressRequest { + return isRecord(request) && typeof request.get === 'function' +} + export interface PaginateQuery { page?: number limit?: number @@ -41,12 +50,12 @@ function parseParam(queryParam: unknown, parserLogic: (param: string, res: an } export const Paginate = createParamDecorator((_data: unknown, ctx: ExecutionContext): PaginateQuery => { - const request: Request = ctx.switchToHttp().getRequest() - const { query } = request + const request: ExpressRequest | FastifyRequest = ctx.switchToHttp().getRequest() + const query = request.query as Record // Determine if Express or Fastify to rebuild the original url and reduce down to protocol, host and base url - let originalUrl: any - if (request.originalUrl) { + let originalUrl: string + if (isExpressRequest(request)) { originalUrl = request.protocol + '://' + request.get('host') + request.originalUrl } else { originalUrl = request.protocol + '://' + request.hostname + request.url