mirror of
https://github.com/the-djmaze/snappymail.git
synced 2026-08-31 04:59:21 +03:00
Moved rl.fetch and rl.fetchJSON to boot.js so that AppData can be fetched as JSON
This commit is contained in:
parent
91bc5931fd
commit
8b70fee072
3 changed files with 80 additions and 84 deletions
74
dev/bootstrap.js
vendored
74
dev/bootstrap.js
vendored
|
|
@ -1,10 +1,6 @@
|
|||
import { Settings } from 'Common/Globals';
|
||||
import { i18n } from 'Common/Translator';
|
||||
|
||||
import { root } from 'Common/Links';
|
||||
|
||||
import { isArray } from 'Common/Utils';
|
||||
|
||||
export default App => {
|
||||
|
||||
rl.app = App;
|
||||
|
|
@ -33,74 +29,4 @@ export default App => {
|
|||
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';
|
||||
let asJSON = 1,
|
||||
XToken = Settings.app('token'),
|
||||
object = {};
|
||||
if (postData instanceof FormData) {
|
||||
postData.forEach((value, key) => {
|
||||
if (value instanceof File) {
|
||||
asJSON = 0;
|
||||
} else if (!Reflect.has(object, key)) {
|
||||
object[key] = value;
|
||||
} else {
|
||||
isArray(object[key]) || (object[key] = [object[key]]);
|
||||
object[key].push(value);
|
||||
}
|
||||
});
|
||||
if (asJSON) {
|
||||
postData = object;
|
||||
} else {
|
||||
postData.set('XToken', XToken);
|
||||
}
|
||||
}
|
||||
if (asJSON) {
|
||||
init.headers['Content-Type'] = 'application/json';
|
||||
postData = JSON.stringify(postData);
|
||||
}
|
||||
init.body = postData;
|
||||
}
|
||||
init.headers['X-SM-Token'] = Settings.app('token');
|
||||
// init.headers = new Headers(init.headers);
|
||||
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) {
|
||||
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(Notifications.JsonParse);
|
||||
return {
|
||||
Result: false,
|
||||
ErrorCode: 952, // Notifications.JsonParse
|
||||
ErrorMessage: e.message,
|
||||
ErrorMessageAdditional: data
|
||||
}
|
||||
}
|
||||
*/
|
||||
return response.json();
|
||||
});
|
||||
};
|
||||
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue