/* Xeslint-disable */ import store from "../store"; import router from "../router"; import auth from "./auth"; import errorHandler from "./errorhandler"; var stringifyPrimitive = function(v) { switch (typeof v) { case "string": return v; case "boolean": return v ? "true" : "false"; case "number": return isFinite(v) ? v : ""; default: return ""; } }; var devShowUnknownError = function(error) { if (errorHandler.devMode) { // eslint-disable-next-line console.log("apiutil::devShowUnknownError, error is:"); // eslint-disable-next-line console.log(error); // eslint-disable-next-line alert( "DEV ERROR apiutil::devShowUnknownError - unexpected error during api operation see console " ); } }; export default { status(response) { //Handle expected api errors if (response.status == 401) { //must reject if not authorized return Promise.reject(new Error("401 - NOT AUTHORIZED")); } if (response.status >= 200 && response.status < 300) { return Promise.resolve(response); } else { //log unhandled api error store.commit( "logItem", "API error: status=" + response.status + ", statusText=" + response.statusText + ", url=" + response.url ); //let it float up for dealing with by caller(s) return Promise.resolve(response); } }, json(response) { return response.json(); }, apiErrorToHumanString(apiError) { //empty error object? if (!apiError) { return "apiErrorToHumanString():: Empty API eror, unknown"; } //convert to readable string return JSON.stringify(apiError); }, patchAuthorizedHeaders() { return { //Accept: "application/json, text/plain, */*", Accept: "application/json", "Content-Type": "application/json-patch+json", Authorization: "Bearer " + store.state.apiToken }; }, postAuthorizedHeaders() { return { Accept: "application/json", "Content-Type": "application/json", Authorization: "Bearer " + store.state.apiToken }; }, postUnAuthorizedHeaders() { return { Accept: "application/json", "Content-Type": "application/json" }; }, fetchPostNoAuthOptions(data) { return { method: "post", mode: "cors", headers: this.postUnAuthorizedHeaders(), body: JSON.stringify(data) }; }, fetchPostOptions(data) { return { method: "post", mode: "cors", headers: this.postAuthorizedHeaders(), body: JSON.stringify(data) }; }, fetchPutOptions(data) { return { method: "put", mode: "cors", headers: this.postAuthorizedHeaders(), body: JSON.stringify(data) }; }, fetchGetOptions() { /* GET WITH AUTH */ return { method: "get", mode: "cors", headers: this.postAuthorizedHeaders() }; }, APIUrl(apiPath) { if ("" == store.state.apiUrl) { //construct the api url and store it //development location? if ( (window.location.hostname == "localhost" || window.location.hostname == "192.168.1.56") && window.location.port == "8080" ) { store.commit("setAPIURL", "http://localhost:7575/api/v8.0/"); store.commit("setHelpURL", "http://localhost:7575/docs/"); store.commit( "logItem", "apiutil::APIUrl -> setting to dev. mode: " + store.state.apiUrl ); } else { //production location //:/ store.commit( "setHelpURL", window.location.protocol + "//" + window.location.host + "/docs/" ); store.commit( "setAPIURL", window.location.protocol + "//" + window.location.host + "/api/v8.0/" ); store.commit( "logItem", "apiutil::APIUrl -> setting to: " + store.state.apiUrl ); } } return store.state.apiUrl + apiPath; }, ///////////////////////////// // ENCODE QUERY STRING // buildQuery(obj, sep, eq, name) { sep = sep || "&"; eq = eq || "="; if (obj === null) { obj = undefined; } if (typeof obj === "object") { return Object.keys(obj) .map(function(k) { var ks = encodeURIComponent(stringifyPrimitive(k)) + eq; if (Array.isArray(obj[k])) { return obj[k] .map(function(v) { return ks + encodeURIComponent(stringifyPrimitive(v)); }) .join(sep); } else { return ks + encodeURIComponent(stringifyPrimitive(obj[k])); } }) .filter(Boolean) .join(sep); } if (!name) return ""; return ( encodeURIComponent(stringifyPrimitive(name)) + eq + encodeURIComponent(stringifyPrimitive(obj)) ); }, /////////////////////////////////// // GET DATA FROM API SERVER // get(route) { var that = this; return new Promise(function(resolve, reject) { fetch(that.APIUrl(route), that.fetchGetOptions()) .then(that.status) .then(that.json) .then(response => { resolve(response); }) .catch(function(error) { //fundamental error, can't proceed with this call var errorMessage = "API error: GET route =" + route + ", message =" + error.message; store.commit("logItem", errorMessage); if (error.message && error.message.includes("401")) { store.commit( "logItem", "User is not authorized, redirecting to login" ); auth.logout(); router.push("/login"); } else { //This should never get called because any issue should be addressed above in a proper error handler devShowUnknownError(error); reject(error); } }); }); }, /////////////////////////////////// // POST / PUT DATA TO API SERVER // upsert(route, data) { var that = this; return new Promise(function(resolve, reject) { //determine if this is a new or existing record var fetchOptions = undefined; if (data.concurrencyToken) { fetchOptions = that.fetchPutOptions(data); } else { fetchOptions = that.fetchPostOptions(data); } fetch(that.APIUrl(route), fetchOptions) .then(that.status) .then(that.json) .then(response => { //Note: response.error indicates there is an error, however this is not an unusual condition //it could be validation errors or other general error so we need to treat it here like it's normal //and let the caller deal with it appropriately resolve(response); }) .catch(function(error) { //fundamental error, can't proceed with this call var errorMessage = "API error: UPSERT route =" + route + ", message =" + error.message; store.commit("logItem", errorMessage); if (error.message && error.message.includes("401")) { store.commit( "logItem", "User is not authorized, redirecting to login" ); auth.logout(); router.push("/login"); } else { //This should never get called because any issue should be addressed above in a proper error handler devShowUnknownError(error); reject(error); } }); }); } //new functions above here };