Skip to content

Commit

Permalink
feat(mvc): Add @UseFilter decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
Romakita committed Jun 21, 2019
1 parent 6fb9c9a commit d30c7d9
Show file tree
Hide file tree
Showing 34 changed files with 391 additions and 289 deletions.
4 changes: 2 additions & 2 deletions docs/docs/filters.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ export class BodyParamsFilter implements IFilter {
Then create the decorator. This decorator will be used on a controller method.

```typescript
import {ParamRegistry} from "@tsed/common";
import {UseFilter} from "@tsed/common";
import {BodyParamsFilter} from "../filters"

export function BodyParams(expression?: string | any, useType?: any): Function {
return ParamRegistry.decorate(BodyParamsFilter, {expression, useType});
return UseFilter(BodyParamsFilter, {expression, useType});
}
```

Expand Down
4 changes: 2 additions & 2 deletions packages/common/src/filters/decorators/bodyParams.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {BodyParamsFilter} from "../components/BodyParamsFilter";
import {ParamTypes} from "../interfaces/ParamTypes";
import {ParamRegistry} from "../registries/ParamRegistry";
import {UseFilter} from "./useFilter";

/**
* BodyParams return the value from [request.body](http://expressjs.com/en/4x/api.html#req.body) object.
Expand Down Expand Up @@ -39,7 +39,7 @@ import {ParamRegistry} from "../registries/ParamRegistry";
* @returns {Function}
*/
export function BodyParams(expression?: string | any, useType?: any): Function {
return ParamRegistry.decorate(BodyParamsFilter, {
return UseFilter(BodyParamsFilter, {
expression,
useType,
useConverter: true,
Expand Down
6 changes: 3 additions & 3 deletions packages/common/src/filters/decorators/cookies.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {ParamTypes} from "../interfaces/ParamTypes";
import {CookiesFilter} from "../components/CookiesFilter";
import {ParamRegistry} from "../registries/ParamRegistry";
import {ParamTypes} from "../interfaces/ParamTypes";
import {UseFilter} from "./useFilter";

/**
* Cookies or CookiesParams return the value from [request.cookies](http://expressjs.com/en/4x/api.html#req.cookies) object.
Expand Down Expand Up @@ -34,7 +34,7 @@ import {ParamRegistry} from "../registries/ParamRegistry";
* @returns {Function}
*/
export function CookiesParams(expression?: string | any, useType?: any): Function {
return ParamRegistry.decorate(CookiesFilter, {
return UseFilter(CookiesFilter, {
expression,
useType,
paramType: ParamTypes.COOKIES
Expand Down
4 changes: 2 additions & 2 deletions packages/common/src/filters/decorators/endpointInfo.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {EndpointMetadata} from "../../mvc/class/EndpointMetadata";
import {ENDPOINT_INFO} from "../constants";
import {ParamRegistry} from "../registries/ParamRegistry";
import {UseFilter} from "./useFilter";

export type EndpointInfo = EndpointMetadata;

Expand All @@ -10,5 +10,5 @@ export type EndpointInfo = EndpointMetadata;
* @decorator
*/
export function EndpointInfo(): Function {
return ParamRegistry.decorate(ENDPOINT_INFO);
return UseFilter(ENDPOINT_INFO);
}
4 changes: 2 additions & 2 deletions packages/common/src/filters/decorators/error.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {ParamRegistry} from "../registries/ParamRegistry";
import {EXPRESS_ERR} from "../constants";
import {UseFilter} from "./useFilter";

/**
*
* @returns {Function}
* @decorators
*/
export function Err(): Function {
return ParamRegistry.decorate(EXPRESS_ERR);
return UseFilter(EXPRESS_ERR);
}
6 changes: 3 additions & 3 deletions packages/common/src/filters/decorators/headerParams.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {ParamTypes} from "../interfaces/ParamTypes";
import {HeaderParamsFilter} from "../components/HeaderParamsFilter";
import {ParamRegistry} from "../registries/ParamRegistry";
import {ParamTypes} from "../interfaces/ParamTypes";
import {UseFilter} from "./useFilter";

/**
* HeaderParams return the value from [request.params](http://expressjs.com/en/4x/api.html#req.params) object.
Expand Down Expand Up @@ -28,7 +28,7 @@ import {ParamRegistry} from "../registries/ParamRegistry";
* @returns {Function}
*/
export function HeaderParams(expression: string): Function {
return ParamRegistry.decorate(HeaderParamsFilter, {
return UseFilter(HeaderParamsFilter, {
expression,
paramType: ParamTypes.HEADER
});
Expand Down
6 changes: 3 additions & 3 deletions packages/common/src/filters/decorators/locals.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {ParamTypes} from "../interfaces/ParamTypes";
import {LocalsFilter} from "../components/LocalsFilter";
import {ParamRegistry} from "../registries/ParamRegistry";
import {ParamTypes} from "../interfaces/ParamTypes";
import {UseFilter} from "./useFilter";

/**
* Locals return the value from [response.locals](http://expressjs.com/en/4x/api.html#res.locals) object.
Expand Down Expand Up @@ -28,7 +28,7 @@ import {ParamRegistry} from "../registries/ParamRegistry";
* @returns {Function}
*/
export function Locals(expression?: string | any): Function {
return ParamRegistry.decorate(LocalsFilter, {
return UseFilter(LocalsFilter, {
expression,
useConverter: false,
paramType: ParamTypes.LOCALS
Expand Down
4 changes: 2 additions & 2 deletions packages/common/src/filters/decorators/next.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as Express from "express";
import {EXPRESS_NEXT_FN} from "../constants";
import {ParamRegistry} from "../registries/ParamRegistry";
import {UseFilter} from "./useFilter";

export type Next = Express.NextFunction;

Expand All @@ -10,5 +10,5 @@ export type Next = Express.NextFunction;
* @decorator
*/
export function Next(): Function {
return ParamRegistry.decorate(EXPRESS_NEXT_FN);
return UseFilter(EXPRESS_NEXT_FN);
}
6 changes: 3 additions & 3 deletions packages/common/src/filters/decorators/pathParams.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {ParamTypes} from "../interfaces/ParamTypes";
import {PathParamsFilter} from "../components/PathParamsFilter";
import {ParamRegistry} from "../registries/ParamRegistry";
import {ParamTypes} from "../interfaces/ParamTypes";
import {UseFilter} from "./useFilter";

/**
* PathParams return the value from [request.params](http://expressjs.com/en/4x/api.html#req.params) object.
Expand Down Expand Up @@ -29,7 +29,7 @@ import {ParamRegistry} from "../registries/ParamRegistry";
* @returns {Function}
*/
export function PathParams(expression?: string | any, useType?: any): Function {
return ParamRegistry.decorate(PathParamsFilter, {
return UseFilter(PathParamsFilter, {
expression,
useType,
paramType: ParamTypes.PATH
Expand Down
4 changes: 2 additions & 2 deletions packages/common/src/filters/decorators/queryParams.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {QueryParamsFilter} from "../components/QueryParamsFilter";
import {ParamTypes} from "../interfaces/ParamTypes";
import {ParamRegistry} from "../registries/ParamRegistry";
import {UseFilter} from "./useFilter";

/**
* QueryParams return the value from [request.query](http://expressjs.com/en/4x/api.html#req.query) object.
Expand Down Expand Up @@ -39,7 +39,7 @@ import {ParamRegistry} from "../registries/ParamRegistry";
* @returns {Function}
*/
export function QueryParams(expression?: string | any, useType?: any): Function {
return ParamRegistry.decorate(QueryParamsFilter, {
return UseFilter(QueryParamsFilter, {
expression,
useType,
useConverter: true,
Expand Down
3 changes: 2 additions & 1 deletion packages/common/src/filters/decorators/request.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {UseFilter} from "./useFilter";
import * as Express from "express";
import {EXPRESS_REQUEST} from "../constants";
import {ParamRegistry} from "../registries/ParamRegistry";
Expand All @@ -21,5 +22,5 @@ export function Request(): Function {
* @alias Request
*/
export function Req() {
return ParamRegistry.decorate(EXPRESS_REQUEST);
return UseFilter(EXPRESS_REQUEST);
}
4 changes: 2 additions & 2 deletions packages/common/src/filters/decorators/response.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as Express from "express";
import {EXPRESS_RESPONSE} from "../constants";
import {ParamRegistry} from "../registries/ParamRegistry";
import {UseFilter} from "./useFilter";

export type Response = Express.Response;
export type Res = Express.Response;
Expand All @@ -21,5 +21,5 @@ export function Response(): Function {
* @alias Request
*/
export function Res() {
return ParamRegistry.decorate(EXPRESS_RESPONSE);
return UseFilter(EXPRESS_RESPONSE);
}
4 changes: 2 additions & 2 deletions packages/common/src/filters/decorators/responseData.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {RESPONSE_DATA} from "../constants";
import {ParamRegistry} from "../registries/ParamRegistry";
import {UseFilter} from "./useFilter";

/**
*
* @returns {function(Function, (string|symbol), number): void}
* @decorator
*/
export function ResponseData(): Function {
return ParamRegistry.decorate(RESPONSE_DATA);
return UseFilter(RESPONSE_DATA);
}
6 changes: 3 additions & 3 deletions packages/common/src/filters/decorators/session.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {ParamTypes} from "../interfaces/ParamTypes";
import {SessionFilter} from "../components/SessionFilter";
import {ParamRegistry} from "../registries/ParamRegistry";
import {ParamTypes} from "../interfaces/ParamTypes";
import {UseFilter} from "./useFilter";

/**
* Session return the value from [request.session](http://expressjs.com/en/4x/api.html#req.session) object.
Expand Down Expand Up @@ -29,7 +29,7 @@ import {ParamRegistry} from "../registries/ParamRegistry";
* @returns {Function}
*/
export function Session(expression?: string | any, useType?: any): Function {
return ParamRegistry.decorate(SessionFilter, {
return UseFilter(SessionFilter, {
expression,
useType,
paramType: ParamTypes.SESSION
Expand Down
22 changes: 22 additions & 0 deletions packages/common/src/filters/decorators/useFilter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {StoreMerge, Type} from "@tsed/core";
import {IInjectableParamSettings} from "../interfaces/IInjectableParamSettings";
import {ParamRegistry} from "../registries/ParamRegistry";

export function UseFilter(token: Type<any> | symbol, options: Partial<IInjectableParamSettings<any>> = {}): ParameterDecorator {
return (target: Type<any>, propertyKey: string | symbol, parameterIndex: number): any => {
const settings = Object.assign(
{
target,
propertyKey,
parameterIndex
},
options
);

if (typeof token === "symbol") {
ParamRegistry.usePreHandler(token, settings);
} else {
ParamRegistry.useFilter(token, settings);
}
};
}
1 change: 1 addition & 0 deletions packages/common/src/filters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export * from "./errors/ParseExpressionError";
export * from "./errors/RequiredParamError";
export * from "./errors/UnknowFilterError";

export * from "./decorators/useFilter";
export * from "./decorators/bodyParams";
export * from "./decorators/cookies";
export * from "./decorators/filter";
Expand Down
25 changes: 12 additions & 13 deletions packages/common/src/filters/registries/ParamRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,39 +26,39 @@ export class ParamRegistry {
/**
*
* @param target
* @param targetKey
* @param propertyKey
* @returns {Array}
*/
static getParams = (target: Type<any>, targetKey?: string | symbol): ParamMetadata[] =>
Metadata.has(PARAM_METADATA, target, targetKey) ? Metadata.get(PARAM_METADATA, target, targetKey) : [];
static getParams(target: Type<any>, propertyKey: string | symbol): ParamMetadata[] {
return Metadata.has(PARAM_METADATA, target, propertyKey) ? Metadata.get(PARAM_METADATA, target, propertyKey) : [];
}

/**
*
* @param target
* @param targetKey
* @param propertyKey
* @param index
* @param paramMetadata
*/
static set(target: Type<any>, targetKey: string | symbol, index: number, paramMetadata: ParamMetadata): void {
const params = Metadata.has(PARAM_METADATA, target, targetKey) ? Metadata.get(PARAM_METADATA, target, targetKey) : [];
static set(target: Type<any>, propertyKey: string | symbol, index: number, paramMetadata: ParamMetadata): void {
const params = this.getParams(target, propertyKey);

params[index] = paramMetadata;

Metadata.set(PARAM_METADATA, params, target, targetKey);
Metadata.set(PARAM_METADATA, params, target, propertyKey);
}

/**
*
* @param service
* @param settings
* @deprecated
*/
static usePreHandler(service: symbol, settings: IParamArgs<any>) {
const param = ParamRegistry.get(settings.target, settings.propertyKey, settings.parameterIndex);
param.service = service;
param.useConverter = false;

ParamRegistry.set(settings.target, settings.propertyKey, settings.parameterIndex, param);

return this;
}

Expand All @@ -68,15 +68,14 @@ export class ParamRegistry {
* @param propertyKey
* @param parameterIndex
* @param allowedRequiredValues
* @deprecated
*/
static required(target: Type<any>, propertyKey: string | symbol, parameterIndex: number, allowedRequiredValues: any[] = []) {
const param = ParamRegistry.get(target, propertyKey, parameterIndex);

param.required = true;
param.allowedRequiredValues = allowedRequiredValues;

ParamRegistry.set(target, propertyKey, parameterIndex, param);

param.store.merge("responses", {
"400": {
description: "BadRequest"
Expand All @@ -91,6 +90,7 @@ export class ParamRegistry {
* @param token
* @param {Partial<IInjectableParamSettings<any>>} options
* @returns {Function}
* @deprecated
*/
static decorate(token: Type<any> | symbol, options: Partial<IInjectableParamSettings<any>> = {}): ParameterDecorator {
return (target: Type<any>, propertyKey: string | symbol, parameterIndex: number): any => {
Expand All @@ -117,6 +117,7 @@ export class ParamRegistry {
*
* @param service
* @param options
* @deprecated
*/
static useFilter(service: Type<any>, options: IInjectableParamSettings<any>): ParamMetadata {
const {propertyKey, parameterIndex, target, useConverter, useValidation, paramType} = options;
Expand Down Expand Up @@ -146,8 +147,6 @@ export class ParamRegistry {
param.useConverter = useConverter;
}

ParamRegistry.set(target, propertyKey, parameterIndex, param);

return param;
}
}
32 changes: 20 additions & 12 deletions packages/common/test/filters/decorators/bodyParams.spec.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,34 @@
import {prototypeOf} from "@tsed/core";
import * as Sinon from "sinon";
import {BodyParams, ParamRegistry, ParamTypes} from "../../../src/filters";
import {BodyParamsFilter} from "../../../src/filters/components/BodyParamsFilter";

class Test {
}

describe("BodyParams", () => {
const sandbox = Sinon.createSandbox();
describe("@BodyParams", () => {
before(() => {
this.decorateStub = Sinon.stub(ParamRegistry, "decorate");
BodyParams("test", Test);
sandbox.stub(ParamRegistry, "useFilter");
});

after(() => {
this.decorateStub.restore();
sandbox.restore();
});
it("should call ParamFilter.useFilter method with the correct parameters", () => {
class Test {
}

it("should have been called ParamFilter.decorate method with the correct parameters", () =>
this.decorateStub.should.have.been.calledOnce.and.calledWithExactly(BodyParamsFilter, {
expression: "test",
class Ctrl {
test(@BodyParams("expression", Test) body: Test) {
}
}

ParamRegistry.useFilter.should.have.been.calledOnce.and.calledWithExactly(BodyParamsFilter, {
target: prototypeOf(Ctrl),
propertyKey: "test",
parameterIndex: 0,
expression: "expression",
useType: Test,
useConverter: true,
useValidation: true,
paramType: ParamTypes.BODY
}));
});
});
});
Loading

0 comments on commit d30c7d9

Please sign in to comment.