Spaces:
Running
Running
Download frontend/src/shared/lib/httpClient.ts from Shadowfilebot/Bot: direct link, hf CLI and curl.
- Browser
- Download file 2.43 kB
-
https://huggingface.co/spaces/Shadowfilebot/Bot/resolve/main/frontend/src/shared/lib/httpClient.ts
- Command line
-
hf download hf://spaces/Shadowfilebot/Bot/frontend/src/shared/lib/httpClient.ts
-
curl -L -o httpClient.ts https://huggingface.co/spaces/Shadowfilebot/Bot/resolve/main/frontend/src/shared/lib/httpClient.ts
2.43 kB
| const TOKEN_KEY = 'panelthtuanit_token'; | |
| export function getToken(): string | null { | |
| if (typeof window === 'undefined') return null; | |
| try { | |
| return window.localStorage.getItem(TOKEN_KEY); | |
| } catch { | |
| return null; | |
| } | |
| } | |
| export function setToken(token: string | null): void { | |
| if (typeof window === 'undefined') return; | |
| try { | |
| if (token) { | |
| window.localStorage.setItem(TOKEN_KEY, token); | |
| document.cookie = `${TOKEN_KEY}=${token}; path=/; SameSite=Lax`; | |
| } else { | |
| window.localStorage.removeItem(TOKEN_KEY); | |
| document.cookie = `${TOKEN_KEY}=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT`; | |
| } | |
| } catch { | |
| // Ignore storage access errors | |
| } | |
| } | |
| export async function requestJson<T = unknown>( | |
| path: string, | |
| options: RequestInit = {}, | |
| withAuth = true, | |
| ): Promise<T> { | |
| const headers = new Headers(options.headers || {}); | |
| if (withAuth) { | |
| const token = getToken(); | |
| if (token && !headers.has('Authorization')) { | |
| headers.set('Authorization', `Bearer ${token}`); | |
| } | |
| } | |
| if (typeof options.body === 'string' && !headers.has('Content-Type')) { | |
| headers.set('Content-Type', 'application/json'); | |
| } | |
| const res = await fetch(path, { ...options, headers }); | |
| if (!res.ok) { | |
| let message = res.statusText || 'Yêu cầu thất bại'; | |
| try { | |
| const data = await res.json(); | |
| if (Array.isArray(data?.detail)) { | |
| message = data.detail | |
| .map((d: { msg?: string }) => d?.msg || JSON.stringify(d)) | |
| .join('; '); | |
| } else { | |
| message = data?.detail || data?.error || message; | |
| } | |
| } catch { | |
| // Non-JSON response | |
| } | |
| throw new Error(message); | |
| } | |
| const ct = res.headers.get('content-type') || ''; | |
| if (ct.includes('application/json')) { | |
| return (await res.json()) as T; | |
| } | |
| return (await res.text()) as unknown as T; | |
| } | |
| export async function downloadAuthenticatedBlob(path: string): Promise<Blob> { | |
| const token = getToken() || ''; | |
| const sep = path.includes('?') ? '&' : '?'; | |
| const url = `${path}${sep}token=${encodeURIComponent(token)}`; | |
| const res = await fetch(url, { | |
| headers: token ? { Authorization: `Bearer ${token}` } : {}, | |
| }); | |
| if (!res.ok) { | |
| let msg = res.statusText || 'Lỗi tải tệp'; | |
| try { | |
| const data = await res.json(); | |
| msg = data?.detail || data?.error || msg; | |
| } catch {} | |
| throw new Error(msg); | |
| } | |
| return await res.blob(); | |
| } | |