trilium/src/public/javascripts/services/server.js

118 lines
3.1 KiB
JavaScript
Raw Normal View History

import utils from './utils.js';
2019-10-20 16:00:18 +08:00
import toastService from "./toast.js";
2019-05-05 16:59:34 +08:00
const REQUEST_LOGGING_ENABLED = false;
2019-10-19 21:12:25 +08:00
function getHeaders(headers) {
// headers need to be lowercase because node.js automatically converts them to lower case
// so hypothetical protectedSessionId becomes protectedsessionid on the backend
// also avoiding using underscores instead of dashes since nginx filters them out by default
2019-10-19 21:12:25 +08:00
const allHeaders = {
...{
'trilium-source-id': glob.sourceId,
'x-csrf-token': glob.csrfToken
},
...headers
};
if (utils.isElectron()) {
// passing it explicitely here because of the electron HTTP bypass
2019-10-19 21:12:25 +08:00
allHeaders.cookie = document.cookie;
}
2019-10-19 21:12:25 +08:00
return allHeaders;
}
2019-10-19 21:12:25 +08:00
async function get(url, headers = {}) {
return await call('GET', url, null, headers);
}
2019-10-19 21:12:25 +08:00
async function post(url, data, headers = {}) {
return await call('POST', url, data, headers);
}
2019-10-19 21:12:25 +08:00
async function put(url, data, headers = {}) {
return await call('PUT', url, data, headers);
}
2019-10-19 21:12:25 +08:00
async function remove(url, headers = {}) {
return await call('DELETE', url, null, headers);
}
let i = 1;
const reqResolves = {};
2019-10-19 21:12:25 +08:00
async function call(method, url, data, headers = {}) {
2018-03-25 11:37:55 +08:00
if (utils.isElectron()) {
const ipc = require('electron').ipcRenderer;
const requestId = i++;
return new Promise((resolve, reject) => {
reqResolves[requestId] = resolve;
2019-05-05 16:59:34 +08:00
if (REQUEST_LOGGING_ENABLED) {
console.log(utils.now(), "Request #" + requestId + " to " + method + " " + url);
}
ipc.send('server-request', {
requestId: requestId,
2019-10-19 21:12:25 +08:00
headers: getHeaders(headers),
method: method,
url: "/" + baseApiUrl + url,
data: data
});
});
}
else {
2019-10-28 05:13:38 +08:00
return await ajax(url, method, data, headers);
}
}
2019-10-28 05:13:38 +08:00
async function ajax(url, method, data, headers) {
const options = {
url: baseApiUrl + url,
type: method,
2019-10-28 05:13:38 +08:00
headers: getHeaders(headers),
timeout: 60000
};
if (data) {
2018-03-28 09:46:38 +08:00
try {
options.data = JSON.stringify(data);
}
catch (e) {
console.log("Can't stringify data: ", data, " because of error: ", e)
}
options.contentType = "application/json";
}
return await $.ajax(options).catch(e => {
const message = "Error when calling " + method + " " + url + ": " + e.status + " - " + e.statusText;
2019-10-20 16:00:18 +08:00
toastService.showError(message);
toastService.throwError(message);
});
}
2018-04-06 11:17:19 +08:00
if (utils.isElectron()) {
const ipc = require('electron').ipcRenderer;
2018-03-26 01:13:26 +08:00
2018-04-06 11:17:19 +08:00
ipc.on('server-response', (event, arg) => {
2019-05-05 16:59:34 +08:00
if (REQUEST_LOGGING_ENABLED) {
console.log(utils.now(), "Response #" + arg.requestId + ": " + arg.statusCode);
}
2018-03-26 01:13:26 +08:00
2018-04-06 11:17:19 +08:00
reqResolves[arg.requestId](arg.body);
2018-03-26 01:13:26 +08:00
2018-04-06 11:17:19 +08:00
delete reqResolves[arg.requestId];
});
}
2018-03-26 01:13:26 +08:00
export default {
get,
post,
put,
remove,
ajax,
// don't remove, used from CKEditor image upload!
getHeaders
};