UNPKG

@focuson/utils

Version:

Common utilities for the @focuson project

97 lines (96 loc) 3.52 kB
"use strict"; /** This is a wrapper for the javascript fetch function. The input parameters are the same as for fetch. The output is the statuscode and a T, where T is the expected json type * * T is probably not meaningful if the status code is not a 2xx * */ Object.defineProperty(exports, "__esModule", { value: true }); exports.loadingCursorFetch = exports.fetchWithDelay = exports.fetchWithPrefix = exports.loggingResultFetchFn = exports.loggingFetchFn = exports.defaultFetchFn = exports.delay = exports.isNode = exports.setIsNodeFetchForTests = void 0; function setIsNodeFetchForTests() { exports.isNode = true; } exports.setIsNodeFetchForTests = setIsNodeFetchForTests; exports.isNode = false; // try {isNode= this===global;}catch(e){isNode= false;} // const actualFetch = isNode ? require ( "node-fetch" ) : fetch function delay(ms) { return new Promise(resolve => { setTimeout(() => { resolve({}); }, ms); }); } exports.delay = delay; var nodeFetch = undefined; function actualFetch(requestInfo, init) { if (exports.isNode) { if (!nodeFetch) { nodeFetch = require("node-fetch"); } return nodeFetch(requestInfo, init); } return fetch(requestInfo, init); } /** Normally we would use the defaultFetchFn or the loggingFetchFn */ const defaultFetchFn = (re, init) => { if (re === "") throw Error('calling defaultFetchFn with empty string as url'); try { return actualFetch(re, init).then((r) => r.ok ? r.json().then((json) => [r.status, json]) : r.text().then((text) => [r.status, text])).catch(e => { console.error(e); throw e; }); } catch (e) { console.error(e); return Promise.reject(e); } }; exports.defaultFetchFn = defaultFetchFn; /** Normally we would use the defaultFetchFn or the loggingFetchFn */ function loggingFetchFn(re, init) { console.log("fetching from", re, init); return (0, exports.defaultFetchFn)(re, init); } exports.loggingFetchFn = loggingFetchFn; /** Normally we would use the defaultFetchFn or the loggingFetchFn */ function loggingResultFetchFn(re, init) { console.log("fetching from", re, init); return (0, exports.defaultFetchFn)(re, init); } exports.loggingResultFetchFn = loggingResultFetchFn; function fetchWithPrefix(prefix, fetchFn) { return (re, init) => { if (typeof re === 'string') return fetchFn(prefix + re, init); else throw new Error(`Cannot handle request ${re}`); }; } exports.fetchWithPrefix = fetchWithPrefix; function fetchWithDelay(ms, fetchFn) { return (re, init) => delay(ms).then(() => fetchFn(re, init)); } exports.fetchWithDelay = fetchWithDelay; function loadingCursorFetch(fetchFn) { var count = 0; return (re, init) => { // console.log ( 'loadingCursorFetch', count ) if (count === 0) { document.body.style.cursor = "wait"; // console.log ( 'loadingCursorFetch - wait' ) } count += 1; return fetchFn(re, init).finally(() => { // console.log ( 'loadingCursorFetch - finally', count ) count -= 1; if (count === 0) if (count === 0) { // console.log ( 'loadingCursorFetch - back to default', count ) document.body.style.cursor = "default"; } }); }; } exports.loadingCursorFetch = loadingCursorFetch;