-
-
Notifications
You must be signed in to change notification settings - Fork 741
/
legacy.ts
140 lines (112 loc) · 3.54 KB
/
legacy.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
// DO NOT MODIFY - generated from packages/feathers/src/hooks/legacy.ts
import { _ } from '../dependencies.ts';
import { LegacyHookFunction } from '../declarations.ts';
const { each } = _;
export function fromBeforeHook (hook: LegacyHookFunction) {
return (context: any, next: any) => {
context.type = 'before';
return Promise.resolve(hook.call(context.self, context)).then(() => {
context.type = null;
return next();
});
};
}
export function fromAfterHook (hook: LegacyHookFunction) {
return (context: any, next: any) => {
return next().then(() => {
context.type = 'after';
return hook.call(context.self, context)
}).then(() => {
context.type = null;
});
}
}
export function fromErrorHooks (hooks: LegacyHookFunction[]) {
return (context: any, next: any) => {
return next().catch((error: any) => {
let promise: Promise<any> = Promise.resolve();
context.original = { ...context };
context.error = error;
context.type = 'error';
delete context.result;
for (const hook of hooks) {
promise = promise.then(() => hook.call(context.self, context))
}
return promise.then(() => {
context.type = null;
if (context.result === undefined) {
throw context.error;
}
});
});
}
}
export function collectLegacyHooks (target: any, method: string) {
const {
before: { [method]: before = [] },
after: { [method]: after = [] },
error: { [method]: error = [] }
} = target.__hooks;
const beforeHooks = before;
const afterHooks = [...after].reverse();
const errorHook = fromErrorHooks(error);
return [errorHook, ...beforeHooks, ...afterHooks];
}
// 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;
}
// Add `.hooks` functionality to an object
export function enableLegacyHooks (
obj: any,
methods: string[] = ['find', 'get', 'create', 'update', 'patch', 'remove'],
types: string[] = ['before', 'after', 'error']
) {
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', {
configurable: true,
value: hookData,
writable: true
});
return function legacyHooks (this: any, allHooks: any) {
each(allHooks, (current: any, type) => {
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 => {
let currentHooks = [...(hooks.all || []), ...(hooks[method] || [])];
this.__hooks[type][method] = this.__hooks[type][method] || [];
if (type === 'before') {
currentHooks = currentHooks.map(fromBeforeHook);
}
if (type === 'after') {
currentHooks = currentHooks.map(fromAfterHook);
}
this.__hooks[type][method].push(...currentHooks);
});
});
return this;
}
}