18 lines
807 B
JavaScript
18 lines
807 B
JavaScript
// Anonymous server-side transport. Expected HTTP/network failures are values;
|
|
// callers can keep the search form usable without falling back to sample data.
|
|
export function createPaperTransport(origin, requestUpstream = fetch) {
|
|
return async function request(path) {
|
|
let response;
|
|
try {
|
|
response = await requestUpstream(new URL(path, origin.replace(/\/$/, "") + "/"), {
|
|
cache: "no-store", signal: AbortSignal.timeout(12000), redirect: "error",
|
|
});
|
|
} catch (error) {
|
|
return { ok: false, status: error?.name === "TimeoutError" || error?.name === "AbortError" ? 504 : 503 };
|
|
}
|
|
if (!response.ok) return { ok: false, status: response.status };
|
|
try { return { ok: true, data: await response.json() }; }
|
|
catch { return { ok: false, status: 502 }; }
|
|
};
|
|
}
|