-
Notifications
You must be signed in to change notification settings - Fork 2
/
filter-annotation.ts
73 lines (70 loc) · 3.22 KB
/
filter-annotation.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
71
72
73
import { FilterConfig } from './contracts/filter-config';
import { FiltersService } from './filters-service';
import { cloneAsLiteral } from './utilities';
/**
* Object literal used by {@link getDefaultFilterConfig} function to build filter configuration.
* This object can be used to change default values of filter configs globally.
* By default it has next values:
* ```Javascript
* {
* coerce: true,
* defaultValue: undefined,
* emptyIsNull: false,
* ignoreOnAutoMap: false,
* omitIfNullOrUndefined: false,
* parameterName: <value of 'propertyName' parameter>,
* parseFormatter: undefined,
* propertyName: <value of 'propertyName' parameter>,
* serializeFormatter: undefined
* }
* ```
*/
export let DefaultFilterConfig = {
coerce: true,
defaultValue: undefined,
emptyIsNull: false,
ignoreOnAutoMap: false,
omitIfNullOrUndefined: false,
parseFormatter: undefined,
serializeFormatter: undefined
} as FilterConfig;
/**
* Returns filter configuration based on {@link DefaultFilterConfig} values with applied `parameterName` and `propertyName` properties values.
* @param propertyName name of the property in `target type`, for which configuration is created. This value will be used to set {@link FilterConfig.propertyName} and {@link FilterConfig.parameterName} values.
* @see {@link FilterConfig}
*/
export function getDefaultFilterConfig(propertyName: string): FilterConfig {
return {
parameterName: propertyName,
propertyName,
...cloneAsLiteral(DefaultFilterConfig)
} as FilterConfig;
}
/**
* Annotation that can be used to configure type property as filter to use with {@link FiltersService}
* @param targetOrNameOrConfig
* - if annotation is applied without any parameters then result of {@link getDefaultFilterConfig} function will be used. Value of {@link FilterConfig.parameterName} property will be equal to annotated property name.
* - if annotation is applied with string parameter then result of {@link getDefaultFilterConfig} function will be used. Value of {@link FilterConfig.parameterName} property will be equal to applied parameter value.
* - if annotation is applied with object as parameter then result of {@link getDefaultFilterConfig} will be used and all properties which were specified in passed object would be applied to resulting configuration via Object.assign.
* @param key specified by TypeScript automatically.
* @see {@link FilterConfig}
*/
export function filter(targetOrNameOrConfig?: string | FilterConfig, key?: string): any {
const decorateWithConfig = (target: object, key2: string): void => {
const config = getDefaultFilterConfig(key2);
if (typeof targetOrNameOrConfig === 'string') {
config.parameterName = targetOrNameOrConfig;
} else {
Object.assign(config, targetOrNameOrConfig);
}
FiltersService.registerFilterConfig(target.constructor, config);
};
if (key) {
const targetTemp = targetOrNameOrConfig;
// tslint:disable-next-line:no-parameter-reassignment
targetOrNameOrConfig = null;
decorateWithConfig(targetTemp as object, key);
return;
}
return decorateWithConfig;
}