Skip to content

Commit

Permalink
refactor: rewrite decorators to use new api standard
Browse files Browse the repository at this point in the history
  • Loading branch information
dominiq007 committed Feb 10, 2024
1 parent 67d5742 commit 2489b3f
Show file tree
Hide file tree
Showing 19 changed files with 90 additions and 51 deletions.
7 changes: 5 additions & 2 deletions src/http/decorators/base_path.decorator.ts
Original file line number Diff line number Diff line change
@@ -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<RoutePath>('basePath', path, target);
return (originalClass) => {
Reflector.defineMetadata<RoutePath>('basePath', path, originalClass);

return originalClass;
};
}
7 changes: 4 additions & 3 deletions src/http/decorators/cookies.decorator.ts
Original file line number Diff line number Diff line change
@@ -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<string, string>): MethodDecorator {
return (_target, _methodName, descriptor) => {
return (originalMethod) => {
Reflector.defineMetadata<Record<string, string>>(
'cookies',
cookies,
descriptor.value as object,
originalMethod,
);

return descriptor;
return originalMethod;
};
}
7 changes: 4 additions & 3 deletions src/http/decorators/headers.decorator.ts
Original file line number Diff line number Diff line change
@@ -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<string, string>): MethodDecorator {
return (_target, _methodName, descriptor) => {
return (originalMethod) => {
Reflector.defineMetadata<Record<string, string>>(
'headers',
headers,
descriptor.value as object,
originalMethod,
);

return descriptor;
return originalMethod;
};
}
7 changes: 4 additions & 3 deletions src/http/decorators/name.decorator.ts
Original file line number Diff line number Diff line change
@@ -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<string>(
'name',
name,
descriptor.value as object,
originalMethod,
);

return descriptor;
return originalMethod;
};
}
7 changes: 4 additions & 3 deletions src/http/decorators/redirect.decorator.ts
Original file line number Diff line number Diff line change
@@ -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>(
'redirectDestination',
destination,
descriptor.value as object,
originalMethod,
);

return descriptor;
return originalMethod;
};
}
7 changes: 4 additions & 3 deletions src/http/decorators/render.decorator.ts
Original file line number Diff line number Diff line change
@@ -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<string>(
'view',
view,
descriptor.value as object,
originalMethod,
);

return descriptor;
return originalMethod;
};
}
7 changes: 4 additions & 3 deletions src/http/decorators/status_code.decorator.ts
Original file line number Diff line number Diff line change
@@ -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<HttpStatus>(
'statusCode',
statusCode,
descriptor.value as object,
originalMethod,
);

return descriptor;
return originalMethod;
};
}
7 changes: 4 additions & 3 deletions src/http/decorators/transform.decorator.ts
Original file line number Diff line number Diff line change
@@ -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<string, Constructor<Pipe>>,
): MethodDecorator {
return (_target, _methodName, descriptor) => {
return (originalMethod) => {
Reflector.defineMetadata<Record<string, Constructor<Pipe>>>(
'pipes',
pipes,
descriptor.value as object,
originalMethod,
);

return descriptor;
return originalMethod;
};
}
7 changes: 4 additions & 3 deletions src/http/decorators/use.decorator.ts
Original file line number Diff line number Diff line change
@@ -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<Middleware>[]): MethodDecorator {
return (_target, _methodName, descriptor) => {
return (originalMethod) => {
Reflector.defineMetadata<Constructor<Middleware>[]>(
'middleware',
middleware,
descriptor.value as object,
originalMethod,
);

return descriptor;
return originalMethod;
};
}
5 changes: 3 additions & 2 deletions src/injector/decorators/non_singleton.decorator.ts
Original file line number Diff line number Diff line change
@@ -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<boolean>('singleton', false, target);
return (originalClass) => {
Reflector.defineMetadata<boolean>('singleton', false, originalClass);
};
}
7 changes: 4 additions & 3 deletions src/router/decorators/route.decorator.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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;
};
}
20 changes: 17 additions & 3 deletions src/router/router.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -434,18 +435,31 @@ export class Router {
methods: EnumValuesUnion<HttpMethod>[],
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<Partial<Route>>(
'route',
{
methods,
path,
...options,
},
descriptor.value as object,
originalMethod,
);

return descriptor;
return originalMethod;
};
};

Expand Down
7 changes: 4 additions & 3 deletions src/scheduler/decorators/interval.decorator.ts
Original file line number Diff line number Diff line change
@@ -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;
};
}
7 changes: 4 additions & 3 deletions src/scheduler/decorators/schedule.decorator.ts
Original file line number Diff line number Diff line change
@@ -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;
};
}
7 changes: 4 additions & 3 deletions src/scheduler/decorators/timeout.decorator.ts
Original file line number Diff line number Diff line change
@@ -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;
};
}
6 changes: 6 additions & 0 deletions src/utils/types/class_decorator.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Constructor } from '../interfaces/constructor.interface.ts';

export type ClassDecorator<TTarget extends Function = unknown> = (
originalClass: Constructor<TTarget>,
context: ClassDecoratorContext,
) => TTarget;
7 changes: 4 additions & 3 deletions src/validator/decorators/validate.decorator.ts
Original file line number Diff line number Diff line change
@@ -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<string, Partial<ValidatorRulesList> | Record<string, unknown>>,
): MethodDecorator {
return (_target, _methodName, descriptor) => {
return (originalMethod) => {
Reflector.defineMetadata<typeof rules>(
'assert',
rules,
descriptor.value as object,
originalMethod,
);

return descriptor;
return originalMethod;
};
}
5 changes: 3 additions & 2 deletions src/web_socket/decorators/channel.decorator.ts
Original file line number Diff line number Diff line change
@@ -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<string>('name', name, target);
return (originalClass) => {
Reflector.defineMetadata<string>('name', name, originalClass);
};
}
7 changes: 4 additions & 3 deletions src/web_socket/decorators/subscribe.decorator.ts
Original file line number Diff line number Diff line change
@@ -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<string>(
'subscribeToEvent',
event,
descriptor.value as object,
originalMethod,
);

return descriptor;
return originalMethod;
};
}

0 comments on commit 2489b3f

Please sign in to comment.