-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
attribute-utils.ts
93 lines (83 loc) · 3.01 KB
/
attribute-utils.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import type { AttributeOptions, ModelStatic } from '../../model.js';
import { Model } from '../../model.js';
import { registerModelAttributeOptions } from '../shared/model.js';
import type {
OptionalParameterizedPropertyDecorator,
RequiredParameterizedPropertyDecorator,
} from './decorator-utils.js';
import {
createOptionallyParameterizedPropertyDecorator,
DECORATOR_NO_DEFAULT,
throwMustBeInstanceProperty,
throwMustBeMethod,
} from './decorator-utils.js';
/**
* Creates a decorator that registers Attribute Options. Parameters are mandatory.
*
* @param decoratorName The name of the decorator (must be equal to its export key)
* @param callback The callback that will return the Attribute Options.
*/
export function createRequiredAttributeOptionsDecorator<T>(
decoratorName: string,
callback: (
option: T,
target: Object,
propertyName: string | symbol,
propertyDescriptor: PropertyDescriptor | undefined,
) => Partial<AttributeOptions>,
): RequiredParameterizedPropertyDecorator<T> {
return createOptionalAttributeOptionsDecorator(decoratorName, DECORATOR_NO_DEFAULT, callback);
}
/**
* Creates a decorator that registers Attribute Options. Parameters are optional.
*
* @param decoratorName The name of the decorator (must be equal to its export key)
* @param defaultValue The default value, if no parameter was provided.
* @param callback The callback that will return the Attribute Options.
*/
export function createOptionalAttributeOptionsDecorator<T>(
decoratorName: string,
defaultValue: T | typeof DECORATOR_NO_DEFAULT,
callback: (
option: T,
target: Object,
propertyName: string,
propertyDescriptor: PropertyDescriptor | undefined,
) => Partial<AttributeOptions>,
): OptionalParameterizedPropertyDecorator<T> {
return createOptionallyParameterizedPropertyDecorator(
decoratorName,
defaultValue,
(decoratorOption, target, propertyName, propertyDescriptor) => {
if (typeof propertyName === 'symbol') {
throw new TypeError('Symbol Model Attributes are not currently supported. We welcome a PR that implements this feature.');
}
const attributeOptions = callback(decoratorOption, target, propertyName, propertyDescriptor);
annotate(decoratorName, target, propertyName, propertyDescriptor, attributeOptions);
},
);
}
function annotate(
decoratorName: string,
target: Object,
propertyName: string,
propertyDescriptor: PropertyDescriptor | undefined,
options: Partial<AttributeOptions>,
): void {
if (typeof target === 'function') {
throwMustBeInstanceProperty(decoratorName, target, propertyName);
}
if (!(target instanceof Model)) {
throwMustBeMethod(decoratorName, target, propertyName);
}
options = { ...options };
if (propertyDescriptor) {
if (propertyDescriptor.get) {
options.get = propertyDescriptor.get;
}
if (propertyDescriptor.set) {
options.set = propertyDescriptor.set;
}
}
registerModelAttributeOptions(target.constructor as ModelStatic, propertyName, options);
}