fix: detect Express/Fastify requests correctly as of Fastify v4.19.0 (#672)
This commit is contained in:
parent
d328e343a4
commit
8be8475fa6
@ -41,6 +41,7 @@ function fastifyContextFactory(query: FastifyRequest['query']): Partial<Executio
|
|||||||
protocol: 'http',
|
protocol: 'http',
|
||||||
hostname: 'localhost',
|
hostname: 'localhost',
|
||||||
url: '/items?search=2423',
|
url: '/items?search=2423',
|
||||||
|
originalUrl: '/items?search=2423',
|
||||||
query: query,
|
query: query,
|
||||||
}),
|
}),
|
||||||
}),
|
}),
|
||||||
|
@ -1,7 +1,16 @@
|
|||||||
import { createParamDecorator, ExecutionContext } from '@nestjs/common'
|
import { createParamDecorator, ExecutionContext } from '@nestjs/common'
|
||||||
import { Request } from 'express'
|
import type { Request as ExpressRequest } from 'express'
|
||||||
|
import type { FastifyRequest } from 'fastify'
|
||||||
import { pickBy, Dictionary, isString, mapKeys } from 'lodash'
|
import { pickBy, Dictionary, isString, mapKeys } from 'lodash'
|
||||||
|
|
||||||
|
function isRecord(data: unknown): data is Record<string, unknown> {
|
||||||
|
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 {
|
export interface PaginateQuery {
|
||||||
page?: number
|
page?: number
|
||||||
limit?: number
|
limit?: number
|
||||||
@ -41,12 +50,12 @@ function parseParam<T>(queryParam: unknown, parserLogic: (param: string, res: an
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const Paginate = createParamDecorator((_data: unknown, ctx: ExecutionContext): PaginateQuery => {
|
export const Paginate = createParamDecorator((_data: unknown, ctx: ExecutionContext): PaginateQuery => {
|
||||||
const request: Request = ctx.switchToHttp().getRequest()
|
const request: ExpressRequest | FastifyRequest = ctx.switchToHttp().getRequest()
|
||||||
const { query } = request
|
const query = request.query as Record<string, unknown>
|
||||||
|
|
||||||
// Determine if Express or Fastify to rebuild the original url and reduce down to protocol, host and base url
|
// Determine if Express or Fastify to rebuild the original url and reduce down to protocol, host and base url
|
||||||
let originalUrl: any
|
let originalUrl: string
|
||||||
if (request.originalUrl) {
|
if (isExpressRequest(request)) {
|
||||||
originalUrl = request.protocol + '://' + request.get('host') + request.originalUrl
|
originalUrl = request.protocol + '://' + request.get('host') + request.originalUrl
|
||||||
} else {
|
} else {
|
||||||
originalUrl = request.protocol + '://' + request.hostname + request.url
|
originalUrl = request.protocol + '://' + request.hostname + request.url
|
||||||
|
Loading…
Reference in New Issue
Block a user