snappymail/dev/bootstrap.js

97 lines
2.1 KiB
JavaScript
Raw Normal View History

2022-03-08 17:52:08 +08:00
import { Settings } from 'Common/Globals';
2019-07-05 03:19:24 +08:00
import { i18n } from 'Common/Translator';
2016-06-07 05:57:52 +08:00
import { root } from 'Common/Links';
import { isArray } from 'Common/Utils';
const FormDataToObject = formData => {
var object = {};
formData.forEach((value, key) => {
if (!Reflect.has(object, key)){
object[key] = value;
} else {
isArray(object[key]) || (object[key] = [object[key]]);
object[key].push(value);
}
});
return object;
};
export default App => {
2015-11-19 01:32:29 +08:00
rl.app = App;
2021-03-24 21:14:21 +08:00
rl.logoutReload = App.logoutReload;
2015-11-19 01:32:29 +08:00
2016-06-17 07:23:49 +08:00
rl.i18n = i18n;
2015-11-19 01:32:29 +08:00
rl.Enums = {
StorageResultType: {
Success: 0,
Error: 1,
Abort: 2
}
};
2015-11-19 01:32:29 +08:00
rl.route = {
root: () => {
rl.route.off();
2022-03-03 23:28:05 +08:00
hasher.setHash(root());
},
reload: () => {
2020-09-17 05:28:29 +08:00
rl.route.root();
setTimeout(() => location.reload(), 100);
},
2021-08-13 16:01:01 +08:00
off: () => hasher.active = false,
2022-03-03 23:28:05 +08:00
on: () => hasher.active = true
};
rl.fetch = (resource, init, postData) => {
init = Object.assign({
mode: 'same-origin',
cache: 'no-cache',
redirect: 'error',
referrerPolicy: 'no-referrer',
credentials: 'same-origin',
headers: {}
}, init);
if (postData) {
init.method = 'POST';
init.headers['Content-Type'] = 'application/json';
postData = (postData instanceof FormData) ? FormDataToObject(postData) : postData;
postData.XToken = Settings.app('token');
init.body = JSON.stringify(postData);
}
return fetch(resource, init);
};
rl.fetchJSON = (resource, init, postData) => {
init = Object.assign({ headers: {} }, init);
init.headers.Accept = 'application/json';
return rl.fetch(resource, init, postData).then(response => {
if (!response.ok) {
2021-02-25 17:12:48 +08:00
return Promise.reject('Network response error: ' + response.status);
}
/* TODO: use this for non-developers?
response.clone()
let data = response.text();
try {
return JSON.parse(data);
} catch (e) {
console.error(e);
// console.log(data);
return Promise.reject(Notification.JsonParse);
return {
Result: false,
ErrorCode: 952, // Notification.JsonParse
ErrorMessage: e.message,
ErrorMessageAdditional: data
}
}
*/
return response.json();
});
};
2016-04-21 01:12:51 +08:00
};