Moved rl.fetch and rl.fetchJSON to boot.js so that AppData can be fetched as JSON

This commit is contained in:
the-djmaze 2023-02-21 09:04:47 +01:00
parent 91bc5931fd
commit 8b70fee072
3 changed files with 80 additions and 84 deletions

View file

@ -82,7 +82,78 @@ window.rl = {
rl.app.refresh();
},
loadScript: loadScript
loadScript: loadScript,
fetch: (resource, init, postData) => {
init = Object.assign({
mode: 'same-origin',
cache: 'no-cache',
redirect: 'error',
referrerPolicy: 'no-referrer',
credentials: 'same-origin',
headers: {}
}, init);
let asJSON = 1,
XToken = RL_APP_DATA.System?.token,
object = {};
if (postData) {
init.method = 'POST';
if (postData instanceof FormData) {
postData.forEach((value, key) => {
if (value instanceof File) {
asJSON = 0;
} else if (!Reflect.has(object, key)) {
object[key] = value;
} else {
Array.isArray(object[key]) || (object[key] = [object[key]]);
object[key].push(value);
}
});
if (asJSON) {
postData = object;
// postData.XToken = XToken;
} else {
XToken && postData.set('XToken', XToken);
}
}
if (asJSON) {
init.headers['Content-Type'] = 'application/json';
postData = JSON.stringify(postData);
}
init.body = postData;
}
XToken && (init.headers['X-SM-Token'] = XToken);
// init.headers = new Headers(init.headers);
return fetch(resource, init);
},
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();
});
}
};
if (!navigator.cookieEnabled) {
@ -92,7 +163,8 @@ if (!navigator.cookieEnabled) {
eId('loading').hidden = true;
eId('BadBrowser').hidden = false;
} else {
loadScript(qUri(`${admin ? 'Admin' : ''}AppData/0/${Math.random().toString().slice(2)}/`))
rl.fetchJSON(qUri(`${admin ? 'Admin' : ''}AppData/0/${Math.random().toString().slice(2)}/`))
.then(data => rl.initData(data))
.catch(e => showError(e));
}

74
dev/bootstrap.js vendored
View file

@ -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();
});
};
};

View file

@ -631,18 +631,16 @@ class ServiceActions
private function localAppData(bool $bAdmin = false) : string
{
\header('Content-Type: application/javascript; charset=utf-8');
\header('Content-Type: application/json; charset=utf-8');
$this->oHttp->ServerNoCache();
try {
$sResult = 'rl.initData('
. Utils::jsonEncode($this->oActions->AppData($bAdmin))
. ');';
$sResult = Utils::jsonEncode($this->oActions->AppData($bAdmin));
$this->Logger()->Write($sResult, \LOG_INFO, 'APPDATA');
return $sResult;
} catch (\Throwable $e) {
return 'alert(' . \json_encode('ERROR: ' . $e->getMessage()) . ');';
} catch (\Throwable $oException) {
$self->Logger()->WriteExceptionShort($oException);
\MailSo\Base\Http::StatusHeader(500);
return $oException->getMessage();
}
}