@feathersjs/feathers 5.0.0-pre.1 → 5.0.0-pre.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +182 -0
- package/LICENSE +1 -1
- package/lib/application.d.ts +19 -8
- package/lib/application.js +80 -78
- package/lib/application.js.map +1 -1
- package/lib/declarations.d.ts +207 -106
- package/lib/dependencies.d.ts +4 -0
- package/lib/dependencies.js +18 -0
- package/lib/dependencies.js.map +1 -0
- package/lib/events.d.ts +4 -4
- package/lib/events.js +16 -69
- package/lib/events.js.map +1 -1
- package/lib/hooks/index.d.ts +13 -2
- package/lib/hooks/index.js +84 -146
- package/lib/hooks/index.js.map +1 -1
- package/lib/hooks/regular.d.ts +12 -0
- package/lib/hooks/regular.js +169 -0
- package/lib/hooks/regular.js.map +1 -0
- package/lib/index.d.ts +9 -4
- package/lib/index.js +9 -12
- package/lib/index.js.map +1 -1
- package/lib/service.d.ts +21 -0
- package/lib/service.js +68 -0
- package/lib/service.js.map +1 -0
- package/lib/version.d.ts +1 -1
- package/lib/version.js +1 -1
- package/lib/version.js.map +1 -1
- package/package.json +12 -13
- package/readme.md +1 -77
- package/src/application.ts +115 -105
- package/src/declarations.ts +274 -121
- package/src/dependencies.ts +5 -0
- package/src/events.ts +18 -75
- package/src/hooks/index.ts +95 -150
- package/src/hooks/regular.ts +207 -0
- package/src/index.ts +10 -17
- package/src/service.ts +91 -0
- package/src/version.ts +1 -1
- package/lib/hooks/base.d.ts +0 -3
- package/lib/hooks/base.js +0 -29
- package/lib/hooks/base.js.map +0 -1
- package/lib/hooks/commons.d.ts +0 -20
- package/lib/hooks/commons.js +0 -298
- package/lib/hooks/commons.js.map +0 -1
- package/src/hooks/base.ts +0 -32
- package/src/hooks/commons.ts +0 -336
package/lib/hooks/commons.js
DELETED
|
@@ -1,298 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.wrap = exports.errorWrapper = exports.finallyWrapper = exports.afterWrapper = exports.beforeWrapper = exports.toFinallyHook = exports.toErrorHook = exports.toAfterHook = exports.toBeforeHook = exports.lastHook = exports.firstHook = exports.enableHooks = exports.processHooks = exports.getHooks = exports.isHookObject = exports.convertHookData = exports.makeArguments = exports.defaultMakeArguments = exports.createHookObject = exports.ACTIVATE_HOOKS = void 0;
|
|
4
|
-
const hooks_1 = require("@feathersjs/hooks");
|
|
5
|
-
const commons_1 = require("@feathersjs/commons");
|
|
6
|
-
const { each, pick, omit } = commons_1._;
|
|
7
|
-
const noop = () => { };
|
|
8
|
-
exports.ACTIVATE_HOOKS = commons_1.createSymbol('__feathersActivateHooks');
|
|
9
|
-
function createHookObject(method, data = {}) {
|
|
10
|
-
const hook = {};
|
|
11
|
-
Object.defineProperty(hook, 'toJSON', {
|
|
12
|
-
value() {
|
|
13
|
-
return pick(this, 'type', 'method', 'path', 'params', 'id', 'data', 'result', 'error');
|
|
14
|
-
}
|
|
15
|
-
});
|
|
16
|
-
return Object.assign(hook, data, {
|
|
17
|
-
method,
|
|
18
|
-
// A dynamic getter that returns the path of the service
|
|
19
|
-
get path() {
|
|
20
|
-
const { app, service } = data;
|
|
21
|
-
if (!service || !app || !app.services) {
|
|
22
|
-
return null;
|
|
23
|
-
}
|
|
24
|
-
return Object.keys(app.services)
|
|
25
|
-
.find(path => app.services[path] === service);
|
|
26
|
-
}
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
|
-
exports.createHookObject = createHookObject;
|
|
30
|
-
// Fallback used by `makeArguments` which usually won't be used
|
|
31
|
-
function defaultMakeArguments(hook) {
|
|
32
|
-
const result = [];
|
|
33
|
-
if (typeof hook.id !== 'undefined') {
|
|
34
|
-
result.push(hook.id);
|
|
35
|
-
}
|
|
36
|
-
if (hook.data) {
|
|
37
|
-
result.push(hook.data);
|
|
38
|
-
}
|
|
39
|
-
result.push(hook.params || {});
|
|
40
|
-
return result;
|
|
41
|
-
}
|
|
42
|
-
exports.defaultMakeArguments = defaultMakeArguments;
|
|
43
|
-
// Turns a hook object back into a list of arguments
|
|
44
|
-
// to call a service method with
|
|
45
|
-
function makeArguments(hook) {
|
|
46
|
-
switch (hook.method) {
|
|
47
|
-
case 'find':
|
|
48
|
-
return [hook.params];
|
|
49
|
-
case 'get':
|
|
50
|
-
case 'remove':
|
|
51
|
-
return [hook.id, hook.params];
|
|
52
|
-
case 'update':
|
|
53
|
-
case 'patch':
|
|
54
|
-
return [hook.id, hook.data, hook.params];
|
|
55
|
-
case 'create':
|
|
56
|
-
return [hook.data, hook.params];
|
|
57
|
-
}
|
|
58
|
-
return defaultMakeArguments(hook);
|
|
59
|
-
}
|
|
60
|
-
exports.makeArguments = makeArguments;
|
|
61
|
-
// Converts different hook registration formats into the
|
|
62
|
-
// same internal format
|
|
63
|
-
function convertHookData(obj) {
|
|
64
|
-
let hook = {};
|
|
65
|
-
if (Array.isArray(obj)) {
|
|
66
|
-
hook = { all: obj };
|
|
67
|
-
}
|
|
68
|
-
else if (typeof obj !== 'object') {
|
|
69
|
-
hook = { all: [obj] };
|
|
70
|
-
}
|
|
71
|
-
else {
|
|
72
|
-
each(obj, function (value, key) {
|
|
73
|
-
hook[key] = !Array.isArray(value) ? [value] : value;
|
|
74
|
-
});
|
|
75
|
-
}
|
|
76
|
-
return hook;
|
|
77
|
-
}
|
|
78
|
-
exports.convertHookData = convertHookData;
|
|
79
|
-
// Duck-checks a given object to be a hook object
|
|
80
|
-
// A valid hook object has `type` and `method`
|
|
81
|
-
function isHookObject(hookObject) {
|
|
82
|
-
return (hookObject instanceof hooks_1.HookContext || (typeof hookObject === 'object' &&
|
|
83
|
-
typeof hookObject.method === 'string' &&
|
|
84
|
-
typeof hookObject.type === 'string'));
|
|
85
|
-
}
|
|
86
|
-
exports.isHookObject = isHookObject;
|
|
87
|
-
// Returns all service and application hooks combined
|
|
88
|
-
// for a given method and type `appLast` sets if the hooks
|
|
89
|
-
// from `app` should be added last (or first by default)
|
|
90
|
-
function getHooks(app, service, type, method, appLast = false) {
|
|
91
|
-
const appHooks = app.__hooks[type][method] || [];
|
|
92
|
-
const serviceHooks = service.__hooks[type][method] || [];
|
|
93
|
-
if (appLast) {
|
|
94
|
-
// Run hooks in the order of service -> app -> finally
|
|
95
|
-
return serviceHooks.concat(appHooks);
|
|
96
|
-
}
|
|
97
|
-
return appHooks.concat(serviceHooks);
|
|
98
|
-
}
|
|
99
|
-
exports.getHooks = getHooks;
|
|
100
|
-
function processHooks(hooks, initialHookObject) {
|
|
101
|
-
let hookObject = initialHookObject;
|
|
102
|
-
const updateCurrentHook = (current) => {
|
|
103
|
-
// Either use the returned hook object or the current
|
|
104
|
-
// hook object from the chain if the hook returned undefined
|
|
105
|
-
if (current) {
|
|
106
|
-
if (!isHookObject(current)) {
|
|
107
|
-
throw new Error(`${hookObject.type} hook for '${hookObject.method}' method returned invalid hook object`);
|
|
108
|
-
}
|
|
109
|
-
hookObject = current;
|
|
110
|
-
}
|
|
111
|
-
return hookObject;
|
|
112
|
-
};
|
|
113
|
-
// Go through all hooks and chain them into our promise
|
|
114
|
-
const promise = hooks.reduce((current, fn) => {
|
|
115
|
-
// @ts-ignore
|
|
116
|
-
const hook = fn.bind(this);
|
|
117
|
-
// Use the returned hook object or the old one
|
|
118
|
-
return current.then((currentHook) => hook(currentHook)).then(updateCurrentHook);
|
|
119
|
-
}, Promise.resolve(hookObject));
|
|
120
|
-
return promise.then(() => hookObject).catch(error => {
|
|
121
|
-
// Add the hook information to any errors
|
|
122
|
-
error.hook = hookObject;
|
|
123
|
-
throw error;
|
|
124
|
-
});
|
|
125
|
-
}
|
|
126
|
-
exports.processHooks = processHooks;
|
|
127
|
-
// Add `.hooks` functionality to an object
|
|
128
|
-
function enableHooks(obj, methods, types) {
|
|
129
|
-
if (typeof obj.hooks === 'function') {
|
|
130
|
-
return obj;
|
|
131
|
-
}
|
|
132
|
-
const hookData = {};
|
|
133
|
-
types.forEach(type => {
|
|
134
|
-
// Initialize properties where hook functions are stored
|
|
135
|
-
hookData[type] = {};
|
|
136
|
-
});
|
|
137
|
-
// Add non-enumerable `__hooks` property to the object
|
|
138
|
-
Object.defineProperty(obj, '__hooks', {
|
|
139
|
-
configurable: true,
|
|
140
|
-
value: hookData,
|
|
141
|
-
writable: true
|
|
142
|
-
});
|
|
143
|
-
return Object.assign(obj, {
|
|
144
|
-
hooks(allHooks) {
|
|
145
|
-
each(allHooks, (current, type) => {
|
|
146
|
-
// @ts-ignore
|
|
147
|
-
if (!this.__hooks[type]) {
|
|
148
|
-
throw new Error(`'${type}' is not a valid hook type`);
|
|
149
|
-
}
|
|
150
|
-
const hooks = convertHookData(current);
|
|
151
|
-
each(hooks, (_value, method) => {
|
|
152
|
-
if (method !== 'all' && methods.indexOf(method) === -1) {
|
|
153
|
-
throw new Error(`'${method}' is not a valid hook method`);
|
|
154
|
-
}
|
|
155
|
-
});
|
|
156
|
-
methods.forEach(method => {
|
|
157
|
-
// @ts-ignore
|
|
158
|
-
const myHooks = this.__hooks[type][method] || (this.__hooks[type][method] = []);
|
|
159
|
-
if (hooks.all) {
|
|
160
|
-
myHooks.push.apply(myHooks, hooks.all);
|
|
161
|
-
}
|
|
162
|
-
if (hooks[method]) {
|
|
163
|
-
myHooks.push.apply(myHooks, hooks[method]);
|
|
164
|
-
}
|
|
165
|
-
});
|
|
166
|
-
});
|
|
167
|
-
return this;
|
|
168
|
-
}
|
|
169
|
-
});
|
|
170
|
-
}
|
|
171
|
-
exports.enableHooks = enableHooks;
|
|
172
|
-
function handleError(hook, context, onError) {
|
|
173
|
-
try {
|
|
174
|
-
return Promise.resolve(hook.call(context.self, context))
|
|
175
|
-
.catch((error) => {
|
|
176
|
-
if (typeof onError === 'function') {
|
|
177
|
-
onError(error, context);
|
|
178
|
-
}
|
|
179
|
-
throw error;
|
|
180
|
-
})
|
|
181
|
-
.then((result) => {
|
|
182
|
-
Object.assign(context, omit(result, 'arguments', 'path'));
|
|
183
|
-
if (typeof context.error !== 'undefined') {
|
|
184
|
-
throw context.error;
|
|
185
|
-
}
|
|
186
|
-
return result;
|
|
187
|
-
});
|
|
188
|
-
}
|
|
189
|
-
catch (errorError) {
|
|
190
|
-
if (typeof onError === 'function') {
|
|
191
|
-
onError(errorError, context);
|
|
192
|
-
}
|
|
193
|
-
throw errorError;
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
function firstHook(context, next) {
|
|
197
|
-
context.type = 'before';
|
|
198
|
-
return next();
|
|
199
|
-
}
|
|
200
|
-
exports.firstHook = firstHook;
|
|
201
|
-
function lastHook(context, next) {
|
|
202
|
-
context.type = 'after';
|
|
203
|
-
return next();
|
|
204
|
-
}
|
|
205
|
-
exports.lastHook = lastHook;
|
|
206
|
-
function toBeforeHook(hook) {
|
|
207
|
-
return (context, next) => {
|
|
208
|
-
return Promise.resolve(hook.call(context.self, context))
|
|
209
|
-
.then((result) => Object.assign(context, omit(result, 'arguments', 'path')))
|
|
210
|
-
.then(() => next());
|
|
211
|
-
};
|
|
212
|
-
}
|
|
213
|
-
exports.toBeforeHook = toBeforeHook;
|
|
214
|
-
function toAfterHook(hook) {
|
|
215
|
-
return (context, next) => {
|
|
216
|
-
return next()
|
|
217
|
-
.then(() => hook.call(context.self, context))
|
|
218
|
-
.then((result) => Object.assign(context, omit(result, 'arguments', 'path')));
|
|
219
|
-
};
|
|
220
|
-
}
|
|
221
|
-
exports.toAfterHook = toAfterHook;
|
|
222
|
-
function toErrorHook(hook, onError, control) {
|
|
223
|
-
return (context, next) => {
|
|
224
|
-
return next()
|
|
225
|
-
.catch((error) => {
|
|
226
|
-
if (typeof control === 'function') {
|
|
227
|
-
control(context);
|
|
228
|
-
}
|
|
229
|
-
context.error = error;
|
|
230
|
-
context.result = undefined;
|
|
231
|
-
return handleError(hook, context, onError);
|
|
232
|
-
});
|
|
233
|
-
};
|
|
234
|
-
}
|
|
235
|
-
exports.toErrorHook = toErrorHook;
|
|
236
|
-
function toFinallyHook(hook, onError, control) {
|
|
237
|
-
return (context, next) => {
|
|
238
|
-
return next()
|
|
239
|
-
.finally(() => {
|
|
240
|
-
if (typeof control === 'function') {
|
|
241
|
-
control(context);
|
|
242
|
-
}
|
|
243
|
-
return handleError(hook, context, onError);
|
|
244
|
-
});
|
|
245
|
-
};
|
|
246
|
-
}
|
|
247
|
-
exports.toFinallyHook = toFinallyHook;
|
|
248
|
-
function beforeWrapper(hooks) {
|
|
249
|
-
return [firstHook, ...[].concat(hooks).map(toBeforeHook)];
|
|
250
|
-
}
|
|
251
|
-
exports.beforeWrapper = beforeWrapper;
|
|
252
|
-
function afterWrapper(hooks) {
|
|
253
|
-
return [...[].concat(hooks).reverse().map(toAfterHook), lastHook];
|
|
254
|
-
}
|
|
255
|
-
exports.afterWrapper = afterWrapper;
|
|
256
|
-
function finallyWrapper(hooks) {
|
|
257
|
-
let errorInFinally;
|
|
258
|
-
const onError = (error, context) => {
|
|
259
|
-
errorInFinally = error;
|
|
260
|
-
context.error = error;
|
|
261
|
-
context.result = undefined;
|
|
262
|
-
};
|
|
263
|
-
const control = () => {
|
|
264
|
-
if (errorInFinally) {
|
|
265
|
-
throw errorInFinally;
|
|
266
|
-
}
|
|
267
|
-
};
|
|
268
|
-
return [].concat(hooks).reverse().map(hook => toFinallyHook(hook, onError, control));
|
|
269
|
-
}
|
|
270
|
-
exports.finallyWrapper = finallyWrapper;
|
|
271
|
-
function errorWrapper(hooks) {
|
|
272
|
-
let errorInError;
|
|
273
|
-
const onError = (error, context) => {
|
|
274
|
-
errorInError = error;
|
|
275
|
-
context.error = error;
|
|
276
|
-
context.result = undefined;
|
|
277
|
-
};
|
|
278
|
-
const control = (context) => {
|
|
279
|
-
if (!context.original) {
|
|
280
|
-
context.original = Object.assign({}, context);
|
|
281
|
-
}
|
|
282
|
-
if (errorInError) {
|
|
283
|
-
throw errorInError;
|
|
284
|
-
}
|
|
285
|
-
context.type = 'error';
|
|
286
|
-
};
|
|
287
|
-
return [noop].concat(hooks).reverse().map(hook => toErrorHook(hook, onError, control));
|
|
288
|
-
}
|
|
289
|
-
exports.errorWrapper = errorWrapper;
|
|
290
|
-
function wrap({ async = [], before = [], after = [] } = {}) {
|
|
291
|
-
return [
|
|
292
|
-
...[].concat(async),
|
|
293
|
-
...beforeWrapper(before),
|
|
294
|
-
...afterWrapper(after)
|
|
295
|
-
];
|
|
296
|
-
}
|
|
297
|
-
exports.wrap = wrap;
|
|
298
|
-
//# sourceMappingURL=commons.js.map
|
package/lib/hooks/commons.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"commons.js","sourceRoot":"","sources":["../../src/hooks/commons.ts"],"names":[],"mappings":";;;AAAA,6CAAgD;AAChD,iDAAsD;AAEtD,MAAM,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,GAAG,WAAC,CAAC;AAC/B,MAAM,IAAI,GAAG,GAAG,EAAE,GAAE,CAAC,CAAC;AAET,QAAA,cAAc,GAAG,sBAAY,CAAC,yBAAyB,CAAC,CAAC;AAEtE,SAAgB,gBAAgB,CAAE,MAAc,EAAE,OAAY,EAAE;IAC9D,MAAM,IAAI,GAAG,EAAE,CAAC;IAEhB,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,QAAQ,EAAE;QACpC,KAAK;YACH,OAAO,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EACxC,QAAQ,EAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,OAAO,CAAC,CAAC;QAC/C,CAAC;KACF,CAAC,CAAC;IAEH,OAAO,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,IAAI,EAAE;QAC/B,MAAM;QACN,wDAAwD;QACxD,IAAI,IAAI;YACN,MAAM,EAAE,GAAG,EAAE,OAAO,EAAE,GAAG,IAAI,CAAC;YAE9B,IAAI,CAAC,OAAO,IAAI,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,QAAQ,EAAE;gBACrC,OAAO,IAAI,CAAC;aACb;YAED,OAAO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ACrE,OAAO;QACL,GAAG,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC;QACnB,GAAG,aAAa,CAAC,MAAM,CAAC;QACxB,GAAG,YAAY,CAAC,KAAK,CAAC;KACvB,CAAC;AACJ,CAAC;AAND,oBAMC"}
|
package/src/hooks/base.ts
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import { _ } from '@feathersjs/commons';
|
|
2
|
-
import { HookContext } from '../declarations';
|
|
3
|
-
|
|
4
|
-
export const assignArguments = (context: HookContext, next: any) => {
|
|
5
|
-
const { service, method } = context;
|
|
6
|
-
const parameters = service.methods[method];
|
|
7
|
-
|
|
8
|
-
context.arguments.forEach((value, index) => {
|
|
9
|
-
(context as any)[parameters[index]] = value;
|
|
10
|
-
});
|
|
11
|
-
|
|
12
|
-
if (!context.params) {
|
|
13
|
-
context.params = {};
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
return next();
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
export const validate = (context: HookContext, next: any) => {
|
|
20
|
-
const { service, method, path } = context;
|
|
21
|
-
const parameters = service.methods[method];
|
|
22
|
-
|
|
23
|
-
if (parameters.includes('id') && context.id === undefined) {
|
|
24
|
-
throw new Error(`An id must be provided to the '${path}.${method}' method`);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
if (parameters.includes('data') && !_.isObjectOrArray(context.data)) {
|
|
28
|
-
throw new Error(`A data object must be provided to the '${path}.${method}' method`);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
return next();
|
|
32
|
-
};
|
package/src/hooks/commons.ts
DELETED
|
@@ -1,336 +0,0 @@
|
|
|
1
|
-
import { HookContext } from '@feathersjs/hooks';
|
|
2
|
-
import { createSymbol, _ } from '@feathersjs/commons';
|
|
3
|
-
|
|
4
|
-
const { each, pick, omit } = _;
|
|
5
|
-
const noop = () => {};
|
|
6
|
-
|
|
7
|
-
export const ACTIVATE_HOOKS = createSymbol('__feathersActivateHooks');
|
|
8
|
-
|
|
9
|
-
export function createHookObject (method: string, data: any = {}) {
|
|
10
|
-
const hook = {};
|
|
11
|
-
|
|
12
|
-
Object.defineProperty(hook, 'toJSON', {
|
|
13
|
-
value () {
|
|
14
|
-
return pick(this, 'type', 'method', 'path',
|
|
15
|
-
'params', 'id', 'data', 'result', 'error');
|
|
16
|
-
}
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
return Object.assign(hook, data, {
|
|
20
|
-
method,
|
|
21
|
-
// A dynamic getter that returns the path of the service
|
|
22
|
-
get path () {
|
|
23
|
-
const { app, service } = data;
|
|
24
|
-
|
|
25
|
-
if (!service || !app || !app.services) {
|
|
26
|
-
return null;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
return Object.keys(app.services)
|
|
30
|
-
.find(path => app.services[path] === service);
|
|
31
|
-
}
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
// Fallback used by `makeArguments` which usually won't be used
|
|
36
|
-
export function defaultMakeArguments (hook: any) {
|
|
37
|
-
const result = [];
|
|
38
|
-
|
|
39
|
-
if (typeof hook.id !== 'undefined') {
|
|
40
|
-
result.push(hook.id);
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
if (hook.data) {
|
|
44
|
-
result.push(hook.data);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
result.push(hook.params || {});
|
|
48
|
-
|
|
49
|
-
return result;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
// Turns a hook object back into a list of arguments
|
|
53
|
-
// to call a service method with
|
|
54
|
-
export function makeArguments (hook: any) {
|
|
55
|
-
switch (hook.method) {
|
|
56
|
-
case 'find':
|
|
57
|
-
return [ hook.params ];
|
|
58
|
-
case 'get':
|
|
59
|
-
case 'remove':
|
|
60
|
-
return [ hook.id, hook.params ];
|
|
61
|
-
case 'update':
|
|
62
|
-
case 'patch':
|
|
63
|
-
return [ hook.id, hook.data, hook.params ];
|
|
64
|
-
case 'create':
|
|
65
|
-
return [ hook.data, hook.params ];
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
return defaultMakeArguments(hook);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
// Converts different hook registration formats into the
|
|
72
|
-
// same internal format
|
|
73
|
-
export function convertHookData (obj: any) {
|
|
74
|
-
let hook: any = {};
|
|
75
|
-
|
|
76
|
-
if (Array.isArray(obj)) {
|
|
77
|
-
hook = { all: obj };
|
|
78
|
-
} else if (typeof obj !== 'object') {
|
|
79
|
-
hook = { all: [ obj ] };
|
|
80
|
-
} else {
|
|
81
|
-
each(obj, function (value, key) {
|
|
82
|
-
hook[key] = !Array.isArray(value) ? [ value ] : value;
|
|
83
|
-
});
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
return hook;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
// Duck-checks a given object to be a hook object
|
|
90
|
-
// A valid hook object has `type` and `method`
|
|
91
|
-
export function isHookObject (hookObject: any) {
|
|
92
|
-
return (
|
|
93
|
-
hookObject instanceof HookContext || (
|
|
94
|
-
typeof hookObject === 'object' &&
|
|
95
|
-
typeof hookObject.method === 'string' &&
|
|
96
|
-
typeof hookObject.type === 'string'
|
|
97
|
-
)
|
|
98
|
-
);
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
// Returns all service and application hooks combined
|
|
102
|
-
// for a given method and type `appLast` sets if the hooks
|
|
103
|
-
// from `app` should be added last (or first by default)
|
|
104
|
-
export function getHooks (app: any, service: any, type: string, method: string, appLast = false) {
|
|
105
|
-
const appHooks = app.__hooks[type][method] || [];
|
|
106
|
-
const serviceHooks = service.__hooks[type][method] || [];
|
|
107
|
-
|
|
108
|
-
if (appLast) {
|
|
109
|
-
// Run hooks in the order of service -> app -> finally
|
|
110
|
-
return serviceHooks.concat(appHooks);
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
return appHooks.concat(serviceHooks);
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
export function processHooks (hooks: any[], initialHookObject: any) {
|
|
117
|
-
let hookObject = initialHookObject;
|
|
118
|
-
|
|
119
|
-
const updateCurrentHook = (current: any) => {
|
|
120
|
-
// Either use the returned hook object or the current
|
|
121
|
-
// hook object from the chain if the hook returned undefined
|
|
122
|
-
if (current) {
|
|
123
|
-
if (!isHookObject(current)) {
|
|
124
|
-
throw new Error(`${hookObject.type} hook for '${hookObject.method}' method returned invalid hook object`);
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
hookObject = current;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
return hookObject;
|
|
131
|
-
};
|
|
132
|
-
// Go through all hooks and chain them into our promise
|
|
133
|
-
const promise = hooks.reduce((current: Promise<any>, fn) => {
|
|
134
|
-
// @ts-ignore
|
|
135
|
-
const hook = fn.bind(this);
|
|
136
|
-
|
|
137
|
-
// Use the returned hook object or the old one
|
|
138
|
-
return current.then((currentHook: any) => hook(currentHook)).then(updateCurrentHook);
|
|
139
|
-
}, Promise.resolve(hookObject));
|
|
140
|
-
|
|
141
|
-
return promise.then(() => hookObject).catch(error => {
|
|
142
|
-
// Add the hook information to any errors
|
|
143
|
-
error.hook = hookObject;
|
|
144
|
-
throw error;
|
|
145
|
-
});
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
// Add `.hooks` functionality to an object
|
|
149
|
-
export function enableHooks (obj: any, methods: string[], types: string[]) {
|
|
150
|
-
if (typeof obj.hooks === 'function') {
|
|
151
|
-
return obj;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
const hookData: any = {};
|
|
155
|
-
|
|
156
|
-
types.forEach(type => {
|
|
157
|
-
// Initialize properties where hook functions are stored
|
|
158
|
-
hookData[type] = {};
|
|
159
|
-
});
|
|
160
|
-
|
|
161
|
-
// Add non-enumerable `__hooks` property to the object
|
|
162
|
-
Object.defineProperty(obj, '__hooks', {
|
|
163
|
-
configurable: true,
|
|
164
|
-
value: hookData,
|
|
165
|
-
writable: true
|
|
166
|
-
});
|
|
167
|
-
|
|
168
|
-
return Object.assign(obj, {
|
|
169
|
-
hooks (allHooks: any) {
|
|
170
|
-
each(allHooks, (current: any, type) => {
|
|
171
|
-
// @ts-ignore
|
|
172
|
-
if (!this.__hooks[type]) {
|
|
173
|
-
throw new Error(`'${type}' is not a valid hook type`);
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
const hooks = convertHookData(current);
|
|
177
|
-
|
|
178
|
-
each(hooks, (_value, method) => {
|
|
179
|
-
if (method !== 'all' && methods.indexOf(method) === -1) {
|
|
180
|
-
throw new Error(`'${method}' is not a valid hook method`);
|
|
181
|
-
}
|
|
182
|
-
});
|
|
183
|
-
|
|
184
|
-
methods.forEach(method => {
|
|
185
|
-
// @ts-ignore
|
|
186
|
-
const myHooks = this.__hooks[type][method] || (this.__hooks[type][method] = []);
|
|
187
|
-
|
|
188
|
-
if (hooks.all) {
|
|
189
|
-
myHooks.push.apply(myHooks, hooks.all);
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
if (hooks[method]) {
|
|
193
|
-
myHooks.push.apply(myHooks, hooks[method]);
|
|
194
|
-
}
|
|
195
|
-
});
|
|
196
|
-
});
|
|
197
|
-
|
|
198
|
-
return this;
|
|
199
|
-
}
|
|
200
|
-
});
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
function handleError (hook: any, context: any, onError: any) {
|
|
204
|
-
try {
|
|
205
|
-
return Promise.resolve(hook.call(context.self, context))
|
|
206
|
-
.catch((error: any) => {
|
|
207
|
-
if (typeof onError === 'function') {
|
|
208
|
-
onError(error, context);
|
|
209
|
-
}
|
|
210
|
-
throw error;
|
|
211
|
-
})
|
|
212
|
-
.then((result: any) => {
|
|
213
|
-
Object.assign(context, omit(result, 'arguments', 'path'));
|
|
214
|
-
|
|
215
|
-
if (typeof context.error !== 'undefined') {
|
|
216
|
-
throw context.error;
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
return result;
|
|
220
|
-
});
|
|
221
|
-
} catch(errorError: any) {
|
|
222
|
-
if (typeof onError === 'function') {
|
|
223
|
-
onError(errorError, context);
|
|
224
|
-
}
|
|
225
|
-
throw errorError;
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
export function firstHook (context: any, next: any) {
|
|
230
|
-
context.type = 'before';
|
|
231
|
-
return next();
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
export function lastHook (context: any, next: any) {
|
|
235
|
-
context.type = 'after';
|
|
236
|
-
return next();
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
export function toBeforeHook (hook: any) {
|
|
240
|
-
return (context: any, next: any) => {
|
|
241
|
-
return Promise.resolve(hook.call(context.self, context))
|
|
242
|
-
.then((result: any) => Object.assign(context, omit(result, 'arguments', 'path')))
|
|
243
|
-
.then(() => next());
|
|
244
|
-
};
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
export function toAfterHook (hook: any) {
|
|
248
|
-
return (context: any, next: any) => {
|
|
249
|
-
return next()
|
|
250
|
-
.then(() => hook.call(context.self, context))
|
|
251
|
-
.then((result: any) => Object.assign(context, omit(result, 'arguments', 'path')));
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
export function toErrorHook (hook: any, onError: any, control: any) {
|
|
256
|
-
return (context: any, next: any) => {
|
|
257
|
-
return next()
|
|
258
|
-
.catch((error: any) => {
|
|
259
|
-
if (typeof control === 'function') {
|
|
260
|
-
control(context);
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
context.error = error;
|
|
264
|
-
context.result = undefined;
|
|
265
|
-
|
|
266
|
-
return handleError(hook, context, onError);
|
|
267
|
-
});
|
|
268
|
-
};
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
export function toFinallyHook (hook: any, onError: any, control: any) {
|
|
272
|
-
return (context: any, next: any) => {
|
|
273
|
-
return next()
|
|
274
|
-
.finally(() => {
|
|
275
|
-
if (typeof control === 'function') {
|
|
276
|
-
control(context);
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
return handleError(hook, context, onError);
|
|
280
|
-
})
|
|
281
|
-
};
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
export function beforeWrapper (hooks: any) {
|
|
285
|
-
return [firstHook, ...[].concat(hooks).map(toBeforeHook)];
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
export function afterWrapper (hooks: any) {
|
|
289
|
-
return [...[].concat(hooks).reverse().map(toAfterHook), lastHook];
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
export function finallyWrapper (hooks: any) {
|
|
293
|
-
let errorInFinally: any;
|
|
294
|
-
|
|
295
|
-
const onError = (error: any, context: any) => {
|
|
296
|
-
errorInFinally = error;
|
|
297
|
-
context.error = error;
|
|
298
|
-
context.result = undefined;
|
|
299
|
-
};
|
|
300
|
-
const control = () => {
|
|
301
|
-
if (errorInFinally) {
|
|
302
|
-
throw errorInFinally;
|
|
303
|
-
}
|
|
304
|
-
};
|
|
305
|
-
|
|
306
|
-
return [].concat(hooks).reverse().map(hook => toFinallyHook(hook, onError, control));
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
export function errorWrapper (hooks: any) {
|
|
310
|
-
let errorInError: any;
|
|
311
|
-
|
|
312
|
-
const onError = (error: any, context: any) => {
|
|
313
|
-
errorInError = error;
|
|
314
|
-
context.error = error;
|
|
315
|
-
context.result = undefined;
|
|
316
|
-
};
|
|
317
|
-
const control = (context: any) => {
|
|
318
|
-
if (!context.original) {
|
|
319
|
-
context.original = { ...context };
|
|
320
|
-
}
|
|
321
|
-
if (errorInError) {
|
|
322
|
-
throw errorInError;
|
|
323
|
-
}
|
|
324
|
-
context.type = 'error';
|
|
325
|
-
};
|
|
326
|
-
|
|
327
|
-
return [noop].concat(hooks).reverse().map(hook => toErrorHook(hook, onError, control));
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
export function wrap ({ async = [], before = [], after = [] }: any = {}) {
|
|
331
|
-
return [
|
|
332
|
-
...[].concat(async),
|
|
333
|
-
...beforeWrapper(before),
|
|
334
|
-
...afterWrapper(after)
|
|
335
|
-
];
|
|
336
|
-
}
|