Skip to content

Instantly share code, notes, and snippets.

@giovannibenussi
Last active January 2, 2024 21:17
Show Gist options
  • Save giovannibenussi/5427e23a230ba5ab7b2e6a7fd04e1249 to your computer and use it in GitHub Desktop.
Save giovannibenussi/5427e23a230ba5ab7b2e6a7fd04e1249 to your computer and use it in GitHub Desktop.
Turso with a Custom Fetch Function
import { createClient } from "@libsql/client";
import { fetch } from "@libsql/hrana-client";
function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
export async function retryOnceOnError(fn: any) {
try {
return await fn();
} catch (e) {
await sleep(5000);
return await fn();
}
}
function customFetch(request: Request): Promise<Response> {
return retryOnceOnError(() => fetch(request));
}
const client = createClient({
url: process.env.TURSO_DATABASE_URL as string,
authToken: process.env.TURSO_DATABASE_TOKEN,
fetch: customFetch,
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment