- Removed unused Content class - Converted async arrow functions to standard function declarations - Added default 'application/json' for createRequest and createPost - Improved readability and consistency across API helpers
65 lines
1.5 KiB
JavaScript
65 lines
1.5 KiB
JavaScript
class API {
|
|
static get REJECT_URL() {
|
|
return `/api/annotation/reject`;
|
|
}
|
|
|
|
static get REJECT_REDIR_URL() {
|
|
return `/envelope/${API.ENV_KEY}`;
|
|
}
|
|
|
|
static get SHARE_URL() {
|
|
return `/api/readonly`
|
|
}
|
|
|
|
static __XSRF_TOKEN
|
|
static get XSRF_TOKEN() {
|
|
API.__XSRF_TOKEN ??= document.getElementsByName('__RequestVerificationToken')[0].value;
|
|
return API.__XSRF_TOKEN;
|
|
}
|
|
|
|
static get ENV_KEY() {
|
|
return ENV_KEY ?? document.querySelector('meta[name="env-key"]').getAttribute('content');
|
|
}
|
|
}
|
|
|
|
function submitForm(form) {
|
|
fetch(form.action, {
|
|
method: form.method,
|
|
body: new FormData(form),
|
|
headers: {
|
|
"X-Requested-With": "XMLHttpRequest"
|
|
}
|
|
})
|
|
}
|
|
|
|
function createRequest(method, url, body, contentType = 'application/json') {
|
|
return fetch(url, {
|
|
credentials: 'include',
|
|
method: method,
|
|
headers: {
|
|
'Content-Type': contentType,
|
|
'X-XSRF-TOKEN': API.XSRF_TOKEN
|
|
},
|
|
body: JSON.stringify(body)
|
|
})
|
|
}
|
|
|
|
function createPost(url, body, contentType = 'application/json') {
|
|
return createRequest('POST', url, body, contentType);
|
|
}
|
|
|
|
function rejectEnvelope(reason) {
|
|
return createPost(API.REJECT_URL, reason, Content.JSON);
|
|
}
|
|
|
|
function redirect(url) {
|
|
return window.location.href = url;
|
|
}
|
|
|
|
function redirRejected() {
|
|
return redirect(API.REJECT_REDIR_URL);
|
|
}
|
|
|
|
function shareEnvelope(receiverMail, dateValid) {
|
|
return createPost(API.SHARE_URL, { receiverMail: receiverMail, dateValid: dateValid }, Content.JSON);
|
|
} |