diff --git a/src/http/decorators/base_path.decorator.ts b/src/http/decorators/base_path.decorator.ts index 2027954..5bff129 100644 --- a/src/http/decorators/base_path.decorator.ts +++ b/src/http/decorators/base_path.decorator.ts @@ -1,8 +1,11 @@ +import { ClassDecorator } from '../../utils/types/class_decorator.type.ts'; import { Reflector } from '../../utils/reflector.class.ts'; import { RoutePath } from '../../router/types/route_path.type.ts'; export function BasePath(path: RoutePath): ClassDecorator { - return (target: object) => { - Reflector.defineMetadata('basePath', path, target); + return (originalClass) => { + Reflector.defineMetadata('basePath', path, originalClass); + + return originalClass; }; } diff --git a/src/http/decorators/cookies.decorator.ts b/src/http/decorators/cookies.decorator.ts index e0f58e9..8005cd0 100644 --- a/src/http/decorators/cookies.decorator.ts +++ b/src/http/decorators/cookies.decorator.ts @@ -1,13 +1,14 @@ +import { MethodDecorator } from '../../utils/types/method_decorator.type.ts'; import { Reflector } from '../../utils/reflector.class.ts'; export function Cookies(cookies: Record): MethodDecorator { - return (_target, _methodName, descriptor) => { + return (originalMethod) => { Reflector.defineMetadata>( 'cookies', cookies, - descriptor.value as object, + originalMethod, ); - return descriptor; + return originalMethod; }; } diff --git a/src/http/decorators/headers.decorator.ts b/src/http/decorators/headers.decorator.ts index 6912df5..b0ae980 100644 --- a/src/http/decorators/headers.decorator.ts +++ b/src/http/decorators/headers.decorator.ts @@ -1,13 +1,14 @@ +import { MethodDecorator } from '../../utils/types/method_decorator.type.ts'; import { Reflector } from '../../utils/reflector.class.ts'; export function Headers(headers: Record): MethodDecorator { - return (_target, _methodName, descriptor) => { + return (originalMethod) => { Reflector.defineMetadata>( 'headers', headers, - descriptor.value as object, + originalMethod, ); - return descriptor; + return originalMethod; }; } diff --git a/src/http/decorators/name.decorator.ts b/src/http/decorators/name.decorator.ts index 1469957..5e7b38c 100644 --- a/src/http/decorators/name.decorator.ts +++ b/src/http/decorators/name.decorator.ts @@ -1,13 +1,14 @@ +import { MethodDecorator } from '../../utils/types/method_decorator.type.ts'; import { Reflector } from '../../utils/reflector.class.ts'; export function Name(name: string): MethodDecorator { - return (_target, _methodName, descriptor) => { + return (originalMethod) => { Reflector.defineMetadata( 'name', name, - descriptor.value as object, + originalMethod, ); - return descriptor; + return originalMethod; }; } diff --git a/src/http/decorators/redirect.decorator.ts b/src/http/decorators/redirect.decorator.ts index 2810df8..c2e0ad8 100644 --- a/src/http/decorators/redirect.decorator.ts +++ b/src/http/decorators/redirect.decorator.ts @@ -1,14 +1,15 @@ +import { MethodDecorator } from '../../utils/types/method_decorator.type.ts'; import { Reflector } from '../../utils/reflector.class.ts'; import { RedirectDestination } from '../../router/types/redirect_destination.type.ts'; export function Redirect(destination: RedirectDestination): MethodDecorator { - return (_target, _methodName, descriptor) => { + return (originalMethod) => { Reflector.defineMetadata( 'redirectDestination', destination, - descriptor.value as object, + originalMethod, ); - return descriptor; + return originalMethod; }; } diff --git a/src/http/decorators/render.decorator.ts b/src/http/decorators/render.decorator.ts index 9784f02..27a1cb0 100644 --- a/src/http/decorators/render.decorator.ts +++ b/src/http/decorators/render.decorator.ts @@ -1,13 +1,14 @@ +import { MethodDecorator } from '../../utils/types/method_decorator.type.ts'; import { Reflector } from '../../utils/reflector.class.ts'; export function Render(view: string): MethodDecorator { - return (_target, _methodName, descriptor) => { + return (originalMethod) => { Reflector.defineMetadata( 'view', view, - descriptor.value as object, + originalMethod, ); - return descriptor; + return originalMethod; }; } diff --git a/src/http/decorators/status_code.decorator.ts b/src/http/decorators/status_code.decorator.ts index 4e4da50..c6f6070 100644 --- a/src/http/decorators/status_code.decorator.ts +++ b/src/http/decorators/status_code.decorator.ts @@ -1,14 +1,15 @@ +import { MethodDecorator } from '../../utils/types/method_decorator.type.ts'; import { Reflector } from '../../utils/reflector.class.ts'; import { HttpStatus } from '../enums/http_status.enum.ts'; export function StatusCode(statusCode: HttpStatus): MethodDecorator { - return (_target, _methodName, descriptor) => { + return (originalMethod) => { Reflector.defineMetadata( 'statusCode', statusCode, - descriptor.value as object, + originalMethod, ); - return descriptor; + return originalMethod; }; } diff --git a/src/http/decorators/transform.decorator.ts b/src/http/decorators/transform.decorator.ts index 6a7f7b8..c8e388e 100644 --- a/src/http/decorators/transform.decorator.ts +++ b/src/http/decorators/transform.decorator.ts @@ -1,17 +1,18 @@ import { Constructor } from '../../utils/interfaces/constructor.interface.ts'; +import { MethodDecorator } from '../../utils/types/method_decorator.type.ts'; import { Pipe } from '../../pipes/interfaces/pipe.interface.ts'; import { Reflector } from '../../utils/reflector.class.ts'; export function Transform( pipes: Record>, ): MethodDecorator { - return (_target, _methodName, descriptor) => { + return (originalMethod) => { Reflector.defineMetadata>>( 'pipes', pipes, - descriptor.value as object, + originalMethod, ); - return descriptor; + return originalMethod; }; } diff --git a/src/http/decorators/use.decorator.ts b/src/http/decorators/use.decorator.ts index 24f53bc..ae341d9 100644 --- a/src/http/decorators/use.decorator.ts +++ b/src/http/decorators/use.decorator.ts @@ -1,15 +1,16 @@ import { Constructor } from '../../utils/interfaces/constructor.interface.ts'; +import { MethodDecorator } from '../../utils/types/method_decorator.type.ts'; import { Middleware } from '../interfaces/middleware.interface.ts'; import { Reflector } from '../../utils/reflector.class.ts'; export function Use(middleware: Constructor[]): MethodDecorator { - return (_target, _methodName, descriptor) => { + return (originalMethod) => { Reflector.defineMetadata[]>( 'middleware', middleware, - descriptor.value as object, + originalMethod, ); - return descriptor; + return originalMethod; }; } diff --git a/src/injector/decorators/non_singleton.decorator.ts b/src/injector/decorators/non_singleton.decorator.ts index ce5a4b4..f06dfde 100644 --- a/src/injector/decorators/non_singleton.decorator.ts +++ b/src/injector/decorators/non_singleton.decorator.ts @@ -1,7 +1,8 @@ +import { ClassDecorator } from '../../utils/types/class_decorator.type.ts'; import { Reflector } from '../../utils/reflector.class.ts'; export function NonSingleton(): ClassDecorator { - return (target: object) => { - Reflector.defineMetadata('singleton', false, target); + return (originalClass) => { + Reflector.defineMetadata('singleton', false, originalClass); }; } diff --git a/src/router/decorators/route.decorator.ts b/src/router/decorators/route.decorator.ts index ef58aef..e8ede0f 100644 --- a/src/router/decorators/route.decorator.ts +++ b/src/router/decorators/route.decorator.ts @@ -1,6 +1,7 @@ import { HttpMethod } from '../../http/enums/http_method.enum.ts'; import { HttpStatus } from '../../http/enums/http_status.enum.ts'; import { inject } from '../../injector/functions/inject.function.ts'; +import { MethodDecorator } from '../../utils/types/method_decorator.type.ts'; import { Reflector } from '../../utils/reflector.class.ts'; import { Router } from '../router.service.ts'; @@ -45,15 +46,15 @@ export const Methods = router.createRouteDecorator(); export function Error( statusCode?: HttpStatus, ): MethodDecorator { - return (_target, _methodName, descriptor) => { + return (originalMethod) => { Reflector.defineMetadata<{ statusCode?: HttpStatus }>( 'httpErrorHandler', { statusCode, }, - descriptor.value as object, + originalMethod, ); - return descriptor; + return originalMethod; }; } diff --git a/src/router/router.service.ts b/src/router/router.service.ts index 1b97396..a5d0d2b 100644 --- a/src/router/router.service.ts +++ b/src/router/router.service.ts @@ -12,6 +12,7 @@ import { HttpRequest } from '../http/http_request.class.ts'; import { HttpStatus } from '../http/enums/http_status.enum.ts'; import { inject } from '../injector/functions/inject.function.ts'; import { Json } from '../http/json.class.ts'; +import { MethodDecorator } from '../utils/types/method_decorator.type.ts'; import { Middleware } from '../http/interfaces/middleware.interface.ts'; import { Pipe } from '../pipes/interfaces/pipe.interface.ts'; import { RedirectDestination } from './types/redirect_destination.type.ts'; @@ -434,7 +435,20 @@ export class Router { methods: EnumValuesUnion[], options: RouteOptions = {}, ): MethodDecorator => { - return (_target, _methodName, descriptor) => { + return (originalMethod, context) => { + if (context.private) { + throw new Error( + `Controller route method ${context.name as string} must be public`, + ); + } + + if (context.static) { + throw new Error( + `Controller route method ${context + .name as string} cannot be static`, + ); + } + Reflector.defineMetadata>( 'route', { @@ -442,10 +456,10 @@ export class Router { path, ...options, }, - descriptor.value as object, + originalMethod, ); - return descriptor; + return originalMethod; }; }; diff --git a/src/scheduler/decorators/interval.decorator.ts b/src/scheduler/decorators/interval.decorator.ts index cb86c1b..7ce10ec 100644 --- a/src/scheduler/decorators/interval.decorator.ts +++ b/src/scheduler/decorators/interval.decorator.ts @@ -1,16 +1,17 @@ import { inject } from '../../injector/functions/inject.function.ts'; +import { MethodDecorator } from '../../utils/types/method_decorator.type.ts'; import { Scheduler } from '../scheduler.service.ts'; export function Interval(milliseconds: number): MethodDecorator { - return (_target, _methodName, descriptor) => { + return (originalMethod) => { const scheduler = inject(Scheduler); const callback = () => { - (descriptor.value as () => unknown)(); + originalMethod(); }; scheduler.interval(callback, milliseconds); - return descriptor; + return originalMethod; }; } diff --git a/src/scheduler/decorators/schedule.decorator.ts b/src/scheduler/decorators/schedule.decorator.ts index 3cd0e40..a4c1edd 100644 --- a/src/scheduler/decorators/schedule.decorator.ts +++ b/src/scheduler/decorators/schedule.decorator.ts @@ -1,19 +1,20 @@ import { inject } from '../../injector/functions/inject.function.ts'; +import { MethodDecorator } from '../../utils/types/method_decorator.type.ts'; import { Scheduler } from '../scheduler.service.ts'; export function Schedule( identifier: string, schedule: string | Deno.CronSchedule, ): MethodDecorator { - return (_target, _methodName, descriptor) => { + return (originalMethod) => { const scheduler = inject(Scheduler); const callback = () => { - (descriptor.value as () => unknown)(); + originalMethod(); }; scheduler.schedule(identifier, callback, schedule); - return descriptor; + return originalMethod; }; } diff --git a/src/scheduler/decorators/timeout.decorator.ts b/src/scheduler/decorators/timeout.decorator.ts index 89ae6d0..81f04c3 100644 --- a/src/scheduler/decorators/timeout.decorator.ts +++ b/src/scheduler/decorators/timeout.decorator.ts @@ -1,16 +1,17 @@ import { inject } from '../../injector/functions/inject.function.ts'; +import { MethodDecorator } from '../../utils/types/method_decorator.type.ts'; import { Scheduler } from '../scheduler.service.ts'; export function Timeout(milliseconds: number): MethodDecorator { - return (_target, _methodName, descriptor) => { + return (originalMethod) => { const scheduler = inject(Scheduler); const callback = () => { - (descriptor.value as () => unknown)(); + originalMethod(); }; scheduler.timeout(callback, milliseconds); - return descriptor; + return originalMethod; }; } diff --git a/src/utils/types/class_decorator.type.ts b/src/utils/types/class_decorator.type.ts new file mode 100644 index 0000000..5c530f2 --- /dev/null +++ b/src/utils/types/class_decorator.type.ts @@ -0,0 +1,6 @@ +import { Constructor } from '../interfaces/constructor.interface.ts'; + +export type ClassDecorator = ( + originalClass: Constructor, + context: ClassDecoratorContext, +) => TTarget; diff --git a/src/validator/decorators/validate.decorator.ts b/src/validator/decorators/validate.decorator.ts index 26bc817..ff4b087 100644 --- a/src/validator/decorators/validate.decorator.ts +++ b/src/validator/decorators/validate.decorator.ts @@ -1,16 +1,17 @@ +import { MethodDecorator } from '../../utils/types/method_decorator.type.ts'; import { Reflector } from '../../utils/reflector.class.ts'; import { ValidatorRulesList } from '../interfaces/validator_rules_list.interface.ts'; export function Validate( rules: Record | Record>, ): MethodDecorator { - return (_target, _methodName, descriptor) => { + return (originalMethod) => { Reflector.defineMetadata( 'assert', rules, - descriptor.value as object, + originalMethod, ); - return descriptor; + return originalMethod; }; } diff --git a/src/web_socket/decorators/channel.decorator.ts b/src/web_socket/decorators/channel.decorator.ts index f5c27ae..0d7a129 100644 --- a/src/web_socket/decorators/channel.decorator.ts +++ b/src/web_socket/decorators/channel.decorator.ts @@ -1,7 +1,8 @@ +import { ClassDecorator } from '../../utils/types/class_decorator.type.ts'; import { Reflector } from '../../utils/reflector.class.ts'; export function Channel(name: string): ClassDecorator { - return (target: object) => { - Reflector.defineMetadata('name', name, target); + return (originalClass) => { + Reflector.defineMetadata('name', name, originalClass); }; } diff --git a/src/web_socket/decorators/subscribe.decorator.ts b/src/web_socket/decorators/subscribe.decorator.ts index 613cf78..f25571d 100644 --- a/src/web_socket/decorators/subscribe.decorator.ts +++ b/src/web_socket/decorators/subscribe.decorator.ts @@ -1,13 +1,14 @@ +import { MethodDecorator } from '../../utils/types/method_decorator.type.ts'; import { Reflector } from '../../utils/reflector.class.ts'; export function Subscribe(event: string): MethodDecorator { - return (_target, _methodName, descriptor) => { + return (originalMethod) => { Reflector.defineMetadata( 'subscribeToEvent', event, - descriptor.value as object, + originalMethod, ); - return descriptor; + return originalMethod; }; }