diff --git a/lib/helpers.ts b/lib/helpers.ts index 973a353..a6e3ccf 100644 --- a/lib/helpers.ts +++ b/lib/helpers.ts @@ -5,23 +5,23 @@ import { PATH_METADATA } from '@nestjs/common/constants'; import { Controller } from '@nestjs/common/interfaces/controllers/controller.interface'; import { BadRequestException } from '@nestjs/common'; -import { ControllerMethod } from './interfaces'; +import { ControllerMethod, Query, Params } from './interfaces'; export function generateUrl( appUrl: string, prefix: string, relativePath: string, - query: any = {}, - params: any = {}, + query?: Query, + params?: Params, ): string { relativePath = putParamsInUrl(relativePath, params); const path = joinRoutes(appUrl, prefix, relativePath); const queryString = stringifyQuery(query); - const fullPath = appendQueryParams(path, queryString); + const fullPath = appendQuery(path, queryString); return fullPath; } -export function stringifyQuery(query: Record): string { +export function stringifyQuery(query?: Query): string { return qsStringify(query); } @@ -53,18 +53,11 @@ export function signatureHasExpired(expirationDate: Date): boolean { return currentDate > expirationDate; } -export function isObjectEmpty(obj = {}): boolean { - return Object.keys(obj).length == 0; -} - function isRouteNotEmpty(route: string): boolean { return !!route && route !== '/'; } -function isParamsNameInUrl( - route: string, - params: Record, -): boolean { +function isParamsNameInUrl(route: string, params: Params): boolean { const routeParts = route .split('/') .filter((path) => path[0] === ':') @@ -79,14 +72,14 @@ function joinRoutes(...routes: string[]): string { return routes.filter((route) => isRouteNotEmpty(route)).join('/'); } -function appendQueryParams(route: string, query: string): string { +export function appendQuery(route: string, query: string): string { if (query) { return `${route}?${query}`; } return route; } -function putParamsInUrl(route: string, params: Record): string { +function putParamsInUrl(route: string, params?: Params): string { if (params) { if (isParamsNameInUrl(route, params)) { for (const [key, value] of Object.entries(params)) { diff --git a/lib/interfaces.ts b/lib/interfaces.ts index c9da70e..19a7643 100644 --- a/lib/interfaces.ts +++ b/lib/interfaces.ts @@ -3,10 +3,10 @@ import { Controller } from '@nestjs/common/interfaces/controllers/controller.int export type ControllerMethod = (...args: any[]) => Promise | any; -interface Query { +export interface Query { [key: string]: any; } -interface Params { +export interface Params { [key: string]: string; } diff --git a/lib/url-generator.service.ts b/lib/url-generator.service.ts index 76a56ec..1b70629 100644 --- a/lib/url-generator.service.ts +++ b/lib/url-generator.service.ts @@ -1,11 +1,5 @@ import { ApplicationConfig } from '@nestjs/core'; -import { - Inject, - Injectable, - Logger, - ForbiddenException, - ConflictException, -} from '@nestjs/common'; +import { Inject, Injectable, Logger, ForbiddenException } from '@nestjs/common'; import { UrlGeneratorModuleOptions } from './url-generator-options.interface'; import { URL_GENERATOR_MODULE_OPTIONS } from './url-generator.constants'; @@ -17,7 +11,7 @@ import { isSignatureEqual, stringifyQuery, generateUrl, - isObjectEmpty, + appendQuery, } from './helpers'; import { @@ -108,7 +102,7 @@ export class UrlGeneratorService { public signUrl({ relativePath, expirationDate, - query, + query = {}, // empty object avoid undefined error params, }: SignUrlArgs): string { const mappedQuery = query as ReservedQuery; @@ -147,14 +141,11 @@ export class UrlGeneratorService { }: IsSignatureValidArgs): boolean { const { signed, ...restQuery } = query; + const path = `${protocol}://${host}${routePath}`; const queryString = stringifyQuery(restQuery); + const fullPath = appendQuery(path, queryString); - let fullUrl = `${protocol}://${host}${routePath}`; - if (!isObjectEmpty(restQuery)) { - fullUrl += `?${queryString}`; - } - - const hmac = generateHmac(fullUrl, this.urlGeneratorModuleOptions.secret); + const hmac = generateHmac(fullPath, this.urlGeneratorModuleOptions.secret); if (!signed || !hmac || signed.length != hmac.length) { throw new ForbiddenException('Invalid Url');