UNPKG

@darwino/darwino

Version:

A set of Javascript classes and utilities

161 lines (138 loc) 5.09 kB
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty"; function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } /*!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 = _objectSpread({}, 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); var jsonContent = null; var 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 (var i = 0; i < parts.length; i++) { var 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> var 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) { var qs = ""; for (var pa in params) { var v = params[pa].toString(); if (qs) qs += '&'; qs += encodeURIComponent(pa) + (v ? '=' + encodeURIComponent(v) : ""); } return qs; } return ""; } export function addQueryString(url, qs) { if (qs) { var ch = url.indexOf('?') >= 0 ? '&' : '?'; url += ch + qs; } return url; } //# sourceMappingURL=Fetch.js.map