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 |
|
15 |
|
16 | export interface Paginated<T> {
|
17 | total: number
|
18 | limit: number
|
19 | skip: number
|
20 | data: T[]
|
21 | }
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | export interface ServiceOptions<MethodTypes = string> {
|
27 | |
28 |
|
29 |
|
30 | events?: string[] | readonly string[]
|
31 | |
32 |
|
33 |
|
34 | methods?: MethodTypes[] | readonly MethodTypes[]
|
35 | |
36 |
|
37 |
|
38 |
|
39 | serviceEvents?: string[] | readonly string[]
|
40 | |
41 |
|
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 |
|
119 |
|
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 |
|
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 |
|
171 |
|
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 |
|
183 |
|
184 | export interface RealTimeConnection {
|
185 | [key: string]: any
|
186 | }
|
187 |
|
188 |
|
189 |
|
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 |
|
202 |
|
203 | version: string
|
204 |
|
205 | |
206 |
|
207 |
|
208 | mixins: ServiceMixin<Application<Services, Settings>>[]
|
209 |
|
210 | |
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 | services: Services
|
217 |
|
218 | |
219 |
|
220 |
|
221 |
|
222 | settings: Settings
|
223 |
|
224 | |
225 |
|
226 |
|
227 | _isSetup: boolean
|
228 |
|
229 | |
230 |
|
231 |
|
232 |
|
233 |
|
234 | get<L extends keyof Settings & string>(name: L): Settings[L]
|
235 |
|
236 | |
237 |
|
238 |
|
239 |
|
240 |
|
241 |
|
242 | set<L extends keyof Settings & string>(name: L, value: Settings[L]): this
|
243 |
|
244 | |
245 |
|
246 |
|
247 |
|
248 |
|
249 | configure(callback: (this: this, app: this) => void): this
|
250 |
|
251 | |
252 |
|
253 |
|
254 |
|
255 |
|
256 |
|
257 |
|
258 | defaultService(location: string): ServiceInterface
|
259 |
|
260 | |
261 |
|
262 |
|
263 |
|
264 |
|
265 |
|
266 |
|
267 |
|
268 |
|
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 |
|
278 |
|
279 |
|
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 |
|
287 |
|
288 |
|
289 |
|
290 |
|
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 |
|
298 |
|
299 |
|
300 |
|
301 | setup(server?: any): Promise<this>
|
302 |
|
303 | |
304 |
|
305 |
|
306 |
|
307 |
|
308 | teardown(server?: any): Promise<this>
|
309 |
|
310 | |
311 |
|
312 |
|
313 |
|
314 |
|
315 | hooks(map: ApplicationHookOptions<this>): this
|
316 | }
|
317 |
|
318 |
|
319 |
|
320 | export interface Application<Services = any, Settings = any>
|
321 | extends FeathersApplication<Services, Settings>,
|
322 | EventEmitter {}
|
323 |
|
324 | export type Id = number | string
|
325 | export type NullableId = Id | null
|
326 |
|
327 | export interface Query {
|
328 | [key: string]: any
|
329 | }
|
330 |
|
331 | export interface Params<Q = Query> {
|
332 | query?: Q
|
333 | provider?: string
|
334 | route?: { [key: string]: any }
|
335 | headers?: { [key: string]: any }
|
336 | }
|
337 |
|
338 | export interface PaginationOptions {
|
339 | default?: number
|
340 | max?: number
|
341 | }
|
342 |
|
343 | export type PaginationParams = false | PaginationOptions
|
344 |
|
345 | export interface Http {
|
346 | |
347 |
|
348 |
|
349 | status?: number
|
350 | |
351 |
|
352 |
|
353 | headers?: { [key: string]: string | string[] }
|
354 | |
355 |
|
356 |
|
357 | location?: string
|
358 | }
|
359 |
|
360 | export type HookType = 'before' | 'after' | 'error' | 'around'
|
361 |
|
362 | type Serv<FA> = FA extends Application<infer S> ? S : never
|
363 |
|
364 | export interface HookContext<A = Application, S = any> extends BaseHookContext<ServiceGenericType<S>> {
|
365 | |
366 |
|
367 |
|
368 |
|
369 | readonly app: A
|
370 | |
371 |
|
372 |
|
373 |
|
374 | readonly method: string
|
375 | |
376 |
|
377 |
|
378 |
|
379 | path: 0 extends 1 & S ? keyof Serv<A> & string : GetKeyByValue<Serv<A>, S> & string
|
380 | |
381 |
|
382 |
|
383 | readonly service: S
|
384 | |
385 |
|
386 |
|
387 | readonly type: HookType
|
388 | |
389 |
|
390 |
|
391 |
|
392 | readonly arguments: any[]
|
393 | |
394 |
|
395 |
|
396 |
|
397 | data?: ServiceGenericData<S>
|
398 | |
399 |
|
400 |
|
401 |
|
402 | error?: any
|
403 | |
404 |
|
405 |
|
406 |
|
407 |
|
408 | id?: Id
|
409 | |
410 |
|
411 |
|
412 |
|
413 | params: ServiceGenericParams<S>
|
414 | |
415 |
|
416 |
|
417 |
|
418 |
|
419 |
|
420 |
|
421 |
|
422 |
|
423 | result?: ServiceGenericType<S>
|
424 | |
425 |
|
426 |
|
427 |
|
428 |
|
429 | dispatch?: ServiceGenericType<S>
|
430 | |
431 |
|
432 |
|
433 |
|
434 |
|
435 |
|
436 | statusCode?: number
|
437 | |
438 |
|
439 |
|
440 | http?: Http
|
441 | |
442 |
|
443 |
|
444 | event: string | null
|
445 | }
|
446 |
|
447 |
|
448 | export type HookFunction<A = Application, S = Service> = (
|
449 | this: S,
|
450 | context: HookContext<A, S>
|
451 | ) => Promise<HookContext<Application, S> | void> | HookContext<Application, S> | void
|
452 |
|
453 | export type Hook<A = Application, S = Service> = HookFunction<A, S>
|
454 |
|
455 | type HookMethodMap<A, S> = {
|
456 | [L in keyof S]?: SelfOrArray<HookFunction<A, S>>
|
457 | } & { all?: SelfOrArray<HookFunction<A, S>> }
|
458 |
|
459 | type HookTypeMap<A, S> = SelfOrArray<HookFunction<A, S>> | HookMethodMap<A, S>
|
460 |
|
461 |
|
462 | export type AroundHookFunction<A = Application, S = Service> = (
|
463 | context: HookContext<A, S>,
|
464 | next: NextFunction
|
465 | ) => Promise<void>
|
466 |
|
467 | export type AroundHookMap<A, S> = {
|
468 | [L in keyof S]?: AroundHookFunction<A, S>[]
|
469 | } & { all?: AroundHookFunction<A, S>[] }
|
470 |
|
471 | export type HookMap<A, S> = {
|
472 | around?: AroundHookMap<A, S>
|
473 | before?: HookTypeMap<A, S>
|
474 | after?: HookTypeMap<A, S>
|
475 | error?: HookTypeMap<A, S>
|
476 | }
|
477 |
|
478 | export type HookOptions<A, S> = AroundHookMap<A, S> | AroundHookFunction<A, S>[] | HookMap<A, S>
|
479 |
|
480 | export interface ApplicationHookContext<A = Application> extends BaseHookContext {
|
481 | app: A
|
482 | server: any
|
483 | }
|
484 |
|
485 | export type ApplicationHookFunction<A> = (
|
486 | context: ApplicationHookContext<A>,
|
487 | next: NextFunction
|
488 | ) => Promise<void>
|
489 |
|
490 | export type ApplicationHookMap<A> = {
|
491 | setup?: ApplicationHookFunction<A>[]
|
492 | teardown?: ApplicationHookFunction<A>[]
|
493 | }
|
494 |
|
495 | export type ApplicationHookOptions<A> = HookOptions<A, any> | ApplicationHookMap<A>
|