From 83aae8b3c1ec6e6479812ce99b9d35f1170f59c9 Mon Sep 17 00:00:00 2001 From: Ujjwal Gupta Date: Sat, 25 Nov 2023 19:22:31 +0400 Subject: [PATCH] rm GenericGuard and use TGuard as type --- src/constants/fort_global.ts | 6 +++--- src/decorators/guards.ts | 7 +++---- src/generics/generic_guard.ts | 13 ------------- src/generics/index.ts | 1 - src/handlers/request_handler.ts | 7 +++---- src/handlers/route_handler.ts | 6 +++--- src/interfaces/worker_info.ts | 4 ++-- src/models/worker_info.ts | 5 ++--- src/types/index.ts | 5 +++-- 9 files changed, 19 insertions(+), 35 deletions(-) delete mode 100644 src/generics/generic_guard.ts diff --git a/src/constants/fort_global.ts b/src/constants/fort_global.ts index b24f016..e21a841 100644 --- a/src/constants/fort_global.ts +++ b/src/constants/fort_global.ts @@ -1,7 +1,7 @@ import { ErrorHandler, Logger } from "../models"; import { ViewEngine, XmlParser, ComponentOption } from "../abstracts"; -import { EtagOption, FolderMap, TSessionStore } from "../types"; -import { GenericGuard, GenericShield, GenericWall, GenericXmlParser } from "../generics"; +import { EtagOption, FolderMap, TGuard, TSessionStore } from "../types"; +import { GenericShield, GenericWall, GenericXmlParser } from "../generics"; import { MustacheViewEngine, DtoValidator } from "../extra"; import { APP_NAME, CURRENT_PATH } from "./index"; import * as path from "path"; @@ -24,7 +24,7 @@ export class FortGlobal { errorHandler: typeof ErrorHandler; connectonKeepAliveTimeout?: number; shields: typeof GenericShield[] = []; - guards: typeof GenericGuard[] = []; + guards: TGuard[] = []; appName: string; diff --git a/src/decorators/guards.ts b/src/decorators/guards.ts index 65f6823..63cf1c0 100644 --- a/src/decorators/guards.ts +++ b/src/decorators/guards.ts @@ -1,10 +1,9 @@ import { RouteHandler } from "../handlers"; -import { GenericGuard } from "../generics"; -import { Guard } from "../abstracts"; +import { TGuard } from "../types"; -export const guards = (...value: Array): MethodDecorator => { +export const guards = (...value: Array): MethodDecorator => { return ((target: any, methodName: string) => { const className = (target.constructor.name as string); - RouteHandler.addGuards(value as Array, className, methodName); + RouteHandler.addGuards(value as Array, className, methodName); }); }; \ No newline at end of file diff --git a/src/generics/generic_guard.ts b/src/generics/generic_guard.ts deleted file mode 100644 index da5719e..0000000 --- a/src/generics/generic_guard.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { Guard } from "../abstracts"; -import { IHttpResult } from "../interfaces"; - -export class GenericGuard extends Guard { - - constructor(...args) { - super(); - } - - check(...args): Promise { - return null; - } -} \ No newline at end of file diff --git a/src/generics/index.ts b/src/generics/index.ts index 66d773c..4390f2e 100644 --- a/src/generics/index.ts +++ b/src/generics/index.ts @@ -1,4 +1,3 @@ -export * from './generic_guard'; export * from './generic_shield'; export * from "./generic_wall"; export * from './generic_xml_parser'; \ No newline at end of file diff --git a/src/handlers/request_handler.ts b/src/handlers/request_handler.ts index 0acf6cd..4260a78 100644 --- a/src/handlers/request_handler.ts +++ b/src/handlers/request_handler.ts @@ -3,8 +3,7 @@ import * as url from 'url'; import { Controller, Wall } from "../abstracts"; import { FORT_GLOBAL } from "../constants/fort_global"; import { parseAndMatchRoute, promise, reverseLoop } from "../helpers"; -import { GenericGuard } from "../generics"; -import { RouteMatch } from "../types"; +import { RouteMatch, TGuard } from "../types"; import { HTTP_METHOD } from "../enums"; import { InjectorHandler } from "./injector_handler"; import { IException, IHttpResult } from "../interfaces"; @@ -80,7 +79,7 @@ export class RequestHandler extends ControllerResultHandler { }); } - private executeGuardsCheck_(guards: Array): Promise<() => void> { + private executeGuardsCheck_(guards: Array): Promise<() => void> { return promise((res, rej) => { let index = 0; const shieldLength = guards.length; @@ -93,7 +92,7 @@ export class RequestHandler extends ControllerResultHandler { const methodArgsValues = InjectorHandler.getMethodValues(guard.name, 'check', guardObj); guardObj.check(...methodArgsValues).then(result => { - result == null ? executeGuardByIndex() : res(this.onResultFromComponent(result)); + result == null ? executeGuardByIndex() : res(this.onResultFromComponent(result as IHttpResult)); }).catch(rej); } else { diff --git a/src/handlers/route_handler.ts b/src/handlers/route_handler.ts index 7cead61..8ac7e47 100644 --- a/src/handlers/route_handler.ts +++ b/src/handlers/route_handler.ts @@ -1,5 +1,5 @@ -import { IWorkerInfo, ParentRoute } from "../types"; -import { GenericShield, GenericGuard } from "../generics"; +import { IWorkerInfo, ParentRoute, TGuard } from "../types"; +import { GenericShield } from "../generics"; import { compareString, isNull } from "../utils"; import { RouteInfo, WorkerInfo } from "../models"; import { IRouteInfo } from "../interfaces"; @@ -122,7 +122,7 @@ export class RouteHandler { } } - static addGuards(guards: Array, className: string, workerName: string) { + static addGuards(guards: Array, className: string, workerName: string) { const route = routerCollection.get(className); const pattern = workerName.toLowerCase(); diff --git a/src/interfaces/worker_info.ts b/src/interfaces/worker_info.ts index b0360d8..8411064 100644 --- a/src/interfaces/worker_info.ts +++ b/src/interfaces/worker_info.ts @@ -1,10 +1,10 @@ import { HTTP_METHOD } from "../enums"; -import { GenericGuard } from "../generics"; +import { TGuard } from "../types"; export type IWorkerInfo = { workerName: string; methodsAllowed: HTTP_METHOD[]; - guards: Array; + guards: Array; pattern: string; values: any[]; expectedQuery?: any; diff --git a/src/models/worker_info.ts b/src/models/worker_info.ts index 26f62a0..ca0bca7 100644 --- a/src/models/worker_info.ts +++ b/src/models/worker_info.ts @@ -1,12 +1,11 @@ import { HTTP_METHOD } from "../enums"; -import { GenericGuard } from "../generics"; import { joinRoute, splitRoute } from "../helpers"; -import { IWorkerInfo } from "../types"; +import { IWorkerInfo, TGuard } from "../types"; export class WorkerInfo implements IWorkerInfo { workerName: string; methodsAllowed: HTTP_METHOD[]; - guards: Array; + guards: Array; // pattern: string[]; values: any[]; expectedQuery?: any; diff --git a/src/types/index.ts b/src/types/index.ts index 36fd5c6..f3df34c 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -1,4 +1,4 @@ -import { Controller } from '../abstracts'; +import { Controller, Guard } from '../abstracts'; import { IHttpResult, ISessonStore } from '../interfaces'; export * from './view_engine_data'; @@ -19,4 +19,5 @@ export * from './http_format_result'; export type ErrorResultMapper = (error: any) => IHttpResult; type Class = new (...args: Args) => I; export type TSessionStore = Class; -export type TController = Class; \ No newline at end of file +export type TController = Class; +export type TGuard = Class; \ No newline at end of file