forked from DefinitelyTyped/DefinitelyTyped
-
Notifications
You must be signed in to change notification settings - Fork 0
/
js-data.d.ts
350 lines (301 loc) · 17 KB
/
js-data.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
// Type definitions for JSData v2.8.0
// Project: https://github.com/js-data/js-data
// Definitions by: Stefan Steinhart <https://github.com/reppners>
// Definitions: https://github.com/borisyankov/DefinitelyTyped
///////////////////////////////////////////////////////////////////////////////
// js-data module (js-data.js)
///////////////////////////////////////////////////////////////////////////////
declare module JSData {
interface JSDataPromise<R> {
then<U>(onFulfilled?:(value:R) => U | JSDataPromise<U>, onRejected?:(error:any) => U | JSDataPromise<U>): JSDataPromise<U>;
catch<U>(onRejected?:(error:any) => U | JSDataPromise<U>): JSDataPromise<U>;
// enhanced with finally
finally<U>(finallyCb?:() => U):JSDataPromise<U>;
}
interface DSConfiguration extends IDSResourceLifecycleEventHandlers {
actions?: Object;
allowSimpleWhere?: boolean;
basePath?: string;
bypassCache?: boolean;
cacheResponse?: boolean;
clearEmptyQueries?:boolean;
debug?:boolean;
defaultAdapter?: string;
defaultFilter?: (collection:Array<any>, resourceName:string, params:DSFilterArg, options:DSConfiguration)=>Array<any>;
defaultValues?:Object;
eagerEject?: boolean;
endpoint?: string;
error?: boolean | ((message?:any, ...optionalParams:any[])=> void);
fallbackAdapters?: Array<string>;
findAllFallbackAdapters?: Array<string>;
findAllStrategy?: string;
findFallbackAdapters?: Array<string>;
findStrategy?: string
findStrictCache?:boolean;
idAttribute?: string;
ignoredChanges?: Array<RegExp | string>;
ignoreMissing?: boolean;
instanceEvents?:boolean;
keepChangeHistory?: boolean;
linkRelations?:boolean;
log?: boolean | ((message?:any, ...optionalParams:any[])=> void);
maxAge?: number;
notify?: boolean;
omit?:Array<string|RegExp>;
onConflict?:string; // "merge"(default) or "replace"
reapAction?: string;
reapInterval?: number;
relationsEnumerable?:boolean;
resetHistoryOnInject?: boolean;
returnMeta?:boolean;
scopes?:Object;
strategy?: string;
upsert?: boolean;
useClass?: boolean;
useFilter?: boolean;
watchChanges?:boolean;
}
interface DSResourceDefinitionConfiguration extends DSConfiguration {
computed?: any;
meta?:any;
methods?: any;
name: string;
relations?: {
hasMany?: Object;
hasOne?: Object;
belongsTo?: Object;
};
}
interface DSFilterParams {
where?: Object;
limit?: number;
skip?: number;
offset?: number;
orderBy?: string | Array<string> | Array<Array<string>>;
sort?: string | Array<string> | Array<Array<string>>;
}
type DSFilterArg = DSFilterParams | Object;
interface DSAdapterOperationConfiguration extends DSConfiguration {
adapter?: string;
params?: {
[paramName: string]: string | number | boolean;
};
}
interface DSSaveConfiguration extends DSAdapterOperationConfiguration {
changesOnly?: boolean;
}
interface DSCollection<T> extends Array<T> {
fetch(params?:DSFilterArg, options?:DSConfiguration):JSDataPromise<Array<T & DSInstanceShorthands<T>>>;
params:DSFilterArg;
resourceName:string;
}
interface DSEvents {
on(name:string, handler:(...args:any[])=>void):void;
off(name:string, handler:(...args:any[])=>void):void;
emit(name:string, ...args:any[]):void;
}
interface DS extends DSEvents {
new(config?:DSConfiguration):DS;
// rather undocumented
errors:DSErrors;
// those are objects containing the defined resources and adapters
definitions:any;
adapters:any;
defaults:DSConfiguration;
changeHistory(resourceName:string, id:string | number):Array<Object>;
changes(resourceName:string, id:string | number, options?:{ignoredChanges:Array<string|RegExp>}):Object;
clear<T>():Array<T & DSInstanceShorthands<T>>;
compute<T>(resourceName:string, idOrInstance:number | string | T):T & DSInstanceShorthands<T>;
create<T>(resourceName:string, attrs:Object, options?:DSConfiguration):JSDataPromise<T & DSInstanceShorthands<T>>;
createCollection<T>(resourceName:string, array?:Array<T>, params?:DSFilterArg, options?:DSConfiguration):DSCollection<T & DSInstanceShorthands<T>>;
createInstance<T>(resourceName:string, attrs?:Object, options?:DSConfiguration):T & DSInstanceShorthands<T>;
destroy(resourceName:string, id:string | number, options?:DSAdapterOperationConfiguration):JSDataPromise<void>;
destroyAll(resourceName:string, params?:DSFilterArg, options?:DSAdapterOperationConfiguration):JSDataPromise<void>;
digest():void;
eject<T>(resourceName:string, id:string | number, options?:DSConfiguration):T & DSInstanceShorthands<T>;
ejectAll<T>(resourceName:string, params:DSFilterArg, options?:DSConfiguration):Array<T & DSInstanceShorthands<T>>;
filter<T>(resourceName:string, params:DSFilterArg, options?:DSConfiguration):Array<T & DSInstanceShorthands<T>>;
find<T>(resourceName:string, id:string | number, options?:DSAdapterOperationConfiguration):JSDataPromise<T & DSInstanceShorthands<T>>;
findAll<T>(resourceName:string, params?:DSFilterArg, options?:DSAdapterOperationConfiguration):JSDataPromise<Array<T & DSInstanceShorthands<T>>>;
get<T>(resourceName:string, id:string | number):T & DSInstanceShorthands<T>;
getAll<T>(resourceName:string, ids?:Array<string | number>):Array<T & DSInstanceShorthands<T>>;
hasChanges(resourceName:string, id:string | number):boolean;
inject<TInject, U>(resourceName:string, attrs:TInject, options?:DSConfiguration):U & DSInstanceShorthands<U>;
inject<TInject, U>(resourceName:string, items:Array<TInject>, options?:DSConfiguration):Array<U & DSInstanceShorthands<U>>;
is(resourceName:string, object:Object): boolean;
lastModified(resourceName:string, id?:string | number):number; // timestamp
lastSaved(resourceName:string, id?:string | number):number; // timestamp
loadRelations<T>(resourceName:string, idOrInstance:string | number, relations:string | Array<string>, options?:DSAdapterOperationConfiguration):JSDataPromise<T & DSInstanceShorthands<T>>;
previous<T>(resourceName:string, id:string | number):T & DSInstanceShorthands<T>;
reap(resourceName:string):JSDataPromise<void>;
refresh<T>(resourceName:string, id:string | number, options?:DSAdapterOperationConfiguration):JSDataPromise<T & DSInstanceShorthands<T>>;
refreshAll<T>(resourceName:string, params?:DSFilterArg, options?:DSAdapterOperationConfiguration):JSDataPromise<Array<T & DSInstanceShorthands<T>>>;
revert<T>(resourceName:string, id:string | number):T & DSInstanceShorthands<T>;
save<T>(resourceName:string, id:string | number, options?:DSSaveConfiguration):JSDataPromise<T & DSInstanceShorthands<T>>;
update<T>(resourceName:string, id:string | number, attrs:Object, options?:DSAdapterOperationConfiguration):JSDataPromise<T & DSInstanceShorthands<T>>;
updateAll<T>(resourceName:string, attrs:Object, params?:DSFilterArg, options?:DSAdapterOperationConfiguration):JSDataPromise<Array<T & DSInstanceShorthands<T>>>;
defineResource<T>(resourceNameOrDefinition:string | DSResourceDefinitionConfiguration):DSResourceDefinition<T>;
defineResource<T, TActions>(resourceNameOrDefinition:string | DSResourceDefinitionConfiguration):DSResourceDefinition<T> & TActions;
registerAdapter(adapterId:string, adapter:IDSAdapter, options?:{default: boolean}):void;
}
interface DSResourceDefinition<T> extends DSResourceDefinitionConfiguration, DSEvents {
changeHistory(id:string | number):Array<Object>;
changes(id:string | number, options?:{ignoredChanges:Array<string|RegExp>}):Object;
clear():Array<T & DSInstanceShorthands<T>>;
compute(idOrInstance:number | string | T):T & DSInstanceShorthands<T>;
create(attrs:Object, options?:DSConfiguration):JSDataPromise<T & DSInstanceShorthands<T>>;
createCollection(array?:Array<T>, params?:DSFilterArg, options?:DSConfiguration):DSCollection<T & DSInstanceShorthands<T>>;
createInstance(attrs?:Object, options?:DSConfiguration):T & DSInstanceShorthands<T>;
destroy(id:string | number, options?:DSAdapterOperationConfiguration):JSDataPromise<void>;
destroyAll(params?:DSFilterArg, options?:DSAdapterOperationConfiguration):JSDataPromise<void>;
digest():void;
eject(id:string | number, options?:DSConfiguration):T & DSInstanceShorthands<T>;
ejectAll(params:DSFilterArg, options?:DSConfiguration):Array<T & DSInstanceShorthands<T>>;
filter(params:DSFilterArg, options?:DSConfiguration):Array<T & DSInstanceShorthands<T>>;
find(id:string | number, options?:DSAdapterOperationConfiguration):JSDataPromise<T & DSInstanceShorthands<T>>;
findAll(params?:DSFilterArg, options?:DSAdapterOperationConfiguration):JSDataPromise<Array<T & DSInstanceShorthands<T>>>;
get(id:string | number):T & DSInstanceShorthands<T>;
getAll(ids?:Array<string | number>):Array<T & DSInstanceShorthands<T>>;
hasChanges(id:string | number):boolean;
inject<TInject>(attrs:TInject, options?:DSConfiguration):T & DSInstanceShorthands<T>;
inject<TInject>(items:Array<TInject>, options?:DSConfiguration):Array<T & DSInstanceShorthands<T>>;
is(object:Object): boolean;
lastModified(id?:string | number):number; // timestamp
lastSaved(id?:string | number):number; // timestamp
loadRelations(idOrInstance:string | number, relations:string | Array<string>, options?:DSAdapterOperationConfiguration):JSDataPromise<T & DSInstanceShorthands<T>>;
previous(id:string | number):T & DSInstanceShorthands<T>;
reap():JSDataPromise<void>;
refresh(id:string | number, options?:DSAdapterOperationConfiguration):JSDataPromise<T & DSInstanceShorthands<T>>;
refreshAll(params?:DSFilterArg, options?:DSAdapterOperationConfiguration):JSDataPromise<Array<T & DSInstanceShorthands<T>>>;
revert(id:string | number):T & DSInstanceShorthands<T>;
save(id:string | number, options?:DSSaveConfiguration):JSDataPromise<T & DSInstanceShorthands<T>>;
update(id:string | number, attrs:Object, options?:DSAdapterOperationConfiguration):JSDataPromise<T & DSInstanceShorthands<T>>;
updateAll(attrs:Object, params?:DSFilterArg, options?:DSAdapterOperationConfiguration):JSDataPromise<Array<T & DSInstanceShorthands<T>>>;
}
// cannot specify T at interface level because the interface is used as generic constraint itself which ends up being recursive
export interface DSInstanceShorthands<T> extends DSEvents {
DSCompute():void;
DSRefresh(options?:DSAdapterOperationConfiguration):JSDataPromise<T & DSInstanceShorthands<T>>;
DSSave(options?:DSSaveConfiguration):JSDataPromise<T & DSInstanceShorthands<T>>;
DSUpdate(options?:DSAdapterOperationConfiguration):JSDataPromise<T & DSInstanceShorthands<T>>;
DSDestroy(options?:DSAdapterOperationConfiguration):JSDataPromise<void>;
DSCreate(options?:DSAdapterOperationConfiguration):JSDataPromise<T & DSInstanceShorthands<T>>;
DSLoadRelations(relations:string | Array<string>, options?:DSAdapterOperationConfiguration):JSDataPromise<T & DSInstanceShorthands<T>>;
DSChangeHistory():Array<Object>;
DSChanges():Object;
DSHasChanges():boolean;
DSLastModified():number; // timestamp
DSLastSaved():number; // timestamp
DSPrevious():T & DSInstanceShorthands<T>;
DSRevert():T & DSInstanceShorthands<T>;
}
type DSSyncLifecycleHookHandler = (resource:DSResourceDefinition<any>, data:any) => void;
type DSAsyncLifecycleHookHandler = (resource:DSResourceDefinition<any>, data:any) => JSDataPromise<any>;
type DSAsyncLifecycleHookHandlerCb = (resource:DSResourceDefinition<any>, data:any, cb:(err:Error, data:any)=>void) => void
interface IDSResourceLifecycleValidateEventHandlers {
beforeValidate?: DSAsyncLifecycleHookHandler | DSAsyncLifecycleHookHandlerCb;
validate?: DSAsyncLifecycleHookHandler | DSAsyncLifecycleHookHandlerCb;
afterValidate?: DSAsyncLifecycleHookHandler | DSAsyncLifecycleHookHandlerCb;
}
interface IDSResourceLifecycleCreateEventHandlers {
beforeCreate?: DSAsyncLifecycleHookHandler | DSAsyncLifecycleHookHandlerCb;
afterCreate?: DSAsyncLifecycleHookHandler | DSAsyncLifecycleHookHandlerCb;
}
interface IDSResourceLifecycleUpdateEventHandlers {
beforeUpdate?: DSAsyncLifecycleHookHandler | DSAsyncLifecycleHookHandlerCb;
afterUpdate?: DSAsyncLifecycleHookHandler | DSAsyncLifecycleHookHandlerCb;
}
interface IDSResourceLifecycleDestroyEventHandlers {
beforeDestroy?: DSAsyncLifecycleHookHandler | DSAsyncLifecycleHookHandlerCb;
afterDestroy?: DSAsyncLifecycleHookHandler | DSAsyncLifecycleHookHandlerCb;
}
interface IDSResourceLifecycleCreateInstanceEventHandlers {
beforeCreateInstance?: DSSyncLifecycleHookHandler;
afterCreateInstance?: DSSyncLifecycleHookHandler;
}
interface IDSResourceLifecycleInjectEventHandlers {
beforeInject?: DSSyncLifecycleHookHandler;
afterInject?: DSSyncLifecycleHookHandler;
}
interface IDSResourceLifecycleEjectEventHandlers {
beforeEject?: DSSyncLifecycleHookHandler;
afterEject?: DSSyncLifecycleHookHandler;
}
interface IDSResourceLifecycleReapEventHandlers {
beforeReap?: DSSyncLifecycleHookHandler;
afterReap?: DSSyncLifecycleHookHandler;
}
interface IDSResourceLifecycleFindEventHandlers {
afterFind?: DSAsyncLifecycleHookHandler | DSAsyncLifecycleHookHandlerCb;
}
interface IDSResourceLifecycleFindAllEventHandlers {
afterFindAll?: DSAsyncLifecycleHookHandler | DSAsyncLifecycleHookHandlerCb;
}
interface IDSResourceLifecycleLoadRelationsEventHandlers {
afterLoadRelations?: DSAsyncLifecycleHookHandler | DSAsyncLifecycleHookHandlerCb;
}
interface IDSResourceLifecycleCreateCollectionEventHandlers {
beforeCreateCollection?: DSSyncLifecycleHookHandler;
afterCreateCollection?: DSSyncLifecycleHookHandler;
}
interface IDSResourceLifecycleEventHandlers extends IDSResourceLifecycleCreateEventHandlers,
IDSResourceLifecycleCreateInstanceEventHandlers,
IDSResourceLifecycleValidateEventHandlers,
IDSResourceLifecycleUpdateEventHandlers,
IDSResourceLifecycleDestroyEventHandlers,
IDSResourceLifecycleInjectEventHandlers,
IDSResourceLifecycleEjectEventHandlers,
IDSResourceLifecycleReapEventHandlers,
IDSResourceLifecycleFindEventHandlers,
IDSResourceLifecycleFindAllEventHandlers,
IDSResourceLifecycleLoadRelationsEventHandlers,
IDSResourceLifecycleCreateCollectionEventHandlers {
}
// errors
interface DSErrors {
// types
IllegalArgumentError:DSError;
IA:DSError;
RuntimeError:DSError;
R:DSError;
NonexistentResourceError:DSError;
NER:DSError;
}
interface DSError extends Error {
new (message?:string):DSError;
message: string;
type: string;
}
// DSAdapter interface
interface IDSAdapter {
create(config:DSResourceDefinition<any>, attrs:Object, options?:DSConfiguration):JSDataPromise<any>;
destroy(config:DSResourceDefinition<any>, id:string | number, options?:DSConfiguration):JSDataPromise<void>;
destroyAll(config:DSResourceDefinition<any>, params:DSFilterArg, options?:DSConfiguration):JSDataPromise<void>;
find(config:DSResourceDefinition<any>, id:string | number, options?:DSConfiguration):JSDataPromise<any>;
findAll(config:DSResourceDefinition<any>, params?:DSFilterArg, options?:DSConfiguration):JSDataPromise<any>;
update(config:DSResourceDefinition<any>, id:string | number, attrs:Object, options?:DSConfiguration):JSDataPromise<any>;
updateAll(config:DSResourceDefinition<any>, attrs:Object, params?:DSFilterArg, options?:DSConfiguration):JSDataPromise<any>;
}
// Custom action config
interface DSActionConfig {
adapter?: string;
endpoint?: string;
pathname?: string;
method?: string;
}
// Custom action method definition
// options are passed to adapter.HTTP() method-call, js-data-http adapter by default uses AXIOS but can also be $http in case of angular
// or a custom adapter implementation. The adapter can be set via the DSActionConfig.
interface DSActionFn {
<T>(id:string | number, options?:Object):JSDataPromise<T>
}
}
// declaring the existing global js object
declare var JSData:{
DS: JSData.DS;
DSErrors: JSData.DSErrors;
};
//Support node require
declare module 'js-data' {
export = JSData;
}