-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparam.ts
More file actions
72 lines (62 loc) · 2.68 KB
/
param.ts
File metadata and controls
72 lines (62 loc) · 2.68 KB
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
71
72
import { PARAMETER_PARAMKEY } from '../constants'
import { getOwnMetadata, defineMetadata, getMetadata } from '../metadata'
import { getFunctionParameters } from '../utils'
export const param = (target: any, targetKey?: string, parameterIndex?: number): any => {
let name;
let config = {};
let decorator = function (target, targetKey, parameterIndex: number) {
let parameterNames = getFunctionParameters(target, targetKey);
let existingParameters: any[] = getOwnMetadata(PARAMETER_PARAMKEY, target, targetKey) || [];
let paramName = parameterNames[parameterIndex];
existingParameters.push({
...config,
from: name || paramName,
parameterIndex,
targetKey,
type: 'param'
});
defineMetadata(PARAMETER_PARAMKEY, existingParameters, target, targetKey);
}
if (typeof target == "string" || typeof target == "undefined" || !target) {
name = target;
return decorator;
} else if (typeof target === 'object' && target && !targetKey) {
name = target.name;
config = target
return decorator
} else {
return decorator(target, targetKey, parameterIndex);
}
}
export const createParameterDecorator = (type: string, defaultConfig?: any) => (target?, targetKey?, parameterIndex?: number): any => {
let config = { ...(defaultConfig || {}) };
const decorator = function (target, targetKey, parameterIndex: number) {
const parameterNames = getFunctionParameters(target, targetKey);
const existingParameters: any[] = getOwnMetadata(PARAMETER_PARAMKEY, target, targetKey) || [];
const paramName = parameterNames[parameterIndex];
existingParameters.push({
from: paramName,
parameterIndex,
targetKey,
type,
config,
target
});
defineMetadata(PARAMETER_PARAMKEY, existingParameters, target, targetKey);
}
if (typeof target == "undefined" || !target) {
return decorator
} else if (typeof target === 'object' && target && !targetKey) {
config = { ...config, ...target }
return decorator
} else {
return decorator(target, targetKey, parameterIndex);
}
}
export const serviceParams = createParameterDecorator('serviceParams')
export const request = createParameterDecorator('request')
export const error = createParameterDecorator('error')
export const result = createParameterDecorator('result')
export const functionalServiceName = createParameterDecorator('functionalServiceName')
export const provider = createParameterDecorator('provider')
export const stage = createParameterDecorator('stage')