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}`, { headers: { Accept: "application/json" }, ...options }).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"; };