UNPKG

respond-framework

Version:
49 lines (47 loc) 1.87 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.argsOut = exports.argsIn = exports.__undefined__ = void 0; exports.default = fetch; var _constants = require("./constants.js"); var _fetchWithTimeout = require("./fetchWithTimeout.js"); async function fetch(apiUrl = defaultApiUrl, body, respond) { const { table, method } = body; const url = `${apiUrl}/${table}/${method}`; const max = respond.options?.fetchTimeout ?? 12000; const start = new Date(); try { const res = await (0, _fetchWithTimeout.default)(url, { headers, method: 'POST', body: JSON.stringify(body) }, max); if (respond.state._serverDown) { respond.state._serverDown = false; respond.options.onServerUp?.(respond.state); } return res.json(); } catch { // timeout exceeded const elapsed = new Date() - start; if (elapsed < max) await timeout(max - elapsed); // instant throw happens when webserver is down respond.state._serverDown = true; respond.options.onServerDown?.(respond.state, body); return fetch(apiUrl, body, respond); // retry every 12 seconds -- see fetchWithTimeout.js (throws after 12 seconds of hanging) } } const defaultApiUrl = _constants.defaultOrigin + '/api'; const headers = { Accept: 'application/json', 'Content-Type': 'application/json' }; const argsIn = args => args.map(a => a === undefined ? __undefined__ : a); // undefined becomes null when stringified, but table functions may depend on undefined args and default parameters, so we convert this back to undefined server side exports.argsIn = argsIn; const argsOut = args => args.map(a => a === __undefined__ ? undefined : a); exports.argsOut = argsOut; const __undefined__ = exports.__undefined__ = '__undefined__'; const timeout = ms => new Promise(res => setTimeout(res, ms));