@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/src/declarations.ts
CHANGED
|
@@ -1,88 +1,288 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import {
|
|
2
|
+
EventEmitter, NextFunction, HookContext as BaseHookContext
|
|
3
|
+
} from './dependencies';
|
|
3
4
|
|
|
4
|
-
|
|
5
|
-
|
|
5
|
+
type SelfOrArray<S> = S | S[];
|
|
6
|
+
type OptionalPick<T, K extends PropertyKey> = Pick<T, Extract<keyof T, K>>
|
|
6
7
|
|
|
7
|
-
export
|
|
8
|
-
|
|
8
|
+
export type { NextFunction };
|
|
9
|
+
|
|
10
|
+
export interface Paginated<T> {
|
|
11
|
+
total: number;
|
|
12
|
+
limit: number;
|
|
13
|
+
skip: number;
|
|
14
|
+
data: T[];
|
|
9
15
|
}
|
|
10
16
|
|
|
11
|
-
export interface
|
|
12
|
-
|
|
17
|
+
export interface ServiceOptions {
|
|
18
|
+
events?: string[];
|
|
19
|
+
methods?: string[];
|
|
20
|
+
serviceEvents?: string[];
|
|
21
|
+
routeParams?: { [key: string]: any };
|
|
13
22
|
}
|
|
14
23
|
|
|
15
|
-
export interface
|
|
16
|
-
|
|
17
|
-
provider?: string;
|
|
18
|
-
route?: { [key: string]: string };
|
|
19
|
-
headers?: { [key: string]: any };
|
|
24
|
+
export interface ServiceMethods<T = any, D = Partial<T>> {
|
|
25
|
+
find (params?: Params): Promise<T | T[]>;
|
|
20
26
|
|
|
21
|
-
|
|
27
|
+
get (id: Id, params?: Params): Promise<T>;
|
|
28
|
+
|
|
29
|
+
create (data: D, params?: Params): Promise<T>;
|
|
30
|
+
|
|
31
|
+
update (id: NullableId, data: D, params?: Params): Promise<T | T[]>;
|
|
32
|
+
|
|
33
|
+
patch (id: NullableId, data: D, params?: Params): Promise<T | T[]>;
|
|
34
|
+
|
|
35
|
+
remove (id: NullableId, params?: Params): Promise<T | T[]>;
|
|
36
|
+
|
|
37
|
+
setup (app: Application, path: string): Promise<void>;
|
|
22
38
|
}
|
|
23
39
|
|
|
24
|
-
export interface
|
|
25
|
-
|
|
40
|
+
export interface ServiceOverloads<T = any, D = Partial<T>> {
|
|
41
|
+
create? (data: D[], params?: Params): Promise<T[]>;
|
|
42
|
+
|
|
43
|
+
update? (id: Id, data: D, params?: Params): Promise<T>;
|
|
26
44
|
|
|
27
|
-
|
|
45
|
+
update? (id: null, data: D, params?: Params): Promise<T[]>;
|
|
28
46
|
|
|
29
|
-
|
|
47
|
+
patch? (id: Id, data: D, params?: Params): Promise<T>;
|
|
30
48
|
|
|
31
|
-
|
|
49
|
+
patch? (id: null, data: D, params?: Params): Promise<T[]>;
|
|
32
50
|
|
|
33
|
-
|
|
51
|
+
remove? (id: Id, params?: Params): Promise<T>;
|
|
52
|
+
|
|
53
|
+
remove? (id: null, params?: Params): Promise<T[]>;
|
|
54
|
+
}
|
|
34
55
|
|
|
35
|
-
|
|
56
|
+
export type Service<T = any, D = Partial<T>> =
|
|
57
|
+
ServiceMethods<T, D> &
|
|
58
|
+
ServiceOverloads<T, D>;
|
|
36
59
|
|
|
37
|
-
|
|
60
|
+
export type ServiceInterface<T = any, D = Partial<T>> =
|
|
61
|
+
Partial<ServiceMethods<T, D>>;
|
|
38
62
|
|
|
39
|
-
|
|
63
|
+
export interface ServiceAddons<A = Application, S = Service> extends EventEmitter {
|
|
64
|
+
id?: string;
|
|
65
|
+
hooks (options: HookOptions<A, S>): this;
|
|
66
|
+
}
|
|
40
67
|
|
|
41
|
-
|
|
68
|
+
export interface ServiceHookOverloads<S> {
|
|
69
|
+
find (
|
|
70
|
+
params: Params,
|
|
71
|
+
context: HookContext
|
|
72
|
+
): Promise<HookContext>;
|
|
73
|
+
|
|
74
|
+
get (
|
|
75
|
+
id: Id,
|
|
76
|
+
params: Params,
|
|
77
|
+
context: HookContext
|
|
78
|
+
): Promise<HookContext>;
|
|
79
|
+
|
|
80
|
+
create (
|
|
81
|
+
data: ServiceGenericData<S> | ServiceGenericData<S>[],
|
|
82
|
+
params: Params,
|
|
83
|
+
context: HookContext
|
|
84
|
+
): Promise<HookContext>;
|
|
85
|
+
|
|
86
|
+
update (
|
|
87
|
+
id: NullableId,
|
|
88
|
+
data: ServiceGenericData<S>,
|
|
89
|
+
params: Params,
|
|
90
|
+
context: HookContext
|
|
91
|
+
): Promise<HookContext>;
|
|
92
|
+
|
|
93
|
+
patch (
|
|
94
|
+
id: NullableId,
|
|
95
|
+
data: ServiceGenericData<S>,
|
|
96
|
+
params: Params,
|
|
97
|
+
context: HookContext
|
|
98
|
+
): Promise<HookContext>;
|
|
99
|
+
|
|
100
|
+
remove (
|
|
101
|
+
id: NullableId,
|
|
102
|
+
params: Params,
|
|
103
|
+
context: HookContext
|
|
104
|
+
): Promise<HookContext>;
|
|
105
|
+
}
|
|
42
106
|
|
|
43
|
-
|
|
107
|
+
export type FeathersService<A = FeathersApplication, S = Service> =
|
|
108
|
+
S & ServiceAddons<A, S> & OptionalPick<ServiceHookOverloads<S>, keyof S>;
|
|
44
109
|
|
|
45
|
-
|
|
110
|
+
export type CustomMethods<T extends {[key: string]: [any, any]}> = {
|
|
111
|
+
[K in keyof T]: (data: T[K][0], params?: Params) => Promise<T[K][1]>;
|
|
112
|
+
}
|
|
46
113
|
|
|
47
|
-
|
|
114
|
+
export type ServiceMixin<A> = (service: FeathersService<A>, path: string, options: ServiceOptions) => void;
|
|
48
115
|
|
|
49
|
-
|
|
116
|
+
export type ServiceGenericType<S> = S extends ServiceInterface<infer T> ? T : any;
|
|
117
|
+
export type ServiceGenericData<S> = S extends ServiceInterface<infer _T, infer D> ? D : any;
|
|
50
118
|
|
|
119
|
+
export interface FeathersApplication<Services = any, Settings = any> {
|
|
120
|
+
/**
|
|
121
|
+
* The Feathers application version
|
|
122
|
+
*/
|
|
123
|
+
version: string;
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* A list of callbacks that run when a new service is registered
|
|
127
|
+
*/
|
|
128
|
+
mixins: ServiceMixin<Application<Services, Settings>>[];
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* The index of all services keyed by their path.
|
|
132
|
+
*
|
|
133
|
+
* __Important:__ Services should always be retrieved via `app.service('name')`
|
|
134
|
+
* not via `app.services`.
|
|
135
|
+
*/
|
|
136
|
+
services: Services;
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* The application settings that can be used via
|
|
140
|
+
* `app.get` and `app.set`
|
|
141
|
+
*/
|
|
142
|
+
settings: Settings;
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* A private-ish indicator if `app.setup()` has been called already
|
|
146
|
+
*/
|
|
147
|
+
_isSetup: boolean;
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Contains all registered application level hooks.
|
|
151
|
+
*/
|
|
152
|
+
appHooks: HookMap<Application<Services, Settings>, any>;
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Retrieve an application setting by name
|
|
156
|
+
*
|
|
157
|
+
* @param name The setting name
|
|
158
|
+
*/
|
|
159
|
+
get<L extends keyof Settings & string> (name: L): Settings[L];
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Set an application setting
|
|
163
|
+
*
|
|
164
|
+
* @param name The setting name
|
|
165
|
+
* @param value The setting value
|
|
166
|
+
*/
|
|
167
|
+
set<L extends keyof Settings & string> (name: L, value: Settings[L]): this;
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Runs a callback configure function with the current application instance.
|
|
171
|
+
*
|
|
172
|
+
* @param callback The callback `(app: Application) => {}` to run
|
|
173
|
+
*/
|
|
51
174
|
configure (callback: (this: this, app: this) => void): this;
|
|
52
175
|
|
|
53
|
-
|
|
176
|
+
/**
|
|
177
|
+
* Returns a fallback service instance that will be registered
|
|
178
|
+
* when no service was found. Usually throws a `NotFound` error
|
|
179
|
+
* but also used to instantiate client side services.
|
|
180
|
+
*
|
|
181
|
+
* @param location The path of the service
|
|
182
|
+
*/
|
|
183
|
+
defaultService (location: string): ServiceInterface;
|
|
54
184
|
|
|
55
|
-
|
|
185
|
+
/**
|
|
186
|
+
* Register a new service or a sub-app. When passed another
|
|
187
|
+
* Feathers application, all its services will be re-registered
|
|
188
|
+
* with the `path` prefix.
|
|
189
|
+
*
|
|
190
|
+
* @param path The path for the service to register
|
|
191
|
+
* @param service The service object to register or another
|
|
192
|
+
* Feathers application to use a sub-app under the `path` prefix.
|
|
193
|
+
* @param options The options for this service
|
|
194
|
+
*/
|
|
195
|
+
use<L extends keyof Services & string> (
|
|
196
|
+
path: L,
|
|
197
|
+
service: keyof any extends keyof Services ? ServiceInterface | Application : Services[L],
|
|
198
|
+
options?: ServiceOptions
|
|
199
|
+
): this;
|
|
56
200
|
|
|
57
|
-
|
|
201
|
+
/**
|
|
202
|
+
* Get the Feathers service instance for a path. This will
|
|
203
|
+
* be the service originally registered with Feathers functionality
|
|
204
|
+
* like hooks and events added.
|
|
205
|
+
*
|
|
206
|
+
* @param path The name of the service.
|
|
207
|
+
*/
|
|
208
|
+
service<L extends keyof Services & string> (
|
|
209
|
+
path: L
|
|
210
|
+
): FeathersService<this, keyof any extends keyof Services ? Service : Services[L]>;
|
|
58
211
|
|
|
59
|
-
|
|
212
|
+
setup (server?: any): Promise<this>;
|
|
60
213
|
|
|
61
|
-
|
|
214
|
+
/**
|
|
215
|
+
* Register application level hooks.
|
|
216
|
+
*
|
|
217
|
+
* @param map The application hook settings.
|
|
218
|
+
*/
|
|
219
|
+
hooks (map: HookOptions<this, any>): this;
|
|
62
220
|
}
|
|
63
221
|
|
|
64
|
-
|
|
65
|
-
|
|
222
|
+
// This needs to be an interface instead of a type
|
|
223
|
+
// so that the declaration can be extended by other modules
|
|
224
|
+
export interface Application<Services = any, Settings = any> extends FeathersApplication<Services, Settings>, EventEmitter {
|
|
66
225
|
|
|
67
|
-
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
export type Id = number | string;
|
|
229
|
+
export type NullableId = Id | null;
|
|
68
230
|
|
|
69
|
-
|
|
231
|
+
export interface Query {
|
|
232
|
+
[key: string]: any;
|
|
70
233
|
}
|
|
71
234
|
|
|
72
|
-
|
|
73
|
-
|
|
235
|
+
export interface Params {
|
|
236
|
+
query?: Query;
|
|
237
|
+
provider?: string;
|
|
238
|
+
route?: { [key: string]: any };
|
|
239
|
+
headers?: { [key: string]: any };
|
|
240
|
+
[key: string]: any; // (JL) not sure if we want this
|
|
241
|
+
}
|
|
74
242
|
|
|
75
|
-
export interface
|
|
243
|
+
export interface Http {
|
|
244
|
+
/**
|
|
245
|
+
* A writeable, optional property that allows to override the standard HTTP status
|
|
246
|
+
* code that should be returned.
|
|
247
|
+
*/
|
|
248
|
+
statusCode?: number;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
export interface HookContext<A = Application, S = any> extends BaseHookContext<ServiceGenericType<S>> {
|
|
76
252
|
/**
|
|
77
253
|
* A read only property that contains the Feathers application object. This can be used to
|
|
78
254
|
* retrieve other services (via context.app.service('name')) or configuration values.
|
|
79
255
|
*/
|
|
80
|
-
readonly app:
|
|
256
|
+
readonly app: A;
|
|
257
|
+
/**
|
|
258
|
+
* A read only property with the name of the service method (one of find, get,
|
|
259
|
+
* create, update, patch, remove).
|
|
260
|
+
*/
|
|
261
|
+
readonly method: string;
|
|
262
|
+
/**
|
|
263
|
+
* A read only property and contains the service name (or path) without leading or
|
|
264
|
+
* trailing slashes.
|
|
265
|
+
*/
|
|
266
|
+
readonly path: string;
|
|
267
|
+
/**
|
|
268
|
+
* A read only property and contains the service this hook currently runs on.
|
|
269
|
+
*/
|
|
270
|
+
readonly service: S;
|
|
271
|
+
/**
|
|
272
|
+
* A read only property with the hook type (one of before, after or error).
|
|
273
|
+
* Will be `null` for asynchronous hooks.
|
|
274
|
+
*/
|
|
275
|
+
readonly type: null | 'before' | 'after' | 'error';
|
|
276
|
+
/**
|
|
277
|
+
* The list of method arguments. Should not be modified, modify the
|
|
278
|
+
* `params`, `data` and `id` properties instead.
|
|
279
|
+
*/
|
|
280
|
+
readonly arguments: any[];
|
|
81
281
|
/**
|
|
82
282
|
* A writeable property containing the data of a create, update and patch service
|
|
83
283
|
* method call.
|
|
84
284
|
*/
|
|
85
|
-
data?:
|
|
285
|
+
data?: ServiceGenericData<S>;
|
|
86
286
|
/**
|
|
87
287
|
* A writeable property with the error object that was thrown in a failed method call.
|
|
88
288
|
* It is only available in error hooks.
|
|
@@ -93,22 +293,12 @@ export interface HookContext<T = any, S = Service<T>> {
|
|
|
93
293
|
* method call. For remove, update and patch context.id can also be null when
|
|
94
294
|
* modifying multiple entries. In all other cases it will be undefined.
|
|
95
295
|
*/
|
|
96
|
-
id?:
|
|
97
|
-
/**
|
|
98
|
-
* A read only property with the name of the service method (one of find, get,
|
|
99
|
-
* create, update, patch, remove).
|
|
100
|
-
*/
|
|
101
|
-
readonly method: 'find' | 'get' | 'create' | 'update' | 'patch' | 'remove';
|
|
296
|
+
id?: Id;
|
|
102
297
|
/**
|
|
103
298
|
* A writeable property that contains the service method parameters (including
|
|
104
299
|
* params.query).
|
|
105
300
|
*/
|
|
106
301
|
params: Params;
|
|
107
|
-
/**
|
|
108
|
-
* A read only property and contains the service name (or path) without leading or
|
|
109
|
-
* trailing slashes.
|
|
110
|
-
*/
|
|
111
|
-
readonly path: string;
|
|
112
302
|
/**
|
|
113
303
|
* A writeable property containing the result of the successful service method call.
|
|
114
304
|
* It is only available in after hooks.
|
|
@@ -118,93 +308,56 @@ export interface HookContext<T = any, S = Service<T>> {
|
|
|
118
308
|
* - A before hook to skip the actual service method (database) call
|
|
119
309
|
* - An error hook to swallow the error and return a result instead
|
|
120
310
|
*/
|
|
121
|
-
result?:
|
|
122
|
-
/**
|
|
123
|
-
* A read only property and contains the service this hook currently runs on.
|
|
124
|
-
*/
|
|
125
|
-
readonly service: S;
|
|
311
|
+
result?: ServiceGenericType<S>;
|
|
126
312
|
/**
|
|
127
313
|
* A writeable, optional property and contains a 'safe' version of the data that
|
|
128
314
|
* should be sent to any client. If context.dispatch has not been set context.result
|
|
129
315
|
* will be sent to the client instead.
|
|
130
316
|
*/
|
|
131
|
-
dispatch?:
|
|
317
|
+
dispatch?: ServiceGenericType<S>;
|
|
132
318
|
/**
|
|
133
319
|
* A writeable, optional property that allows to override the standard HTTP status
|
|
134
320
|
* code that should be returned.
|
|
321
|
+
*
|
|
322
|
+
* @deprecated Use `http.statusCode` instead.
|
|
135
323
|
*/
|
|
136
324
|
statusCode?: number;
|
|
137
325
|
/**
|
|
138
|
-
* A
|
|
326
|
+
* A writeable, optional property that contains options specific to HTTP transports.
|
|
139
327
|
*/
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
export interface HookMap<T = any> {
|
|
146
|
-
all: Hook<T> | Hook<T>[];
|
|
147
|
-
find: Hook<T> | Hook<T>[];
|
|
148
|
-
get: Hook<T> | Hook<T>[];
|
|
149
|
-
create: Hook<T> | Hook<T>[];
|
|
150
|
-
update: Hook<T> | Hook<T>[];
|
|
151
|
-
patch: Hook<T> | Hook<T>[];
|
|
152
|
-
remove: Hook<T> | Hook<T>[];
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
export interface HooksObject<T = any> {
|
|
156
|
-
before: Partial<HookMap<T>> | Hook<T> | Hook<T>[];
|
|
157
|
-
after: Partial<HookMap<T>> | Hook<T> | Hook<T>[];
|
|
158
|
-
error: Partial<HookMap<T>> | Hook<T> | Hook<T>[];
|
|
159
|
-
async?: Partial<HookMap<T>> | Hook<T> | Hook<T>[];
|
|
160
|
-
finally?: Partial<HookMap<T>> | Hook<T> | Hook<T>[];
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
export interface ServiceMethods<T> {
|
|
164
|
-
[key: string]: any;
|
|
165
|
-
|
|
166
|
-
find (params?: Params): Promise<T | T[]>;
|
|
167
|
-
|
|
168
|
-
get (id: Id, params?: Params): Promise<T>;
|
|
169
|
-
|
|
170
|
-
create (data: Partial<T> | Partial<T>[], params?: Params): Promise<T | T[]>;
|
|
171
|
-
|
|
172
|
-
update (id: NullableId, data: T, params?: Params): Promise<T | T[]>;
|
|
173
|
-
|
|
174
|
-
patch (id: NullableId, data: Partial<T>, params?: Params): Promise<T | T[]>;
|
|
175
|
-
|
|
176
|
-
remove (id: NullableId, params?: Params): Promise<T | T[]>;
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
export interface SetupMethod {
|
|
180
|
-
setup (app: Application, path: string): void;
|
|
328
|
+
http?: Http;
|
|
329
|
+
/**
|
|
330
|
+
* The event emitted by this method. Can be set to `null` to skip event emitting.
|
|
331
|
+
*/
|
|
332
|
+
event: string|null;
|
|
181
333
|
}
|
|
182
334
|
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
create? (data: Partial<T>[], params?: Params): Promise<T[]>;
|
|
187
|
-
|
|
188
|
-
update? (id: Id, data: T, params?: Params): Promise<T>;
|
|
335
|
+
// Regular hook typings
|
|
336
|
+
export type RegularHookFunction<A = Application, S = Service> =
|
|
337
|
+
(this: S, context: HookContext<A, S>) => (Promise<HookContext<Application, S> | void> | HookContext<Application, S> | void);
|
|
189
338
|
|
|
190
|
-
|
|
339
|
+
export type Hook<A = Application, S = Service> = RegularHookFunction<A, S>;
|
|
191
340
|
|
|
192
|
-
|
|
341
|
+
type RegularHookMethodMap<A, S> =
|
|
342
|
+
{ [L in keyof S]?: SelfOrArray<RegularHookFunction<A, S>>; } &
|
|
343
|
+
{ all?: SelfOrArray<RegularHookFunction<A, S>> };
|
|
193
344
|
|
|
194
|
-
|
|
345
|
+
type RegularHookTypeMap<A, S> =
|
|
346
|
+
SelfOrArray<RegularHookFunction<A, S>> | RegularHookMethodMap<A, S>;
|
|
195
347
|
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
348
|
+
export type RegularHookMap<A, S> = {
|
|
349
|
+
before?: RegularHookTypeMap<A, S>,
|
|
350
|
+
after?: RegularHookTypeMap<A, S>,
|
|
351
|
+
error?: RegularHookTypeMap<A, S>
|
|
199
352
|
}
|
|
200
353
|
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
methods: { [method: string]: string[] };
|
|
205
|
-
hooks (hooks: Partial<HooksObject<T>>): this;
|
|
206
|
-
}
|
|
354
|
+
// New @feathersjs/hook typings
|
|
355
|
+
export type HookFunction<A = Application, S = Service> =
|
|
356
|
+
(context: HookContext<A, S>, next: NextFunction) => Promise<void>;
|
|
207
357
|
|
|
208
|
-
export type
|
|
358
|
+
export type HookMap<A, S> = {
|
|
359
|
+
[L in keyof S]?: HookFunction<A, S>[];
|
|
360
|
+
};
|
|
209
361
|
|
|
210
|
-
export type
|
|
362
|
+
export type HookOptions<A, S> =
|
|
363
|
+
HookMap<A, S> | HookFunction<A, S>[] | RegularHookMap<A, S>;
|
package/src/events.ts
CHANGED
|
@@ -1,88 +1,31 @@
|
|
|
1
|
-
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
4
|
-
import { HookContext, Service, Application } from './declarations';
|
|
1
|
+
import { NextFunction, EventEmitter } from './dependencies';
|
|
2
|
+
import { HookContext, FeathersService } from './declarations';
|
|
3
|
+
import { getServiceOptions, defaultEventMap } from './service';
|
|
5
4
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
return function (ctx: HookContext) {
|
|
10
|
-
const { app, service, method, event, type, result } = ctx;
|
|
5
|
+
export function eventHook (context: HookContext, next: NextFunction) {
|
|
6
|
+
const { events } = getServiceOptions((context as any).self);
|
|
7
|
+
const defaultEvent = (defaultEventMap as any)[context.method] || null;
|
|
11
8
|
|
|
12
|
-
|
|
13
|
-
const isHookEvent = service._hookEvents && service._hookEvents.indexOf(eventName) !== -1;
|
|
9
|
+
context.event = defaultEvent;
|
|
14
10
|
|
|
15
|
-
|
|
16
|
-
if
|
|
17
|
-
|
|
11
|
+
return next().then(() => {
|
|
12
|
+
// Send the event only if the service does not do so already (indicated in the `events` option)
|
|
13
|
+
// This is used for custom events and for client services receiving event from the server
|
|
14
|
+
if (typeof context.event === 'string' && !events.includes(context.event)) {
|
|
15
|
+
const results = Array.isArray(context.result) ? context.result : [ context.result ];
|
|
18
16
|
|
|
19
|
-
results.forEach(element =>
|
|
17
|
+
results.forEach(element => (context as any).self.emit(context.event, element, context));
|
|
20
18
|
}
|
|
21
|
-
};
|
|
19
|
+
});
|
|
22
20
|
}
|
|
23
21
|
|
|
24
|
-
|
|
25
|
-
export function eventMixin (this: Application, service: Service<any>) {
|
|
26
|
-
if (service._serviceEvents) {
|
|
27
|
-
return;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
const app = this;
|
|
31
|
-
// Indicates if the service is already an event emitter
|
|
22
|
+
export function eventMixin<A> (service: FeathersService<A>) {
|
|
32
23
|
const isEmitter = typeof service.on === 'function' &&
|
|
33
24
|
typeof service.emit === 'function';
|
|
34
25
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
service.mixin(EventEmitter.prototype);
|
|
26
|
+
if (!isEmitter) {
|
|
27
|
+
Object.assign(service, EventEmitter.prototype);
|
|
38
28
|
}
|
|
39
29
|
|
|
40
|
-
|
|
41
|
-
Object.defineProperties(service, {
|
|
42
|
-
// A list of all events that this service sends
|
|
43
|
-
_serviceEvents: {
|
|
44
|
-
value: Array.isArray(service.events) ? service.events.slice() : []
|
|
45
|
-
},
|
|
46
|
-
|
|
47
|
-
// A list of events that should be handled through the event hooks
|
|
48
|
-
_hookEvents: {
|
|
49
|
-
value: []
|
|
50
|
-
}
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
// `app.eventMappings` has the mapping from method name to event name
|
|
54
|
-
Object.keys(app.eventMappings).forEach(method => {
|
|
55
|
-
const event = app.eventMappings[method];
|
|
56
|
-
const alreadyEmits = service._serviceEvents.indexOf(event) !== -1;
|
|
57
|
-
|
|
58
|
-
// Add events for known methods to _serviceEvents and _hookEvents
|
|
59
|
-
// if the service indicated it does not send it itself yet
|
|
60
|
-
if (typeof service[method] === 'function' && !alreadyEmits) {
|
|
61
|
-
service._serviceEvents.push(event);
|
|
62
|
-
service._hookEvents.push(event);
|
|
63
|
-
}
|
|
64
|
-
});
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
export default function () {
|
|
68
|
-
return function (app: any) {
|
|
69
|
-
// Mappings from service method to event name
|
|
70
|
-
Object.assign(app, {
|
|
71
|
-
eventMappings: {
|
|
72
|
-
create: 'created',
|
|
73
|
-
update: 'updated',
|
|
74
|
-
remove: 'removed',
|
|
75
|
-
patch: 'patched'
|
|
76
|
-
}
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
// Register the event hook
|
|
80
|
-
// `finally` hooks always run last after `error` and `after` hooks
|
|
81
|
-
app.hooks({ finally: eventHook() });
|
|
82
|
-
|
|
83
|
-
// Make the app an event emitter
|
|
84
|
-
Proto.mixin(EventEmitter.prototype, app);
|
|
85
|
-
|
|
86
|
-
app.mixins.push(eventMixin);
|
|
87
|
-
};
|
|
30
|
+
return service;
|
|
88
31
|
}
|