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

Feat/572 serializer deserializer #576

Closed
wants to merge 10 commits into from
Closed
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ notifications:
node_js:
- '11.6.0'
- '10'
- '9'
- '8'
after_success:
- npm run travis:deploy-once "npm run semantic-release"
Expand Down
2 changes: 1 addition & 1 deletion docs/api/common/jsonschema/decorators/JsonProperty.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,4 @@ According to the previous example, the JsonSchema generated will be as follow:
@jsonschema
@property

:::
:::
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,4 @@
"packages": "packages",
"test": "test"
}
}
}
10 changes: 5 additions & 5 deletions packages/common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@
"url": "https://github.com/TypedProject/ts-express-decorators.git"
},
"dependencies": {
"@types/json-schema": "^6.0.1",
"globby": "^8.0.1",
"@types/json-schema": "^7.0.3",
"globby": "^9.2.0",
"json-schema": "^0.2.3",
"rxjs": "^6.4.0",
"ts-httpexceptions": "^3.0.0",
"rxjs": "^6.5.2",
"ts-httpexceptions": "^4.1.0",
"ts-log-debug": "^5.1.0",
"tslib": "^1.9.0"
"tslib": "^1.10.0"
},
"peerDependencies": {
"@tsed/core": "0.0.0-PLACEHOLDER",
Expand Down
17 changes: 14 additions & 3 deletions packages/common/src/converters/services/ConverterService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,20 @@ export class ConverterService {
keys.forEach(propertyKey => {
if (typeof obj[propertyKey] !== "function") {
let propertyMetadata = ConverterService.getPropertyMetadata(properties, propertyKey);

let propertyValue = obj[propertyKey];
propertyMetadata = propertyMetadata || ({} as any);
plainObject[propertyMetadata!.name || propertyKey] = this.serialize(obj[propertyKey], {

propertyValue = this.serialize(propertyValue, {
checkRequiredValue // ,
// TODO revert change
// type: propertyMetadata!.type
});

if (typeof propertyMetadata!.onSerialize === "function") {
propertyValue = propertyMetadata!.onSerialize(propertyValue);
}

plainObject[propertyMetadata!.name || propertyKey] = propertyValue;
}
});

Expand Down Expand Up @@ -228,11 +235,15 @@ export class ConverterService {

propertyMetadata = propertyMetadata || ({} as any);

const propertyValue = obj[propertyMetadata!.name] || obj[propertyName];
let propertyValue = obj[propertyMetadata!.name] || obj[propertyName];
const propertyKey = propertyMetadata!.propertyKey || propertyName;

try {
if (typeof instance[propertyKey] !== "function") {
if (typeof propertyMetadata!.onDeserialize === "function") {
propertyValue = propertyMetadata!.onDeserialize(propertyValue);
}

instance[propertyKey] = this.deserialize(
propertyValue,
propertyMetadata!.isCollection ? propertyMetadata!.collectionType : propertyMetadata!.type,
Expand Down
6 changes: 3 additions & 3 deletions packages/common/src/filters/class/FilterBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Type} from "@tsed/core";
import {nameOf, Type} from "@tsed/core";
import {InjectorService} from "@tsed/di";
import {ConverterService} from "../../converters";
import {ParseExpressionError} from "../errors/ParseExpressionError";
Expand Down Expand Up @@ -88,7 +88,7 @@ export class FilterBuilder {
filter,
(value: any) => {
if (param.isRequired(value)) {
throw new RequiredParamError(param.name, param.expression);
throw new RequiredParamError(nameOf(param.service), param.expression);
}

return value;
Expand Down Expand Up @@ -139,7 +139,7 @@ export class FilterBuilder {
try {
validationService.validate(value, type, collectionType);
} catch (err) {
throw new ParseExpressionError(param.name, param.expression, err);
throw new ParseExpressionError(nameOf(param.service), param.expression, err);
}

return value;
Expand Down
193 changes: 12 additions & 181 deletions packages/common/src/filters/class/ParamMetadata.ts
Original file line number Diff line number Diff line change
@@ -1,202 +1,33 @@
import {nameOf, NotEnumerable, Storable, Type} from "@tsed/core";
import {Enumerable, Storable, Type} from "@tsed/core";
import {IParamOptions} from "../interfaces";
import {ParamTypes} from "../interfaces/ParamTypes";

export class ParamMetadata extends Storable implements IParamOptions<any> {
/**
*
*/
@NotEnumerable()
protected _expression: string | RegExp;
@Enumerable()
public expression: string | RegExp;
/**
*
* @type {boolean}
*/
@NotEnumerable()
protected _useConverter: boolean = true;
@Enumerable()
public paramType: ParamTypes;
/**
*
* @type {boolean}
*/
@NotEnumerable()
private _useValidation: boolean = false;

/**
*
*/
@NotEnumerable()
protected _service: string | Type<any> | symbol;

/**
* Allowed value when the entity is required.
* @type {Array}
*/
@NotEnumerable()
private _allowedRequiredValues: any[] = [];

/**
* Required entity.
*/
@NotEnumerable()
protected _required: boolean = false;
/**
*
*/
@NotEnumerable()
private _paramType: ParamTypes;

/**
*
* @returns {string|RegExp}
*/
get expression(): string | RegExp {
return this._expression;
}

@Enumerable()
public useValidation: boolean = false;
/**
*
* @param value
*/
set expression(value: string | RegExp) {
this._expression = value;
}

/**
*
* @returns {symbol}
*/
get service(): Type<any> | symbol {
return this._service as any;
}

/**
*
* @param value
*/
set service(value: Type<any> | symbol) {
this._service = value;
this.name = nameOf(value);
}

/**
*
* @param value
*/
set useConverter(value: boolean) {
this._useConverter = value;
}

/**
*
* @returns {boolean}
*/
get useConverter(): boolean {
return this._useConverter;
}

/**
*
* @returns {boolean}
*/
get useValidation(): boolean {
return this._useValidation;
}

/**
*
* @param {boolean} value
*/
set useValidation(value: boolean) {
this._useValidation = value;
}

/**
* Return the required state.
* @returns {boolean}
*/
get required(): boolean {
return this._required;
}

/**
* Change the state of the required data.
* @param value
*/
set required(value: boolean) {
this._required = value;
}

/**
* Return the allowed values.
* @returns {any[]}
*/
get allowedRequiredValues(): any[] {
return this._allowedRequiredValues;
}

/**
* Set the allowed values when the value is required.
* @param {any[]} value
*/
set allowedRequiredValues(value: any[]) {
this._allowedRequiredValues = value;
}

/**
*
* @returns {ParamTypes}
*/
get paramType(): ParamTypes {
return this._paramType;
}

/**
*
* @param {ParamTypes} value
*/
set paramType(value: ParamTypes) {
this._paramType = value;
}

/**
* This method use `EntityDescription.required` and `allowedRequiredValues` to validate the value.
* @param value
* @returns {boolean}
* @deprecated
*/
isValidRequiredValue(value: any): boolean {
if (this.required) {
if (value === undefined || value === null || value === "") {
if (this.allowedRequiredValues.indexOf(value) === -1) {
return false;
}
}
}

return true;
}

/**
*
* @param value
* @returns {boolean}
* @type {boolean}
*/
isRequired(value: any): boolean {
return this.required && [undefined, null, ""].indexOf(value) > -1 && this.allowedRequiredValues.indexOf(value) === -1;
}

@Enumerable()
public useConverter: boolean = true;
/**
*
* @returns {{service: (string|symbol), name: string, expression: string, required: boolean, use: undefined, baseType: undefined}}
*/
toJSON() {
return {
service: nameOf(this._service),
name: this.name,
expression: this._expression,
required: this._required,
use: this.typeName,
baseType: this.collectionName
};
}
@Enumerable()
public service: string | Type<any> | symbol;
}
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);
}
Loading