UNPKG

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