@darwino/darwino
Version:
A set of Javascript classes and utilities
136 lines (125 loc) • 3.78 kB
JavaScript
/*!COPYRIGHT HEADER! - CONFIDENTIAL
*
* Darwino Inc Confidential.
*
* (c) Copyright Darwino Inc. 2014-2016.
*
* Notice: The information contained in the source code for these files is the property
* of Darwino Inc. which, with its licensors, if any, owns all the intellectual property
* rights, including all copyright rights thereto. Such information may only be used
* for debugging, troubleshooting and informational purposes. All other uses of this information,
* including any production or commercial uses, are prohibited.
*/
import Utils from "./Utils"
import DEV_OPTIONS from "./dev";
/**
* JavaScript fetch utilities
*
* The exported functions extend the JavaScript 'fetch' function by capturing HTTP error and
* throw an exception to the promise (standard 'fetch' only does that for HTTP errors). It also
* detects if the application is running in development mode ('webpack dev server') and then
* preprend the data server URL while ensuring that the cross domain credentials are also
* passed by the request.
*
*/
function isError(response) {
return !response.ok
}
export function fetchEx(input,init,config) {
input = makeUrl(input);
// if(DEV_OPTIONS.WEBPACK) {
// if(Utils.isString(input) && DEV_OPTIONS.serverPrefix) {
// if(input.indexOf("://")<0) {
// input = Utils.concatPath(DEV_OPTIONS.serverPrefix,input);
// }
// }
// }
init = {...init, credentials: DEV_OPTIONS.credentials} // "same-origin" for cookies
return fetch(input,init)
.then(response => {
if(((config && config.isError)||isError)(response)) {
return response.text().then(content => {
console.log("Fetch error "+response.status + ": " + response.statusText+"\n"+content);
let jsonContent = null;
let message = response.status + ": " + response.statusText
try {
jsonContent = JSON.parse(content);
if(jsonContent.message) {
message += "\n"+jsonContent.message
}
} catch(e) {} // Ok, not JSON
throw {message,statusText:response.statusText,status:response.status,content,jsonContent};
});
}
return response;
})
}
export function fetchJson(input,init,config) {
return fetchEx(input,init,config).then((response) => {
try {
return response.json();
} catch(e) {
throw {message:e.toString(),exception:e}
}
});
}
export function fetchText(input,init,config) {
return fetchEx(input,init,config).then((response) => {
return response.text();
});
}
export function makeUrl(url,parts,params) {
if(!url) url = ""
if(DEV_OPTIONS.WEBPACK) {
if(url.indexOf("://")<0 && DEV_OPTIONS.serverPrefix) {
url = Utils.concatPath(DEV_OPTIONS.serverPrefix,url);
}
}
if(parts && parts.length) {
for(let i=0; i<parts.length; i++) {
let part = parts[i].toString();
if(part) {
url = Utils.concatPath(url,encodeURIComponent(part));
}
}
}
if(params) {
return addQueryString(url,makeQueryString(params))
}
return url;
}
export function makeWsUrl(url) {
if(!url) url = ""
if(url.indexOf("://")<0) {
if(DEV_OPTIONS.WEBPACK) {
if(DEV_OPTIONS.wsPrefix) {
url = Utils.concatPath(DEV_OPTIONS.wsPrefix,url);
}
} else {
// Make an absolute URL using a <a>
const a = document.createElement('a');
a.href = url;
url = "ws" + a.href.substring(4); // Replace http by ws (works also with https -> wss)
}
}
return url;
}
export function makeQueryString(params) {
if(params) {
let qs = "";
for(let pa in params) {
let v = params[pa].toString();
if(qs) qs += '&'
qs += encodeURIComponent(pa) + (v ? ('=' + encodeURIComponent(v)) : "");
}
return qs;
}
return "";
}
export function addQueryString(url,qs) {
if(qs) {
const ch = url.indexOf('?')>=0 ? '&' : '?'
url += ch + qs
}
return url
}