@feathersjs/feathers 5.0.0-pre.22 → 5.0.0-pre.23

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,134 +1,112 @@
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
 
10
9
  export interface Paginated<T> {
11
- total: number;
12
- limit: number;
13
- skip: number;
14
- data: T[];
10
+ total: number
11
+ limit: number
12
+ skip: number
13
+ data: T[]
15
14
  }
16
15
 
17
16
  export interface ServiceOptions {
18
- events?: string[];
19
- methods?: string[];
20
- serviceEvents?: string[];
21
- routeParams?: { [key: string]: any };
17
+ events?: string[]
18
+ methods?: string[]
19
+ serviceEvents?: string[]
20
+ routeParams?: { [key: string]: any }
22
21
  }
23
22
 
24
23
  export interface ServiceMethods<T = any, D = Partial<T>, P = Params> {
25
- find (params?: P): Promise<T | T[]>;
24
+ find(params?: P): Promise<T | T[]>
26
25
 
27
- get (id: Id, params?: P): Promise<T>;
26
+ get(id: Id, params?: P): Promise<T>
28
27
 
29
- create (data: D, params?: P): Promise<T>;
28
+ create(data: D, params?: P): Promise<T>
30
29
 
31
- update (id: NullableId, data: D, params?: P): Promise<T | T[]>;
30
+ update(id: NullableId, data: D, params?: P): Promise<T | T[]>
32
31
 
33
- patch (id: NullableId, data: D, params?: P): Promise<T | T[]>;
32
+ patch(id: NullableId, data: D, params?: P): Promise<T | T[]>
34
33
 
35
- remove (id: NullableId, params?: P): Promise<T | T[]>;
34
+ remove(id: NullableId, params?: P): Promise<T | T[]>
36
35
 
37
- setup? (app: Application, path: string): Promise<void>;
36
+ setup?(app: Application, path: string): Promise<void>
38
37
 
39
- teardown? (app: Application, path: string): Promise<void>;
38
+ teardown?(app: Application, path: string): Promise<void>
40
39
  }
41
40
 
42
41
  export interface ServiceOverloads<T = any, D = Partial<T>, P = Params> {
43
- create? (data: D[], params?: P): Promise<T[]>;
42
+ create?(data: D[], params?: P): Promise<T[]>
44
43
 
45
- update? (id: Id, data: D, params?: P): Promise<T>;
44
+ update?(id: Id, data: D, params?: P): Promise<T>
46
45
 
47
- update? (id: null, data: D, params?: P): Promise<T[]>;
46
+ update?(id: null, data: D, params?: P): Promise<T[]>
48
47
 
49
- patch? (id: Id, data: D, params?: P): Promise<T>;
48
+ patch?(id: Id, data: D, params?: P): Promise<T>
50
49
 
51
- patch? (id: null, data: D, params?: P): Promise<T[]>;
50
+ patch?(id: null, data: D, params?: P): Promise<T[]>
52
51
 
53
- remove? (id: Id, params?: P): Promise<T>;
52
+ remove?(id: Id, params?: P): Promise<T>
54
53
 
55
- remove? (id: null, params?: P): Promise<T[]>;
54
+ remove?(id: null, params?: P): Promise<T[]>
56
55
  }
57
56
 
58
- export type Service<T = any, D = Partial<T>, P = Params> =
59
- ServiceMethods<T, D, P> &
60
- ServiceOverloads<T, D, P>;
57
+ export type Service<T = any, D = Partial<T>, P = Params> = ServiceMethods<T, D, P> & ServiceOverloads<T, D, P>
61
58
 
62
- export type ServiceInterface<T = any, D = Partial<T>, P = Params> =
63
- Partial<ServiceMethods<T, D, P>>;
59
+ export type ServiceInterface<T = any, D = Partial<T>, P = Params> = Partial<ServiceMethods<T, D, P>>
64
60
 
65
61
  export interface ServiceAddons<A = Application, S = Service> extends EventEmitter {
66
- id?: string;
67
- hooks (options: HookOptions<A, S>): this;
62
+ id?: string
63
+ hooks(options: HookOptions<A, S>): this
68
64
  }
69
65
 
