Browse Source

Merge pull request #184 from reilem/add-types

Add typescript type defenitions
Travis Nuttall 6 years ago
parent
commit
ba11d32e7e
No account linked to committer's email address
1 changed files with 621 additions and 0 deletions
  1. 621
    0
      index.d.ts

+ 621
- 0
index.d.ts View File

@@ -0,0 +1,621 @@
1
+// Type definitions for react-native-fetch-blob 0.10
2
+// Project: https://github.com/wkh237/react-native-fetch-blob#readme
3
+// Definitions by: MNB <https://github.com/MNBuyskih>
4
+// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
5
+
6
+export const RNFetchBlob: RNFetchBlobStatic;
7
+export type RNFetchBlob = RNFetchBlobStatic;
8
+export default RNFetchBlob;
9
+
10
+interface RNFetchBlobStatic {
11
+    fetch(method: Methods, url: string, headers?: { [key: string]: string }, body?: any
12
+        | null): StatefulPromise<FetchBlobResponse>;
13
+    base64: { encode(input: string): string; decode(input: string): string };
14
+    android: AndroidApi;
15
+    ios: IOSApi;
16
+    config(options: RNFetchBlobConfig): RNFetchBlobStatic;
17
+    session(name: string): RNFetchBlobSession;
18
+    fs: FS;
19
+    wrap(path: string): string;
20
+    net: Net;
21
+    polyfill: Polyfill;
22
+    // this require external module https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/oboe
23
+    JSONStream: any;
24
+}
25
+
26
+export interface Polyfill {
27
+    Blob: PolyfillBlob;
28
+    File: PolyfillFile;
29
+    XMLHttpRequest: PolyfillXMLHttpRequest;
30
+    ProgressEvent: PolyfillProgressEvent;
31
+    Event: PolyfillEvent;
32
+    FileReader: PolyfillFileReader;
33
+    Fetch: PolyfillFetch;
34
+}
35
+
36
+export declare class PolyfillFetch extends RNFetchBlobFetchPolyfill {
37
+    constructor(config: RNFetchBlobConfig);
38
+}
39
+
40
+export declare class RNFetchBlobFetchPolyfill {
41
+    constructor(config: RNFetchBlobConfig);
42
+
43
+    build(): (url: string, options: RNFetchBlobConfig) => StatefulPromise<RNFetchBlobFetchRepsonse>;
44
+}
45
+
46
+export interface RNFetchBlobFetchRepsonse {
47
+    arrayBuffer(): Promise<any[]>;
48
+    blob(): Promise<PolyfillBlob>;
49
+    json(): Promise<any>;
50
+    rawResp(): Promise<FetchBlobResponse>;
51
+    text(): Promise<string>;
52
+    bodyUsed: boolean;
53
+    headers: any;
54
+    ok: boolean;
55
+    resp: FetchBlobResponse;
56
+    rnfbResp: FetchBlobResponse;
57
+    rnfbRespInfo: RNFetchBlobResponseInfo;
58
+    status: number;
59
+    type: string;
60
+}
61
+
62
+/**
63
+ * RNFetchBlob response object class.
64
+ */
65
+export interface FetchBlobResponse {
66
+    taskId: string;
67
+    /**
68
+     * get path of response temp file
69
+     * @return File path of temp file.
70
+     */
71
+    path(): string;
72
+    type: "base64" | "path" | "utf8";
73
+    data: any;
74
+    /**
75
+     * Convert result to javascript RNFetchBlob object.
76
+     * @return Return a promise resolves Blob object.
77
+     */
78
+    blob(contentType: string, sliceSize: number): Promise<PolyfillBlob>;
79
+    /**
80
+     * Convert result to text.
81
+     * @return Decoded base64 string.
82
+     */
83
+    text(): string | Promise<any>;
84
+    /**
85
+     * Convert result to JSON object.
86
+     * @return Parsed javascript object.
87
+     */
88
+    json(): any;
89
+    /**
90
+     * Return BASE64 string directly.
91
+     * @return BASE64 string of response body.
92
+     */
93
+    base64(): any;
94
+    /**
95
+     * Remove cahced file
96
+     */
97
+    flush(): void;
98
+    respInfo: RNFetchBlobResponseInfo;
99
+    session(name: string): RNFetchBlobSession | null;
100
+    /**
101
+     * Read file content with given encoding, if the response does not contains
102
+     * a file path, show warning message
103
+     * @param  encode Encode type, should be one of `base64`, `ascrii`, `utf8`.
104
+     */
105
+    readFile(encode: Encoding): Promise<any> | null;
106
+    /**
107
+     * Start read stream from cached file
108
+     * @param  encode Encode type, should be one of `base64`, `ascrii`, `utf8`.
109
+     */
110
+    readStream(encode: Encoding): RNFetchBlobStream | null;
111
+}
112
+
113
+export interface PolyfillFileReader extends EventTarget {
114
+    isRNFBPolyFill: boolean;
115
+    onloadstart(e: Event): void;
116
+    onprogress(e: Event): void;
117
+    onload(e: Event): void;
118
+    onabort(e: Event): void;
119
+    onerror(e: Event): void;
120
+    onloadend(e: Event): void;
121
+
122
+    abort(): void;
123
+    readAsArrayBuffer(b: PolyfillBlob): void;
124
+    readAsBinaryString(b: PolyfillBlob): void;
125
+    readAsText(b: PolyfillBlob, label?: string): void;
126
+    readAsDataURL(b: PolyfillBlob): void;
127
+
128
+    readyState: number;
129
+    result: number;
130
+}
131
+
132
+export declare namespace PolyfillFileReader {
133
+    const EMPTY: number;
134
+    const LOADING: number;
135
+    const DONE: number;
136
+}
137
+
138
+export declare class PolyfillEvent {
139
+}
140
+
141
+export interface PolyfillProgressEvent extends EventTarget {
142
+    lengthComputable: boolean;
143
+    loaded: number;
144
+    total: number;
145
+}
146
+
147
+export declare class PolyfillBlob extends EventTarget {
148
+    /**
149
+     * RNFetchBlob Blob polyfill, create a Blob directly from file path, BASE64
150
+     * encoded data, and string. The conversion is done implicitly according to
151
+     * given `mime`. However, the blob creation is asynchronously, to register
152
+     * event `onCreated` is need to ensure the Blob is creadted.
153
+     *
154
+     * @param data Content of Blob object
155
+     * @param cType Content type settings of Blob object, `text/plain` by default
156
+     * @param defer When this argument set to `true`, blob constructor will not invoke blob created event automatically.
157
+     */
158
+    constructor(data: any, cType: any, defer: boolean);
159
+
160
+    /**
161
+     * Since Blob content will asynchronously write to a file during creation,
162
+     * use this method to register an event handler for Blob initialized event.
163
+     * @param  fn An event handler invoked when Blob created
164
+     * @return The Blob object instance itself
165
+     */
166
+    onCreated(fn: () => void): PolyfillBlob;
167
+
168
+    markAsDerived(): void;
169
+
170
+    /**
171
+     * Get file reference of the Blob object.
172
+     * @return Blob file reference which can be consumed by RNFetchBlob fs
173
+     */
174
+    getRNFetchBlobRef(): string;
175
+
176
+    /**
177
+     * Create a Blob object which is sliced from current object
178
+     * @param  start    Start byte number
179
+     * @param  end      End byte number
180
+     * @param  contentType Optional, content type of new Blob object
181
+     */
182
+    slice(start?: number, end?: number, contentType?: string): PolyfillBlob;
183
+
184
+    /**
185
+     * Read data of the Blob object, this is not standard method.
186
+     * @param  encoding Read data with encoding
187
+     */
188
+    readBlob(encoding: string): Promise<any>;
189
+
190
+    /**
191
+     * Release the resource of the Blob object.
192
+     * @nonstandard
193
+     */
194
+    close(): Promise<void>;
195
+}
196
+
197
+export declare namespace PolyfillBlob {
198
+    function clearCache(): void;
199
+
200
+    function build(data: any, cType: any): Promise<PolyfillBlob>;
201
+
202
+    function setLog(level: number): void;
203
+}
204
+
205
+export declare class PolyfillFile extends PolyfillBlob {
206
+}
207
+
208
+export interface PolyfillXMLHttpRequest extends PolyfillXMLHttpRequestEventTarget {
209
+    upload: PolyfillXMLHttpRequestEventTarget;
210
+    readonly UNSENT: number;
211
+    readonly OPENED: number;
212
+    readonly HEADERS_RECEIVED: number;
213
+    readonly LOADING: number;
214
+    readonly DONE: number;
215
+
216
+    /**
217
+     * XMLHttpRequest.open, always async, user and password not supported. When
218
+     * this method invoked, headers should becomes empty again.
219
+     * @param  method Request method
220
+     * @param  url Request URL
221
+     * @param  async Always async
222
+     * @param  user NOT SUPPORTED
223
+     * @param  password NOT SUPPORTED
224
+     */
225
+    open(method: string, url: string, async: true, user: any, password: any): void;
226
+
227
+    /**
228
+     * Invoke this function to send HTTP request, and set body.
229
+     * @param body Body in RNfetchblob flavor
230
+     */
231
+    send(body: any): void;
232
+
233
+    overrideMimeType(mime: string): void;
234
+
235
+    setRequestHeader(name: string, value: string): void;
236
+
237
+    abort(): void;
238
+
239
+    getResponseHeader(field: string): string | null;
240
+
241
+    getAllResponseHeaders(): string | null;
242
+
243
+    onreadystatechange(e: Event): void;
244
+    readyState: number;
245
+    status: number;
246
+    statusText: string;
247
+    response: any;
248
+    responseText: any;
249
+    responseURL: string;
250
+    responseHeaders: any;
251
+    timeout: number;
252
+    responseType: string;
253
+}
254
+
255
+export declare namespace PolyfillXMLHttpRequest {
256
+    const binaryContentTypes: string[];
257
+    const UNSENT: number;
258
+    const OPENED: number;
259
+    const HEADERS_RECEIVED: number;
260
+    const LOADING: number;
261
+    const DONE: number;
262
+
263
+    function setLog(level: number): void;
264
+
265
+    function addBinaryContentType(substr: string): void;
266
+
267
+    function removeBinaryContentType(): void;
268
+}
269
+
270
+export interface PolyfillXMLHttpRequestEventTarget extends EventTarget {
271
+    onabort(e: Event): void;
272
+    onerror(e: Event): void;
273
+    onload(e: Event): void;
274
+    onloadstart(e: Event): void;
275
+    onprogress(e: Event): void;
276
+    ontimeout(e: Event): void;
277
+    onloadend(e: Event): void;
278
+}
279
+
280
+export interface Net {
281
+    /**
282
+     * Get cookie according to the given url.
283
+     * @param  domain Domain of the cookies to be removed, remove all
284
+     * @return     Cookies of a specific domain.
285
+     */
286
+    getCookies(domain: string): Promise<string[]>;
287
+
288
+    /**
289
+     * Remove cookies for a specific domain
290
+     * @param  domain Domain of the cookies to be removed, remove all
291
+     * cookies when this is null.
292
+     */
293
+    removeCookies(domain?: string): Promise<null>;
294
+}
295
+
296
+export interface FS {
297
+    RNFetchBlobSession: RNFetchBlobSession;
298
+
299
+    /**
300
+     * Remove file at path.
301
+     * @param    path:string Path of target file.
302
+     */
303
+    unlink(path: string): Promise<void>;
304
+
305
+    /**
306
+     * Create a directory.
307
+     * @param  path Path of directory to be created
308
+     */
309
+    mkdir(path: string): Promise<void>;
310
+
311
+    /**
312
+     * Get a file cache session
313
+     * @param  name Stream ID
314
+     */
315
+    session(name: string): RNFetchBlobSession;
316
+
317
+    ls(path: string): Promise<string[]>;
318
+
319
+    /**
320
+     * Create file stream from file at `path`.
321
+     * @param  path   The file path.
322
+     * @param  encoding Data encoding, should be one of `base64`, `utf8`, `ascii`
323
+     * @param  bufferSize Size of stream buffer.
324
+     * @return RNFetchBlobStream stream instance.
325
+     */
326
+    readStream(path: string, encoding: Encoding, bufferSize?: number, tick?: number): Promise<RNFetchBlobReadStream>;
327
+
328
+    mv(path: string, dest: string): Promise<boolean>;
329
+
330
+    cp(path: string, dest: string): Promise<boolean>;
331
+
332
+    /**
333
+     * Create write stream to a file.
334
+     * @param  path Target path of file stream.
335
+     * @param  encoding Encoding of input data.
336
+     * @param  append  A flag represent if data append to existing ones.
337
+     * @return A promise resolves a `WriteStream` object.
338
+     */
339
+    writeStream(path: string, encoding: Encoding, append?: boolean): Promise<RNFetchBlobWriteStream>;
340
+
341
+    /**
342
+     * Write data to file.
343
+     * @param  path  Path of the file.
344
+     * @param  data Data to write to the file.
345
+     * @param  encoding Encoding of data (Optional).
346
+     */
347
+    writeFile(path: string, data: string | number[], encoding?: Encoding): Promise<void>;
348
+
349
+    appendFile(path: string, data: string | number[], encoding?: Encoding): Promise<void>;
350
+
351
+    /**
352
+     * Wrapper method of readStream.
353
+     * @param  path Path of the file.
354
+     * @param  encoding Encoding of read stream.
355
+     */
356
+    readFile(path: string, encoding: Encoding, bufferSize?: number): Promise<any>;
357
+    /**
358
+     * Check if file exists and if it is a folder.
359
+     * @param  path Path to check
360
+     */
361
+    exists(path: string): Promise<boolean>;
362
+
363
+    createFile(path: string, data: string, encoding: Encoding): Promise<void>;
364
+
365
+    isDir(path: string): Promise<boolean>;
366
+
367
+    /**
368
+     * Show statistic data of a path.
369
+     * @param  path Target path
370
+     */
371
+    stat(path: string): Promise<RNFetchBlobStat>;
372
+
373
+    lstat(path: string): Promise<RNFetchBlobStat[]>;
374
+
375
+    /**
376
+     * Android only method, request media scanner to scan the file.
377
+     * @param  pairs Array contains Key value pairs with key `path` and `mime`.
378
+     */
379
+    scanFile(pairs: Array<{ [key: string]: string }>): Promise<void>;
380
+
381
+    dirs: Dirs;
382
+
383
+    slice(src: string, dest: string, start: number, end: number): Promise<void>;
384
+    asset(path: string): string;
385
+    df(): Promise<{ free: number, total: number }>;
386
+}
387
+
388
+export interface Dirs {
389
+    DocumentDir: string;
390
+    CacheDir: string;
391
+    PictureDir: string;
392
+    MusicDir: string;
393
+    MovieDir: string;
394
+    DownloadDir: string;
395
+    DCIMDir: string;
396
+    SDCardDir: string;
397
+    MainBundleDir: string;
398
+}
399
+
400
+export interface RNFetchBlobWriteStream {
401
+    id: string;
402
+    encoding: string;
403
+    append: boolean;
404
+
405
+    write(data: string): Promise<void>;
406
+    close(): void;
407
+}
408
+
409
+export interface RNFetchBlobReadStream {
410
+    path: string;
411
+    encoding: Encoding;
412
+    bufferSize?: number;
413
+    closed: boolean;
414
+    tick: number;
415
+
416
+    open(): void;
417
+
418
+    onData(fn: (chunk: string | number[]) => void): void;
419
+
420
+    onError(fn: (err: any) => void): void;
421
+
422
+    onEnd(fn: () => void): void;
423
+}
424
+
425
+type Encoding = "utf8" | "ascii" | "base64";
426
+
427
+/* tslint:disable-next-line interface-name*/
428
+export interface IOSApi {
429
+    /**
430
+     * Open a file in {@link https://developer.apple.com/reference/uikit/uidocumentinteractioncontroller UIDocumentInteractionController},
431
+     * this is the default document viewer of iOS, supports several kinds of files. On Android, there's an similar method {@link android.actionViewIntent}.
432
+     * @param path This is a required field, the path to the document. The path should NOT contains any scheme prefix.
433
+     */
434
+    previewDocument(path: string): void;
435
+
436
+    /**
437
+     * Show options menu for interact with the file.
438
+     * @param path This is a required field, the path to the document. The path should NOT contains any scheme prefix.
439
+     */
440
+    openDocument(path: string): void;
441
+}
442
+
443
+export interface AndroidApi {
444
+    /**
445
+     * When sending an ACTION_VIEW intent with given file path and MIME type, system will try to open an
446
+     * App to handle the file. For example, open Gallery app to view an image, or install APK.
447
+     * @param path Path of the file to be opened.
448
+     * @param mime Basically system will open an app according to this MIME type.
449
+     */
450
+    actionViewIntent(path: string, mime: string): Promise<any>;
451
+}
452
+
453
+type Methods = "POST" | "GET" | "DELETE" | "PUT" | "post" | "get" | "delete" | "put";
454
+
455
+/**
456
+ * A declare class inherits Promise, it has extra method like progress, uploadProgress,
457
+ * and cancel which can help managing an asynchronous task's state.
458
+ */
459
+export interface StatefulPromise<T> extends Promise<T> {
460
+    /**
461
+     * Cancel the request when invoke this method.
462
+     */
463
+    cancel(cb?: (reason: any) => void): StatefulPromise<FetchBlobResponse>;
464
+
465
+    /**
466
+     * Add an event listener which triggers when data receiving from server.
467
+     */
468
+    progress(callback: (received: number, total: number) => void): StatefulPromise<FetchBlobResponse>;
469
+
470
+    /**
471
+     * Add an event listener with custom configuration
472
+     */
473
+    progress(config: { count?: number, interval?: number }, callback: (received: number, total: number) => void): StatefulPromise<FetchBlobResponse>;
474
+
475
+    /**
476
+     * Add an event listener with custom configuration.
477
+     */
478
+    uploadProgress(callback: (sent: number, total: number) => void): StatefulPromise<FetchBlobResponse>;
479
+
480
+    /**
481
+     * Add an event listener with custom configuration
482
+     */
483
+    uploadProgress(config: { count?: number, interval?: number }, callback: (sent: number, total: number) => void): StatefulPromise<FetchBlobResponse>;
484
+
485
+    /**
486
+     * An IOS only API, when IOS app turns into background network tasks will be terminated after ~180 seconds,
487
+     * in order to handle these expired tasks, you can register an event handler, which will be called after the
488
+     * app become active.
489
+     */
490
+    expire(callback: () => void): StatefulPromise<void>;
491
+}
492
+
493
+export declare class RNFetchBlobSession {
494
+    constructor(name: string, list: string[]);
495
+
496
+    add(path: string): RNFetchBlobSession;
497
+
498
+    remove(path: string): RNFetchBlobSession;
499
+
500
+    dispose(): Promise<void>;
501
+
502
+    list(): string[];
503
+
504
+    name: string;
505
+
506
+    static getSession(name: string): any;
507
+
508
+    static setSession(name: string): void;
509
+
510
+    static removeSession(name: string): void;
511
+}
512
+
513
+/**
514
+ * A set of configurations that will be injected into a fetch method, with the following properties.
515
+ */
516
+export interface RNFetchBlobConfig {
517
+    /**
518
+     * When this property is true, the downloaded data will overwrite the existing file. (true by default)
519
+     */
520
+    overwrite?: boolean;
521
+
522
+    /**
523
+     * Set timeout of the request (in milliseconds).
524
+     */
525
+    timeout?: number;
526
+
527
+    /**
528
+     * Set this property to true to display a network indicator on status bar, this feature is only supported on IOS.
529
+     */
530
+    indicator?: boolean;
531
+
532
+    /**
533
+     * Set this property to true will allow the request create connection with server have self-signed SSL
534
+     * certification. This is not recommended to use in production.
535
+     */
536
+    trusty?: boolean;
537
+
538
+    /**
539
+     * Set this property to true will makes response data of the fetch stored in a temp file, by default the temp
540
+     * file will stored in App's own root folder with file name template RNFetchBlob_tmp${timestamp}.
541
+     */
542
+    fileCache?: boolean;
543
+
544
+    /**
545
+     * Set this property to change temp file extension that created by fetch response data.
546
+     */
547
+    appendExt?: string;
548
+
549
+    /**
550
+     * When this property has value, fetch API will try to store response data in the path ignoring fileCache and
551
+     * appendExt property.
552
+     */
553
+    path?: string;
554
+
555
+    session?: string;
556
+
557
+    addAndroidDownloads?: AddAndroidDownloads;
558
+
559
+    /**
560
+     * Fix IOS request timeout issue #368 by change default request setting to defaultSessionConfiguration, and make backgroundSessionConfigurationWithIdentifier optional
561
+     */
562
+    IOSBackgroundTask?: boolean;
563
+}
564
+
565
+export interface AddAndroidDownloads {
566
+    /**
567
+     * download file using Android download manager or not.
568
+     */
569
+    useDownloadManager?: boolean;
570
+    /**
571
+     * title of the file
572
+     */
573
+    title?: string;
574
+    /**
575
+     * File description of the file.
576
+     */
577
+    description?: string;
578
+    /**
579
+     * The destination which the file will be downloaded, it SHOULD be a location on external storage (DCIMDir).
580
+     */
581
+    path?: string;
582
+    /**
583
+     * MIME type of the file. By default is text/plain
584
+     */
585
+    mime?: string;
586
+    /**
587
+     * A boolean value, see Officail Document
588
+     * (https://developer.android.com/reference/android/app/DownloadManager.html#addCompletedDownload(java.lang.String, java.lang.String, boolean, java.lang.String, java.lang.String, long, boolean))
589
+     */
590
+    mediaScannable?: boolean;
591
+    /**
592
+     * A boolean value decide whether show a notification when download complete.
593
+     */
594
+    notification?: boolean;
595
+}
596
+
597
+export interface RNFetchBlobResponseInfo {
598
+    taskId: string;
599
+    state: number;
600
+    headers: any;
601
+    status: number;
602
+    respType: "text" | "blob" | "" | "json";
603
+    rnfbEncode: "path" | "base64" | "ascii" | "utf8";
604
+}
605
+
606
+export interface RNFetchBlobStream {
607
+    onData(): void;
608
+    onError(): void;
609
+    onEnd(): void;
610
+}
611
+
612
+export declare class RNFetchBlobFile {
613
+}
614
+
615
+export declare class RNFetchBlobStat {
616
+    lastModified: string;
617
+    size: string;
618
+    type: "directory" | "file";
619
+    path: string;
620
+    filename: string;
621
+}