diff --git a/EnvelopeGenerator.Web/wwwroot/js/envelope-api.js b/EnvelopeGenerator.Web/wwwroot/js/envelope-api.js index 20564c93..36dc32fd 100644 --- a/EnvelopeGenerator.Web/wwwroot/js/envelope-api.js +++ b/EnvelopeGenerator.Web/wwwroot/js/envelope-api.js @@ -33,6 +33,14 @@ 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); } @@ -48,8 +56,7 @@ function signEnvelope(annotations) { } async function getAnnotationParams(leftInInch = 0, topInInch = 0, inchToPointFactor = 72) { - const annotParams = await getRequest("/api/Config/Annotations") - .then(res => res.json()); + const annotParams = await getJson("/api/Config/Annotations"); for (var key in annotParams) { var annot = annotParams[key]; @@ -77,8 +84,7 @@ function redirRejected() { } async function setLanguage(language) { - const hasLang = await getRequest('/api/localization/lang') - .then(res => res.json()) + const hasLang = await getJson('/api/localization/lang') .then(langs => langs.includes(language)); if (hasLang) diff --git a/EnvelopeGenerator.Web/wwwroot/js/envelope-api.min.js b/EnvelopeGenerator.Web/wwwroot/js/envelope-api.min.js index ba73abde..38073145 100644 --- a/EnvelopeGenerator.Web/wwwroot/js/envelope-api.min.js +++ b/EnvelopeGenerator.Web/wwwroot/js/envelope-api.min.js @@ -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`}); \ No newline at end of file +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`}); \ No newline at end of file