70
66
  export interface ServiceHookOverloads<S, P = Params> {
71
- find (
72
- params: P,
73
- context: HookContext
74
- ): Promise<HookContext>;
67
+ find(params: P, context: HookContext): Promise<HookContext>
75
68
 
76
- get (
77
- id: Id,
78
- params: P,
79
- context: HookContext
80
- ): Promise<HookContext>;
69
+ get(id: Id, params: P, context: HookContext): Promise<HookContext>
81
70
 
82
- create (
71
+ create(
83
72
  data: ServiceGenericData<S> | ServiceGenericData<S>[],
84
73
  params: P,
85
74
  context: HookContext
86
- ): Promise<HookContext>;
75
+ ): Promise<HookContext>
87
76
 
88
- update (
89
- id: NullableId,
90
- data: ServiceGenericData<S>,
91
- params: P,
92
- context: HookContext
93
- ): Promise<HookContext>;
77
+ update(id: NullableId, data: ServiceGenericData<S>, params: P, context: HookContext): Promise<HookContext>
94
78
 
95
- patch (
96
- id: NullableId,
97
- data: ServiceGenericData<S>,
98
- params: P,
99
- context: HookContext
100
- ): Promise<HookContext>;
79
+ patch(id: NullableId, data: ServiceGenericData<S>, params: P, context: HookContext): Promise<HookContext>
101
80
 
102
- remove (
103
- id: NullableId,
104
- params: P,
105
- context: HookContext
106
- ): Promise<HookContext>;
81
+ remove(id: NullableId, params: P, context: HookContext): Promise<HookContext>
107
82
  }
108
83
 
109
- export type FeathersService<A = FeathersApplication, S = Service> =
110
- S & ServiceAddons<A, S> & OptionalPick<ServiceHookOverloads<S>, keyof S>;
84
+ export type FeathersService<A = FeathersApplication, S = Service> = S &
85
+ ServiceAddons<A, S> &
86
+ OptionalPick<ServiceHookOverloads<S>, keyof S>
111
87
 
