1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.hookMixin = exports.FeathersHookManager = exports.createContext = exports.enableHooks = exports.collectHooks = exports.convertHookData = void 0;
|
4 | const hooks_1 = require("@feathersjs/hooks");
|
5 | const service_1 = require("./service");
|
6 | const types = ['before', 'after', 'error', 'around'];
|
7 | const isType = (value) => types.includes(value);
|
8 |
|
9 |
|
10 | function convertHookData(input) {
|
11 | const result = {};
|
12 | if (Array.isArray(input)) {
|
13 | result.all = input;
|
14 | }
|
15 | else if (typeof input !== 'object') {
|
16 | result.all = [input];
|
17 | }
|
18 | else {
|
19 | for (const key of Object.keys(input)) {
|
20 | const value = input[key];
|
21 | result[key] = Array.isArray(value) ? value : [value];
|
22 | }
|
23 | }
|
24 | return result;
|
25 | }
|
26 | exports.convertHookData = convertHookData;
|
27 | function collectHooks(target, method) {
|
28 | const { collected, collectedAll, around } = target.__hooks;
|
29 | return [
|
30 | ...(around.all || []),
|
31 | ...(around[method] || []),
|
32 | ...(collectedAll.before || []),
|
33 | ...(collected[method] || []),
|
34 | ...(collectedAll.after || [])
|
35 | ];
|
36 | }
|
37 | exports.collectHooks = collectHooks;
|
38 |
|
39 | function enableHooks(object) {
|
40 | const store = {
|
41 | around: {},
|
42 | before: {},
|
43 | after: {},
|
44 | error: {},
|
45 | collected: {},
|
46 | collectedAll: {}
|
47 | };
|
48 | Object.defineProperty(object, '__hooks', {
|
49 | configurable: true,
|
50 | value: store,
|
51 | writable: true
|
52 | });
|
53 | return function registerHooks(input) {
|
54 | const store = this.__hooks;
|
55 | const map = Object.keys(input).reduce((map, type) => {
|
56 | if (!isType(type)) {
|
57 | throw new Error(`'${type}' is not a valid hook type`);
|
58 | }
|
59 | map[type] = convertHookData(input[type]);
|
60 | return map;
|
61 | }, {});
|
62 | const types = Object.keys(map);
|
63 | types.forEach((type) => Object.keys(map[type]).forEach((method) => {
|
64 | var _a;
|
65 | const mapHooks = map[type][method];
|
66 | const storeHooks = ((_a = store[type])[method] || (_a[method] = []));
|
67 | storeHooks.push(...mapHooks);
|
68 | if (method === 'all') {
|
69 | if (store.before[method] || store.error[method]) {
|
70 | const beforeAll = (0, hooks_1.collect)({
|
71 | before: store.before[method] || [],
|
72 | error: store.error[method] || []
|
73 | });
|
74 | store.collectedAll.before = [beforeAll];
|
75 | }
|
76 | if (store.after[method]) {
|
77 | const afterAll = (0, hooks_1.collect)({
|
78 | after: store.after[method] || []
|
79 | });
|
80 | store.collectedAll.after = [afterAll];
|
81 | }
|
82 | }
|
83 | else {
|
84 | if (store.before[method] || store.after[method] || store.error[method]) {
|
85 | const collected = (0, hooks_1.collect)({
|
86 | before: store.before[method] || [],
|
87 | after: store.after[method] || [],
|
88 | error: store.error[method] || []
|
89 | });
|
90 | store.collected[method] = [collected];
|
91 | }
|
92 | }
|
93 | }));
|
94 | return this;
|
95 | };
|
96 | }
|
97 | exports.enableHooks = enableHooks;
|
98 | function createContext(service, method, data = {}) {
|
99 | const createContext = service[method].createContext;
|
100 | if (typeof createContext !== 'function') {
|
101 | throw new Error(`Can not create context for method ${method}`);
|
102 | }
|
103 | return createContext(data);
|
104 | }
|
105 | exports.createContext = createContext;
|
106 | class FeathersHookManager extends hooks_1.HookManager {
|
107 | constructor(app, method) {
|
108 | super();
|
109 | this.app = app;
|
110 | this.method = method;
|
111 | this._middleware = [];
|
112 | }
|
113 | collectMiddleware(self, args) {
|
114 | const appHooks = collectHooks(this.app, this.method);
|
115 | const middleware = super.collectMiddleware(self, args);
|
116 | const methodHooks = collectHooks(self, this.method);
|
117 | return [...appHooks, ...middleware, ...methodHooks];
|
118 | }
|
119 | initializeContext(self, args, context) {
|
120 | const ctx = super.initializeContext(self, args, context);
|
121 | ctx.params = ctx.params || {};
|
122 | return ctx;
|
123 | }
|
124 | middleware(mw) {
|
125 | this._middleware.push(...mw);
|
126 | return this;
|
127 | }
|
128 | }
|
129 | exports.FeathersHookManager = FeathersHookManager;
|
130 | function hookMixin(service, path, options) {
|
131 | if (typeof service.hooks === 'function') {
|
132 | return service;
|
133 | }
|
134 | const hookMethods = (0, service_1.getHookMethods)(service, options);
|
135 | const serviceMethodHooks = hookMethods.reduce((res, method) => {
|
136 | const params = service_1.defaultServiceArguments[method] || ['data', 'params'];
|
137 | res[method] = new FeathersHookManager(this, method).params(...params).props({
|
138 | app: this,
|
139 | path,
|
140 | method,
|
141 | service,
|
142 | event: null,
|
143 | type: 'around',
|
144 | get statusCode() {
|
145 | var _a;
|
146 | return (_a = this.http) === null || _a === void 0 ? void 0 : _a.status;
|
147 | },
|
148 | set statusCode(value) {
|
149 | this.http = this.http || {};
|
150 | this.http.status = value;
|
151 | }
|
152 | });
|
153 | return res;
|
154 | }, {});
|
155 | const registerHooks = enableHooks(service);
|
156 | (0, hooks_1.hooks)(service, serviceMethodHooks);
|
157 | service.hooks = function (hookOptions) {
|
158 | if (hookOptions.before || hookOptions.after || hookOptions.error || hookOptions.around) {
|
159 | return registerHooks.call(this, hookOptions);
|
160 | }
|
161 | if (Array.isArray(hookOptions)) {
|
162 | return (0, hooks_1.hooks)(this, hookOptions);
|
163 | }
|
164 | Object.keys(hookOptions).forEach((method) => {
|
165 | const manager = (0, hooks_1.getManager)(this[method]);
|
166 | if (!(manager instanceof FeathersHookManager)) {
|
167 | throw new Error(`Method ${method} is not a Feathers hooks enabled service method`);
|
168 | }
|
169 | manager.middleware(hookOptions[method]);
|
170 | });
|
171 | return this;
|
172 | };
|
173 | return service;
|
174 | }
|
175 | exports.hookMixin = hookMixin;
|
176 |
|
\ | No newline at end of file |