Ingen beskrivning

index.d.ts 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  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. session(name: string): RNFetchBlobSession | null;
  92. /**
  93. * Read file content with given encoding, if the response does not contains
  94. * a file path, show warning message
  95. * @param encode Encode type, should be one of `base64`, `ascrii`, `utf8`.
  96. */
  97. readFile(encode: Encoding): Promise<any> | null;
  98. /**
  99. * Start read stream from cached file
  100. * @param encode Encode type, should be one of `base64`, `ascrii`, `utf8`.
  101. */
  102. readStream(encode: Encoding): RNFetchBlobStream | null;
  103. }
  104. export interface PolyfillFileReader extends EventTarget {
  105. isRNFBPolyFill: boolean;
  106. onloadstart(e: Event): void;
  107. onprogress(e: Event): void;
  108. onload(e: Event): void;
  109. onabort(e: Event): void;
  110. onerror(e: Event): void;
  111. onloadend(e: Event): void;
  112. abort(): void;
  113. readAsArrayBuffer(b: PolyfillBlob): void;
  114. readAsBinaryString(b: PolyfillBlob): void;
  115. readAsText(b: PolyfillBlob, label?: string): void;
  116. readAsDataURL(b: PolyfillBlob): void;
  117. readyState: number;
  118. result: number;
  119. }
  120. export declare namespace PolyfillFileReader {
  121. const EMPTY: number;
  122. const LOADING: number;
  123. const DONE: number;
  124. }
  125. export declare class PolyfillEvent {
  126. }
  127. export interface PolyfillProgressEvent extends EventTarget {
  128. lengthComputable: boolean;
  129. loaded: number;
  130. total: number;
  131. }
  132. export declare class PolyfillBlob implements EventTarget {
  133. /**
  134. * RNFetchBlob Blob polyfill, create a Blob directly from file path, BASE64
  135. * encoded data, and string. The conversion is done implicitly according to
  136. * given `mime`. However, the blob creation is asynchronously, to register
  137. * event `onCreated` is need to ensure the Blob is creadted.
  138. *
  139. * @param data Content of Blob object
  140. * @param cType Content type settings of Blob object, `text/plain` by default
  141. * @param defer When this argument set to `true`, blob constructor will not invoke blob created event automatically.
  142. */
  143. constructor(data: any, cType: any, defer: boolean);
  144. /**
  145. * Since Blob content will asynchronously write to a file during creation,
  146. * use this method to register an event handler for Blob initialized event.
  147. * @param fn An event handler invoked when Blob created
  148. * @return The Blob object instance itself
  149. */
  150. onCreated(fn: () => void): PolyfillBlob;
  151. markAsDerived(): void;
  152. /**
  153. * Get file reference of the Blob object.
  154. * @return Blob file reference which can be consumed by RNFetchBlob fs
  155. */
  156. getRNFetchBlobRef(): string;
  157. /**
  158. * Create a Blob object which is sliced from current object
  159. * @param start Start byte number
  160. * @param end End byte number
  161. * @param contentType Optional, content type of new Blob object
  162. */
  163. slice(start?: number, end?: number, contentType?: string): PolyfillBlob;
  164. /**
  165. * Read data of the Blob object, this is not standard method.
  166. * @param encoding Read data with encoding
  167. */
  168. readBlob(encoding: string): Promise<any>;
  169. /**
  170. * Release the resource of the Blob object.
  171. * @nonstandard
  172. */
  173. close(): Promise<void>;
  174. }
  175. export declare namespace PolyfillBlob {
  176. function clearCache(): void;
  177. function build(data: any, cType: any): Promise<PolyfillBlob>;
  178. function setLog(level: number): void;
  179. }
  180. export declare class PolyfillFile extends PolyfillBlob {
  181. }
  182. export interface PolyfillXMLHttpRequest extends PolyfillXMLHttpRequestEventTarget {
  183. upload: PolyfillXMLHttpRequestEventTarget;
  184. readonly UNSENT: number;
  185. readonly OPENED: number;
  186. readonly HEADERS_RECEIVED: number;
  187. readonly LOADING: number;
  188. readonly DONE: number;
  189. /**
  190. * XMLHttpRequest.open, always async, user and password not supported. When
  191. * this method invoked, headers should becomes empty again.
  192. * @param method Request method
  193. * @param url Request URL
  194. * @param async Always async
  195. * @param user NOT SUPPORTED
  196. * @param password NOT SUPPORTED
  197. */
  198. open(method: string, url: string, async: true, user: any, password: any): void;
  199. /**
  200. * Invoke this function to send HTTP request, and set body.
  201. * @param body Body in RNfetchblob flavor
  202. */
  203. send(body: any): void;
  204. overrideMimeType(mime: string): void;
  205. setRequestHeader(name: string, value: string): void;
  206. abort(): void;
  207. getResponseHeader(field: string): string | null;
  208. getAllResponseHeaders(): string | null;
  209. onreadystatechange(e: Event): void;
  210. readyState: number;
  211. status: number;
  212. statusText: string;
  213. response: any;
  214. responseText: any;
  215. responseURL: string;
  216. responseHeaders: any;
  217. timeout: number;
  218. responseType: string;
  219. }
  220. export declare namespace PolyfillXMLHttpRequest {
  221. const binaryContentTypes: string[];
  222. const UNSENT: number;
  223. const OPENED: number;
  224. const HEADERS_RECEIVED: number;
  225. const LOADING: number;
  226. const DONE: number;
  227. function setLog(level: number): void;
  228. function addBinaryContentType(substr: string): void;
  229. function removeBinaryContentType(): void;
  230. }
  231. export interface PolyfillXMLHttpRequestEventTarget extends EventTarget {
  232. onabort(e: Event): void;
  233. onerror(e: Event): void;
  234. onload(e: Event): void;
  235. onloadstart(e: Event): void;
  236. onprogress(e: Event): void;
  237. ontimeout(e: Event): void;
  238. onloadend(e: Event): void;
  239. }
  240. export interface Net {
  241. /**
  242. * Get cookie according to the given url.
  243. * @param domain Domain of the cookies to be removed, remove all
  244. * @return Cookies of a specific domain.
  245. */
  246. getCookies(domain: string): Promise<string[]>;
  247. /**
  248. * Remove cookies for a specific domain
  249. * @param domain Domain of the cookies to be removed, remove all
  250. * cookies when this is null.
  251. */
  252. removeCookies(domain?: string): Promise<null>;
  253. }
  254. export interface FS {
  255. RNFetchBlobSession: RNFetchBlobSession;
  256. /**
  257. * Remove file at path.
  258. * @param path:string Path of target file.
  259. */
  260. unlink(path: string): Promise<void>;
  261. /**
  262. * Create a directory.
  263. * @param path Path of directory to be created
  264. */
  265. mkdir(path: string): Promise<void>;
  266. /**
  267. * Get a file cache session
  268. * @param name Stream ID
  269. */
  270. session(name: string): RNFetchBlobSession;
  271. ls(path: string): Promise<string[]>;
  272. /**
  273. * Create file stream from file at `path`.
  274. * @param path The file path.
  275. * @param encoding Data encoding, should be one of `base64`, `utf8`, `ascii`
  276. * @param bufferSize Size of stream buffer.
  277. * @return RNFetchBlobStream stream instance.
  278. */
  279. readStream(path: string, encoding: Encoding, bufferSize?: number, tick?: number): Promise<RNFetchBlobReadStream>;
  280. mv(path: string, dest: string): Promise<boolean>;
  281. cp(path: string, dest: string): Promise<boolean>;
  282. /**
  283. * Create write stream to a file.
  284. * @param path Target path of file stream.
  285. * @param encoding Encoding of input data.
  286. * @param append A flag represent if data append to existing ones.
  287. * @return A promise resolves a `WriteStream` object.
  288. */
  289. writeStream(path: string, encoding: Encoding, append?: boolean): Promise<RNFetchBlobWriteStream>;
  290. /**
  291. * Write data to file.
  292. * @param path Path of the file.
  293. * @param data Data to write to the file.
  294. * @param encoding Encoding of data (Optional).
  295. */
  296. writeFile(path: string, data: string | number[], encoding?: Encoding): Promise<void>;
  297. appendFile(path: string, data: string | number[], encoding?: Encoding): Promise<void>;
  298. /**
  299. * Wrapper method of readStream.
  300. * @param path Path of the file.
  301. * @param encoding Encoding of read stream.
  302. */
  303. readFile(path: string, encoding: Encoding, bufferSize?: number): Promise<any>;
  304. /**
  305. * Check if file exists and if it is a folder.
  306. * @param path Path to check
  307. */
  308. exists(path: string): Promise<boolean>;
  309. createFile(path: string, data: string, encoding: Encoding): Promise<void>;
  310. isDir(path: string): Promise<boolean>;
  311. /**
  312. * Show statistic data of a path.
  313. * @param path Target path
  314. */
  315. stat(path: string): Promise<RNFetchBlobStat>;
  316. lstat(path: string): Promise<RNFetchBlobStat[]>;
  317. /**
  318. * Android only method, request media scanner to scan the file.
  319. * @param pairs Array contains Key value pairs with key `path` and `mime`.
  320. */
  321. scanFile(pairs: Array<{ [key: string]: string }>): Promise<void>;
  322. dirs: Dirs;
  323. slice(src: string, dest: string, start: number, end: number): Promise<void>;
  324. asset(path: string): string;
  325. df(): Promise<{ free: number, total: number }>;
  326. }
  327. export interface Dirs {
  328. DocumentDir: string;
  329. CacheDir: string;
  330. PictureDir: string;
  331. MusicDir: string;
  332. MovieDir: string;
  333. DownloadDir: string;
  334. DCIMDir: string;
  335. SDCardDir: string;
  336. MainBundleDir: string;
  337. }
  338. export interface RNFetchBlobWriteStream {
  339. id: string;
  340. encoding: string;
  341. append: boolean;
  342. write(data: string): Promise<void>;
  343. close(): void;
  344. }
  345. export interface RNFetchBlobReadStream {
  346. path: string;
  347. encoding: Encoding;
  348. bufferSize?: number;
  349. closed: boolean;
  350. tick: number;
  351. open(): void;
  352. onData(fn: (chunk: string | number[]) => void): void;
  353. onError(fn: (err: any) => void): void;
  354. onEnd(fn: () => void): void;
  355. }
  356. type Encoding = "utf8" | "ascii" | "base64";
  357. /* tslint:disable-next-line interface-name*/
  358. export interface IOSApi {
  359. /**
  360. * Open a file in {@link https://developer.apple.com/reference/uikit/uidocumentinteractioncontroller UIDocumentInteractionController},
  361. * this is the default document viewer of iOS, supports several kinds of files. On Android, there's an similar method {@link android.actionViewIntent}.
  362. * @param path This is a required field, the path to the document. The path should NOT contains any scheme prefix.
  363. */
  364. previewDocument(path: string): void;
  365. /**
  366. * Show options menu for interact with the file.
  367. * @param path This is a required field, the path to the document. The path should NOT contains any scheme prefix.
  368. */
  369. openDocument(path: string): void;
  370. }
  371. export interface AndroidApi {
  372. /**
  373. * When sending an ACTION_VIEW intent with given file path and MIME type, system will try to open an
  374. * App to handle the file. For example, open Gallery app to view an image, or install APK.
  375. * @param path Path of the file to be opened.
  376. * @param mime Basically system will open an app according to this MIME type.
  377. */
  378. actionViewIntent(path: string, mime: string): Promise<any>;
  379. }
  380. type Methods = "POST" | "GET" | "DELETE" | "PUT" | "post" | "get" | "delete" | "put";
  381. /**
  382. * A declare class inherits Promise, it has extra method like progress, uploadProgress,
  383. * and cancel which can help managing an asynchronous task's state.
  384. */
  385. export interface StatefulPromise<T> extends Promise<T> {
  386. /**
  387. * Cancel the request when invoke this method.
  388. */
  389. cancel(cb?: (reason: any) => void): StatefulPromise<FetchBlobResponse>;
  390. /**
  391. * Add an event listener which triggers when data receiving from server.
  392. */
  393. progress(callback: (received: number, total: number) => void): StatefulPromise<FetchBlobResponse>;
  394. /**
  395. * Add an event listener with custom configuration
  396. */
  397. progress(config: { count?: number, interval?: number }, callback: (received: number, total: number) => void): StatefulPromise<FetchBlobResponse>;
  398. /**
  399. * Add an event listener with custom configuration.
  400. */
  401. uploadProgress(callback: (sent: number, total: number) => void): StatefulPromise<FetchBlobResponse>;
  402. /**
  403. * Add an event listener with custom configuration
  404. */
  405. uploadProgress(config: { count?: number, interval?: number }, callback: (sent: number, total: number) => void): StatefulPromise<FetchBlobResponse>;
  406. /**
  407. * An IOS only API, when IOS app turns into background network tasks will be terminated after ~180 seconds,
  408. * in order to handle these expired tasks, you can register an event handler, which will be called after the
  409. * app become active.
  410. */
  411. expire(callback: () => void): StatefulPromise<void>;
  412. }
  413. export declare class RNFetchBlobSession {
  414. constructor(name: string, list: string[]);
  415. add(path: string): RNFetchBlobSession;
  416. remove(path: string): RNFetchBlobSession;
  417. dispose(): Promise<void>;
  418. list(): string[];
  419. name: string;
  420. static getSession(name: string): any;
  421. static setSession(name: string): void;
  422. static removeSession(name: string): void;
  423. }
  424. /**
  425. * A set of configurations that will be injected into a fetch method, with the following properties.
  426. */
  427. export interface RNFetchBlobConfig {
  428. /**
  429. * When this property is true, the downloaded data will overwrite the existing file. (true by default)
  430. */
  431. overwrite?: boolean;
  432. /**
  433. * Set timeout of the request (in milliseconds).
  434. */
  435. timeout?: number;
  436. /**
  437. * Set this property to true to display a network indicator on status bar, this feature is only supported on IOS.
  438. */
  439. indicator?: boolean;
  440. /**
  441. * Set this property to true will allow the request create connection with server have self-signed SSL
  442. * certification. This is not recommended to use in production.
  443. */
  444. trusty?: boolean;
  445. /**
  446. * Set this property to true will makes response data of the fetch stored in a temp file, by default the temp
  447. * file will stored in App's own root folder with file name template RNFetchBlob_tmp${timestamp}.
  448. */
  449. fileCache?: boolean;
  450. /**
  451. * Set this property to change temp file extension that created by fetch response data.
  452. */
  453. appendExt?: string;
  454. /**
  455. * When this property has value, fetch API will try to store response data in the path ignoring fileCache and
  456. * appendExt property.
  457. */
  458. path?: string;
  459. session?: string;
  460. addAndroidDownloads?: AddAndroidDownloads;
  461. /**
  462. * Fix IOS request timeout issue #368 by change default request setting to defaultSessionConfiguration, and make backgroundSessionConfigurationWithIdentifier optional
  463. */
  464. IOSBackgroundTask?: boolean;
  465. }
  466. export interface AddAndroidDownloads {
  467. /**
  468. * download file using Android download manager or not.
  469. */
  470. useDownloadManager?: boolean;
  471. /**
  472. * title of the file
  473. */
  474. title?: string;
  475. /**
  476. * File description of the file.
  477. */
  478. description?: string;
  479. /**
  480. * The destination which the file will be downloaded, it SHOULD be a location on external storage (DCIMDir).
  481. */
  482. path?: string;
  483. /**
  484. * MIME type of the file. By default is text/plain
  485. */
  486. mime?: string;
  487. /**
  488. * A boolean value, see Officail Document
  489. * (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))
  490. */
  491. mediaScannable?: boolean;
  492. /**
  493. * A boolean value decide whether show a notification when download complete.
  494. */
  495. notification?: boolean;
  496. }
  497. export interface RNFetchBlobResponseInfo {
  498. taskId: string;
  499. state: number;
  500. headers: any;
  501. status: number;
  502. respType: "text" | "blob" | "" | "json";
  503. rnfbEncode: "path" | "base64" | "ascii" | "utf8";
  504. }
  505. export interface RNFetchBlobStream {
  506. onData(): void;
  507. onError(): void;
  508. onEnd(): void;
  509. }
  510. export declare class RNFetchBlobFile {
  511. }
  512. export declare class RNFetchBlobStat {
  513. lastModified: string;
  514. size: string;
  515. type: "directory" | "file";
  516. path: string;
  517. filename: string;
  518. }