1 | ;
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.Post = exports.Pre = exports.post = exports.pre = void 0;
|
4 | const constants_1 = require("./internal/constants");
|
5 | const errors_1 = require("./internal/errors");
|
6 | const utils_1 = require("./internal/utils");
|
7 | const logSettings_1 = require("./logSettings");
|
8 | // TSDoc for the hooks can't be added without adding it to *every* overload
|
9 | const hooks = {
|
10 | pre(...args) {
|
11 | return (target) => addToHooks(target, 'pre', args);
|
12 | },
|
13 | post(...args) {
|
14 | return (target) => addToHooks(target, 'post', args);
|
15 | },
|
16 | };
|
17 | /**
|
18 | * Add a hook to the hooks Array
|
19 | * @param target Target Class
|
20 | * @param hookType What type is it
|
21 | * @param args All Arguments, that should be passed-through
|
22 | */
|
23 | function addToHooks(target, hookType, args) {
|
24 | // Convert Method to array if only a string is provided
|
25 | const methods = Array.isArray(args[0]) ? args[0] : [args[0]];
|
26 | const func = args[1];
|
27 | const hookOptions = args[2];
|
28 | (0, utils_1.assertion)(typeof func === 'function', () => new errors_1.ExpectedTypeError('fn', 'function', func));
|
29 | if (args.length > 3) {
|
30 | logSettings_1.logger.warn(`"addToHooks" parameter "args" has a length of over 3 (length: ${args.length})`);
|
31 | }
|
32 | logSettings_1.logger.info('Adding hooks for "[%s]" to "%s" as type "%s"', methods.join(','), (0, utils_1.getName)(target), hookType);
|
33 | switch (hookType) {
|
34 | case 'post':
|
35 | const postHooks = Array.from(Reflect.getMetadata(constants_1.DecoratorKeys.HooksPost, target) ?? []);
|
36 | postHooks.push({ func, methods, options: hookOptions });
|
37 | Reflect.defineMetadata(constants_1.DecoratorKeys.HooksPost, postHooks, target);
|
38 | break;
|
39 | case 'pre':
|
40 | const preHooks = Array.from(Reflect.getMetadata(constants_1.DecoratorKeys.HooksPre, target) ?? []);
|
41 | preHooks.push({ func, methods, options: hookOptions });
|
42 | Reflect.defineMetadata(constants_1.DecoratorKeys.HooksPre, preHooks, target);
|
43 | break;
|
44 | }
|
45 | }
|
46 | exports.pre = hooks.pre;
|
47 | exports.post = hooks.post;
|
48 | // Export it PascalCased
|
49 | exports.Pre = hooks.pre;
|
50 | exports.Post = hooks.post;
|
51 | //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaG9va3MuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi9zcmMvaG9va3MudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7O0FBa0JBLG9EQUFxRDtBQUNyRCw4Q0FBc0Q7QUFDdEQsNENBQXNEO0FBQ3RELCtDQUF1QztBQTJIdkMsMkVBQTJFO0FBQzNFLE1BQU0sS0FBSyxHQUFVO0lBQ25CLEdBQUcsQ0FBQyxHQUFHLElBQUk7UUFDVCxPQUFPLENBQUMsTUFBVyxFQUFFLEVBQUUsQ0FBQyxVQUFVLENBQUMsTUFBTSxFQUFFLEtBQUssRUFBRSxJQUFJLENBQUMsQ0FBQztJQUMxRCxDQUFDO0lBQ0QsSUFBSSxDQUFDLEdBQUcsSUFBSTtRQUNWLE9BQU8sQ0FBQyxNQUFXLEVBQUUsRUFBRSxDQUFDLFVBQVUsQ0FBQyxNQUFNLEVBQUUsTUFBTSxFQUFFLElBQUksQ0FBQyxDQUFDO0lBQzNELENBQUM7Q0FDRixDQUFDO0FBRUY7Ozs7O0dBS0c7QUFDSCxTQUFTLFVBQVUsQ0FBQyxNQUFXLEVBQUUsUUFBd0IsRUFBRSxJQUFXO0lBQ3BFLHVEQUF1RDtJQUN2RCxNQUFNLE9BQU8sR0FBMkIsS0FBSyxDQUFDLE9BQU8sQ0FBQyxJQUFJLENBQUMsQ0FBQyxDQUFDLENBQUMsQ0FBQyxDQUFDLENBQUMsSUFBSSxDQUFDLENBQUMsQ0FBQyxDQUFDLENBQUMsQ0FBQyxDQUFDLElBQUksQ0FBQyxDQUFDLENBQUMsQ0FBQyxDQUFDO0lBQ3JGLE1BQU0sSUFBSSxHQUE2QixJQUFJLENBQUMsQ0FBQyxDQUFD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