1 | import { EventEmitter } from 'events';
|
2 | import { NextFunction, HookContext as BaseHookContext } from '@feathersjs/hooks';
|
3 | type SelfOrArray<S> = S | S[];
|
4 | type OptionalPick<T, K extends PropertyKey> = Pick<T, Extract<keyof T, K>>;
|
5 | type Entries<T> = {
|
6 | [K in keyof T]: [K, T[K]];
|
7 | }[keyof T][];
|
8 | type GetKeyByValue<Obj, Value> = Extract<Entries<Obj>[number], [PropertyKey, Value]>[0];
|
9 | export type { NextFunction };
|
10 |
|
11 |
|
12 |
|
13 | export interface Paginated<T> {
|
14 | total: number;
|
15 | limit: number;
|
16 | skip: number;
|
17 | data: T[];
|
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?: {
|
40 | [key: string]: any;
|
41 | };
|
42 | }
|
43 | export interface ClientService<Result = any, Data = Partial<Result>, PatchData = Data, FindResult = Paginated<Result>, P = Params> {
|
44 | find(params?: P): Promise<FindResult>;
|
45 | get(id: Id, params?: P): Promise<Result>;
|
46 | create(data: Data[], params?: P): Promise<Result[]>;
|
47 | create(data: Data, params?: P): Promise<Result>;
|
48 | update(id: Id, data: Data, params?: P): Promise<Result>;
|
49 | update(id: NullableId, data: Data, params?: P): Promise<Result | Result[]>;
|
50 | update(id: null, data: Data, params?: P): Promise<Result[]>;
|
51 | patch(id: NullableId, data: PatchData, params?: P): Promise<Result | Result[]>;
|
52 | patch(id: Id, data: PatchData, params?: P): Promise<Result>;
|
53 | patch(id: null, data: PatchData, params?: P): Promise<Result[]>;
|
54 | remove(id: NullableId, params?: P): Promise<Result | Result[]>;
|
55 | remove(id: Id, params?: P): Promise<Result>;
|
56 | remove(id: null, params?: P): Promise<Result[]>;
|
57 | }
|
58 | export interface ServiceMethods<Result = any, Data = Partial<Result>, ServiceParams = Params, PatchData = Partial<Data>> {
|
59 | find(params?: ServiceParams & {
|
60 | paginate?: PaginationParams;
|
61 | }): Promise<Result | Result[]>;
|
62 | get(id: Id, params?: ServiceParams): Promise<Result>;
|
63 | create(data: Data, params?: ServiceParams): Promise<Result>;
|
64 | update(id: NullableId, data: Data, params?: ServiceParams): Promise<Result | Result[]>;
|
65 | patch(id: NullableId, data: PatchData, params?: ServiceParams): Promise<Result | Result[]>;
|
66 | remove(id: NullableId, params?: ServiceParams): Promise<Result | Result[]>;
|
67 | setup?(app: Application, path: string): Promise<void>;
|
68 | teardown?(app: Application, path: string): Promise<void>;
|
69 | }
|
70 | export interface ServiceOverloads<Result = any, Data = Partial<Result>, ServiceParams = Params, PatchData = Partial<Data>> {
|
71 | create?(data: Data[], params?: ServiceParams): Promise<Result[]>;
|
72 | update?(id: Id, data: Data, params?: ServiceParams): Promise<Result>;
|
73 | update?(id: null, data: Data, params?: ServiceParams): Promise<Result[]>;
|
74 | patch?(id: Id, data: PatchData, params?: ServiceParams): Promise<Result>;
|
75 | patch?(id: null, data: PatchData, params?: ServiceParams): Promise<Result[]>;
|
76 | remove?(id: Id, params?: ServiceParams): Promise<Result>;
|
77 | remove?(id: null, params?: ServiceParams): Promise<Result[]>;
|
78 | }
|
79 |
|
80 |
|
81 |
|
82 |
|
83 | export type Service<Result = any, Data = Partial<Result>, ServiceParams = Params, PatchData = Partial<Data>> = ServiceMethods<Result, Data, ServiceParams> & ServiceOverloads<Result, Data, ServiceParams, PatchData>;
|
84 |
|
85 |
|
86 |
|
87 | export type ServiceInterface<Result = any, Data = Partial<Result>, ServiceParams = Params, PatchData = Partial<Data>> = Partial<ServiceMethods<Result, Data, ServiceParams, PatchData>>;
|
88 | export interface ServiceAddons<A = Application, S = Service> extends EventEmitter {
|
89 | id?: string;
|
90 | hooks(options: HookOptions<A, S>): this;
|
91 | }
|
92 | export interface ServiceHookOverloads<S, P = Params> {
|
93 | find(params: P & {
|
94 | paginate?: PaginationParams;
|
95 | }, context: HookContext): Promise<HookContext>;
|
96 | get(id: Id, params: P, context: HookContext): Promise<HookContext>;
|
97 | create(data: ServiceGenericData<S> | ServiceGenericData<S>[], params: P, context: HookContext): Promise<HookContext>;
|
98 | update(id: NullableId, data: ServiceGenericData<S>, params: P, context: HookContext): Promise<HookContext>;
|
99 | patch(id: NullableId, data: ServiceGenericData<S>, params: P, context: HookContext): Promise<HookContext>;
|
100 | remove(id: NullableId, params: P, context: HookContext): Promise<HookContext>;
|
101 | }
|
102 | export type FeathersService<A = FeathersApplication, S = Service> = S & ServiceAddons<A, S> & OptionalPick<ServiceHookOverloads<S>, keyof S>;
|
103 | export type CustomMethods<T extends {
|
104 | [key: string]: [any, any];
|
105 | }> = {
|
106 | [K in keyof T]: (data: T[K][0], params?: Params) => Promise<T[K][1]>;
|
107 | };
|
108 |
|
109 |
|
110 |
|
111 |
|
112 | export type TransportConnection<Services = any> = {
|
113 | (app: Application<Services>): void;
|
114 | Service: any;
|
115 | service: <L extends keyof Services & string>(name: L) => keyof any extends keyof Services ? ServiceInterface : Services[L];
|
116 | };
|
117 |
|
118 |
|
119 |
|
120 | export interface RealTimeConnection {
|
121 | [key: string]: any;
|
122 | }
|
123 |
|
124 |
|
125 |
|
126 | export type CustomMethod<T = any, R = T, P extends Params = Params> = (data: T, params?: P) => Promise<R>;
|
127 | export type ServiceMixin<A> = (service: FeathersService<A>, path: string, options: ServiceOptions) => void;
|
128 | export type ServiceGenericType<S> = S extends ServiceInterface<infer T> ? T : any;
|
129 | export type ServiceGenericData<S> = S extends ServiceInterface<infer _T, infer D> ? D : any;
|
130 | export type ServiceGenericParams<S> = S extends ServiceInterface<infer _T, infer _D, infer P> ? P : any;
|
131 | export interface FeathersApplication<Services = any, Settings = any> {
|
132 | |
133 |
|
134 |
|
135 | version: string;
|
136 | |
137 |
|
138 |
|
139 | mixins: ServiceMixin<Application<Services, Settings>>[];
|
140 | |
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 | services: Services;
|
147 | |
148 |
|
149 |
|
150 |
|
151 | settings: Settings;
|
152 | |
153 |
|
154 |
|
155 | _isSetup: boolean;
|
156 | |
157 |
|
158 |
|
159 |
|
160 |
|
161 | get<L extends keyof Settings & string>(name: L): Settings[L];
|
162 | |
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 | set<L extends keyof Settings & string>(name: L, value: Settings[L]): this;
|
169 | |
170 |
|
171 |
|
172 |
|
173 |
|
174 | configure(callback: (this: this, app: this) => void): this;
|
175 | |
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 | defaultService(location: string): ServiceInterface;
|
183 | |
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 | use<L extends keyof Services & string>(path: L, service: keyof any extends keyof Services ? ServiceInterface | Application : Services[L], options?: ServiceOptions<keyof any extends keyof Services ? string : keyof Services[L]>): this;
|
194 | |
195 |
|
196 |
|
197 |
|
198 |
|
199 | unuse<L extends keyof Services & string>(path: L): Promise<FeathersService<this, keyof any extends keyof Services ? Service : Services[L]>>;
|
200 | |
201 |
|
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 | service<L extends keyof Services & string>(path: L): FeathersService<this, keyof any extends keyof Services ? Service : Services[L]>;
|
208 | |
209 |
|
210 |
|
211 |
|
212 |
|
213 | setup(server?: any): Promise<this>;
|
214 | |
215 |
|
216 |
|
217 |
|
218 |
|
219 | teardown(server?: any): Promise<this>;
|
220 | |
221 |
|
222 |
|
223 |
|
224 |
|
225 | hooks(map: ApplicationHookOptions<this>): this;
|
226 | }
|
227 | export interface Application<Services = any, Settings = any> extends FeathersApplication<Services, Settings>, EventEmitter {
|
228 | }
|
229 | export type Id = number | string;
|
230 | export type NullableId = Id | null;
|
231 | export interface Query {
|
232 | [key: string]: any;
|
233 | }
|
234 | export interface Params<Q = Query> {
|
235 | query?: Q;
|
236 | provider?: string;
|
237 | route?: {
|
238 | [key: string]: any;
|
239 | };
|
240 | headers?: {
|
241 | [key: string]: any;
|
242 | };
|
243 | }
|
244 | export interface PaginationOptions {
|
245 | default?: number;
|
246 | max?: number;
|
247 | }
|
248 | export type PaginationParams = false | PaginationOptions;
|
249 | export interface Http {
|
250 | |
251 |
|
252 |
|
253 | status?: number;
|
254 | |
255 |
|
256 |
|
257 | headers?: {
|
258 | [key: string]: string | string[];
|
259 | };
|
260 | |
261 |
|
262 |
|
263 | location?: string;
|
264 | }
|
265 | export type HookType = 'before' | 'after' | 'error' | 'around';
|
266 | type Serv<FA> = FA extends Application<infer S> ? S : never;
|
267 | export interface HookContext<A = Application, S = any> extends BaseHookContext<ServiceGenericType<S>> {
|
268 | |
269 |
|
270 |
|
271 |
|
272 | readonly app: A;
|
273 | |
274 |
|
275 |
|
276 |
|
277 | readonly method: string;
|
278 | |
279 |
|
280 |
|
281 |
|
282 | path: 0 extends 1 & S ? keyof Serv<A> & string : GetKeyByValue<Serv<A>, S> & string;
|
283 | |
284 |
|
285 |
|
286 | readonly service: S;
|
287 | |
288 |
|
289 |
|
290 | readonly type: HookType;
|
291 | |
292 |
|
293 |
|
294 |
|
295 | readonly arguments: any[];
|
296 | |
297 |
|
298 |
|
299 |
|
300 | data?: ServiceGenericData<S>;
|
301 | |
302 |
|
303 |
|
304 |
|
305 | error?: any;
|
306 | |
307 |
|
308 |
|
309 |
|
310 |
|
311 | id?: Id;
|
312 | |
313 |
|
314 |
|
315 |
|
316 | params: ServiceGenericParams<S>;
|
317 | |
318 |
|
319 |
|
320 |
|
321 |
|
322 |
|
323 |
|
324 |
|
325 |
|
326 | result?: ServiceGenericType<S>;
|
327 | |
328 |
|
329 |
|
330 |
|
331 |
|
332 | dispatch?: ServiceGenericType<S>;
|
333 | |
334 |
|
335 |
|
336 |
|
337 |
|
338 |
|
339 | statusCode?: number;
|
340 | |
341 |
|
342 |
|
343 | http?: Http;
|
344 | |
345 |
|
346 |
|
347 | event: string | null;
|
348 | }
|
349 | export type HookFunction<A = Application, S = Service> = (this: S, context: HookContext<A, S>) => Promise<HookContext<Application, S> | void> | HookContext<Application, S> | void;
|
350 | export type Hook<A = Application, S = Service> = HookFunction<A, S>;
|
351 | type HookMethodMap<A, S> = {
|
352 | [L in keyof S]?: SelfOrArray<HookFunction<A, S>>;
|
353 | } & {
|
354 | all?: SelfOrArray<HookFunction<A, S>>;
|
355 | };
|
356 | type HookTypeMap<A, S> = SelfOrArray<HookFunction<A, S>> | HookMethodMap<A, S>;
|
357 | export type AroundHookFunction<A = Application, S = Service> = (context: HookContext<A, S>, next: NextFunction) => Promise<void>;
|
358 | export type AroundHookMap<A, S> = {
|
359 | [L in keyof S]?: AroundHookFunction<A, S>[];
|
360 | } & {
|
361 | all?: AroundHookFunction<A, S>[];
|
362 | };
|
363 | export type HookMap<A, S> = {
|
364 | around?: AroundHookMap<A, S>;
|
365 | before?: HookTypeMap<A, S>;
|
366 | after?: HookTypeMap<A, S>;
|
367 | error?: HookTypeMap<A, S>;
|
368 | };
|
369 | export type HookOptions<A, S> = AroundHookMap<A, S> | AroundHookFunction<A, S>[] | HookMap<A, S>;
|
370 | export interface ApplicationHookContext<A = Application> extends BaseHookContext {
|
371 | app: A;
|
372 | server: any;
|
373 | }
|
374 | export type ApplicationHookFunction<A> = (context: ApplicationHookContext<A>, next: NextFunction) => Promise<void>;
|
375 | export type ApplicationHookMap<A> = {
|
376 | setup?: ApplicationHookFunction<A>[];
|
377 | teardown?: ApplicationHookFunction<A>[];
|
378 | };
|
379 | export type ApplicationHookOptions<A> = HookOptions<A, any> | ApplicationHookMap<A>;
|