/
ParamMetadata.ts
70 lines (56 loc) · 1.83 KB
/
ParamMetadata.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import {ancestorsOf, DecoratorTypes, Enumerable, prototypeOf, Type} from "@tsed/core";
import {JsonEntityComponent, JsonEntityStore, JsonEntityStoreOptions, JsonParameter} from "@tsed/schema";
import {ParamTypes} from "./ParamTypes";
export interface ParamConstructorOptions extends JsonEntityStoreOptions {
expression?: string;
useType?: Type<any>;
paramType?: string | ParamTypes;
pipes?: Type<IPipe>[];
}
export interface IPipe<T = any, R = any> {
transform(value: T, metadata: ParamMetadata): R;
}
@JsonEntityComponent(DecoratorTypes.PARAM)
export class ParamMetadata extends JsonEntityStore implements ParamConstructorOptions {
/**
*
*/
@Enumerable()
public expression: string;
/**
*
*/
@Enumerable()
public paramType: string | ParamTypes;
@Enumerable()
public pipes: Type<IPipe>[] = [];
constructor(options: ParamConstructorOptions) {
super(options);
const {expression, paramType, pipes} = options;
this.expression = expression || this.expression;
this.paramType = paramType || this.paramType;
this.pipes = pipes || [];
}
/**
* Return the JsonOperation
*/
get parameter(): JsonParameter {
return this._parameter;
}
static get(target: Type<any>, propertyKey: string | symbol, index: number): ParamMetadata {
return JsonEntityStore.from<ParamMetadata>(prototypeOf(target), propertyKey, index);
}
static getParams(target: Type<any>, propertyKey: string | symbol): ParamMetadata[] {
const params: ParamMetadata[] = [];
const klass = ancestorsOf(target)
.reverse()
.find((target) => JsonEntityStore.fromMethod(target, propertyKey).children.size);
if (!klass) {
return [];
}
JsonEntityStore.fromMethod(klass, propertyKey).children.forEach((param: ParamMetadata, index) => {
params[+index] = param;
});
return params;
}
}