refactor: unify and simplify request helper methods
This commit: - Replaces `createRequest`, `createPost`, and `submitForm` with a single `sendRequest` helper. - Adds `getRequest` and `postRequest` wrappers for clarity. - Removes redundant content type constants and parameters. - Updates envelope-related methods (`signEnvelope`, `rejectEnvelope`, `shareEnvelope`) to use the new request helpers. - Simplifies `redirect` and `redirRejected` functions.
This commit is contained in:
parent
f1f4c6eaef
commit
ceff62cb64
@ -1,3 +1,4 @@
|
|||||||
|
//#region parameters
|
||||||
const env = {
|
const env = {
|
||||||
xsrfToken: document.getElementsByName('__RequestVerificationToken')[0].value,
|
xsrfToken: document.getElementsByName('__RequestVerificationToken')[0].value,
|
||||||
envKey: document.querySelector('meta[name="env-key"]').getAttribute('content')
|
envKey: document.querySelector('meta[name="env-key"]').getAttribute('content')
|
||||||
@ -8,53 +9,53 @@ const url = {
|
|||||||
rejectRedir: `/envelope/${env.envKey}`,
|
rejectRedir: `/envelope/${env.envKey}`,
|
||||||
share: `/api/readonly`
|
share: `/api/readonly`
|
||||||
};
|
};
|
||||||
|
//#endregion
|
||||||
|
|
||||||
//#region request helper methods
|
//#region request helper methods
|
||||||
function submitForm(form) {
|
function sendRequest(method, url, body = undefined) {
|
||||||
fetch(form.action, {
|
const options = {
|
||||||
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',
|
credentials: 'include',
|
||||||
method: method,
|
method: method,
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': contentType,
|
|
||||||
'X-XSRF-TOKEN': env.xsrfToken
|
'X-XSRF-TOKEN': env.xsrfToken
|
||||||
},
|
}
|
||||||
body: JSON.stringify(body)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function createPost(url, body, contentType = 'application/json') {
|
if (body !== undefined) {
|
||||||
return createRequest('POST', url, body, contentType);
|
options.body = JSON.stringify(body);
|
||||||
|
options.headers['Content-Type'] = 'application/json';
|
||||||
|
}
|
||||||
|
|
||||||
|
return fetch(url, options);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getRequest(url) {
|
||||||
|
return sendRequest('GET', url);
|
||||||
|
}
|
||||||
|
|
||||||
|
function postRequest(url, body) {
|
||||||
|
return sendRequest('POST', url, body);
|
||||||
}
|
}
|
||||||
|
|
||||||
function redirect(url) {
|
function redirect(url) {
|
||||||
return window.location.href = url;
|
window.location.href = url;
|
||||||
}
|
}
|
||||||
//#endregion
|
//#endregion
|
||||||
|
|
||||||
//#region envelope
|
//#region envelope
|
||||||
function signEnvelope(annotations) {
|
function signEnvelope(annotations) {
|
||||||
return createPost(`/api/annotation`, annotations, contentType)
|
return postRequest(`/api/annotation`, annotations)
|
||||||
}
|
}
|
||||||
|
|
||||||
function rejectEnvelope(reason) {
|
function rejectEnvelope(reason) {
|
||||||
return createPost(url.reject, reason, Content.JSON);
|
return postRequest(url.reject, reason);
|
||||||
}
|
}
|
||||||
|
|
||||||
function shareEnvelope(receiverMail, dateValid) {
|
function shareEnvelope(receiverMail, dateValid) {
|
||||||
return createPost(url.share, { receiverMail: receiverMail, dateValid: dateValid }, Content.JSON);
|
return postRequest(url.share, { receiverMail: receiverMail, dateValid: dateValid });
|
||||||
}
|
}
|
||||||
//#endregion
|
//#endregion
|
||||||
|
|
||||||
function redirRejected() {
|
function redirRejected() {
|
||||||
return redirect(url.rejectRedir);
|
redirect(url.rejectRedir);
|
||||||
}
|
}
|
||||||
@ -1 +1 @@
|
|||||||
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":env.xsrfToken},body:JSON.stringify(i)})}function createPost(n,t,i="application/json"){return createRequest("POST",n,t,i)}function redirect(n){return window.location.href=n}function signEnvelope(n){return createPost(`/api/annotation`,n,contentType)}function rejectEnvelope(n){return createPost(url.reject,n,Content.JSON)}function shareEnvelope(n,t){return createPost(url.share,{receiverMail:n,dateValid:t},Content.JSON)}function redirRejected(){return redirect(url.rejectRedir)}const env={xsrfToken:document.getElementsByName("__RequestVerificationToken")[0].value,envKey:document.querySelector('meta[name="env-key"]').getAttribute("content")},url={reject:`/api/annotation/reject`,rejectRedir:`/envelope/${env.envKey}`,share:`/api/readonly`};
|
function sendRequest(n,t,i=undefined){const r={credentials:"include",method:n,headers:{"X-XSRF-TOKEN":env.xsrfToken}};return i!==undefined&&(r.body=JSON.stringify(i),r.headers["Content-Type"]="application/json"),fetch(t,r)}function getRequest(n){return sendRequest("GET",n)}function postRequest(n,t){return sendRequest("POST",n,t)}function redirect(n){window.location.href=n}function signEnvelope(n){return postRequest(`/api/annotation`,n)}function rejectEnvelope(n){return postRequest(url.reject,n)}function shareEnvelope(n,t){return postRequest(url.share,{receiverMail:n,dateValid:t})}function redirRejected(){redirect(url.rejectRedir)}const env={xsrfToken:document.getElementsByName("__RequestVerificationToken")[0].value,envKey:document.querySelector('meta[name="env-key"]').getAttribute("content")},url={reject:`/api/annotation/reject`,rejectRedir:`/envelope/${env.envKey}`,share:`/api/readonly`};
|
||||||
Loading…
x
Reference in New Issue
Block a user