Skip to content

Commit

Permalink
rm GenericGuard and use TGuard as type
Browse files Browse the repository at this point in the history
  • Loading branch information
ujjwalguptaofficial committed Nov 25, 2023
1 parent 14c72dc commit 83aae8b
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 35 deletions.
6 changes: 3 additions & 3 deletions src/constants/fort_global.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -24,7 +24,7 @@ export class FortGlobal {
errorHandler: typeof ErrorHandler;
connectonKeepAliveTimeout?: number;
shields: typeof GenericShield[] = [];
guards: typeof GenericGuard[] = [];
guards: TGuard[] = [];

appName: string;

Expand Down
7 changes: 3 additions & 4 deletions src/decorators/guards.ts
Original file line number Diff line number Diff line change
@@ -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<typeof Guard>): MethodDecorator => {
export const guards = (...value: Array<TGuard>): MethodDecorator => {
return ((target: any, methodName: string) => {
const className = (target.constructor.name as string);
RouteHandler.addGuards(value as Array<typeof GenericGuard>, className, methodName);
RouteHandler.addGuards(value as Array<TGuard>, className, methodName);
});
};
13 changes: 0 additions & 13 deletions src/generics/generic_guard.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/generics/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from './generic_guard';
export * from './generic_shield';
export * from "./generic_wall";
export * from './generic_xml_parser';
7 changes: 3 additions & 4 deletions src/handlers/request_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -80,7 +79,7 @@ export class RequestHandler extends ControllerResultHandler {
});
}

private executeGuardsCheck_(guards: Array<typeof GenericGuard>): Promise<() => void> {
private executeGuardsCheck_(guards: Array<TGuard>): Promise<() => void> {
return promise((res, rej) => {
let index = 0;
const shieldLength = guards.length;
Expand All @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions src/handlers/route_handler.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -122,7 +122,7 @@ export class RouteHandler {
}
}

static addGuards(guards: Array<typeof GenericGuard>, className: string, workerName: string) {
static addGuards(guards: Array<TGuard>, className: string, workerName: string) {

const route = routerCollection.get(className);
const pattern = workerName.toLowerCase();
Expand Down
4 changes: 2 additions & 2 deletions src/interfaces/worker_info.ts
Original file line number Diff line number Diff line change
@@ -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<typeof GenericGuard>;
guards: Array<TGuard>;
pattern: string;
values: any[];
expectedQuery?: any;
Expand Down
5 changes: 2 additions & 3 deletions src/models/worker_info.ts
Original file line number Diff line number Diff line change
@@ -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<typeof GenericGuard>;
guards: Array<TGuard>;
// pattern: string[];
values: any[];
expectedQuery?: any;
Expand Down
5 changes: 3 additions & 2 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Controller } from '../abstracts';
import { Controller, Guard } from '../abstracts';
import { IHttpResult, ISessonStore } from '../interfaces';

export * from './view_engine_data';
Expand All @@ -19,4 +19,5 @@ export * from './http_format_result';
export type ErrorResultMapper = (error: any) => IHttpResult;
type Class<I, Args extends any[] = any[]> = new (...args: Args) => I;
export type TSessionStore = Class<ISessonStore, [string]>;
export type TController = Class<Controller>;
export type TController = Class<Controller>;
export type TGuard = Class<Guard>;

0 comments on commit 83aae8b

Please sign in to comment.