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