deno.land / x / hooks@v0.6.5 / src / base.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import { Middleware } from './compose';import { copyToSelf, copyProperties } from './utils';
export const HOOKS: string = Symbol('@feathersjs/hooks') as any;
export type HookContextData = { [key: string]: any };
/** * The base hook context. */export class BaseHookContext<C = any> { self?: C; [key: string]: any;
constructor (data: HookContextData = {}) { Object.assign(this, data); }}
export interface HookContext<T = any, C = any> extends BaseHookContext<C> { result?: T; method?: string; arguments: any[];}
export type HookContextConstructor = new (data?: { [key: string]: any }) => BaseHookContext;
export type HookDefaultsInitializer = (self?: any, args?: any[], context?: HookContext) => HookContextData;
export class HookManager { _parent?: this|null = null; _params: string[]|null = null; _middleware: Middleware[]|null = null; _props: HookContextData|null = null; _defaults?: HookDefaultsInitializer;
parent (parent: this|null) { this._parent = parent;
return this; }
middleware (middleware?: Middleware[]) { this._middleware = middleware?.length ? middleware : null;
return this; }
getMiddleware (): Middleware[]|null { const previous = this._parent?.getMiddleware();
if (previous && this._middleware) { return previous.concat(this._middleware); }
return previous || this._middleware; }
collectMiddleware (self: any, _args: any[]): Middleware[] { const otherMiddleware = getMiddleware(self); const middleware = this.getMiddleware();
if (otherMiddleware && middleware) { return otherMiddleware.concat(middleware); }
return otherMiddleware || middleware || []; }
props (props: HookContextData) { if (!this._props) { this._props = {}; }
copyProperties(this._props, props);
return this; }
getProps (): HookContextData|null { const previous = this._parent?.getProps();
if (previous && this._props) { return copyProperties({}, previous, this._props); }
return previous || this._props || null; }
params (...params: string[]) { this._params = params;
return this; }
getParams (): string[]|null { const previous = this._parent?.getParams();
if (previous && this._params) { return previous.concat(this._params); }
return previous || this._params; }
defaults (defaults: HookDefaultsInitializer) { this._defaults = defaults;
return this; }
getDefaults (self: any, args: any[], context: HookContext): HookContextData|null { const defaults = typeof this._defaults === 'function' ? this._defaults(self, args, context) : null; const previous = this._parent?.getDefaults(self, args, context);
if (previous && defaults) { return Object.assign({}, previous, defaults); }
return previous || defaults; }
getContextClass (Base: HookContextConstructor = BaseHookContext): HookContextConstructor { const ContextClass = class ContextClass extends Base { constructor (data: any) { super(data);
copyToSelf(this); } }; const params = this.getParams(); const props = this.getProps();
if (params) { params.forEach((name, index) => { if (props?.[name] !== undefined) { throw new Error(`Hooks can not have a property and param named '${name}'. Use .defaults instead.`); }
Object.defineProperty(ContextClass.prototype, name, { enumerable: true, get () { return this?.arguments[index]; }, set (value: any) { this.arguments[index] = value; } }); }); }
if (props) { copyProperties(ContextClass.prototype, props); }
return ContextClass; }
initializeContext (self: any, args: any[], context: HookContext): HookContext { const ctx = this._parent ? this._parent.initializeContext(self, args, context) : context; const defaults = this.getDefaults(self, args, ctx);
if (self) { ctx.self = self; }
ctx.arguments = args;
if (defaults) { for (const name of Object.keys(defaults)) { if (ctx[name] === undefined) { ctx[name] = defaults[name]; } } }
return ctx; }}
export type HookOptions = HookManager|Middleware[]|null;
export function convertOptions (options: HookOptions = null) { if (!options) { return new HookManager() }
return Array.isArray(options) ? new HookManager().middleware(options) : options;}
export function getManager (target: any): HookManager|null { return (target && target[HOOKS]) || null;}
export function setManager<T> (target: T, manager: HookManager) { const parent = getManager(target);
(target as any)[HOOKS] = manager.parent(parent);
return target;}
export function getMiddleware (target: any): Middleware[]|null { const manager = getManager(target);
return manager ? manager.getMiddleware() : null;}
export function setMiddleware<T> (target: T, middleware: Middleware[]) { const manager = new HookManager().middleware(middleware);
return setManager(target, manager);}
hooks

Version Info

Tagged at
3 years ago