Last active
January 2, 2024 21:17
-
-
Save giovannibenussi/5427e23a230ba5ab7b2e6a7fd04e1249 to your computer and use it in GitHub Desktop.
Turso with a Custom Fetch Function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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