This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.d.ts
592 lines (542 loc) · 17 KB
/
index.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
/**
* Object which will returns as result of completed request
* Example:
* kinka.get('/data').then(response => {
* // response it is this interface
* })
* @interface KinkaResponse
* @template T
*/
export declare interface KinkaResponse<T = any> {
/**
* response data of the request.
*
* @type {((T | null | any))}
* @memberof KinkaResponse
*/
data: (T | null | any);
/**
* this property indicates that request been failed too.
* And also return error message, or bad response, or empty object.
*
* @type {((object | T | null | any))}
* @memberof KinkaResponse
*/
err: (object | T | null | any);
/**
* Map of the response headers.
*
* @type {object}
* @memberof KinkaResponse
*/
headers: object;
/**
* This property indicates that request has been failed.
*
* @type {boolean}
* @memberof KinkaResponse
*/
isError: boolean;
/**
* This property indicates that request has been completed successfully.
*
* @type {boolean}
* @memberof KinkaResponse
*/
isSuccess: boolean,
/**
* Raw response data from http request.
* Read more: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/response
*
* @type {((ArrayBuffer | Blob | Document | Object | string | any))}
* @memberof KinkaResponse
*/
response: (ArrayBuffer | Blob | Document | Object | string | any),
/**
* Status code of http request.
* Status codes list: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status
*
* @type {number}
* @memberof KinkaResponse
*/
status: number,
/**
* Status message of http request.
* Read more: https://developer.mozilla.org/en-US/docs/Web/API/Response/statusText
*
* @type {string}
* @memberof KinkaResponse
*/
statusText: string,
/**
* Original response type from http request.
* Read more: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/responseType
*
* @type {(('arraybuffer' | 'blob' | 'document' | 'json' | 'text' | 'ms-stream' | 'moz-chunked-arraybuffer'))}
* @memberof KinkaResponse
*/
type: ('arraybuffer' | 'blob' | 'document' | 'json' | 'text' | 'ms-stream' | 'moz-chunked-arraybuffer'),
}
/**
* Configuration object for your request
* All properties in this object will override instance options
* Example:
* kinka.get('/url', { baseURL: 'https://overriden-api.com' })
* @interface KinkaRequestOptions
*/
export declare interface KinkaRequestOptions{
/**
* With abortable key your request have ability to cancel last request if request with the same key is start launching
*
* @type {string}
* @memberof KinkaRequestOptions
*/
cancelToken?: string;
/**
* Sets data for the instance `auth` mixin.
* Only works if `auth` mixin is setted in instance options
*
* @type {*}
* @memberof KinkaRequestOptions
*/
auth?:any;
/**
* Sets the `baseURL` for instance.
* Allows to set base url address for server.
*
* @type {string}
* @memberof KinkaRequestOptions
*/
baseURL?: string;
/**
* Sets the request body. It is content which needed to send on server
*
* @type {*}
* @memberof KinkaRequestOptions
*/
data?: any;
/**
* Allows to set request headers for this request
*
* @type {object}
* @memberof KinkaRequestOptions
*/
headers?: object;
/**
* With {true} your responses will not be throwing exceptions and you don't need to wrap your requests in try/catch.
* And if you want to catch exception you can get this from {response.err} or {response.isError}
* Example:
* const { err, status } = await kinka.get('/bad-request')
* if(err){
* // catched exception
* }
* Another example:
* try {
* const response = await kinka.get('/bad-request', {
* omitCatches: false
* })
* } catch (e) {
* console.error(e.err)
* // catched exception
* }
*
* @type {boolean}
* @memberof KinkaRequestOptions
*/
omitCatches?: boolean;
/**
* Allows to handle progress of the request download
*
* @type {function}
* @memberof KinkaRequestOptions
*/
onDownloadProgress?: (progressEvent: ProgressEvent)=>any
/**
* Allows to handle progress of the request upload
*
* @type {function}
* @memberof KinkaRequestOptions
*/
onUploadProgress?: (progressEvent: ProgressEvent)=>any
/**
* query params for your http request
* Example:
* kinka.get('/all', {
* query: {
* disabled: true,
* sortBy: 'date'
* }})
* // request will have url {{baseURL}}/all?disabled=true&sortBy=date
*
* @type {object}
* @memberof KinkaRequestOptions
*/
query?: object;
/**
* Allows to set specific success status for your http request
* If you added this property with 201 value then all another responses
* with success status codes will be catches an exception,
* or will have fulfilled `err` property
*
* @type {number}
* @memberof KinkaRequestOptions
*/
successStatus?: number;
/**
* Sets the number of milliseconds after which request automatically will be terminated. 0 value means no timeout.
* Read more: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/timeout
*
* @type {number}
* @memberof KinkaRequestOptions
*/
timeout?: number;
/**
* Indicates that this request should use credentials (like cookies or specific auth headers)
* Sets flag {request.withCredentials}
* Read more: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/withCredentials
*
* @type {boolean}
* @memberof KinkaRequestOptions
*/
credentials?: boolean;
}
/**
* Object which sending when user creating a new instance
* @interface KinkaInstanceOptions
*/
export declare interface KinkaInstanceOptions{
/**
* Allows to attach auth mixin for requests in your kinka instance.
* It mixin will be modify your request options before sending request.
* Example:
*
* const api = kinka.create({
* baseURL: `${baseURL}/${apiPath}`,
* auth: ({ username, password }) => ({
* headers: {
* Auth: `Token ${username}:${stringToSHA256(password)}`,
* },
* }),
* })
* api.get('/data', {
* auth: { username: 'TheFlash', password: 'SpeedF0rce' },
* })
*
* @type {function}
* @memberof KinkaInstanceOptions
*/
auth?: (authData:any)=>(KinkaRequestOptions|any)
/**
* Sets the `baseURL` for instance.
* Allows to set base url address for server.
* Example:
* const api = kinka.create({ baseURL: 'https://api.com' })
* api.get('/data') //GET: https://api.com/data
*
* @type {string}
* @memberof KinkaInstanceOptions
*/
baseURL?: string;
/**
* Allows to create instance methods which will have special http methods.
*
* @type {((string[] | null))}
* @memberof KinkaInstanceOptions
*/
customMethods?: (string[] | null);
/**
* Allows to set specific headers for each request created via instance
* Example:
* const api = kinka.create({
* baseURL: 'https://api.com',
* headers: {
* 'API_VERSION': '01',
* },
* })
* api.get('/data')
* GET: https://api.com/data
* headers: {
* "API_VERSION": "01"
* }
*
* @type {object}
* @memberof KinkaInstanceOptions
*/
headers?: object;
/**
* Allows to attach inspectors to your kinka instance.
* Inspectors it is watchers for requests or responses
* which allows dynamically change request options or response data.
* If needed to change request options or response data then
* need to return modified options/response.
* Example:
* const api = kinka.create({
* baseURL: `${baseURL}/${apiPath}`,
* inspectors: {
* request: (url, method, options) => {
* console.log(`request ${url}`, options)
* // here request will be not modified
* },
* response: (url, method, response) => {
* console.log(`response ${url}`, response)
* if(!response.data){
* response.data = null
* }
* // here response will be modified
* // and data will be null
* return response
* },
* },
* })
*
* @type {{ request?: function, response?: function }} object with optionable `request` and `response` keys
* @memberof KinkaInstanceOptions
*/
inspectors?: {
/**
* callback which will be called always when request will been created
*
* @type {function}
*/
request?: (url: string, method: string, options?: KinkaRequestOptions)=>(KinkaRequestOptions|any),
/**
* callback which will be called always when request returned response
*
* @type {function}
*/
response?: <T = any, R = KinkaResponse<T>>(url: string, method: string, response: R)=>(R|any),
}
/**
* With {true} your responses will not be throwing exceptions and you don't need to wrap your requests in try/catch.
* And if you want to catch exception you can get this from {response.err} or {response.isError}
* Example:
* const api = kinka.create('https://api.com', { omitCatches: true })
* ...
* // here the application is not been crashed
* const { err } = await api.get('/bad-request')
* if(err){
* // catched error
* console.log(err)
* }
*
* @type {boolean} - by default will be {true}
* @memberof KinkaInstanceOptions
*/
omitCatches?: boolean;
/**
* Sets the number of milliseconds after which
* request automatically will be terminated. 0 value means no timeout.
* Read more: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/timeout
*
* @type {number} - by default no timeout
* @memberof KinkaInstanceOptions
*/
timeout?: number;
/**
* Indicates that all requests should use credentials (like cookies or specific auth headers)
* Sets flag {request.withCredentials}
* Read more: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/withCredentials
*
* @type {boolean}
* @memberof KinkaInstanceOptions
*/
credentials?: boolean;
}
/**
* Properties and methods of the kinka instance
*
* @export
* @interface KinkaInstance
*/
export interface KinkaInstance {
/**
* abort request by cancel token.
* Example:
* kinka.get('/users', { cancelToken: 'usersKey' })
* kinka.abort('usersKey')
* //GET:/users request will been canceled
*
* @param {string} cancelToken
* @returns {undefined}
* @memberof KinkaInstance
*/
abort(cancelToken: string):undefined;
/**
* That method can helps if needed to wait more than one request
* Return a promise that is fulfilled when all the items in the array are fulfilled.
* Example:
* const [friends, family, donuts] = await kinka.all([
* kinka.get('/friends'),
* kinka.get('/family'),
* kinka.get('/donuts'),
* ])
* console.log(friends.data)
* console.log(family.data)
* console.log(donuts.data)
*
* @template T
* @param {Promise<T>[]} promises
* @returns {Promise<T[]>}
* @memberof KinkaInstance
*/
all<T>(promises: Promise<T>[]): Promise<T[]>;
/**
* create new kinka instance with your own options.
* Example:
* const api = kinka.create({baseURL: 'myapi.com'})
* api.get('/all') // GET: myapi.com/all promise
*
* @param {KinkaInstanceOptions} [options]
* @returns {KinkaInstance}
* @memberof KinkaInstance
*/
create(options?: KinkaInstanceOptions): KinkaInstance;
/**
* create a new copy of the current kinka instance.
* Example:
* const api = kinka.create({baseURL: 'myapi.com'})
* api.config.headers['Some-Extra'] = 'some extra'
* const copyApi = api.copy()
* copyApi.config.headers['Some-Extra'] // 'some extra'
*
* @returns {KinkaInstance}
* @memberof KinkaInstance
*/
copy(): KinkaInstance;
/**
* create request with custom method name.
* Example:
* const api = kinka.create({baseURL: 'myapi.com'})
* const promise = api.custom('kill', '/all')
* // KILL: myapi.com/all promise
*
* @template T
* @template R
* @param {string} method
* @param {string} path
* @param {KinkaRequestOptions} [options]
* @returns {Promise<R>}
* @memberof KinkaInstance
*/
custom<T = any, R = KinkaResponse<T>>(method: string, path: string, options?: KinkaRequestOptions): Promise<R>;
/**
* create request with {DELETE} method.
* Example:
* const api = kinka.create({baseURL: 'myapi.com'})
* const promise = api.delete('/all')
* // DELETE: myapi.com/all promise
*
* @template T
* @template R
* @param {string} path
* @param {KinkaRequestOptions} [options]
* @returns {Promise<R>}
* @memberof KinkaInstance
*/
delete<T = any, R = KinkaResponse<T>>(path: string, options?: KinkaRequestOptions): Promise<R>;
/**
* create request with {GET} method.
* Example:
* const api = kinka.create({baseURL: 'myapi.com'})
* const promise = api.get('/all')
* // GET: myapi.com/all promise
*
* @template T
* @template R
* @param {string} path
* @param {KinkaRequestOptions} [options]
* @returns {Promise<R>}
* @memberof KinkaInstance
*/
get<T = any, R = KinkaResponse<T>>(path: string, options?: KinkaRequestOptions): Promise<R>;
/**
* create request with {HEAD} method.
* Example:
* const api = kinka.create({baseURL: 'myapi.com'})
* const promise = api.head('/all')
* // HEAD: myapi.com/all promise
*
* @template T
* @template R
* @param {string} path
* @param {KinkaRequestOptions} [options]
* @returns {Promise<R>}
* @memberof KinkaInstance
*/
head<T = any, R = KinkaResponse<T>>(path: string, options?: KinkaRequestOptions): Promise<R>;
/**
* create request with {OPTIONS} method.
* Example:
* const api = kinka.create({baseURL: 'myapi.com'})
* const promise = api.options('/all')
* // OPTIONS: myapi.com/all promise
*
* @template T
* @template R
* @param {string} path
* @param {KinkaRequestOptions} [options]
* @returns {Promise<R>}
* @memberof KinkaInstance
*/
options<T = any, R = KinkaResponse<T>>(path: string, options?: KinkaRequestOptions): Promise<R>;
/**
* create request with {PATCH} method.
* Example:
* const api = kinka.create({baseURL: 'myapi.com'})
* const promise = api.patch('/all')
* // PATCH: myapi.com/all promise
*
* @template T
* @template R
* @param {string} path
* @param {*} [data]
* @param {KinkaRequestOptions} [options]
* @returns {Promise<R>}
* @memberof KinkaInstance
*/
patch<T = any, R = KinkaResponse<T>>(path: string, data?: any, options?: KinkaRequestOptions): Promise<R>;
/**
* create request with {POST} method.
* Example:
* const api = kinka.create({baseURL: 'myapi.com'})
* const promise = api.post('/all')
* // POST: myapi.com/all promise
*
* @template T
* @template R
* @param {string} path
* @param {*} [data]
* @param {KinkaRequestOptions} [options]
* @returns {Promise<R>}
* @memberof KinkaInstance
*/
post<T = any, R = KinkaResponse<T>>(path: string, data?: any, options?: KinkaRequestOptions): Promise<R>;
/**
* create request with {PUT} method.
* Example:
* const api = kinka.create({baseURL: 'myapi.com'})
* const promise = api.put('/all')
* // PUT: myapi.com/all promise
*
* @template T
* @template R
* @param {string} path
* @param {*} [data]
* @param {KinkaRequestOptions} [options]
* @returns {Promise<R>}
* @memberof KinkaInstance
*/
put<T = any, R = KinkaResponse<T>>(path: string, data?: any, options?: KinkaRequestOptions): Promise<R>;
/**
* custom method works only if
* this method name send to 'customMethods' to instance options when you was created new kinka instance.
* Otherwise will be catched an error - "kinka['property'] is not a function"
* Example:
* const api = kinka.create({ baseURL: 'myapi.com', customMethods: ['pull'] })
* api.pull('/changes') // PULL: myapi.com/changes promise
*/
[customMethod: string]: <T = any, R = KinkaResponse<T>>(path: string, options?: KinkaRequestOptions) => Promise<R>;
}
export declare type MiddlewareFunction = (instance: KinkaInstance) => any
declare const kinka: KinkaInstance;
export default kinka