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