Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Passing options with UseInterceptor #860

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/RoutingControllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ export class RoutingControllers<T extends BaseDriver> {
* Creates interceptors from the given "use interceptors".
*/
protected prepareInterceptors(uses: InterceptorMetadata[]): Function[] {
uses.sort((interceptor1, interceptor2) => interceptor2.priority - interceptor1.priority);
return uses.map(use => {
if (use.interceptor.prototype && use.interceptor.prototype.intercept) {
// if this is function instance of InterceptorInterface
Expand Down
34 changes: 31 additions & 3 deletions src/decorator/UseInterceptor.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,57 @@
import { getMetadataArgsStorage } from '../index';
import { Action } from '../Action';
import { UseInterceptorMetadataArgs } from '../metadata/args/UseInterceptorMetadataArgs';

/**
* Specifies a given interceptor middleware or interceptor function, to which UseInterceptorMetadataArgs can be applied, to be used for controller or controller action.
* Must be set to controller action or controller class.
*/
export function UseInterceptor(
optionsOrInterceptor: Partial<UseInterceptorMetadataArgs> | Function,
...interceptors: Array<Function>
): Function;

/**
* Specifies a given interceptor middleware or interceptor function to be used for controller or controller action.
* Must be set to controller action or controller class.
*/
export function UseInterceptor(...interceptors: Array<Function>): Function;

/**
* Specifies a given interceptor middleware or interceptor function, to which UseInterceptorMetadataArgs can be applied, to be used for controller or controller action.
* Must be set to controller action or controller class.
*/
export function UseInterceptor(
optionsOrInterceptor: Partial<UseInterceptorMetadataArgs> | ((action: Action, result: any) => any),
...interceptors: Array<(action: Action, result: any) => any>
): Function;

/**
* Specifies a given interceptor middleware or interceptor function to be used for controller or controller action.
* Must be set to controller action or controller class.
*/
export function UseInterceptor(...interceptors: Array<(action: Action, result: any) => any>): Function;

/**
* Specifies a given interceptor middleware or interceptor function to be used for controller or controller action.
* Specifies a given interceptor middleware or interceptor function, to which UseInterceptorMetadataArgs can be applied, to be used for controller or controller action.
* Must be set to controller action or controller class.
*/
export function UseInterceptor(...interceptors: Array<Function | ((action: Action, result: any) => any)>): Function {
export function UseInterceptor(
optionsOrInterceptor: Partial<UseInterceptorMetadataArgs> | Function | ((action: Action, result: any) => any),
...interceptors: Array<Function | ((action: Action, result: any) => any)>
): Function {
const optionsIsAnInterceptor = optionsOrInterceptor instanceof Function || Array.isArray(optionsOrInterceptor);
const options: Partial<UseInterceptorMetadataArgs> = optionsIsAnInterceptor
? {}
: (optionsOrInterceptor as Partial<UseInterceptorMetadataArgs>);
return function (objectOrFunction: Object | Function, methodName?: string) {
interceptors.forEach(interceptor => {
[...(optionsIsAnInterceptor ? [optionsOrInterceptor as Function] : []), ...interceptors].forEach(interceptor => {
getMetadataArgsStorage().useInterceptors.push({
...options,
interceptor: interceptor,
target: methodName ? objectOrFunction.constructor : (objectOrFunction as Function),
method: methodName,
priority: options.priority ?? 0,
});
});
};
Expand Down