-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: rewrite decorators to use new api standard
- Loading branch information
1 parent
67d5742
commit cd86589
Showing
19 changed files
with
91 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { Constructor } from '../interfaces/constructor.interface.ts'; | ||
|
||
// deno-lint-ignore no-explicit-any | ||
export type ClassDecorator<TTarget extends Constructor = any> = ( | ||
originalClass: Constructor<TTarget>, | ||
context: ClassDecoratorContext, | ||
) => TTarget; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
} |