First commit

This commit is contained in:
2026-03-18 22:42:33 +09:00
commit 50657066a6
64 changed files with 5290 additions and 0 deletions
+6
View File
@@ -0,0 +1,6 @@
export default class lynqError extends Error {
constructor(message: string) {
super(message);
this.name = "lynqError";
}
}
+56
View File
@@ -0,0 +1,56 @@
import lynqError from "./error";
export default async function lynqFetch(
origin: string,
retryCount: number,
waitingTime: number,
endpoint: string,
init?: RequestInit,
) {
const waiting = (ms: number) =>
new Promise<void>(resolve => setTimeout(resolve, ms));
let lastError;
for (let i = 0; i < retryCount; i++) {
try {
if (init?.signal?.aborted) {
throw new DOMException("Aborted", "AbortError");
}
const req = await fetch(new URL(`/api/${endpoint}`, origin), init);
if (
Math.floor(req.status / 200) === 1 ||
Math.floor(req.status / 300) === 1
) {
return req;
}
if (Math.floor(req.status / 400) === 1)
throw new lynqError(`Client error: HTTP${req.status} - ${req.statusText}`);
lastError = new Error(`Request failed: HTTP${req.status} - ${req.statusText}`);
} catch (err) {
if (err instanceof DOMException && err.name === "AbortError") {
throw err;
}
if (
err instanceof lynqError &&
err.name === "Client error" &&
err.message.startsWith("HTTP4")
)
throw err;
lastError = err;
}
if (i < retryCount - 1) {
const waitTime = waitingTime * 2 ** i;
await waiting(waitTime);
}
}
throw lastError ?? new Error("Unknown Error");
}