-
-
Notifications
You must be signed in to change notification settings - Fork 751
/
hooks.ts
193 lines (155 loc) · 5.04 KB
/
hooks.ts
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
import { createSymbol, _ } from './utils';
const { each, pick } = _;
export const ACTIVATE_HOOKS = createSymbol('__feathersActivateHooks');
export function createHookObject (method: string, data: any = {}) {
const hook = {};
Object.defineProperty(hook, 'toJSON', {
value () {
return pick(this, 'type', 'method', 'path',
'params', 'id', 'data', 'result', 'error');
}
});
return Object.assign(hook, data, {
method,
// A dynamic getter that returns the path of the service
get path () {
const { app, service } = data;
if (!service || !app || !app.services) {
return null;
}
return Object.keys(app.services)
.find(path => app.services[path] === service);
}
});
}
// Fallback used by `makeArguments` which usually won't be used
export function defaultMakeArguments (hook: any) {
const result = [];
if (typeof hook.id !== 'undefined') {
result.push(hook.id);
}
if (hook.data) {
result.push(hook.data);
}
result.push(hook.params || {});
return result;
}
// Turns a hook object back into a list of arguments
// to call a service method with
export function makeArguments (hook: any) {
switch (hook.method) {
case 'find':
return [ hook.params ];
case 'get':
case 'remove':
return [ hook.id, hook.params ];
case 'update':
case 'patch':
return [ hook.id, hook.data, hook.params ];
case 'create':
return [ hook.data, hook.params ];
}
return defaultMakeArguments(hook);
}
// Converts different hook registration formats into the
// same internal format
export function convertHookData (obj: any) {
let hook: any = {};
if (Array.isArray(obj)) {
hook = { all: obj };
} else if (typeof obj !== 'object') {
hook = { all: [ obj ] };
} else {
each(obj, function (value, key) {
hook[key] = !Array.isArray(value) ? [ value ] : value;
});
}
return hook;
}
// Duck-checks a given object to be a hook object
// A valid hook object has `type` and `method`
export function isHookObject (hookObject: any) {
return typeof hookObject === 'object' &&
typeof hookObject.method === 'string' &&
typeof hookObject.type === 'string';
}
// Returns all service and application hooks combined
// for a given method and type `appLast` sets if the hooks
// from `app` should be added last (or first by default)
export function getHooks (app: any, service: any, type: string, method: string, appLast: boolean = false) {
const appHooks = app.__hooks[type][method] || [];
const serviceHooks = service.__hooks[type][method] || [];
if (appLast) {
// Run hooks in the order of service -> app -> finally
return serviceHooks.concat(appHooks);
}
return appHooks.concat(serviceHooks);
}
export function processHooks (hooks: any[], initialHookObject: any) {
let hookObject = initialHookObject;
const updateCurrentHook = (current: any) => {
// Either use the returned hook object or the current
// hook object from the chain if the hook returned undefined
if (current) {
if (!isHookObject(current)) {
throw new Error(`${hookObject.type} hook for '${hookObject.method}' method returned invalid hook object`);
}
hookObject = current;
}
return hookObject;
};
// Go through all hooks and chain them into our promise
const promise = hooks.reduce((current: Promise<any>, fn) => {
// @ts-ignore
const hook = fn.bind(this);
// Use the returned hook object or the old one
return current.then((currentHook: any) => hook(currentHook)).then(updateCurrentHook);
}, Promise.resolve(hookObject));
return promise.then(() => hookObject).catch(error => {
// Add the hook information to any errors
error.hook = hookObject;
throw error;
});
}
// Add `.hooks` functionality to an object
export function enableHooks (obj: any, methods: string[], types: string[]) {
if (typeof obj.hooks === 'function') {
return obj;
}
const hookData: any = {};
types.forEach(type => {
// Initialize properties where hook functions are stored
hookData[type] = {};
});
// Add non-enumerable `__hooks` property to the object
Object.defineProperty(obj, '__hooks', {
value: hookData
});
return Object.assign(obj, {
hooks (allHooks: any) {
each(allHooks, (current: any, type) => {
// @ts-ignore
if (!this.__hooks[type]) {
throw new Error(`'${type}' is not a valid hook type`);
}
const hooks = convertHookData(current);
each(hooks, (_value, method) => {
if (method !== 'all' && methods.indexOf(method) === -1) {
throw new Error(`'${method}' is not a valid hook method`);
}
});
methods.forEach(method => {
// @ts-ignore
const myHooks = this.__hooks[type][method] || (this.__hooks[type][method] = []);
if (hooks.all) {
myHooks.push.apply(myHooks, hooks.all);
}
if (hooks[method]) {
myHooks.push.apply(myHooks, hooks[method]);
}
});
});
return this;
}
});
}