Spaces:
Running
Running
Download frontend/src/api.js from thebickersteth/voxpreference: direct link, hf CLI and curl.
- Browser
- Download file 1.53 kB
-
https://huggingface.co/spaces/thebickersteth/voxpreference/resolve/main/frontend/src/api.js
- Command line
-
hf download hf://spaces/thebickersteth/voxpreference/frontend/src/api.js
-
curl -L -o api.js https://huggingface.co/spaces/thebickersteth/voxpreference/resolve/main/frontend/src/api.js
1.53 kB
| const DEFAULT_TIMEOUT_MS = 25000; | |
| export const apiBaseUrl = import.meta.env.VITE_API_BASE_URL || ""; | |
| export async function fetchJson(path, options = {}) { | |
| const controller = new AbortController(); | |
| const timeoutMs = options.timeoutMs || DEFAULT_TIMEOUT_MS; | |
| const timeout = window.setTimeout(() => controller.abort(), timeoutMs); | |
| try { | |
| const response = await fetch(`${apiBaseUrl}${path}`, { | |
| ...options, | |
| signal: controller.signal, | |
| }); | |
| const text = await response.text(); | |
| let data = null; | |
| if (text) { | |
| try { | |
| data = JSON.parse(text); | |
| } catch { | |
| throw new Error("The server returned an unreadable response. Please try again."); | |
| } | |
| } | |
| if (!response.ok) { | |
| throw new Error( | |
| data?.detail || | |
| data?.errors?.join(" ") || | |
| data?.error || | |
| `Request failed with status ${response.status}.` | |
| ); | |
| } | |
| return data; | |
| } catch (error) { | |
| if (error.name === "AbortError") { | |
| throw new Error("The request timed out. Check your connection and try again."); | |
| } | |
| if (error instanceof TypeError) { | |
| throw new Error("Could not reach the server. Check your connection and try again."); | |
| } | |
| throw error; | |
| } finally { | |
| window.clearTimeout(timeout); | |
| } | |
| } | |
| // Analyses of audio take minutes (long recordings much longer), so they get a longer timeout. | |
| export function postForm(path, formData, timeoutMs = 30 * 60 * 1000) { | |
| return fetchJson(path, { method: "POST", body: formData, timeoutMs }); | |
| } | |