From a25b8a39cda37df5f720e22bcf1039b38a5f0ceb Mon Sep 17 00:00:00 2001 From: "Peng.Cao" Date: Tue, 15 Jan 2019 17:30:08 +0800 Subject: [PATCH] add rn-fetch-blob support --- types/rn-fetch-blob/index.d.ts | 7 +- types/rn-fetch-blob/rn-fetch-blob-tests.ts | 114 ++++++++++----------- types/rn-fetch-blob/tsconfig.json | 2 +- types/rn-fetch-blob/tslint.json | 8 +- 4 files changed, 62 insertions(+), 69 deletions(-) diff --git a/types/rn-fetch-blob/index.d.ts b/types/rn-fetch-blob/index.d.ts index 4378f7ac65..79758b4511 100644 --- a/types/rn-fetch-blob/index.d.ts +++ b/types/rn-fetch-blob/index.d.ts @@ -1,7 +1,6 @@ -// Type definitions for rn-fetch-blob +// Type definitions for rn-fetch-blob 1.2 // Project: https://github.com/joltup/rn-fetch-blob#readme -// Definitions by: MNB -// Definitions by Peng Cao +// Definitions by: Cao Peng // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped export const RNFetchBlob: RNFetchBlobStatic; @@ -9,7 +8,7 @@ export type RNFetchBlob = RNFetchBlobStatic; export default RNFetchBlob; interface RNFetchBlobStatic { - fetch(method: Methods, url: string, headers?: { [key: string]: string }, body?: any + fetch(method: Methods, url: string, headers?: { [key: string]: string }, body?: {} | null): StatefulPromise; base64: { encode(input: string): string; decode(input: string): string }; android: AndroidApi; diff --git a/types/rn-fetch-blob/rn-fetch-blob-tests.ts b/types/rn-fetch-blob/rn-fetch-blob-tests.ts index 680e334ed9..5df33226a9 100644 --- a/types/rn-fetch-blob/rn-fetch-blob-tests.ts +++ b/types/rn-fetch-blob/rn-fetch-blob-tests.ts @@ -5,7 +5,7 @@ const PATH_TO_THE_FILE = '/some/path.png'; // Download example: Fetch files that need authorization token RNFetchBlob - .fetch('GET', 'http://www.example.com/images/img1.png', {Authorization: 'Bearer access-token...'}) + .fetch('GET', 'http://www.example.com/images/img1.png', { Authorization: 'Bearer access-token...' }) .then(res => { const base64Str = res.base64(); const text = res.text(); @@ -108,25 +108,25 @@ RNFetchBlob.fetch('POST', 'http://www.example.com/upload-form', { otherHeader: "foo", 'Content-Type': 'multipart/form-data', }, [ - // element with property `filename` will be transformed into `file` in form data - {name: 'avatar', filename: 'avatar.png', data: BASE64_IMAGE_STRING}, - // custom content type - {name: 'avatar-png', filename: 'avatar-png.png', type: 'image/png', data: BASE64_IMAGE_STRING}, - // part file from storage - {name: 'avatar-foo', filename: 'avatar-foo.png', type: 'image/foo', data: RNFetchBlob.wrap(PATH_TO_THE_FILE)}, - // elements without property `filename` will be sent as plain text - {name: 'name', data: 'user'}, - { - name: 'info', data: JSON.stringify({ - mail: 'example@example.com', - tel: '12345678' - }) - }, -]).then((resp) => { - // ... -}).catch((err) => { - // ... -}); + // element with property `filename` will be transformed into `file` in form data + { name: 'avatar', filename: 'avatar.png', data: BASE64_IMAGE_STRING }, + // custom content type + { name: 'avatar-png', filename: 'avatar-png.png', type: 'image/png', data: BASE64_IMAGE_STRING }, + // part file from storage + { name: 'avatar-foo', filename: 'avatar-foo.png', type: 'image/foo', data: RNFetchBlob.wrap(PATH_TO_THE_FILE) }, + // elements without property `filename` will be sent as plain text + { name: 'name', data: 'user' }, + { + name: 'info', data: JSON.stringify({ + mail: 'example@example.com', + tel: '12345678' + }) + }, + ]).then((resp) => { + // ... + }).catch((err) => { + // ... + }); RNFetchBlob.fetch('POST', 'http://www.example.com/upload-form', { Authorization: "Bearer access-token", @@ -134,42 +134,42 @@ RNFetchBlob.fetch('POST', 'http://www.example.com/upload-form', { // this is required, otherwise it won't be process as a multipart/form-data request 'Content-Type': 'multipart/form-data', }, [ - // append field data from file path - { - name: 'avatar', - filename: 'avatar.png', - // Change BASE64 encoded data to a file path with prefix `RNFetchBlob-file://`. - // Or simply wrap the file path with RNFetchBlob.wrap(). - data: RNFetchBlob.wrap(PATH_TO_THE_FILE) - }, - { - name: 'ringtone', - filename: 'ring.mp3', - // use custom MIME type - type: 'application/mp3', - // upload a file from asset is also possible in version >= 0.6.2 - data: RNFetchBlob.wrap(RNFetchBlob.fs.asset('default-ringtone.mp3')) - }, - // elements without property `filename` will be sent as plain text - {name: 'name', data: 'user'}, - { - name: 'info', data: JSON.stringify({ - mail: 'example@example.com', - tel: '12345678' - }) - }, -]).then((resp) => { - // ... -}).catch((err) => { - // ... -}); + // append field data from file path + { + name: 'avatar', + filename: 'avatar.png', + // Change BASE64 encoded data to a file path with prefix `RNFetchBlob-file://`. + // Or simply wrap the file path with RNFetchBlob.wrap(). + data: RNFetchBlob.wrap(PATH_TO_THE_FILE) + }, + { + name: 'ringtone', + filename: 'ring.mp3', + // use custom MIME type + type: 'application/mp3', + // upload a file from asset is also possible in version >= 0.6.2 + data: RNFetchBlob.wrap(RNFetchBlob.fs.asset('default-ringtone.mp3')) + }, + // elements without property `filename` will be sent as plain text + { name: 'name', data: 'user' }, + { + name: 'info', data: JSON.stringify({ + mail: 'example@example.com', + tel: '12345678' + }) + }, + ]).then((resp) => { + // ... + }).catch((err) => { + // ... + }); // Upload/Download progress RNFetchBlob.fetch('POST', 'http://www.example.com/upload', { // ... some headers, 'Content-Type': 'octet-stream' }, BASE64_IMAGE_STRING) -// listen to upload progress event + // listen to upload progress event .uploadProgress((written, total) => { console.log('uploaded', written / total); }) @@ -188,12 +188,12 @@ RNFetchBlob.fetch('POST', 'http://www.example.com/upload', { // ... some headers, 'Content-Type': 'octet-stream' }, BASE64_IMAGE_STRING) -// listen to upload progress event, emit every 250ms - .uploadProgress({interval: 250}, (written, total) => { + // listen to upload progress event, emit every 250ms + .uploadProgress({ interval: 250 }, (written, total) => { console.log('uploaded', written / total); }) // listen to download progress event, every 10% - .progress({count: 10}, (received, total) => { + .progress({ count: 10 }, (received, total) => { console.log('progress', received / total); }) .then((resp) => { @@ -207,7 +207,7 @@ RNFetchBlob.fetch('POST', 'http://www.example.com/upload', { const task = RNFetchBlob.fetch('GET', 'http://example.com/file/1'); task.then(() => { }) -// handle request cancelled rejection + // handle request cancelled rejection .catch((err) => { console.log(err); }); @@ -222,7 +222,7 @@ RNFetchBlob path: dirs.DCIMDir + '/music.mp3' }) .fetch('GET', 'http://example.com/music.mp3') - .then((res) => RNFetchBlob.fs.scanFile([{path: res.path(), mime: 'audio/mpeg'}])) + .then((res) => RNFetchBlob.fs.scanFile([{ path: res.path(), mime: 'audio/mpeg' }])) .then(() => { // scan file success }) @@ -326,7 +326,7 @@ RNFetchBlob.fs.writeStream( // Cache File Management // remove file using RNFetchblobResponse.flush() object method RNFetchBlob - .config({fileCache: true}) + .config({ fileCache: true }) .fetch('GET', 'http://example.com/download/file') .then((res) => { // remove cached file from storage @@ -370,7 +370,7 @@ RNFetchBlob.session('foo').dispose().then(() => { }); // Transfer Encoding -RNFetchBlob.fetch('POST', 'http://example.com/upload', {'Transfer-Encoding': 'Chunked'}, 'bodyData'); +RNFetchBlob.fetch('POST', 'http://example.com/upload', { 'Transfer-Encoding': 'Chunked' }, 'bodyData'); // Self-Signed SSL Server RNFetchBlob.config({ diff --git a/types/rn-fetch-blob/tsconfig.json b/types/rn-fetch-blob/tsconfig.json index fc061c8889..219ad3c49b 100644 --- a/types/rn-fetch-blob/tsconfig.json +++ b/types/rn-fetch-blob/tsconfig.json @@ -21,4 +21,4 @@ "index.d.ts", "rn-fetch-blob-tests.ts" ] -} +} \ No newline at end of file diff --git a/types/rn-fetch-blob/tslint.json b/types/rn-fetch-blob/tslint.json index b1439230db..3db14f85ea 100644 --- a/types/rn-fetch-blob/tslint.json +++ b/types/rn-fetch-blob/tslint.json @@ -1,7 +1 @@ -{ - "extends": "dtslint/dt.json", - "rules": { - // TODO - "no-any-union": false - } -} +{ "extends": "dtslint/dt.json" }