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 |
|
7 | export type { NextFunction }
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | export interface Paginated<T> {
|
13 | total: number
|
14 | limit: number
|
15 | skip: number
|
16 | data: T[]
|
17 | }
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | export interface ServiceOptions<MethodTypes = string> {
|
23 | |
24 |
|
25 |
|
26 | events?: string[] | readonly string[]
|
27 | |
28 |
|
29 |
|
30 | methods?: MethodTypes[] | readonly MethodTypes[]
|
31 | |
32 |
|
33 |
|
34 |
|
35 | serviceEvents?: string[] | readonly string[]
|
36 | |
37 |
|
38 |
|
39 | routeParams?: { [key: string]: any }
|
40 | }
|
41 |
|
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>
|
50 |
|
51 | get(id: Id, params?: P): Promise<Result>
|
52 |
|
53 | create(data: Data[], params?: P): Promise<Result[]>
|
54 | create(data: Data, params?: P): Promise<Result>
|
55 |
|
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[]>
|
59 |
|
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[]>
|
63 |
|
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[]>
|
67 | }
|
68 |
|
69 | export interface ServiceMethods<
|
70 | Result = any,
|
71 | Data = Partial<Result>,
|
72 | ServiceParams = Params,
|
73 | PatchData = Partial<Data>
|
74 | > {
|
75 | find(params?: ServiceParams & { paginate?: PaginationParams }): Promise<Result | Result[]>
|
76 |
|
77 | get(id: Id, params?: ServiceParams): Promise<Result>
|
78 |
|
79 | create(data: Data, params?: ServiceParams): Promise<Result>
|
80 |
|
81 | update(id: NullableId, data: Data, params?: ServiceParams): Promise<Result | Result[]>
|
82 |
|
83 | patch(id: NullableId, data: PatchData, params?: ServiceParams): Promise<Result | Result[]>
|
84 |
|
85 | remove(id: NullableId, params?: ServiceParams): Promise<Result | Result[]>
|
86 |
|
87 | setup?(app: Application, path: string): Promise<void>
|
88 |
|
89 | teardown?(app: Application, path: string): Promise<void>
|
90 | }
|
91 |
|
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>
|
105 |
|
106 | patch?(id: null, data: PatchData, params?: ServiceParams): Promise<Result[]>
|
107 |
|
108 | remove?(id: Id, params?: ServiceParams): Promise<Result>
|
109 |
|
110 | remove?(id: null, params?: ServiceParams): Promise<Result[]>
|
111 | }
|
112 |
|
113 |
|
114 |
|
115 |
|
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 |
|
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 | }
|
138 |
|
139 | export interface ServiceHookOverloads<S, P = Params> {
|
140 | find(params: P & { paginate?: PaginationParams }, context: HookContext): Promise<HookContext>
|
141 |
|
142 | get(id: Id, params: P, context: HookContext): Promise<HookContext>
|
143 |
|
144 | create(
|
145 | data: ServiceGenericData<S> | ServiceGenericData<S>[],
|
146 | params: P,
|
147 | context: HookContext
|
148 | ): Promise<HookContext>
|
149 |
|
150 | update(id: NullableId, data: ServiceGenericData<S>, params: P, context: HookContext): Promise<HookContext>
|
151 |
|
152 | patch(id: NullableId, data: ServiceGenericData<S>, params: P, context: HookContext): Promise<HookContext>
|
153 |
|
154 | remove(id: NullableId, params: P, context: HookContext): Promise<HookContext>
|
155 | }
|
156 |
|
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 |
|
167 |
|
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 | }
|
176 |
|
177 |
|
178 |
|
179 |
|
180 | export interface RealTimeConnection {
|
181 | [key: string]: any
|
182 | }
|
183 |
|
184 |
|
185 |
|
186 |
|
187 | export type CustomMethod<T = any, R = T, P extends Params = Params> = (data: T, params?: P) => Promise<R>
|
188 |
|
189 | export type ServiceMixin<A> = (service: FeathersService<A>, path: string, options: ServiceOptions) => void
|
190 |
|
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> {
|
196 | |
197 |
|
198 |
|
199 | version: string
|
200 |
|
201 | |
202 |
|
203 |
|
204 | mixins: ServiceMixin<Application<Services, Settings>>[]
|
205 |
|
206 | |
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 | services: Services
|
213 |
|
214 | |
215 |
|
216 |
|
217 |
|
218 | settings: Settings
|
219 |
|
220 | |
221 |
|
222 |
|
223 | _isSetup: boolean
|
224 |
|
225 | |
226 |
|
227 |
|
228 |
|
229 |
|
230 | get<L extends keyof Settings & string>(name: L): Settings[L]
|
231 |
|
232 | |
233 |
|
234 |
|
235 |
|
236 |
|
237 |
|
238 | set<L extends keyof Settings & string>(name: L, value: Settings[L]): this
|
239 |
|
240 | |
241 |
|
242 |
|
243 |
|
244 |
|
245 | configure(callback: (this: this, app: this) => void): this
|
246 |
|
247 | |
248 |
|
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 | defaultService(location: string): ServiceInterface
|
255 |
|
256 | |
257 |
|
258 |
|
259 |
|
260 |
|
261 |
|
262 |
|
263 |
|
264 |
|
265 |
|
266 | use<L extends keyof Services & string>(
|
267 | path: L,
|
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 |
|
274 |
|
275 |
|
276 |
|
277 | unuse<L extends keyof Services & string>(
|
278 | path: L
|
279 | ): Promise<FeathersService<this, keyof any extends keyof Services ? Service : Services[L]>>
|
280 |
|
281 | |
282 |
|
283 |
|
284 |
|
285 |
|
286 |
|
287 |
|
288 | service<L extends keyof Services & string>(
|
289 | path: L
|
290 | ): FeathersService<this, keyof any extends keyof Services ? Service : Services[L]>
|
291 |
|
292 | |
293 |
|
294 |
|
295 |
|
296 |
|
297 | setup(server?: any): Promise<this>
|
298 |
|
299 | |
300 |
|
301 |
|
302 |
|
303 |
|
304 | teardown(server?: any): Promise<this>
|
305 |
|
306 | |
307 |
|
308 |
|
309 |
|
310 |
|
311 | hooks(map: ApplicationHookOptions<this>): this
|
312 | }
|
313 |
|
314 |
|
315 |
|
316 | export interface Application<Services = any, Settings = any>
|
317 | extends FeathersApplication<Services, Settings>,
|
318 | EventEmitter {}
|
319 |
|
320 | export type Id = number | string
|
321 | export type NullableId = Id | null
|
322 |
|
323 | export interface Query {
|
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 }
|
332 | }
|
333 |
|
334 | export interface PaginationOptions {
|
335 | default?: number
|
336 | max?: number
|
337 | }
|
338 |
|
339 | export type PaginationParams = false | PaginationOptions
|
340 |
|
341 | export interface Http {
|
342 | |
343 |
|
344 |
|
345 | status?: number
|
346 | |
347 |
|
348 |
|
349 | headers?: { [key: string]: string | string[] }
|
350 | |
351 |
|
352 |
|
353 | location?: string
|
354 | }
|
355 |
|
356 | export type HookType = 'before' | 'after' | 'error' | 'around'
|
357 |
|
358 | export interface HookContext<A = Application, S = any> extends BaseHookContext<ServiceGenericType<S>> {
|
359 | |
360 |
|
361 |
|
362 |
|
363 | readonly app: A
|
364 | |
365 |
|
366 |
|
367 |
|
368 | readonly method: string
|
369 | |
370 |
|
371 |
|
372 |
|
373 | readonly path: string
|
374 | |
375 |
|
376 |
|
377 | readonly service: S
|
378 | |
379 |
|
380 |
|
381 | readonly type: HookType
|
382 | |
383 |
|
384 |
|
385 |
|
386 | readonly arguments: any[]
|
387 | |
388 |
|
389 |
|
390 |
|
391 | data?: ServiceGenericData<S>
|
392 | |
393 |
|
394 |
|
395 |
|
396 | error?: any
|
397 | |
398 |
|
399 |
|
400 |
|
401 |
|
402 | id?: Id
|
403 | |
404 |
|
405 |
|
406 |
|
407 | params: ServiceGenericParams<S>
|
408 | |
409 |
|
410 |
|
411 |
|
412 |
|
413 |
|
414 |
|
415 |
|
416 |
|
417 | result?: ServiceGenericType<S>
|
418 | |
419 |
|
420 |
|
421 |
|
422 |
|
423 | dispatch?: ServiceGenericType<S>
|
424 | |
425 |
|
426 |
|
427 |
|
428 |
|
429 |
|
430 | statusCode?: number
|
431 | |
432 |
|
433 |
|
434 | http?: Http
|
435 | |
436 |
|
437 |
|
438 | event: string | null
|
439 | }
|
440 |
|
441 |
|
442 | export type HookFunction<A = Application, S = Service> = (
|
443 | this: S,
|
444 | context: HookContext<A, S>
|
445 | ) => Promise<HookContext<Application, S> | void> | HookContext<Application, S> | void
|
446 |
|
447 | export type Hook<A = Application, S = Service> = HookFunction<A, S>
|
448 |
|
449 | type HookMethodMap<A, S> = {
|
450 | [L in keyof S]?: SelfOrArray<HookFunction<A, S>>
|
451 | } & { all?: SelfOrArray<HookFunction<A, S>> }
|
452 |
|
453 | type HookTypeMap<A, S> = SelfOrArray<HookFunction<A, S>> | HookMethodMap<A, S>
|
454 |
|
455 |
|
456 | export type AroundHookFunction<A = Application, S = Service> = (
|
457 | context: HookContext<A, S>,
|
458 | next: NextFunction
|
459 | ) => Promise<void>
|
460 |
|
461 | export type AroundHookMap<A, S> = {
|
462 | [L in keyof S]?: AroundHookFunction<A, S>[]
|
463 | } & { all?: AroundHookFunction<A, S>[] }
|
464 |
|
465 | export type HookMap<A, S> = {
|
466 | around?: AroundHookMap<A, S>
|
467 | before?: HookTypeMap<A, S>
|
468 | after?: HookTypeMap<A, S>
|
469 | error?: HookTypeMap<A, S>
|
470 | }
|
471 |
|
472 | export type HookOptions<A, S> = AroundHookMap<A, S> | AroundHookFunction<A, S>[] | HookMap<A, S>
|
473 |
|
474 | export interface ApplicationHookContext<A = Application> extends BaseHookContext {
|
475 | app: A
|
476 | server: any
|
477 | }
|
478 |
|
479 | export type ApplicationHookFunction<A> = (
|
480 | context: ApplicationHookContext<A>,
|
481 | next: NextFunction
|
482 | ) => Promise<void>
|
483 |
|
484 | export type ApplicationHookMap<A> = {
|
485 | setup?: ApplicationHookFunction<A>[]
|
486 | teardown?: ApplicationHookFunction<A>[]
|
487 | }
|
488 |
|
489 | export type ApplicationHookOptions<A> = HookOptions<A, any> | ApplicationHookMap<A>
|