@feathersjs/feathers 5.0.0-pre.9 → 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.
- package/CHANGELOG.md +258 -233
- package/LICENSE +1 -1
- package/{readme.md → README.md} +7 -8
- package/lib/application.d.ts +20 -15
- package/lib/application.js +83 -44
- package/lib/application.js.map +1 -1
- package/lib/declarations.d.ts +193 -78
- package/lib/events.d.ts +2 -2
- package/lib/events.js +5 -6
- package/lib/events.js.map +1 -1
- package/lib/hooks.d.ts +42 -0
- package/lib/hooks.js +176 -0
- package/lib/hooks.js.map +1 -0
- package/lib/index.d.ts +2 -2
- package/lib/index.js +8 -4
- package/lib/index.js.map +1 -1
- package/lib/service.d.ts +3 -1
- package/lib/service.js +27 -26
- package/lib/service.js.map +1 -1
- package/lib/version.d.ts +1 -1
- package/lib/version.js +1 -1
- package/lib/version.js.map +1 -1
- package/package.json +17 -16
- package/src/application.ts +162 -101
- package/src/declarations.ts +281 -147
- package/src/events.ts +15 -15
- package/src/hooks.ts +234 -0
- package/src/index.ts +13 -12
- package/src/service.ts +38 -50
- package/src/version.ts +1 -1
- package/lib/dependencies.d.ts +0 -4
- package/lib/dependencies.js +0 -18
- package/lib/dependencies.js.map +0 -1
- package/lib/hooks/index.d.ts +0 -14
- package/lib/hooks/index.js +0 -84
- package/lib/hooks/index.js.map +0 -1
- package/lib/hooks/legacy.d.ts +0 -7
- package/lib/hooks/legacy.js +0 -114
- package/lib/hooks/legacy.js.map +0 -1
- package/src/dependencies.ts +0 -5
- package/src/hooks/index.ts +0 -109
- package/src/hooks/legacy.ts +0 -138
package/src/declarations.ts
CHANGED
|
@@ -1,130 +1,207 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
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
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
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
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
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
|
-
|
|
51
|
+
get(id: Id, params?: P): Promise<Result>
|
|
29
52
|
|
|
30
|
-
|
|
53
|
+
create(data: Data[], params?: P): Promise<Result[]>
|
|
54
|
+
create(data: Data, params?: P): Promise<Result>
|
|
31
55
|
|
|
32
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
40
|
-
|
|
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
|
-
|
|
77
|
+
get(id: Id, params?: ServiceParams): Promise<Result>
|
|
43
78
|
|
|
44
|
-
|
|
79
|
+
create(data: Data, params?: ServiceParams): Promise<Result>
|
|
45
80
|
|
|
46
|
-
|
|
81
|
+
update(id: NullableId, data: Data, params?: ServiceParams): Promise<Result | Result[]>
|
|
47
82
|
|
|
48
|
-
patch
|
|
83
|
+
patch(id: NullableId, data: PatchData, params?: ServiceParams): Promise<Result | Result[]>
|
|
49
84
|
|
|
50
|
-
remove
|
|
85
|
+
remove(id: NullableId, params?: ServiceParams): Promise<Result | Result[]>
|
|
51
86
|
|
|
52
|
-
|
|
87
|
+
setup?(app: Application, path: string): Promise<void>
|
|
88
|
+
|
|
89
|
+
teardown?(app: Application, path: string): Promise<void>
|
|
53
90
|
}
|
|
54
91
|
|
|
55
|
-
export
|
|
56
|
-
|
|
57
|
-
|
|
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
|
-
|
|
60
|
-
Partial<ServiceMethods<T, D>>;
|
|
106
|
+
patch?(id: null, data: PatchData, params?: ServiceParams): Promise<Result[]>
|
|
61
107
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
108
|
+
remove?(id: Id, params?: ServiceParams): Promise<Result>
|
|
109
|
+
|
|
110
|
+
remove?(id: null, params?: ServiceParams): Promise<Result[]>
|
|
65
111
|
}
|
|
66
112
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
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
|
-
|
|
74
|
-
|
|
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
|
-
|
|
142
|
+
get(id: Id, params: P, context: HookContext): Promise<HookContext>
|
|
143
|
+
|
|
144
|
+
create(
|
|
80
145
|
data: ServiceGenericData<S> | ServiceGenericData<S>[],
|
|
81
|
-
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
|
|
107
|
-
|
|
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
|
-
|
|
110
|
-
|
|
177
|
+
/**
|
|
178
|
+
* A real-time connection object
|
|
179
|
+
*/
|
|
180
|
+
export interface RealTimeConnection {
|
|
181
|
+
[key: string]: any
|
|
111
182
|
}
|
|
112
183
|
|
|
113
|
-
|
|
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
|
|
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
|
|
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<
|
|
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:
|
|
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:
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
266
|
+
use<L extends keyof Services & string>(
|
|
195
267
|
path: L,
|
|
196
|
-
service: keyof any extends keyof
|
|
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
|
|
288
|
+
service<L extends keyof Services & string>(
|
|
208
289
|
path: L
|
|
209
|
-
): FeathersService<this, keyof any extends keyof
|
|
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
|
-
|
|
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
|
|
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<
|
|
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
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
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:
|
|
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:
|
|
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
|
-
//
|
|
321
|
-
export type
|
|
322
|
-
|
|
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
|
|
440
|
+
export type Hook<A = Application, S = Service> = HookFunction<A, S>
|
|
325
441
|
|
|
326
|
-
type
|
|
327
|
-
|
|
328
|
-
|
|
442
|
+
type HookMethodMap<A, S> = {
|
|
443
|
+
[L in keyof S]?: SelfOrArray<HookFunction<A, S>>
|
|
444
|
+
} & { all?: SelfOrArray<HookFunction<A, S>> }
|
|
329
445
|
|
|
330
|
-
type
|
|
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
|
|
341
|
-
|
|
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
|
-
|
|
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,
|
|
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 {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
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
|
|
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 : [
|
|
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>
|
|
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
|
}
|