UNPKG

12.9 kBTypeScriptView Raw
1/// <reference types='node' />
2
3import { EventEmitter } from 'events';
4import * as http from 'http';
5
6declare const createApplication: Feathers;
7export = createApplication;
8
9interface Feathers {
10 <T = any>(): createApplication.Application<T>;
11 readonly ACTIVATE_HOOKS: unique symbol;
12 version: string;
13 default: Feathers;
14 // TODO: Write a definition for activateHooks.
15 // activateHooks(): void
16}
17
18declare namespace createApplication {
19 type Id = number | string;
20 type NullableId = Id | null;
21
22 interface Query {
23 [key: string]: any;
24 }
25
26 interface PaginationOptions {
27 default: number;
28 max: number;
29 }
30
31 type ClientSideParams = Pick<Params, 'query' | 'paginate'>;
32 type ServerSideParams = Params;
33
34 /**
35 * Service call parameters
36 *
37 * @see {@link https://docs.feathersjs.com/api/services.html#params}
38 */
39 interface Params {
40 query?: Query;
41 paginate?: false | Pick<PaginationOptions, 'max'>;
42 provider?: string;
43 route?: {[key: string]: string};
44 headers?: {[key: string]: any};
45 user?: {[key: string]: any};
46
47 [key: string]: any; // (JL) not sure if we want this
48 }
49
50 interface Paginated<T> {
51 total: number;
52 limit: number;
53 skip: number;
54 data: T[];
55 }
56
57 // tslint:disable-next-line void-return
58 type Hook<T = any, S = Service<T>> = (context: HookContext<T, S>) => (Promise<HookContext<T, S> | void> | HookContext<T, S> | void);
59
60 interface HookContext<T = any, S = Service<T>> {
61 /**
62 * A read only property that contains the Feathers application object. This can be used to
63 * retrieve other services (via context.app.service('name')) or configuration values.
64 */
65 readonly app: Application;
66 /**
67 * A writeable property containing the data of a create, update and patch service
68 * method call.
69 */
70 data?: T;
71 /**
72 * A writeable property with the error object that was thrown in a failed method call.
73 * It is only available in error hooks.
74 */
75 error?: any;
76 /**
77 * A writeable property and the id for a get, remove, update and patch service
78 * method call. For remove, update and patch context.id can also be null when
79 * modifying multiple entries. In all other cases it will be undefined.
80 */
81 id?: string | number;
82 /**
83 * A read only property with the name of the service method (one of find, get,
84 * create, update, patch, remove).
85 */
86 readonly method: 'find' | 'get' | 'create' | 'update' | 'patch' | 'remove';
87 /**
88 * A writeable property that contains the service method parameters (including
89 * params.query).
90 */
91 params: Params;
92 /**
93 * A read only property and contains the service name (or path) without leading or
94 * trailing slashes.
95 */
96 readonly path: string;
97 /**
98 * A writeable property containing the result of the successful service method call.
99 * It is only available in after hooks.
100 *
101 * `context.result` can also be set in
102 *
103 * - A before hook to skip the actual service method (database) call
104 * - An error hook to swallow the error and return a result instead
105 */
106 result?: T;
107 /**
108 * A read only property and contains the service this hook currently runs on.
109 */
110 readonly service: S;
111 /**
112 * A writeable, optional property and contains a 'safe' version of the data that
113 * should be sent to any client. If context.dispatch has not been set context.result
114 * will be sent to the client instead.
115 */
116 dispatch?: T;
117 /**
118 * A writeable, optional property that allows to override the standard HTTP status
119 * code that should be returned.
120 */
121 statusCode?: number;
122 /**
123 * A read only property with the hook type (one of before, after or error).
124 */
125 readonly type: 'before' | 'after' | 'error';
126 /**
127 * A writeable, optional property that allows service events to be skipped by
128 * setting it to `null`
129 */
130 event?: null;
131 }
132
133 interface HookMap<T = any> {
134 all: Hook<T> | Hook<T>[];
135 find: Hook<T> | Hook<T>[];
136 get: Hook<T> | Hook<T>[];
137 create: Hook<T> | Hook<T>[];
138 update: Hook<T> | Hook<T>[];
139 patch: Hook<T> | Hook<T>[];
140 remove: Hook<T> | Hook<T>[];
141 }
142
143 interface HooksObject<T = any> {
144 before: Partial<HookMap<T>> | Hook<T> | Hook<T>[];
145 after: Partial<HookMap<T>> | Hook<T> | Hook<T>[];
146 error: Partial<HookMap<T>> | Hook<T> | Hook<T>[];
147 finally?: Partial<HookMap<T>> | Hook<T> | Hook<T>[];
148 }
149
150 interface SetupMethod {
151 setup (app: Application, path: string): void;
152 }
153
154 interface ServiceMethods<T> {
155 [key: string]: any;
156
157 /**
158 * Retrieve all resources from this service.
159 *
160 * @param params - Service call parameters {@link Params}
161 * @see {@link https://docs.feathersjs.com/api/services.html#find-params|Feathers API Documentation: .find(params)}
162 */
163 find (params?: Params): Promise<T | T[] | Paginated<T>>;
164
165 /**
166 * Retrieve a single resource matching the given ID.
167 *
168 * @param id - ID of the resource to locate
169 * @param params - Service call parameters {@link Params}
170 * @see {@link https://docs.feathersjs.com/api/services.html#get-id-params|Feathers API Documentation: .get(id, params)}
171 */
172 get (id: Id, params?: Params): Promise<T>;
173
174 /**
175 * Create a new resource for this service.
176 *
177 * @param data - Data to insert into this service.
178 * @param params - Service call parameters {@link Params}
179 * @see {@link https://docs.feathersjs.com/api/services.html#create-data-params|Feathers API Documentation: .create(data, params)}
180 */
181 create (data: Partial<T> | Partial<T>[], params?: Params): Promise<T | T[]>;
182
183 /**
184 * Replace any resources matching the given ID with the given data.
185 *
186 * @param id - ID of the resource to be updated
187 * @param data - Data to be put in place of the current resource.
188 * @param params - Service call parameters {@link Params}
189 * @see {@link https://docs.feathersjs.com/api/services.html#update-id-data-params|Feathers API Documentation: .update(id, data, params)}
190 */
191 update (id: NullableId, data: T, params?: Params): Promise<T | T[]>;
192
193 /**
194 * Merge any resources matching the given ID with the given data.
195 *
196 * @param id - ID of the resource to be patched
197 * @param data - Data to merge with the current resource.
198 * @param params - Service call parameters {@link Params}
199 * @see {@link https://docs.feathersjs.com/api/services.html#patch-id-data-params|Feathers API Documentation: .patch(id, data, params)}
200 */
201 patch (id: NullableId, data: Partial<T>, params?: Params): Promise<T | T[]>;
202
203 /**
204 * Remove resources matching the given ID from the this service.
205 *
206 * @param id - ID of the resource to be removed
207 * @param params - Service call parameters {@link Params}
208 * @see {@link https://docs.feathersjs.com/api/services.html#remove-id-params|Feathers API Documentation: .remove(id, params)}
209 */
210 remove (id: NullableId, params?: Params): Promise<T | T[]>;
211 }
212
213 interface ServiceOverloads<T> {
214 /**
215 * Create a new resource for this service.
216 *
217 * @param data - Data to insert into this service.
218 * @param params - Service call parameters {@link Params}
219 * @see {@link https://docs.feathersjs.com/api/services.html#create-data-params|Feathers API Documentation: .create(data, params)}
220 */
221 create? (data: Partial<T>, params?: Params): Promise<T>;
222
223 /**
224 * Create a new resource for this service.
225 *
226 * @param data - Data to insert into this service.
227 * @param params - Service call parameters {@link Params}
228 * @see {@link https://docs.feathersjs.com/api/services.html#create-data-params|Feathers API Documentation: .create(data, params)}
229 */
230 create? (data: Partial<T>[], params?: Params): Promise<T[]>;
231
232 /**
233 * Replace any resources matching the given ID with the given data.
234 *
235 * @param id - ID of the resource to be updated
236 * @param data - Data to be put in place of the current resource.
237 * @param params - Service call parameters {@link Params}
238 * @see {@link https://docs.feathersjs.com/api/services.html#update-id-data-params|Feathers API Documentation: .update(id, data, params)}
239 */
240 update? (id: Id, data: T, params?: Params): Promise<T>;
241
242 /**
243 * Replace any resources matching the given ID with the given data.
244 *
245 * @param id - ID of the resource to be updated
246 * @param data - Data to be put in place of the current resource.
247 * @param params - Service call parameters {@link Params}
248 * @see {@link https://docs.feathersjs.com/api/services.html#update-id-data-params|Feathers API Documentation: .update(id, data, params)}
249 */
250 update? (id: null, data: T, params?: Params): Promise<T[]>;
251
252 /**
253 * Merge any resources matching the given ID with the given data.
254 *
255 * @param id - ID of the resource to be patched
256 * @param data - Data to merge with the current resource.
257 * @param params - Service call parameters {@link Params}
258 * @see {@link https://docs.feathersjs.com/api/services.html#patch-id-data-params|Feathers API Documentation: .patch(id, data, params)}
259 */
260 patch? (id: Id, data: Partial<T>, params?: Params): Promise<T>;
261
262 /**
263 * Merge any resources matching the given ID with the given data.
264 *
265 * @param id - ID of the resource to be patched
266 * @param data - Data to merge with the current resource.
267 * @param params - Service call parameters {@link Params}
268 * @see {@link https://docs.feathersjs.com/api/services.html#patch-id-data-params|Feathers API Documentation: .patch(id, data, params)}
269 */
270 patch? (id: null, data: Partial<T>, params?: Params): Promise<T[]>;
271
272 /**
273 * Remove resources matching the given ID from the this service.
274 *
275 * @param id - ID of the resource to be removed
276 * @param params - Service call parameters {@link Params}
277 * @see {@link https://docs.feathersjs.com/api/services.html#remove-id-params|Feathers API Documentation: .remove(id, params)}
278 */
279 remove? (id: Id, params?: Params): Promise<T>;
280
281 /**
282 * Remove resources matching the given ID from the this service.
283 *
284 * @param id - ID of the resource to be removed
285 * @param params - Service call parameters {@link Params}
286 * @see {@link https://docs.feathersjs.com/api/services.html#remove-id-params|Feathers API Documentation: .remove(id, params)}
287 */
288 remove? (id: null, params?: Params): Promise<T[]>;
289 }
290
291 interface ServiceAddons<T> extends EventEmitter {
292 id?: any;
293 _serviceEvents: string[];
294 methods: {[method: string]: string[]};
295 hooks (hooks: Partial<HooksObject>): this;
296 }
297
298 type Service<T> = ServiceOverloads<T> & ServiceAddons<T> & ServiceMethods<T>;
299
300 type ServiceMixin = (service: Service<any>, path: string) => void;
301
302 interface Application<ServiceTypes = {}> extends EventEmitter {
303 version: string;
304
305 services: keyof ServiceTypes extends never ? any : ServiceTypes;
306
307 mixins: ServiceMixin[];
308
309 methods: string[];
310
311 get (name: string): any;
312
313 set (name: string, value: any): this;
314
315 disable (name: string): this;
316
317 disabled (name: string): boolean;
318
319 enable (name: string): this;
320
321 enabled (name: string): boolean;
322
323 configure (callback: (this: this, app: this) => void): this;
324
325 hooks (hooks: Partial<HooksObject>): this;
326
327 setup (server?: any): this;
328
329 service<L extends keyof ServiceTypes> (location: L): ServiceTypes[L];
330
331 service (location: string): keyof ServiceTypes extends never ? any : never;
332
333 use (path: string, service: Partial<ServiceMethods<any> & SetupMethod> | Application, options?: any): this;
334
335 listen (port: number): http.Server;
336 }
337}