1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-15 09:04:04 +02:00
joplin/ReactNativeClient/lib/shim-init-react.js
2017-07-10 20:16:59 +01:00

43 lines
1.2 KiB
JavaScript

import { shim } from 'lib/shim.js';
import { GeolocationReact } from 'lib/geolocation-react.js';
import RNFetchBlob from 'react-native-fetch-blob';
function shimInit() {
shim.Geolocation = GeolocationReact;
shim.fetchBlob = async function(url, options) {
if (!options || !options.path) throw new Error('fetchBlob: target file path is missing');
if (!options.method) options.method = 'GET';
let headers = options.headers ? options.headers : {};
let method = options.method ? options.method : 'GET';
let dirs = RNFetchBlob.fs.dirs;
let localFilePath = options.path;
if (localFilePath.indexOf('/') !== 0) localFilePath = dirs.DocumentDir + '/' + localFilePath;
delete options.path;
try {
let response = await RNFetchBlob.config({
path: localFilePath
}).fetch(method, url, headers);
// Returns an object that's roughtly compatible with a standard Response object
let output = {
ok: response.respInfo.status < 400,
path: response.data,
text: response.text,
json: response.json,
status: response.respInfo.status,
headers: response.respInfo.headers,
};
return output;
} catch (error) {
throw new Error('fetchBlob: ' + method + ' ' + url + ': ' + error.toString());
}
}
}
export { shimInit }