-
-
Notifications
You must be signed in to change notification settings - Fork 741
/
index.ts
111 lines (87 loc) · 3.07 KB
/
index.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
// DO NOT MODIFY - generated from packages/feathers/src/hooks/index.ts
import {
getManager, HookContextData, HookManager, HookMap, HOOKS, hooks, Middleware
} from '../dependencies.ts';
import {
Service, ServiceOptions, HookContext, FeathersService, Application
} from '../declarations.ts';
import { defaultServiceArguments, getHookMethods } from '../service.ts';
import {
collectLegacyHooks,
enableLegacyHooks,
fromAfterHook,
fromBeforeHook,
fromErrorHooks
} from './legacy.ts';
export { fromAfterHook, fromBeforeHook, fromErrorHooks };
export function createContext (service: Service<any>, method: string, data: HookContextData = {}) {
const createContext = (service as any)[method].createContext;
if (typeof createContext !== 'function') {
throw new Error(`Can not create context for method ${method}`);
}
return createContext(data) as HookContext;
}
export class FeathersHookManager<A> extends HookManager {
constructor (public app: A, public method: string) {
super();
this._middleware = [];
}
collectMiddleware (self: any, args: any[]): Middleware[] {
const app = this.app as any as Application;
const appHooks = app.appHooks[HOOKS].concat(app.appHooks[this.method] || []);
const legacyAppHooks = collectLegacyHooks(this.app, this.method);
const middleware = super.collectMiddleware(self, args);
const legacyHooks = collectLegacyHooks(self, this.method);
return [...appHooks, ...legacyAppHooks, ...middleware, ...legacyHooks];
}
initializeContext (self: any, args: any[], context: HookContext) {
const ctx = super.initializeContext(self, args, context);
ctx.params = ctx.params || {};
return ctx;
}
middleware (mw: Middleware[]) {
this._middleware.push(...mw);
return this;
}
}
export function hookMixin<A> (
this: A, service: FeathersService<A>, path: string, options: ServiceOptions
) {
if (typeof service.hooks === 'function') {
return service;
}
const app = this;
const serviceMethodHooks = getHookMethods(service, options).reduce((res, method) => {
const params = (defaultServiceArguments as any)[method] || [ 'data', 'params' ];
res[method] = new FeathersHookManager<A>(app, method)
.params(...params)
.props({
app,
path,
method,
service,
event: null,
type: null
});
return res;
}, {} as HookMap);
const handleLegacyHooks = enableLegacyHooks(service);
hooks(service, serviceMethodHooks);
service.hooks = function (this: any, hookOptions: any) {
if (hookOptions.before || hookOptions.after || hookOptions.error) {
return handleLegacyHooks.call(this, hookOptions);
}
if (Array.isArray(hookOptions)) {
return hooks(this, hookOptions);
}
Object.keys(hookOptions).forEach(method => {
const manager = getManager(this[method]);
if (!(manager instanceof FeathersHookManager)) {
throw new Error(`Method ${method} is not a Feathers hooks enabled service method`);
}
manager.middleware(hookOptions[method]);
});
return this;
}
return service;
}