Spaces:
Runtime error
Runtime error
Download src/lib/utils/fetchJSON.ts from evijit/pluralchat: direct link, hf CLI and curl.
- Browser
- Download file 603 Bytes
-
https://huggingface.co/spaces/evijit/pluralchat/resolve/refs%2Fpr%2F1/src/lib/utils/fetchJSON.ts
- Command line
-
hf download hf://spaces/evijit/pluralchat@refs/pr/1/src/lib/utils/fetchJSON.ts
-
curl -L -o fetchJSON.ts https://huggingface.co/spaces/evijit/pluralchat/resolve/refs%2Fpr%2F1/src/lib/utils/fetchJSON.ts
603 Bytes
| export async function fetchJSON<T>( | |
| url: string, | |
| options?: { | |
| fetch?: typeof window.fetch; | |
| allowNull?: boolean; | |
| } | |
| ): Promise<T> { | |
| const response = await (options?.fetch ?? fetch)(url); | |
| if (!response.ok) { | |
| throw new Error(`Failed to fetch ${url}: ${response.status} ${response.statusText}`); | |
| } | |
| // Handle empty responses (which parse to null) | |
| const text = await response.text(); | |
| if (!text || text.trim() === "") { | |
| if (options?.allowNull) { | |
| return null as T; | |
| } | |
| throw new Error(`Received empty response from ${url} but allowNull is not set to true`); | |
| } | |
| return JSON.parse(text); | |
| } | |