Download apps/readest-app/src/utils/fetch.ts from echodict/readest: direct link, hf CLI and curl.
- Browser
- Download file 924 Bytes
-
https://huggingface.co/datasets/echodict/readest/resolve/main/apps/readest-app/src/utils/fetch.ts
- Command line
-
hf download hf://datasets/echodict/readest/apps/readest-app/src/utils/fetch.ts
-
curl -L -o fetch.ts https://huggingface.co/datasets/echodict/readest/resolve/main/apps/readest-app/src/utils/fetch.ts
924 Bytes
| import { getAccessToken } from './access'; | |
| export const fetchWithTimeout = (url: string, options: RequestInit = {}, timeout = 10000) => { | |
| const controller = new AbortController(); | |
| const id = setTimeout(() => controller.abort('Request timed out'), timeout); | |
| return fetch(url, { | |
| ...options, | |
| signal: controller.signal, | |
| }).finally(() => clearTimeout(id)); | |
| }; | |
| export const fetchWithAuth = async (url: string, options: RequestInit) => { | |
| const token = await getAccessToken(); | |
| if (!token) { | |
| throw new Error('Not authenticated'); | |
| } | |
| const headers = { | |
| ...options.headers, | |
| Authorization: `Bearer ${token}`, | |
| }; | |
| const response = await fetch(url, { ...options, headers }); | |
| if (!response.ok) { | |
| const errorData = await response.json(); | |
| console.error('Error:', errorData.error || response.statusText); | |
| throw new Error(errorData.error || 'Request failed'); | |
| } | |
| return response; | |
| }; | |