UNPKG

6.99 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 interface Params {
35 query?: Query;
36 paginate?: false | Pick<PaginationOptions, 'max'>;
37 provider?: string;
38 route?: {[key: string]: string};
39 headers?: {[key: string]: any};
40
41 [key: string]: any; // (JL) not sure if we want this
42 }
43
44 interface Paginated<T> {
45 total: number;
46 limit: number;
47 skip: number;
48 data: T[];
49 }
50
51 // tslint:disable-next-line void-return
52 type Hook<T = any, S = Service<T>> = (hook: HookContext<T, S>) => (Promise<HookContext<T, S> | void> | HookContext<T, S> | void);
53
54 interface HookContext<T = any, S = Service<T>> {
55 /**
56 * A read only property that contains the Feathers application object. This can be used to
57 * retrieve other services (via context.app.service('name')) or configuration values.
58 */
59 readonly app: Application;
60 /**
61 * A writeable property containing the data of a create, update and patch service
62 * method call.
63 */
64 data?: T;
65 /**
66 * A writeable property with the error object that was thrown in a failed method call.
67 * It is only available in error hooks.
68 */
69 error?: any;
70 /**
71 * A writeable property and the id for a get, remove, update and patch service
72 * method call. For remove, update and patch context.id can also be null when
73 * modifying multiple entries. In all other cases it will be undefined.
74 */
75 id?: string | number;
76 /**
77 * A read only property with the name of the service method (one of find, get,
78 * create, update, patch, remove).
79 */
80 readonly method: string;
81 /**
82 * A writeable property that contains the service method parameters (including
83 * params.query).
84 */
85 params: Params;
86 /**
87 * A read only property and contains the service name (or path) without leading or
88 * trailing slashes.
89 */
90 readonly path: string;
91 /**
92 * A writeable property containing the result of the successful service method call.
93 * It is only available in after hooks.
94 *
95 * `context.result` can also be set in
96 *
97 * - A before hook to skip the actual service method (database) call
98 * - An error hook to swallow the error and return a result instead
99 */
100 result?: T;
101 /**
102 * A read only property and contains the service this hook currently runs on.
103 */
104 readonly service: S;
105 /**
106 * A writeable, optional property and contains a 'safe' version of the data that
107 * should be sent to any client. If context.dispatch has not been set context.result
108 * will be sent to the client instead.
109 */
110 dispatch?: T;
111 /**
112 * A writeable, optional property that allows to override the standard HTTP status
113 * code that should be returned.
114 */
115 statusCode?: number;
116 /**
117 * A read only property with the hook type (one of before, after or error).
118 */
119 readonly type: 'before' | 'after' | 'error';
120 }
121
122 interface HookMap {
123 all: Hook | Hook[];
124 find: Hook | Hook[];
125 get: Hook | Hook[];
126 create: Hook | Hook[];
127 update: Hook | Hook[];
128 patch: Hook | Hook[];
129 remove: Hook | Hook[];
130 }
131
132 interface HooksObject {
133 before: Partial<HookMap> | Hook | Hook[];
134 after: Partial<HookMap> | Hook | Hook[];
135 error: Partial<HookMap> | Hook | Hook[];
136 finally?: Partial<HookMap> | Hook | Hook[];
137 }
138
139 interface ServiceMethods<T> {
140 [key: string]: any;
141
142 find (params?: Params): Promise<T | T[] | Paginated<T>>;
143
144 get (id: Id, params?: Params): Promise<T>;
145
146 create (data: Partial<T> | Array<Partial<T>>, params?: Params): Promise<T | T[]>;
147
148 update (id: NullableId, data: T, params?: Params): Promise<T | T[]>;
149
150 patch (id: NullableId, data: Partial<T>, params?: Params): Promise<T | T[]>;
151
152 remove (id: NullableId, params?: Params): Promise<T | T[]>;
153 }
154
155 interface SetupMethod {
156 setup (app: Application, path: string): void;
157 }
158
159 interface ServiceOverloads<T> {
160 create? (data: Partial<T>, params?: Params): Promise<T>;
161
162 create? (data: Array<Partial<T>>, params?: Params): Promise<T[]>;
163
164 update? (id: Id, data: T, params?: Params): Promise<T>;
165
166 update? (id: null, data: T, params?: Params): Promise<T[]>;
167
168 patch? (id: Id, data: Partial<T>, params?: Params): Promise<T>;
169
170 patch? (id: null, data: Partial<T>, params?: Params): Promise<T[]>;
171
172 remove? (id: Id, params?: Params): Promise<T>;
173
174 remove? (id: null, params?: Params): Promise<T[]>;
175 }
176
177 interface ServiceAddons<T> extends EventEmitter {
178 id?: any;
179 _serviceEvents: string[];
180 methods: {[method: string]: string[]};
181 hooks (hooks: Partial<HooksObject>): this;
182 }
183
184 type Service<T> = ServiceOverloads<T> & ServiceAddons<T> & ServiceMethods<T>;
185
186 type ServiceMixin = (service: Service<any>, path: string) => void;
187
188 interface Application<ServiceTypes = {}> extends EventEmitter {
189 version: string;
190
191 services: keyof ServiceTypes extends never ? any : ServiceTypes;
192
193 mixins: ServiceMixin[];
194
195 methods: string[];
196
197 get (name: string): any;
198
199 set (name: string, value: any): this;
200
201 disable (name: string): this;
202
203 disabled (name: string): boolean;
204
205 enable (name: string): this;
206
207 enabled (name: string): boolean;
208
209 configure (callback: (this: this, app: this) => void): this;
210
211 hooks (hooks: Partial<HooksObject>): this;
212
213 setup (server?: any): this;
214
215 service<L extends keyof ServiceTypes> (location: L): ServiceTypes[L];
216
217 service (location: string): keyof ServiceTypes extends never ? any : never;
218
219 use (path: string, service: Partial<ServiceMethods<any> & SetupMethod> | Application, options?: any): this;
220
221 listen (port: number): http.Server;
222 }
223}