refactor: replace repeated GET+JSON parsing with reusable getJson helper
This commit is contained in:
parent
9c730e8f42
commit
877c88d52b
@ -33,6 +33,14 @@ function getRequest(url) {
|
|||||||
return sendRequest('GET', 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) {
|
function postRequest(url, body = undefined) {
|
||||||
return sendRequest('POST', url, body);
|
return sendRequest('POST', url, body);
|
||||||
}
|
}
|
||||||
@ -48,8 +56,7 @@ function signEnvelope(annotations) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function getAnnotationParams(leftInInch = 0, topInInch = 0, inchToPointFactor = 72) {
|
async function getAnnotationParams(leftInInch = 0, topInInch = 0, inchToPointFactor = 72) {
|
||||||
const annotParams = await getRequest("/api/Config/Annotations")
|
const annotParams = await getJson("/api/Config/Annotations");
|
||||||
.then(res => res.json());
|
|
||||||
|
|
||||||
for (var key in annotParams) {
|
for (var key in annotParams) {
|
||||||
var annot = annotParams[key];
|
var annot = annotParams[key];
|
||||||
@ -77,8 +84,7 @@ function redirRejected() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function setLanguage(language) {
|
async function setLanguage(language) {
|
||||||
const hasLang = await getRequest('/api/localization/lang')
|
const hasLang = await getJson('/api/localization/lang')
|
||||||
.then(res => res.json())
|
|
||||||
.then(langs => langs.includes(language));
|
.then(langs => langs.includes(language));
|
||||||
|
|
||||||
if (hasLang)
|
if (hasLang)
|
||||||
|
|||||||
@ -1 +1 @@
|
|||||||
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=undefined){return sendRequest("POST",n,t)}function redirect(n){window.location.href=n}function signEnvelope(n){return postRequest(`/api/annotation`,n)}async function getAnnotationParams(n=0,t=0,i=72){var f,r;const u=await getRequest("/api/Config/Annotations").then(n=>n.json());for(f in u)r=u[f],r.width*=i,r.height*=i,r.left+=n-.7,r.left*=i,r.top+=t-.5,r.top*=i;return u}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)}async function setLanguage(n){const t=await getRequest("/api/localization/lang").then(n=>n.json()).then(t=>t.includes(n));t&&postRequest(`/api/localization/lang/${n}`).then(n=>{n.redirected&&redirect(n.url)})}function logout(){return postRequest(`/auth/logout`).then(n=>{n.ok&&(window.location.href="/")})}const env=Object.freeze({xsrfToken:document.getElementsByName("__RequestVerificationToken")[0].value,envKey:document.querySelector('meta[name="env-key"]').getAttribute("content")}),url=Object.freeze({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 getJson(n){return sendRequest("GET",n).then(n=>{if(n.ok)return n.json();throw new Error(`Request failed with status ${n.status}`);})}function postRequest(n,t=undefined){return sendRequest("POST",n,t)}function redirect(n){window.location.href=n}function signEnvelope(n){return postRequest(`/api/annotation`,n)}async function getAnnotationParams(n=0,t=0,i=72){var f,r;const u=await getJson("/api/Config/Annotations");for(f in u)r=u[f],r.width*=i,r.height*=i,r.left+=n-.7,r.left*=i,r.top+=t-.5,r.top*=i;return u}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)}async function setLanguage(n){const t=await getJson("/api/localization/lang").then(t=>t.includes(n));t&&postRequest(`/api/localization/lang/${n}`).then(n=>{n.redirected&&redirect(n.url)})}function logout(){return postRequest(`/auth/logout`).then(n=>{n.ok&&(window.location.href="/")})}const env=Object.freeze({xsrfToken:document.getElementsByName("__RequestVerificationToken")[0].value,envKey:document.querySelector('meta[name="env-key"]').getAttribute("content")}),url=Object.freeze({reject:`/api/annotation/reject`,rejectRedir:`/envelope/${env.envKey}`,share:`/api/readonly`});
|
||||||
Loading…
x
Reference in New Issue
Block a user