112
- export type CustomMethods<T extends {[key: string]: [any, any]}> = {
113
- [K in keyof T]: (data: T[K][0], params?: Params) => Promise<T[K][1]>;
88
+ export type CustomMethods<T extends { [key: string]: [any, any] }> = {
89
+ [K in keyof T]: (data: T[K][0], params?: Params) => Promise<T[K][1]>
114
90
  }
115
91
 
116
- export type ServiceMixin<A> = (service: FeathersService<A>, path: string, options: ServiceOptions) => void;
92
+ export type CustomMethod<T = any, R = T, P extends Params = Params> = (data: T, params?: P) => Promise<R>
117
93
 
118
- export type ServiceGenericType<S> = S extends ServiceInterface<infer T> ? T : any;
119
- export type ServiceGenericData<S> = S extends ServiceInterface<infer _T, infer D> ? D : any;
120
- export type ServiceGenericParams<S> = S extends ServiceInterface<infer _T, infer _D, infer P> ? P : any;
94
+ export type ServiceMixin<A> = (service: FeathersService<A>, path: string, options: ServiceOptions) => void
95
+
96
+ export type ServiceGenericType<S> = S extends ServiceInterface<infer T> ? T : any
97
+ export type ServiceGenericData<S> = S extends ServiceInterface<infer _T, infer D> ? D : any
98
+ export type ServiceGenericParams<S> = S extends ServiceInterface<infer _T, infer _D, infer P> ? P : any
121
99
 
122
100
  export interface FeathersApplication<Services = any, Settings = any> {
123
101
  /**
124
102
  * The Feathers application version
125
103
  */
126
- version: string;
104
+ version: string
127
105
 
128
106
  /**
129
107
  * A list of callbacks that run when a new service is registered
130
108
  */
131
- mixins: ServiceMixin<Application<Services, Settings>>[];
109
+ mixins: ServiceMixin<Application<Services, Settings>>[]
132
110
 
133
111
  /**
134
112
  * The index of all services keyed by their path.
@@ -136,30 +114,25 @@ export interface FeathersApplication<Services = any, Settings = any> {
136
114
  * __Important:__ Services should always be retrieved via `app.service('name')`
137
115
  * not via `app.services`.
138
116
  */
139
- services: Services;
117
+ services: Services
140
118
 
141
119
  /**
142
120
  * The application settings that can be used via
143
121
  * `app.get` and `app.set`
144
122
  */
145
- settings: Settings;
123
+ settings: Settings
146
124
 
147
125
  /**
148
126
  * A private-ish indicator if `app.setup()` has been called already
149
127
  */
150
- _isSetup: boolean;
151
-
152
- /**
153
- * Contains all registered application level hooks.
154
- */
155
- appHooks: HookMap<Application<Services, Settings>, any>;
128
+ _isSetup: boolean
156
129
 
157
130
  /**
158
131
  * Retrieve an application setting by name
159
132
  *
160
133
  * @param name The setting name
161
134
  */
162
- get<L extends keyof Settings & string> (name: L): Settings[L];
135
+ get<L extends keyof Settings & string>(name: L): Settings[L]
163
136
 
164
137
  /**
165
138
  * Set an application setting
@@ -167,14 +140,14 @@ export interface FeathersApplication<Services = any, Settings = any> {
167
140
  * @param name The setting name
168
141
  * @param value The setting value
169
142
  */
170
- set<L extends keyof Settings & string> (name: L, value: Settings[L]): this;
143
+ set<L extends keyof Settings & string>(name: L, value: Settings[L]): this
171
144
 
172
145
  /**
173
146
  * Runs a callback configure function with the current application instance.
174
147
  *
175
148
  * @param callback The callback `(app: Application) => {}` to run
176
149
  */
177
- configure (callback: (this: this, app: this) => void): this;
150
+ configure(callback: (this: this, app: this) => void): this
178
151
 
179
152
  /**
180
153
  * Returns a fallback service instance that will be registered
@@ -183,7 +156,7 @@ export interface FeathersApplication<Services = any, Settings = any> {
183
156
  *
184
157
  * @param location The path of the service
185
158
  */
186
- defaultService (location: string): ServiceInterface;
159
+ defaultService(location: string): ServiceInterface
187
160
 
188
161
  /**
189
162
  * Register a new service or a sub-app. When passed another
@@ -195,11 +168,11 @@ export interface FeathersApplication<Services = any, Settings = any> {
195
168
  * Feathers application to use a sub-app under the `path` prefix.
196
169
  * @param options The options for this service
197
170
  */
198
- use<L extends keyof Services & string> (
171
+ use<L extends keyof Services & string>(
199
172
  path: L,
200
173
  service: keyof any extends keyof Services ? ServiceInterface | Application : Services[L],
201
174
  options?: ServiceOptions
202
- ): this;
175
+ ): this
203
176
 
204
177
  /**
205
178
  * Get the Feathers service instance for a path. This will
@@ -208,65 +181,65 @@ export interface FeathersApplication<Services = any, Settings = any> {
208
181
  *
209
182
  * @param path The name of the service.
210
183
  */
211
- service<L extends keyof Services & string> (
184
+ service<L extends keyof Services & string>(
212
185
  path: L
213
- ): FeathersService<this, keyof any extends keyof Services ? Service : Services[L]>;
186
+ ): FeathersService<this, keyof any extends keyof Services ? Service : Services[L]>
214
187
 
215
188
  /**
216
189
  * Set up the application and call all services `.setup` method if available.
217
190
  *
218
191
  * @param server A server instance (optional)
219
192
  */
220
- setup (server?: any): Promise<this>;
193
+ setup(server?: any): Promise<this>
221
194
 
222
195
  /**
223
196
  * Tear down the application and call all services `.teardown` method if available.
224
197
  *
225
198
  * @param server A server instance (optional)
226
199
  */
227
- teardown (server?: any): Promise<this>;
200
+ teardown(server?: any): Promise<this>
228
201
 
229
202
  /**
230
203
  * Register application level hooks.
231
204
  *
232
205
  * @param map The application hook settings.
233
206
  */
234
- hooks (map: ApplicationHookOptions<this>): this;
207
+ hooks(map: ApplicationHookOptions<this>): this
235
208
  }
236
209
 
237
210
  // This needs to be an interface instead of a type
238
211
  // so that the declaration can be extended by other modules
239
- export interface Application<Services = any, Settings = any> extends FeathersApplication<Services, Settings>, EventEmitter {
240
-
241
- }
212
+ export interface Application<Services = any, Settings = any>
213
+ extends FeathersApplication<Services, Settings>,
214
+ EventEmitter {}
242
215
 
243
- export type Id = number | string;
244
- export type NullableId = Id | null;
216
+ export type Id = number | string
217
+ export type NullableId = Id | null
245
218
 
246
219
  export interface Query {
247
- [key: string]: any;
220
+ [key: string]: any
248
221
  }
249
222
 
250
223
  export interface Params<Q = Query> {
251
- query?: Q;
252
- provider?: string;
253
- route?: { [key: string]: any };
254
- headers?: { [key: string]: any };
224
+ query?: Q
225
+ provider?: string
226
+ route?: { [key: string]: any }
227
+ headers?: { [key: string]: any }
255
228
  }
256
229
 
257
230
  export interface Http {
258
231
  /**
259
232
  * A writeable, optional property with status code override.
260
233
  */
261
- status?: number;
234
+ status?: number
262
235
  /**
263
236
  * A writeable, optional property with headers.
264
237
  */
265
- headers?: { [key: string]: string | string[] };
238
+ headers?: { [key: string]: string | string[] }
266
239
  /**
267
240
  * A writeable, optional property with `Location` header's value.
268
241
  */
269
- location?: string;
242
+ location?: string
270
243
  }
271
244
 
272
245
  export interface HookContext<A = Application, S = any> extends BaseHookContext<ServiceGenericType<S>> {
@@ -274,52 +247,52 @@ export interface HookContext<A = Application, S = any> extends BaseHookContext<S
274
247
  * A read only property that contains the Feathers application object. This can be used to
275
248
  * retrieve other services (via context.app.service('name')) or configuration values.
276
249
  */
277
- readonly app: A;
250
+ readonly app: A
278
251
  /**
279
252
  * A read only property with the name of the service method (one of find, get,
280
253
  * create, update, patch, remove).
281
254
  */
282
- readonly method: string;
255
+ readonly method: string
283
256
  /**
284
257
  * A read only property and contains the service name (or path) without leading or
285
258
  * trailing slashes.
286
259
  */
287
- readonly path: string;
260
+ readonly path: string
288
261
  /**
289
262
  * A read only property and contains the service this hook currently runs on.
290
263
  */
291
- readonly service: S;
264
+ readonly service: S
292
265
  /**
293
266
  * A read only property with the hook type (one of before, after or error).
294
267
  * Will be `null` for asynchronous hooks.
295
268
  */
296
- readonly type: null | 'before' | 'after' | 'error';
269
+ readonly type: null | 'before' | 'after' | 'error'
297
270
  /**
298
271
  * The list of method arguments. Should not be modified, modify the
299
272
  * `params`, `data` and `id` properties instead.
300
273
  */
301
- readonly arguments: any[];
274
+ readonly arguments: any[]
302
275
  /**
303
276
  * A writeable property containing the data of a create, update and patch service
304
277
  * method call.
305
278
  */
306
- data?: ServiceGenericData<S>;
279
+ data?: ServiceGenericData<S>
307
280
  /**
308
281
  * A writeable property with the error object that was thrown in a failed method call.
309
282
  * It is only available in error hooks.
310
283
  */
311
- error?: any;
284
+ error?: any
312
285
  /**
313
286
  * A writeable property and the id for a get, remove, update and patch service
314
287
  * method call. For remove, update and patch context.id can also be null when
315
288
  * modifying multiple entries. In all other cases it will be undefined.
316
289
  */
317
- id?: Id;
290
+ id?: Id
318
291
  /**
319
292
  * A writeable property that contains the service method parameters (including
320
293
  * params.query).
321
294
  */
322
- params: ServiceGenericParams<S>;
295
+ params: ServiceGenericParams<S>
323
296
  /**
324
297
  * A writeable property containing the result of the successful service method call.
325
298
  * It is only available in after hooks.
@@ -329,70 +302,75 @@ export interface HookContext<A = Application, S = any> extends BaseHookContext<S
329
302
  * - A before hook to skip the actual service method (database) call
330
303
  * - An error hook to swallow the error and return a result instead
331
304
  */
332
- result?: ServiceGenericType<S>;
305
+ result?: ServiceGenericType<S>
333
306
  /**
334
307
  * A writeable, optional property and contains a 'safe' version of the data that
335
308
  * should be sent to any client. If context.dispatch has not been set context.result
336
309
  * will be sent to the client instead.
337
310
  */
338
- dispatch?: ServiceGenericType<S>;
311
+ dispatch?: ServiceGenericType<S>
339
312
  /**
340
313
  * A writeable, optional property that allows to override the standard HTTP status
341
314
  * code that should be returned.
342
315
  *
343
316
  * @deprecated Use `http.status` instead.
344
317
  */
345
- statusCode?: number;
318
+ statusCode?: number
346
319
  /**
347
320
  * A writeable, optional property with options specific to HTTP transports.
348
321
  */
349
- http?: Http;
322
+ http?: Http
350
323
  /**
351
324
  * The event emitted by this method. Can be set to `null` to skip event emitting.
352
325
  */
353
- event: string|null;
326
+ event: string | null
354
327
  }
355
328
 
356
329
  // Regular hook typings
357
- export type RegularHookFunction<A = Application, S = Service> =
358
- (this: S, context: HookContext<A, S>) => (Promise<HookContext<Application, S> | void> | HookContext<Application, S> | void);
359
-
360
- export type Hook<A = Application, S = Service> = RegularHookFunction<A, S>;
330
+ export type HookFunction<A = Application, S = Service> = (
331
+ this: S,
332
+ context: HookContext<A, S>
333
+ ) => Promise<HookContext<Application, S> | void> | HookContext<Application, S> | void
361
334
 
362
- type RegularHookMethodMap<A, S> =
363
- { [L in keyof S]?: SelfOrArray<RegularHookFunction<A, S>>; } &
364
- { all?: SelfOrArray<RegularHookFunction<A, S>> };
335
+ export type Hook<A = Application, S = Service> = HookFunction<A, S>
365
336
 
366
- type RegularHookTypeMap<A, S> =
367
- SelfOrArray<RegularHookFunction<A, S>> | RegularHookMethodMap<A, S>;
337
+ type HookMethodMap<A, S> = {
338
+ [L in keyof S]?: SelfOrArray<HookFunction<A, S>>
339
+ } & { all?: SelfOrArray<HookFunction<A, S>> }
368
340
 
369
- export type RegularHookMap<A, S> = {
370
- before?: RegularHookTypeMap<A, S>,
371
- after?: RegularHookTypeMap<A, S>,
372
- error?: RegularHookTypeMap<A, S>
373
- }
341
+ type HookTypeMap<A, S> = SelfOrArray<HookFunction<A, S>> | HookMethodMap<A, S>
374
342
 
375
343
  // New @feathersjs/hook typings
376
- export type HookFunction<A = Application, S = Service> =
377
- (context: HookContext<A, S>, next: NextFunction) => Promise<void>;
344
+ export type AroundHookFunction<A = Application, S = Service> = (
345
+ context: HookContext<A, S>,
346
+ next: NextFunction
347
+ ) => Promise<void>
348
+
349
+ export type AroundHookMap<A, S> = {
350
+ [L in keyof S]?: AroundHookFunction<A, S>[]
351
+ } & { all?: AroundHookFunction<A, S>[] }
378
352
 
379
353
  export type HookMap<A, S> = {
380
- [L in keyof S]?: HookFunction<A, S>[];
381
- };
354
+ around?: AroundHookMap<A, S>
355
+ before?: HookTypeMap<A, S>
356
+ after?: HookTypeMap<A, S>
357
+ error?: HookTypeMap<A, S>
358
+ }
382
359
 
383
- export type HookOptions<A, S> =
384
- HookMap<A, S> | HookFunction<A, S>[] | RegularHookMap<A, S>;
360
+ export type HookOptions<A, S> = AroundHookMap<A, S> | AroundHookFunction<A, S>[] | HookMap<A, S>
385
361
 
386
362
  export interface ApplicationHookContext<A = Application> extends BaseHookContext {
387
- app: A;
388
- server: any;
363
+ app: A
364
+ server: any
389
365
  }
390
366
 
391
- export type ApplicationHookFunction<A> =
392
- (context: ApplicationHookContext<A>, next: NextFunction) => Promise<void>;
367
+ export type ApplicationHookFunction<A> = (
368
+ context: ApplicationHookContext<A>,
369
+ next: NextFunction
370
+ ) => Promise<void>
393
371
 
394
372
  export type ApplicationHookMap<A> = {
395
- setup?: ApplicationHookFunction<A>[],
373
+ setup?: ApplicationHookFunction<A>[]
396
374
  teardown?: ApplicationHookFunction<A>[]
397
375
  }
398
376
 
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
  }