@feathersjs/feathers 5.0.0-pre.6 → 5.0.0

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.
@@ -1,130 +1,207 @@
1
- import {
2
- EventEmitter, NextFunction, HookContext as BaseHookContext
3
- } from './dependencies';
1
+ import { EventEmitter } from 'events'
2
+ import { NextFunction, HookContext as BaseHookContext } from '@feathersjs/hooks'
4
3
 
5
- type SelfOrArray<S> = S | S[];
4
+ type SelfOrArray<S> = S | S[]
6
5
  type OptionalPick<T, K extends PropertyKey> = Pick<T, Extract<keyof T, K>>
7
6
 
8
- export type { NextFunction };
7
+ export type { NextFunction }
9
8
 
9
+ /**
10
+ * The object returned from `.find` call by standard database adapters
11
+ */
10
12
  export interface Paginated<T> {
11
- total: number;
12
- limit: number;
13
- skip: number;
14
- data: T[];
13
+ total: number
14
+ limit: number
15
+ skip: number
16
+ data: T[]
15
17
  }
16
18
 
17
- export interface ServiceOptions {
18
- events?: string[];
19
- methods?: string[];
20
- serviceEvents?: string[];
19
+ /**
20
+ * Options that can be passed when registering a service via `app.use(name, service, options)`
21
+ */
22
+ export interface ServiceOptions<MethodTypes = string> {
23
+ /**
24
+ * A list of custom events that this service emits to clients
25
+ */
26
+ events?: string[] | readonly string[]
27
+ /**
28
+ * A list of service methods that should be available __externally__ to clients
29
+ */
30
+ methods?: MethodTypes[] | readonly MethodTypes[]
31
+ /**
32
+ * Provide a full list of events that this service should emit to clients.
33
+ * Unlike the `events` option, this will not be merged with the default events.
34
+ */
35
+ serviceEvents?: string[] | readonly string[]
36
+ /**
37
+ * Initial data to always add as route params to this service.
38
+ */
39
+ routeParams?: { [key: string]: any }
21
40
  }
22
41
 
23
- export interface ServiceMethods<T, D = Partial<T>> {
24
- find (params?: Params): Promise<T | T[]>;
25
-
26
- get (id: Id, params?: Params): Promise<T>;
42
+ export interface ClientService<
43
+ Result = any,
44
+ Data = Partial<Result>,
45
+ PatchData = Data,
46
+ FindResult = Paginated<Result>,
47
+ P = Params
48
+ > {
49
+ find(params?: P): Promise<FindResult>
27
50
 
28
- create (data: D, params?: Params): Promise<T>;
51
+ get(id: Id, params?: P): Promise<Result>
29
52
 
30
- update (id: NullableId, data: D, params?: Params): Promise<T | T[]>;
53
+ create(data: Data[], params?: P): Promise<Result[]>
54
+ create(data: Data, params?: P): Promise<Result>
31
55
 
32
- patch (id: NullableId, data: D, params?: Params): Promise<T | T[]>;
56
+ update(id: Id, data: Data, params?: P): Promise<Result>
57
+ update(id: NullableId, data: Data, params?: P): Promise<Result | Result[]>
58
+ update(id: null, data: Data, params?: P): Promise<Result[]>
33
59
 
34
- remove (id: NullableId, params?: Params): Promise<T | T[]>;
60
+ patch(id: NullableId, data: PatchData, params?: P): Promise<Result | Result[]>
61
+ patch(id: Id, data: PatchData, params?: P): Promise<Result>
62
+ patch(id: null, data: PatchData, params?: P): Promise<Result[]>
35
63
 
36
- setup (app: Application, path: string): Promise<void>;
64
+ remove(id: NullableId, params?: P): Promise<Result | Result[]>
65
+ remove(id: Id, params?: P): Promise<Result>
66
+ remove(id: null, params?: P): Promise<Result[]>
37
67
  }
38
68
 
39
- export interface ServiceOverloads<T, D> {
40
- create? (data: D[], params?: Params): Promise<T[]>;
69
+ export interface ServiceMethods<
70
+ Result = any,
71
+ Data = Partial<Result>,
72
+ ServiceParams = Params,
73
+ PatchData = Partial<Data>
74
+ > {
75
+ find(params?: ServiceParams): Promise<Result | Result[]>
41
76
 
42
- update? (id: Id, data: D, params?: Params): Promise<T>;
77
+ get(id: Id, params?: ServiceParams): Promise<Result>
43
78
 
44
- update? (id: null, data: D, params?: Params): Promise<T[]>;
79
+ create(data: Data, params?: ServiceParams): Promise<Result>
45
80
 
46
- patch? (id: Id, data: D, params?: Params): Promise<T>;
81
+ update(id: NullableId, data: Data, params?: ServiceParams): Promise<Result | Result[]>
47
82
 
48
- patch? (id: null, data: D, params?: Params): Promise<T[]>;
83
+ patch(id: NullableId, data: PatchData, params?: ServiceParams): Promise<Result | Result[]>
49
84
 
50
- remove? (id: Id, params?: Params): Promise<T>;
85
+ remove(id: NullableId, params?: ServiceParams): Promise<Result | Result[]>
51
86
 
52
- remove? (id: null, params?: Params): Promise<T[]>;
87
+ setup?(app: Application, path: string): Promise<void>
88
+
89
+ teardown?(app: Application, path: string): Promise<void>
53
90
  }
54
91
 
55
- export type Service<T, D = Partial<T>> =
56
- ServiceMethods<T, D> &
57
- ServiceOverloads<T, D>;
92
+ export interface ServiceOverloads<
93
+ Result = any,
94
+ Data = Partial<Result>,
95
+ ServiceParams = Params,
96
+ PatchData = Partial<Data>
97
+ > {
98
+ create?(data: Data[], params?: ServiceParams): Promise<Result[]>
99
+
100
+ update?(id: Id, data: Data, params?: ServiceParams): Promise<Result>
101
+
102
+ update?(id: null, data: Data, params?: ServiceParams): Promise<Result[]>
103
+
104
+ patch?(id: Id, data: PatchData, params?: ServiceParams): Promise<Result>
58
105
 
59
- export type ServiceInterface<T, D = Partial<T>> =
60
- Partial<ServiceMethods<T, D>>;
106
+ patch?(id: null, data: PatchData, params?: ServiceParams): Promise<Result[]>
61
107
 
62
- export interface ServiceAddons<A = Application, S = Service<any, any>> extends EventEmitter {
63
- id?: string;
64
- hooks (options: HookOptions<A, S>): this;
108
+ remove?(id: Id, params?: ServiceParams): Promise<Result>
109
+
110
+ remove?(id: null, params?: ServiceParams): Promise<Result[]>
65
111
  }
66
112
 
67
- export interface ServiceHookOverloads<S> {
68
- find (
69
- params: Params,
70
- context: HookContext
71
- ): Promise<HookContext>;
113
+ /**
114
+ * A complete service interface. The `ServiceInterface` type should be preferred for customs service
115
+ * implementations
116
+ */
117
+ export type Service<
118
+ Result = any,
119
+ Data = Partial<Result>,
120
+ ServiceParams = Params,
121
+ PatchData = Partial<Data>
122
+ > = ServiceMethods<Result, Data, ServiceParams> & ServiceOverloads<Result, Data, ServiceParams, PatchData>
123
+
124
+ /**
125
+ * The `Service` service interface but with none of the methods required.
126
+ */
127
+ export type ServiceInterface<
128
+ Result = any,
129
+ Data = Partial<Result>,
130
+ ServiceParams = Params,
131
+ PatchData = Partial<Data>
132
+ > = Partial<ServiceMethods<Result, Data, ServiceParams, PatchData>>
133
+
134
+ export interface ServiceAddons<A = Application, S = Service> extends EventEmitter {
135
+ id?: string
136
+ hooks(options: HookOptions<A, S>): this
137
+ }
72
138
 
73
- get (
74
- id: Id,
75
- params: Params,
76
- context: HookContext
77
- ): Promise<HookContext>;
139
+ export interface ServiceHookOverloads<S, P = Params> {
140
+ find(params: P, context: HookContext): Promise<HookContext>
78
141
 
79
- create (
142
+ get(id: Id, params: P, context: HookContext): Promise<HookContext>
143
+
144
+ create(
80
145
  data: ServiceGenericData<S> | ServiceGenericData<S>[],
81
- params: Params,
146
+ params: P,
82
147
  context: HookContext
83
- ): Promise<HookContext>;
148
+ ): Promise<HookContext>
84
149
 
85
- update (
86
- id: NullableId,
87
- data: ServiceGenericData<S>,
88
- params: Params,
89
- context: HookContext
90
- ): Promise<HookContext>;
150
+ update(id: NullableId, data: ServiceGenericData<S>, params: P, context: HookContext): Promise<HookContext>
91
151
 
92
- patch (
93
- id: NullableId,
94
- data: ServiceGenericData<S>,
95
- params: Params,
96
- context: HookContext
97
- ): Promise<HookContext>;
152
+ patch(id: NullableId, data: ServiceGenericData<S>, params: P, context: HookContext): Promise<HookContext>
98
153
 
99
- remove (
100
- id: NullableId,
101
- params: Params,
102
- context: HookContext
103
- ): Promise<HookContext>;
154
+ remove(id: NullableId, params: P, context: HookContext): Promise<HookContext>
104
155
  }
105
156
 
106
- export type FeathersService<A = FeathersApplication, S = Service<any>> =
107
- S & ServiceAddons<A, S> & OptionalPick<ServiceHookOverloads<S>, keyof S>;
157
+ export type FeathersService<A = FeathersApplication, S = Service> = S &
158
+ ServiceAddons<A, S> &
159
+ OptionalPick<ServiceHookOverloads<S>, keyof S>
160
+
161
+ export type CustomMethods<T extends { [key: string]: [any, any] }> = {
162
+ [K in keyof T]: (data: T[K][0], params?: Params) => Promise<T[K][1]>
163
+ }
164
+
165
+ /**
166
+ * An interface usually use by transport clients that represents a e.g. HTTP or websocket
167
+ * connection that can be configured on the application.
168
+ */
169
+ export type TransportConnection<Services = any> = {
170
+ (app: Application<Services>): void
171
+ Service: any
172
+ service: <L extends keyof Services & string>(
173
+ name: L
174
+ ) => keyof any extends keyof Services ? ServiceInterface : Services[L]
175
+ }
108
176
 
109
- export type CustomMethod<Methods extends string> = {
110
- [k in Methods]: <X = any> (data: any, params?: Params) => Promise<X>;
177
+ /**
178
+ * A real-time connection object
179
+ */
180
+ export interface RealTimeConnection {
181
+ [key: string]: any
111
182
  }
112
183
 
113
- export type ServiceMixin<A> = (service: FeathersService<A>, path: string, options?: ServiceOptions) => void;
184
+ /**
185
+ * The interface for a custom service method. Can e.g. be used to type client side services.
186
+ */
187
+ export type CustomMethod<T = any, R = T, P extends Params = Params> = (data: T, params?: P) => Promise<R>
114
188
 
115
- export type ServiceGenericType<S> = S extends ServiceInterface<infer T> ? T : any;
116
- export type ServiceGenericData<S> = S extends ServiceInterface<infer _T, infer D> ? D : any;
189
+ export type ServiceMixin<A> = (service: FeathersService<A>, path: string, options: ServiceOptions) => void
117
190
 
118
- export interface FeathersApplication<ServiceTypes = any, AppSettings = any> {
191
+ export type ServiceGenericType<S> = S extends ServiceInterface<infer T> ? T : any
192
+ export type ServiceGenericData<S> = S extends ServiceInterface<infer _T, infer D> ? D : any
193
+ export type ServiceGenericParams<S> = S extends ServiceInterface<infer _T, infer _D, infer P> ? P : any
194
+
195
+ export interface FeathersApplication<Services = any, Settings = any> {
119
196
  /**
120
197
  * The Feathers application version
121
198
  */
122
- version: string;
199
+ version: string
123
200
 
124
201
  /**
125
202
  * A list of callbacks that run when a new service is registered
126
203
  */
127
- mixins: ServiceMixin<Application<ServiceTypes, AppSettings>>[];
204
+ mixins: ServiceMixin<Application<Services, Settings>>[]
128
205
 
129
206
  /**
130
207
  * The index of all services keyed by their path.
@@ -132,30 +209,25 @@ export interface FeathersApplication<ServiceTypes = any, AppSettings = any> {
132
209
  * __Important:__ Services should always be retrieved via `app.service('name')`
133
210
  * not via `app.services`.
134
211
  */
135
- services: ServiceTypes;
212
+ services: Services
136
213
 
137
214
  /**
138
215
  * The application settings that can be used via
139
216
  * `app.get` and `app.set`
140
217
  */
141
- settings: AppSettings;
218
+ settings: Settings
142
219
 
143
220
  /**
144
221
  * A private-ish indicator if `app.setup()` has been called already
145
222
  */
146
- _isSetup: boolean;
147
-
148
- /**
149
- * Contains all registered application level hooks.
150
- */
151
- appHooks: HookMap<Application<ServiceTypes, AppSettings>, any>;
223
+ _isSetup: boolean
152
224
 
153
225
  /**
154
226
  * Retrieve an application setting by name
155
227
  *
156
228
  * @param name The setting name
157
229
  */
158
- get<L extends keyof AppSettings & string> (name: L): AppSettings[L];
230
+ get<L extends keyof Settings & string>(name: L): Settings[L]
159
231
 
160
232
  /**
161
233
  * Set an application setting
@@ -163,14 +235,14 @@ export interface FeathersApplication<ServiceTypes = any, AppSettings = any> {
163
235
  * @param name The setting name
164
236
  * @param value The setting value
165
237
  */
166
- set<L extends keyof AppSettings & string> (name: L, value: AppSettings[L]): this;
238
+ set<L extends keyof Settings & string>(name: L, value: Settings[L]): this
167
239
 
168
240
  /**
169
241
  * Runs a callback configure function with the current application instance.
170
242
  *
171
243
  * @param callback The callback `(app: Application) => {}` to run
172
244
  */
173
- configure (callback: (this: this, app: this) => void): this;
245
+ configure(callback: (this: this, app: this) => void): this
174
246
 
175
247
  /**
176
248
  * Returns a fallback service instance that will be registered
@@ -179,7 +251,7 @@ export interface FeathersApplication<ServiceTypes = any, AppSettings = any> {
179
251
  *
180
252
  * @param location The path of the service
181
253
  */
182
- defaultService (location: string): ServiceInterface<any>;
254
+ defaultService(location: string): ServiceInterface
183
255
 
184
256
  /**
185
257
  * Register a new service or a sub-app. When passed another
@@ -191,11 +263,20 @@ export interface FeathersApplication<ServiceTypes = any, AppSettings = any> {
191
263
  * Feathers application to use a sub-app under the `path` prefix.
192
264
  * @param options The options for this service
193
265
  */
194
- use<L extends keyof ServiceTypes & string> (
266
+ use<L extends keyof Services & string>(
195
267
  path: L,
196
- service: keyof any extends keyof ServiceTypes ? ServiceInterface<any> | Application : ServiceTypes[L],
197
- options?: ServiceOptions
198
- ): this;
268
+ service: keyof any extends keyof Services ? ServiceInterface | Application : Services[L],
269
+ options?: ServiceOptions<keyof any extends keyof Services ? string : keyof Services[L]>
270
+ ): this
271
+
272
+ /**
273
+ * Unregister an existing service.
274
+ *
275
+ * @param path The name of the service to unregister
276
+ */
277
+ unuse<L extends keyof Services & string>(
278
+ path: L
279
+ ): Promise<FeathersService<this, keyof any extends keyof Services ? Service : Services[L]>>
199
280
 
200
281
  /**
201
282
  * Get the Feathers service instance for a path. This will
@@ -204,92 +285,119 @@ export interface FeathersApplication<ServiceTypes = any, AppSettings = any> {
204
285
  *
205
286
  * @param path The name of the service.
206
287
  */
207
- service<L extends keyof ServiceTypes & string> (
288
+ service<L extends keyof Services & string>(
208
289
  path: L
209
- ): FeathersService<this, keyof any extends keyof ServiceTypes ? Service<any> : ServiceTypes[L]>;
290
+ ): FeathersService<this, keyof any extends keyof Services ? Service : Services[L]>
291
+
292
+ /**
293
+ * Set up the application and call all services `.setup` method if available.
294
+ *
295
+ * @param server A server instance (optional)
296
+ */
297
+ setup(server?: any): Promise<this>
210
298
 
211
- setup (server?: any): Promise<this>;
299
+ /**
300
+ * Tear down the application and call all services `.teardown` method if available.
301
+ *
302
+ * @param server A server instance (optional)
303
+ */
304
+ teardown(server?: any): Promise<this>
212
305
 
213
306
  /**
214
307
  * Register application level hooks.
215
308
  *
216
309
  * @param map The application hook settings.
217
310
  */
218
- hooks (map: HookOptions<this, any>): this;
311
+ hooks(map: ApplicationHookOptions<this>): this
219
312
  }
220
313
 
221
314
  // This needs to be an interface instead of a type
222
315
  // so that the declaration can be extended by other modules
223
- export interface Application<ServiceTypes = any, AppSettings = any> extends FeathersApplication<ServiceTypes, AppSettings>, EventEmitter {
316
+ export interface Application<Services = any, Settings = any>
317
+ extends FeathersApplication<Services, Settings>,
318
+ EventEmitter {}
224
319
 
225
- }
226
-
227
- export type Id = number | string;
228
- export type NullableId = Id | null;
320
+ export type Id = number | string
321
+ export type NullableId = Id | null
229
322
 
230
323
  export interface Query {
231
- [key: string]: any;
324
+ [key: string]: any
325
+ }
326
+
327
+ export interface Params<Q = Query> {
328
+ query?: Q
329
+ provider?: string
330
+ route?: { [key: string]: any }
331
+ headers?: { [key: string]: any }
232
332
  }
233
333
 
234
- export interface Params {
235
- query?: Query;
236
- provider?: string;
237
- route?: { [key: string]: string };
238
- headers?: { [key: string]: any };
239
- [key: string]: any; // (JL) not sure if we want this
334
+ export interface Http {
335
+ /**
336
+ * A writeable, optional property with status code override.
337
+ */
338
+ status?: number
339
+ /**
340
+ * A writeable, optional property with headers.
341
+ */
342
+ headers?: { [key: string]: string | string[] }
343
+ /**
344
+ * A writeable, optional property with `Location` header's value.
345
+ */
346
+ location?: string
240
347
  }
241
348
 
349
+ export type HookType = 'before' | 'after' | 'error' | 'around'
350
+
242
351
  export interface HookContext<A = Application, S = any> extends BaseHookContext<ServiceGenericType<S>> {
243
352
  /**
244
353
  * A read only property that contains the Feathers application object. This can be used to
245
354
  * retrieve other services (via context.app.service('name')) or configuration values.
246
355
  */
247
- readonly app: A;
356
+ readonly app: A
248
357
  /**
249
358
  * A read only property with the name of the service method (one of find, get,
250
359
  * create, update, patch, remove).
251
360
  */
252
- readonly method: string;
361
+ readonly method: string
253
362
  /**
254
363
  * A read only property and contains the service name (or path) without leading or
255
364
  * trailing slashes.
256
365
  */
257
- readonly path: string;
366
+ readonly path: string
258
367
  /**
259
368
  * A read only property and contains the service this hook currently runs on.
260
369
  */
261
- readonly service: S;
370
+ readonly service: S
262
371
  /**
263
- * A read only property with the hook type (one of before, after or error).
264
- * Will be `null` for asynchronous hooks.
372
+ * A read only property with the hook type (one of 'around', 'before', 'after' or 'error').
265
373
  */
266
- readonly type: null | 'before' | 'after' | 'error';
374
+ readonly type: HookType
267
375
  /**
268
376
  * The list of method arguments. Should not be modified, modify the
269
377
  * `params`, `data` and `id` properties instead.
270
378
  */
271
- readonly arguments: any[];
379
+ readonly arguments: any[]
272
380
  /**
273
381
  * A writeable property containing the data of a create, update and patch service
274
382
  * method call.
275
383
  */
276
- data?: ServiceGenericData<S>;
384
+ data?: ServiceGenericData<S>
277
385
  /**
278
386
  * A writeable property with the error object that was thrown in a failed method call.
279
387
  * It is only available in error hooks.
280
388
  */
281
- error?: any;
389
+ error?: any
282
390
  /**
283
391
  * A writeable property and the id for a get, remove, update and patch service
284
392
  * method call. For remove, update and patch context.id can also be null when
285
393
  * modifying multiple entries. In all other cases it will be undefined.
286
394
  */
287
- id?: Id;
395
+ id?: Id
288
396
  /**
289
397
  * A writeable property that contains the service method parameters (including
290
398
  * params.query).
291
399
  */
292
- params: Params;
400
+ params: ServiceGenericParams<S>
293
401
  /**
294
402
  * A writeable property containing the result of the successful service method call.
295
403
  * It is only available in after hooks.
@@ -299,50 +407,76 @@ export interface HookContext<A = Application, S = any> extends BaseHookContext<S
299
407
  * - A before hook to skip the actual service method (database) call
300
408
  * - An error hook to swallow the error and return a result instead
301
409
  */
302
- result?: ServiceGenericType<S>;
410
+ result?: ServiceGenericType<S>
303
411
  /**
304
412
  * A writeable, optional property and contains a 'safe' version of the data that
305
413
  * should be sent to any client. If context.dispatch has not been set context.result
306
414
  * will be sent to the client instead.
307
415
  */
308
- dispatch?: ServiceGenericType<S>;
416
+ dispatch?: ServiceGenericType<S>
309
417
  /**
310
418
  * A writeable, optional property that allows to override the standard HTTP status
311
419
  * code that should be returned.
420
+ *
421
+ * @deprecated Use `http.status` instead.
312
422
  */
313
- statusCode?: number;
423
+ statusCode?: number
424
+ /**
425
+ * A writeable, optional property with options specific to HTTP transports.
426
+ */
427
+ http?: Http
314
428
  /**
315
429
  * The event emitted by this method. Can be set to `null` to skip event emitting.
316
430
  */
317
- event: string|null;
431
+ event: string | null
318
432
  }
319
433
 
320
- // Legacy hook typings
321
- export type LegacyHookFunction<A = Application, S = Service<any, any>> =
322
- (this: S, context: HookContext<A, S>) => (Promise<HookContext<Application, S> | void> | HookContext<Application, S> | void);
434
+ // Regular hook typings
435
+ export type HookFunction<A = Application, S = Service> = (
436
+ this: S,
437
+ context: HookContext<A, S>
438
+ ) => Promise<HookContext<Application, S> | void> | HookContext<Application, S> | void
323
439
 
324
- export type Hook<A = Application, S = Service<any, any>> = LegacyHookFunction<A, S>;
440
+ export type Hook<A = Application, S = Service> = HookFunction<A, S>
325
441
 
326
- type LegacyHookMethodMap<A, S> =
327
- { [L in keyof S]?: SelfOrArray<LegacyHookFunction<A, S>>; } &
328
- { all?: SelfOrArray<LegacyHookFunction<A, S>> };
442
+ type HookMethodMap<A, S> = {
443
+ [L in keyof S]?: SelfOrArray<HookFunction<A, S>>
444
+ } & { all?: SelfOrArray<HookFunction<A, S>> }
329
445
 
330
- type LegacyHookTypeMap<A, S> =
331
- SelfOrArray<LegacyHookFunction<A, S>> | LegacyHookMethodMap<A, S>;
332
-
333
- export type LegacyHookMap<A, S> = {
334
- before?: LegacyHookTypeMap<A, S>,
335
- after?: LegacyHookTypeMap<A, S>,
336
- error?: LegacyHookTypeMap<A, S>
337
- }
446
+ type HookTypeMap<A, S> = SelfOrArray<HookFunction<A, S>> | HookMethodMap<A, S>
338
447
 
339
448
  // New @feathersjs/hook typings
340
- export type HookFunction<A = Application, S = Service<any, any>> =
341
- (context: HookContext<A, S>, next: NextFunction) => Promise<void>;
449
+ export type AroundHookFunction<A = Application, S = Service> = (
450
+ context: HookContext<A, S>,
451
+ next: NextFunction
452
+ ) => Promise<void>
453
+
454
+ export type AroundHookMap<A, S> = {
455
+ [L in keyof S]?: AroundHookFunction<A, S>[]
456
+ } & { all?: AroundHookFunction<A, S>[] }
342
457
 
343
458
  export type HookMap<A, S> = {
344
- [L in keyof S]?: HookFunction<A, S>[];
345
- };
459
+ around?: AroundHookMap<A, S>
460
+ before?: HookTypeMap<A, S>
461
+ after?: HookTypeMap<A, S>
462
+ error?: HookTypeMap<A, S>
463
+ }
464
+
465
+ export type HookOptions<A, S> = AroundHookMap<A, S> | AroundHookFunction<A, S>[] | HookMap<A, S>
466
+
467
+ export interface ApplicationHookContext<A = Application> extends BaseHookContext {
468
+ app: A
469
+ server: any
470
+ }
471
+
472
+ export type ApplicationHookFunction<A> = (
473
+ context: ApplicationHookContext<A>,
474
+ next: NextFunction
475
+ ) => Promise<void>
476
+
477
+ export type ApplicationHookMap<A> = {
478
+ setup?: ApplicationHookFunction<A>[]
479
+ teardown?: ApplicationHookFunction<A>[]
480
+ }
346
481
 
347
- export type HookOptions<A, S> =
348
- HookMap<A, S> | HookFunction<A, S>[] | LegacyHookMap<A, S>;
482
+ export type ApplicationHookOptions<A> = HookOptions<A, any> | ApplicationHookMap<A>
package/src/events.ts CHANGED
@@ -1,31 +1,31 @@
1
- import { NextFunction, EventEmitter } from './dependencies';
2
- import { HookContext, FeathersService } from './declarations';
3
- import { getServiceOptions, defaultEventMap } from './service';
1
+ import { EventEmitter } from 'events'
2
+ import { NextFunction } from '@feathersjs/hooks'
3
+ import { HookContext, FeathersService } from './declarations'
4
+ import { getServiceOptions, defaultEventMap } from './service'
4
5
 
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;
6
+ export function eventHook(context: HookContext, next: NextFunction) {
7
+ const { events } = getServiceOptions((context as any).self)
8
+ const defaultEvent = (defaultEventMap as any)[context.method] || null
8
9
 
9
- context.event = defaultEvent;
10
+ context.event = defaultEvent
10
11
 
11
12
  return next().then(() => {
12
13
  // Send the event only if the service does not do so already (indicated in the `events` option)
13
14
  // This is used for custom events and for client services receiving event from the server
14
15
  if (typeof context.event === 'string' && !events.includes(context.event)) {
15
- const results = Array.isArray(context.result) ? context.result : [ context.result ];
16
+ const results = Array.isArray(context.result) ? context.result : [context.result]
16
17
 
17
- results.forEach(element => (context as any).self.emit(context.event, element, context));
18
+ results.forEach((element) => (context as any).self.emit(context.event, element, context))
18
19
  }
19
- });
20
+ })
20
21
  }
21
22
 
22
- export function eventMixin<A> (service: FeathersService<A>) {
23
- const isEmitter = typeof service.on === 'function' &&
24
- typeof service.emit === 'function';
23
+ export function eventMixin<A>(service: FeathersService<A>) {
24
+ const isEmitter = typeof service.on === 'function' && typeof service.emit === 'function'
25
25
 
26
26
  if (!isEmitter) {
27
- Object.assign(service, EventEmitter.prototype);
27
+ Object.assign(service, EventEmitter.prototype)
28
28
  }
29
29
 
30
- return service;
30
+ return service
31
31
  }