This repository has been archived by the owner on May 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(bootstrap): Refactor the bootstrap into a function in the core t…
…o reduce implementation boilerplate. Implement BeforeAll and AfterAll middleware decorators
- Loading branch information
Showing
8 changed files
with
189 additions
and
58 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
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
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,30 +1,69 @@ | ||
import { AbstractController } from '../controllers/abstract.controller'; | ||
import { InjectableMiddlewareFactory } from './index'; | ||
import { AbstractController, MiddlewareRegistry } from '../controllers/abstract.controller'; | ||
import { MiddlewareFactory } from './index'; | ||
import { initializeRelationMap } from '../../common/relations/index'; | ||
|
||
/** | ||
* Decorator for assigning middleware method in a controller | ||
* Decorator for assigning before middleware method in a controller | ||
* @returns {function(any, string, TypedPropertyDescriptor<T>): undefined} | ||
* @constructor | ||
* @param middlewareFactories | ||
*/ | ||
export function Before<T>(...middlewareFactories:InjectableMiddlewareFactory[]): MethodDecorator { | ||
export function Before<T>(...middlewareFactories: MiddlewareFactory[]): MethodDecorator { | ||
|
||
return function (target: AbstractController, propertyKey: string, descriptor: TypedPropertyDescriptor<T>) { | ||
|
||
target.registerMiddleware(propertyKey, 'before', middlewareFactories); | ||
target.registerMiddleware('before', middlewareFactories, propertyKey); | ||
}; | ||
} | ||
|
||
/** | ||
* Decorator for assigning middleware method in a controller | ||
* Decorator for assigning after middleware method in a controller | ||
* @returns {function(any, string, TypedPropertyDescriptor<T>): undefined} | ||
* @constructor | ||
* @param middlewareFactories | ||
*/ | ||
export function After<T>(...middlewareFactories:InjectableMiddlewareFactory[]): MethodDecorator { | ||
export function After<T>(...middlewareFactories: MiddlewareFactory[]): MethodDecorator { | ||
|
||
return function (target: AbstractController, propertyKey: string, descriptor: TypedPropertyDescriptor<T>) { | ||
|
||
target.registerMiddleware(propertyKey, 'after', middlewareFactories); | ||
target.registerMiddleware('after', middlewareFactories, propertyKey); | ||
}; | ||
} | ||
|
||
export function initializeMiddlewareRegister(target: AbstractController): void { | ||
if (!target.registeredMiddleware) { | ||
target.registeredMiddleware = { | ||
methods: new Map<string, MiddlewareRegistry>(), | ||
all: { | ||
before: [], | ||
after: [] | ||
} | ||
}; | ||
} | ||
} | ||
|
||
/** | ||
* Decorator for assigning before middleware to all methods in a controller | ||
* @param middlewareFactories | ||
* @returns {function(AbstractController): void} | ||
* @constructor | ||
*/ | ||
export function BeforeAll(...middlewareFactories: MiddlewareFactory[]): ClassDecorator { | ||
return function (target: Function): void { | ||
initializeMiddlewareRegister(target.prototype); | ||
target.prototype.registeredMiddleware.all.before.push(...middlewareFactories); | ||
} | ||
} | ||
|
||
/** | ||
* Decorator for assigning after middleware to all methods in a controller | ||
* @param middlewareFactories | ||
* @returns {function(AbstractController): void} | ||
* @constructor | ||
*/ | ||
export function AfterAll(...middlewareFactories: MiddlewareFactory[]): ClassDecorator { | ||
return function (target: Function): void { | ||
initializeMiddlewareRegister(target.prototype); | ||
target.prototype.registeredMiddleware.all.after.push(...middlewareFactories); | ||
} | ||
} |
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
Oops, something went wrong.