"use strict"; const Promise = require("bluebird"); module.exports = function createApiRequester(apiUrl) { return function apiRequest(path, body) { return Promise.try(() => { let targetUrl = apiUrl + path; return Promise.try(() => { return fetch(targetUrl, { body: JSON.stringify(body), headers: { 'content-type': 'application/json' }, method: 'POST', }); }).then((response) => { if (response.status >= 200 && response.status < 400) { return response.json(); } else { throw new Error(`Non-200 response code for ${targetUrl}: ${response.status}`); } }); }); }; };