From a4353873de3dfb4afaa54cf869a20704b300cfcf Mon Sep 17 00:00:00 2001 From: Rubin Bhandari Date: Sun, 8 Oct 2023 15:23:52 +0545 Subject: [PATCH] fix: stricter types and conditionals --- src/common/@types/classes/offset.response.ts | 4 +-- src/common/@types/enums/misc.enum.ts | 25 +++++++++++-------- .../@types/interfaces/pagination.interface.ts | 4 +-- src/common/misc/workers.ts | 9 +++---- src/main.ts | 6 +++-- 5 files changed, 27 insertions(+), 21 deletions(-) diff --git a/src/common/@types/classes/offset.response.ts b/src/common/@types/classes/offset.response.ts index c3fae13d..0518afb2 100644 --- a/src/common/@types/classes/offset.response.ts +++ b/src/common/@types/classes/offset.response.ts @@ -42,9 +42,9 @@ export class OffsetMeta { constructor({ pageOptionsDto, - itemCount, + itemCount, }: { - pageOptionsDto: OffsetPaginationDto + pageOptionsDto: Omit itemCount: number }) { this.page = pageOptionsDto.page; diff --git a/src/common/@types/enums/misc.enum.ts b/src/common/@types/enums/misc.enum.ts index dc43cd50..19fe8be1 100644 --- a/src/common/@types/enums/misc.enum.ts +++ b/src/common/@types/enums/misc.enum.ts @@ -37,11 +37,26 @@ export enum TemplateEngine { HBS = "HBS", } + export const FileType: Record = { IMAGE: /(jpg|jpeg|png|gif|svg)$/i, DOC: /(pdf|doc|txt|key|csv|docx|xls|xlsx|ppt|pptx)$/i, }; +export const ThreadFunctions = { + HASH_STRING: "hashString", +}; + +export const RoutingKey = { + SEND_MAIL: "send-mail", + SEND_NEWSLETTER: "send-newsletter", +}; + +export const Queues = { + MAIL: "mail", + HTTP: "http", +}; + // database enums export enum CursorType { @@ -65,16 +80,6 @@ export enum ReferralStatus { COMPLETED = "COMPLETED", } -export enum RoutingKey { - SEND_MAIL = "send-mail", - SEND_NEWSLETTER = "send-newsletter", -} - -export enum Queues { - MAIL = "mail", - HTTP = "http", -} - export enum PaginationType { OFFSET = "OFFSET", CURSOR = "CURSOR", diff --git a/src/common/@types/interfaces/pagination.interface.ts b/src/common/@types/interfaces/pagination.interface.ts index 97b5c692..f0267547 100644 --- a/src/common/@types/interfaces/pagination.interface.ts +++ b/src/common/@types/interfaces/pagination.interface.ts @@ -16,7 +16,7 @@ export interface QBCursorPaginationOptions { } export interface QBOffsetPaginationOptions { - pageOptionsDto: OffsetPaginationDto & { searchField: keyof T; alias: string } + pageOptionsDto: Omit & { searchField: keyof T; alias: string } qb: QueryBuilder } @@ -35,7 +35,7 @@ export interface PaginationAbstractResponse { } export type Order = "$gt" | "$lt"; -export type OppositeOrder = "$gte" | "$lte"; +export type OppositeOrder = `${Order}e`; export function getCursorType(cursor: QueryCursor): CursorType { return cursor === QueryCursor.DATE ? CursorType.NUMBER : CursorType.STRING; diff --git a/src/common/misc/workers.ts b/src/common/misc/workers.ts index 7890bc6d..126b9e79 100644 --- a/src/common/misc/workers.ts +++ b/src/common/misc/workers.ts @@ -1,19 +1,18 @@ import { ThreadWorker } from "poolifier"; import { HelperService } from "@common/helpers"; +import { ThreadFunctions } from "@common/@types"; + -enum ThreadFunctions { - HashString = "hashString", -} // all expensive process goes here to avoid blocking the main thread function workerFunction(data: { functionName: string; input: string }) { switch (data.functionName) { - case ThreadFunctions.HashString: { + case ThreadFunctions.HASH_STRING: { return HelperService.hashString(data.input); } default: { - throw new Error("Invalid function name"); + throw new Error(`Invalid thread function name, available are ${Object.keys(ThreadFunctions).join(" ,")}`); } } } diff --git a/src/main.ts b/src/main.ts index f1ad899b..b254139d 100644 --- a/src/main.ts +++ b/src/main.ts @@ -52,7 +52,7 @@ async function bootstrap() { app.use(helmet()); app.enableCors({ credentials: true, - methods: "GET,HEAD,PUT,PATCH,POST,DELETE,OPTIONS", + methods: ["GET","HEAD","PUT","PATCH","POST","DELETE","OPTIONS"], maxAge: 3600, origin: configService.get("app.allowedOrigins", { infer: true }), }); @@ -64,13 +64,15 @@ async function bootstrap() { const globalPrefix = configService.get("app.prefix", { infer: true }); + app.setGlobalPrefix(globalPrefix); + app.useGlobalPipes(new ValidationPipe(AppUtils.validationPipeOptions())); app.useGlobalFilters(new I18nValidationExceptionFilter({ detailedErrors: false })); app.useGlobalInterceptors(new LoggerErrorInterceptor()); - app.setGlobalPrefix(globalPrefix); + // ========================================================= // configure socket