Added automatic injection of the envKey query parameter into all request URLs within sendRequest. Updated URL handling to use the URL API, ensuring consistent parameter merging and preventing missing envKey issues.
111 lines
2.7 KiB
JavaScript
111 lines
2.7 KiB
JavaScript
//#region parameters
|
|
const env = Object.freeze({
|
|
__lazyXsrfToken: new Lazy(() => document.getElementsByName('__RequestVerificationToken')[0].value),
|
|
get xsrfToken() {
|
|
return this.__lazyXsrfToken.value;
|
|
}
|
|
})
|
|
|
|
const url = Object.freeze({
|
|
reject: `/api/annotation/reject`,
|
|
share: `/api/readonly`
|
|
});
|
|
//#endregion
|
|
|
|
//#region request helper methods
|
|
function sendRequest(method, url, body = undefined) {
|
|
const urlObj = new URL(url, window.location.origin);
|
|
if (!urlObj.searchParams.has("envKey")) {
|
|
urlObj.searchParams.set("envKey", ENV_KEY);
|
|
}
|
|
|
|
const options = {
|
|
credentials: 'include',
|
|
method: method,
|
|
headers: {
|
|
'X-XSRF-TOKEN': env.xsrfToken
|
|
}
|
|
}
|
|
|
|
if (body !== undefined) {
|
|
options.body = JSON.stringify(body);
|
|
options.headers['Content-Type'] = 'application/json';
|
|
}
|
|
|
|
return fetch(urlObj, options);
|
|
}
|
|
|
|
function getRequest(url) {
|
|
return sendRequest('GET', url);
|
|
}
|
|
|
|
function getJson(url) {
|
|
return sendRequest('GET', url).then(res => {
|
|
if (res.ok)
|
|
return res.json();
|
|
throw new Error(`Request failed with status ${res.status}`);
|
|
});
|
|
}
|
|
|
|
function postRequest(url, body = undefined) {
|
|
return sendRequest('POST', url, body);
|
|
}
|
|
|
|
function reload() {
|
|
window.location.reload();
|
|
}
|
|
|
|
function redirect(url) {
|
|
window.location.href = url;
|
|
}
|
|
//#endregion
|
|
|
|
//#region envelope
|
|
function signEnvelope(annotations) {
|
|
return postRequest(`/api/annotation`, annotations)
|
|
}
|
|
|
|
async function getAnnotationParams(leftInInch = 0, topInInch = 0, inchToPointFactor = 72) {
|
|
const annotParams = await getJson("/api/Config/Annotations");
|
|
|
|
for (var key in annotParams) {
|
|
var annot = annotParams[key];
|
|
annot.width *= inchToPointFactor;
|
|
annot.height *= inchToPointFactor;
|
|
annot.left += leftInInch - 0.7;
|
|
annot.left *= inchToPointFactor;
|
|
annot.top += topInInch - 0.5;
|
|
annot.top *= inchToPointFactor;
|
|
}
|
|
|
|
return annotParams;
|
|
}
|
|
|
|
function rejectEnvelope(reason) {
|
|
return postRequest(url.reject, reason);
|
|
}
|
|
|
|
function shareEnvelope(receiverMail, dateValid) {
|
|
return postRequest(url.share, { receiverMail: receiverMail, dateValid: dateValid });
|
|
}
|
|
//#endregion
|
|
|
|
async function setLanguage(language) {
|
|
const hasLang = await getJson('/api/localization/lang')
|
|
.then(langs => langs.includes(language));
|
|
|
|
if (hasLang)
|
|
postRequest(`/api/localization/lang/${language}`)
|
|
.then(response => {
|
|
if (response.redirected)
|
|
redirect(response.url);
|
|
});
|
|
}
|
|
|
|
function logout() {
|
|
return postRequest(`/auth/logout`)
|
|
.then(res => {
|
|
if (res.ok)
|
|
window.location.href = "/";
|
|
});
|
|
} |