Ei kuvausta

index.d.ts 20KB

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