refactor(api): replace arrow functions with function declarations and simplify defaults

- 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
This commit is contained in:
tekh 2025-09-19 09:59:57 +02:00
parent a8a73724e6
commit bac9aebbc3
2 changed files with 27 additions and 21 deletions

View File

@ -1,9 +1,3 @@
class Content {
static get JSON() {
return 'application/json';
}
}
class API {
static get REJECT_URL() {
return `/api/annotation/reject`;
@ -28,15 +22,17 @@ class API {
}
}
const submitForm = async form => await fetch(form.action, {
method: form.method,
body: new FormData(form),
headers: {
"X-Requested-With": "XMLHttpRequest"
}
})
function submitForm(form) {
fetch(form.action, {
method: form.method,
body: new FormData(form),
headers: {
"X-Requested-With": "XMLHttpRequest"
}
})
}
const createRequest = async (method, url, body, contentType) => {
function createRequest(method, url, body, contentType = 'application/json') {
return fetch(url, {
credentials: 'include',
method: method,
@ -48,12 +44,22 @@ const createRequest = async (method, url, body, contentType) => {
})
}
const createPost = (url, body, contentType) => createRequest('POST', url, body, contentType);
function createPost(url, body, contentType = 'application/json') {
return createRequest('POST', url, body, contentType);
}
const rejectEnvelope = (reason) => createPost(API.REJECT_URL, reason, Content.JSON);
function rejectEnvelope(reason) {
return createPost(API.REJECT_URL, reason, Content.JSON);
}
const redirect = (url) => window.location.href = url;
function redirect(url) {
return window.location.href = url;
}
const redirRejected = () => redirect(API.REJECT_REDIR_URL);
function redirRejected() {
return redirect(API.REJECT_REDIR_URL);
}
const shareEnvelope = (receiverMail, dateValid) => createPost(API.SHARE_URL, { receiverMail: receiverMail, dateValid: dateValid }, Content.JSON);
function shareEnvelope(receiverMail, dateValid) {
return createPost(API.SHARE_URL, { receiverMail: receiverMail, dateValid: dateValid }, Content.JSON);
}

View File

@ -1,2 +1,2 @@
class Content{static get JSON(){return"application/json"}}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(){return API.__XSRF_TOKEN??=document.getElementsByName("__RequestVerificationToken")[0].value,API.__XSRF_TOKEN}static get ENV_KEY(){return ENV_KEY??document.querySelector('meta[name="env-key"]').getAttribute("content")}}const submitForm=async n=>await fetch(n.action,{method:n.method,body:new FormData(n),headers:{"X-Requested-With":"XMLHttpRequest"}}),createRequest=async(n,t,i,r)=>fetch(t,{credentials:"include",method:n,headers:{"Content-Type":r,"X-XSRF-TOKEN":API.XSRF_TOKEN},body:JSON.stringify(i)}),createPost=(n,t,i)=>createRequest("POST",n,t,i),rejectEnvelope=n=>createPost(API.REJECT_URL,n,Content.JSON),redirect=n=>window.location.href=n,redirRejected=()=>redirect(API.REJECT_REDIR_URL),shareEnvelope=(n,t)=>createPost(API.SHARE_URL,{receiverMail:n,dateValid:t},Content.JSON);
function submitForm(n){fetch(n.action,{method:n.method,body:new FormData(n),headers:{"X-Requested-With":"XMLHttpRequest"}})}function createRequest(n,t,i,r="application/json"){return fetch(t,{credentials:"include",method:n,headers:{"Content-Type":r,"X-XSRF-TOKEN":API.XSRF_TOKEN},body:JSON.stringify(i)})}function createPost(n,t,i="application/json"){return createRequest("POST",n,t,i)}function rejectEnvelope(n){return createPost(API.REJECT_URL,n,Content.JSON)}function redirect(n){return window.location.href=n}function redirRejected(){return redirect(API.REJECT_REDIR_URL)}function shareEnvelope(n,t){return createPost(API.SHARE_URL,{receiverMail:n,dateValid:t},Content.JSON)}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(){return API.__XSRF_TOKEN??=document.getElementsByName("__RequestVerificationToken")[0].value,API.__XSRF_TOKEN}static get ENV_KEY(){return ENV_KEY??document.querySelector('meta[name="env-key"]').getAttribute("content")}}