const hallwayPrefix = (() => { let prefix; return () => { if (prefix === undefined) { const meta = document.querySelector('meta[name="z-hallway-prefix"]'); prefix = meta ? meta.content : ""; } return prefix; }; })(); const fetchJson = (url, options) => fetch(`${hallwayPrefix()}${url}`, { ...options, headers: { Accept: "application/json", ...(options && options.headers) // npm package uses a default parameter } }).then(checkStatus); const checkStatus = response => { if (!response.ok) throw response; return response.json(); }; const getErrorMessage = response => { if (response.json) return response .json() .catch(error => { if (process.env.NODE_ENV !== "production") console.error(error); throw `${response.status} - ${response.statusText}`; }) .then(json => { if (process.env.NODE_ENV !== "production") console.error(json); if (json.csrf) location.reload(); throw json.message; }); if (process.env.NODE_ENV !== "production") console.error(response); throw "Unknown Error"; };