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