-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecorators.ts
More file actions
343 lines (323 loc) · 8.77 KB
/
decorators.ts
File metadata and controls
343 lines (323 loc) · 8.77 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/**
* @module decorators
* A decorator library based off of:
* https://www.typescriptlang.org/docs/handbook/decorators.html
* Description: A middleware enabled framework for simplifying creating decorators
*/
import {
noop,
} from "../combinators";
import {
type ApiRecord,
createMiddlewareApi,
MiddlewareParams,
} from "../middleware";
import type {
AnyObject,
ArrowFunction,
Callable,
Constructable,
ConstructorFunction,
} from "../types";
import {
filterUndefinedFromObject,
updateConstObject,
} from "../utils";
/**
* Name: Decorator<T, V, R>
* Description: Superset Decorator type
* @public
* @template T, V, R - T: target type, V: TypedPropertyDescriptor value, R: return type
* @typedef {Function} Decorator<T, V, R>
*/
export type Decorator<
T extends
| Callable
| Constructable,
V = any,
R extends DecoratorReturnType<V> = DecoratorReturnType<V>
> = Callable<[
target: T,
propertyKey?:
| string
| symbol,
optionalParam?:
| number
| PropertyDescriptor
| TypedPropertyDescriptor<V>,
], R>;
/**
* Name: DecoratorCallback<T, V>
* Description:
* @public
* @template T, V - T: target type, V: TypedPropertyDescriptor value
* @typedef {Function} DecoratorCallback<T, V>
*/
export type DecoratorCallback<T, V, R = any> = Callable<DecoratorCallbackParams<T, V>, R>;
/**
* Name: DecoratorCallbackParams<T, V>
* Description: Superset Decorator type
* @public
* @template T, V - T: target type, V: TypedPropertyDescriptor value
* @typedef {Object} DecoratorCallbackParams<T, V>
* @property {any} target - Target type
* @property {string | symbol} propertyKey - Name of the property key
* @property {PropertyDescriptor | TypedPropertyDescriptor<V>} descriptor - Decorator descriptor
* @property {number} parameterIndex - 0-index number parameter order
* @property {Function} api - Middleware API interface
*/
export type DecoratorCallbackParams<T, V> = {
target: T,
propertyKey?:
| string
| symbol,
descriptor?:
| PropertyDescriptor
| TypedPropertyDescriptor<V>,
parameterIndex?: number,
api?: Callable<any[], ApiRecord>,
};
/**
* Name: DecoratorReturnType
* Description: Pattern matching type for all possible return types for a decorator
* @public
* @template V - TypedPropertyDescriptor value
* @typedef {void | Callable | Constructable | typeof Function | PropertyDescriptor | TypedPropertyDescriptor<D>} - creates a union type: 'DecoratorReturnType'
*/
export type DecoratorReturnType<V = any> =
| void
| Callable
| Constructable
| typeof Function
| PropertyDescriptor
| TypedPropertyDescriptor<V>;
/**
* Name: CreateDecoratorParams<T, V>
* Description: Parameters for createDecorator
* @private
* @template T, V - T: target type, V: TypedPropertyDescriptor value
* @typedef {Object} CreateDecoratorParams<T, V>
* @property {Function} callback - DecoratorCallback<T, V> handler
*/
type CreateDecoratorParams<T, V> = {
callback: DecoratorCallback<T, V>;
} & MiddlewareParams;
/**
* Name: createDecorator<T, V, R>
* Description: Generic decorator factory
* @public
* @template T, V, R - T: target type, V: TypedPropertyDescriptor value, R: return type
* @param params - CreateDecoratorParams<T, V>
* @returns Decorator<T, V, R>
*/
export const createDecorator = <
T extends
| ArrowFunction
| ConstructorFunction,
V = any,
R extends DecoratorReturnType<V> = DecoratorReturnType<V>
>(params: CreateDecoratorParams<T, V>): Decorator<T, V, R> => {
const {
callback,
createApi,
middleware,
sort,
transform,
} = params;
const api = createMiddlewareApi({
createApi,
middleware,
sort,
transform,
});
return (
target: T,
propertyKey?:
| string
| symbol,
optionalParam?:
| number
| PropertyDescriptor
| TypedPropertyDescriptor<V>
): R => {
let descriptor:
| PropertyDescriptor
| TypedPropertyDescriptor<V>;
let parameterIndex: number;
if (typeof optionalParam === 'number') {
parameterIndex = optionalParam;
} else if (optionalParam) {
descriptor = optionalParam;
}
const values: AnyObject = {
api,
descriptor,
parameterIndex,
propertyKey,
target,
};
const filteredValues = <DecoratorCallbackParams<T, V>>filterUndefinedFromObject(values);
return callback(filteredValues);
};
};
/**
* Decorator factories
*/
/**
* Name: createClassDecorator<T, V, R>
* Description: Class (top level constructor) decorator factory
* @public
* @param params CreateDecoratorParams<T, V>
* @returns Decorator<T, V, R>
*/
export const createClassDecorator = <
T extends ConstructorFunction = ConstructorFunction,
V = any,
R extends DecoratorReturnType<V> =
| void
| FunctionConstructor,
>(params: CreateDecoratorParams<T, V>) => {
return createDecorator<T, V, R>(params);
};
/**
* Name: createClassMemberDecorator<T, V, R>
* Description: Class Member (inner level) decorator factory
* @public
* @param params CreateDecoratorParams<T, V>
* @returns Decorator<T, V, R>
*/
export const createClassMemberDecorator = <
T extends ConstructorFunction = ConstructorFunction,
V = any,
R extends DecoratorReturnType<() => void> =
| void
| PropertyDescriptor
| TypedPropertyDescriptor<() => void>,
>(params: CreateDecoratorParams<T, V>) : any => {
return createDecorator<T, V, R>(params);
};
/**
* Handler factories
* - Handles updates for Class and Class members
* - Slimplified factories without middleware support
* - To be used in decorator callbacks to streamline decorator handling
* - Main focus is accessor/descriptor updates
*/
/**
* Name: createClassHandler
* Description: Class (top level constructor) decorator handler
* @public
* @param params calllback for handler
* @returns Class decorator (target) => void
*/
export const createClassHandler = (callback: Callable<any[], void>) => {
return (target: ConstructorFunction) => {
callback(target);
}
};
/**
* Class Method Handler
* - Only descriptors can be updated.
* - See: https://www.typescriptlang.org/docs/handbook/decorators.html#method-decorators
*
* Name: createClassMethodHandler
* Description: Class method (inner level) decorator handler
* @public
* @param params calllback: callback for handler, newDescriptor: descriptor override
* @returns Class method decorator (target, propertyKey, descriptor) => void
*/
export const createClassMethodHandler = <V = any>(
{
callback,
newDescriptor,
} : {
callback?: Callable<any[], void>,
newDescriptor:
| PropertyDescriptor
| TypedPropertyDescriptor<V>
}
) => {
return (
target: any,
propertyKey:
| string
| symbol,
descriptor:
| PropertyDescriptor
| TypedPropertyDescriptor<V>
) : void => {
if (callback) {
callback(target, propertyKey, descriptor);
} else if (newDescriptor) {
updateConstObject(descriptor, newDescriptor);
}
};
};
/**
* Class Method Parameter Handler
* https://www.typescriptlang.org/docs/handbook/decorators.html#parameter-decorators
*
* Name: createClassParameterHandler
* Description: Class parameter (inner level) decorator handler
* @public
* @param params calllback: callback for handler
* @returns Class parameter decorator (target, propertyKey, parameterIndex) => void
*/
export const createClassParameterHandler = (
callback: Callable<any[], void> = noop
) => {
return (
target: AnyObject,
propertyKey:
| string
| symbol,
parameterIndex: number
) => {
callback(target, propertyKey, parameterIndex);
};
};
/**
* Class Accessor/Property Handler
* - Property get and set can be changed.
* - See: https://www.typescriptlang.org/docs/handbook/decorators.html#property-decorators
* - NOTE: For property decorators to work, you must either use the 'declare' keyword or
* 'useDefineForClassFields' must be set to false in tsconfig.json.
* https://github.com/microsoft/TypeScript/issues/35081
*
* Example:
*
* `class Test {
* @Decorator
* declare prop: string;
* }`
*
* Name: createClassPropertyHandler
* Description: Class Member (inner level) decorator handler
* @public
* @param params calllback: callback for handler, accessors: accessor override
* @returns Class member decorator (target, propertyKey) => void
*/
export const createClassPropertyHandler = (
{
callback,
accessors,
}: {
callback?: Callable<any[], void>,
accessors?: {
get?: Callable<null, any>,
set?: Callable<any, void>,
}
}) => {
return (
target: any,
propertyKey:
| string
| symbol
) : void => {
if (callback) {
callback(target, propertyKey);
} else if (accessors) {
Reflect.defineProperty(target, propertyKey, accessors);
}
}
};