export * from "./client-hints"; export * from "./composeEvents"; export * from "./csv"; export * from "./debounce"; export * from "./debugEvents"; export * from "./dereferenceId"; export * from "./LazyPreload"; export * from "./makeColumn"; export * from "./nanoid"; export * from "./request-info"; export * from "./rollup"; export * from "./setPath"; export * from "./shallow"; export * from "./webauthn"; export function maybeDate(date: string | number | Date | undefined) { if (!date) { return undefined; } if (date instanceof Date) { return date; } if (typeof date === "number") { if (isNaN(date)) { return undefined; } return new Date(date); } return new Date(date); } interface HTTPErrorOptions extends ErrorOptions { status?: number; } class HTTPError extends Error { status?: number; constructor(message: string, options?: HTTPErrorOptions) { super(message, { cause: options?.cause }); this.status = options?.status; } } export async function checkResponse(response: Response) { const message = await response.text(); if (!response.ok) { throw new HTTPError(message || response.statusText, { status: response.status, }); } // If the response is JSON, parse it if (response.headers.get("Content-Type")?.includes("application/json")) { try { return JSON.parse(message); } catch (e) { console.error("Endpoint did not return JSON", message); return message; } } else { return message; } }