@feathersjs/feathers 5.0.0-pre.21 → 5.0.0-pre.24

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