Spaces:
Sleeping
Sleeping
Download app/web/src/api.ts from HatemSaadallah/aemon: direct link, hf CLI and curl.
- Browser
- Download file 1.26 kB
-
https://huggingface.co/spaces/HatemSaadallah/aemon/resolve/main/app/web/src/api.ts
- Command line
-
hf download hf://spaces/HatemSaadallah/aemon/app/web/src/api.ts
-
curl -L -o api.ts https://huggingface.co/spaces/HatemSaadallah/aemon/resolve/main/app/web/src/api.ts
1.26 kB
| // One request to the FastAPI backend. Signed out: back to the sign-in page. Errors carry the server's own message. | |
| export async function api<T>(path: string, options?: RequestInit): Promise<T> { | |
| const response = await fetch(path, { credentials: 'same-origin', ...options }); | |
| if (response.status === 401) { | |
| window.location.replace('/login'); | |
| throw new Error('Signed out'); | |
| } | |
| const data: unknown = await response.json().catch(() => ({})); | |
| if (!response.ok) { | |
| const detail = data && typeof data === 'object' && 'detail' in data ? (data as { detail: unknown }).detail : undefined; | |
| throw new Error(typeof detail === 'string' ? detail : `Request failed: ${response.status}`); | |
| } | |
| return data as T; | |
| } | |
| export const postJSON = <T>(path: string, body: unknown): Promise<T> => | |
| api<T>(path, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body) }); | |
| export const errorText = (error: unknown): string => (error instanceof Error ? error.message : String(error)); | |
| export const sendJSON = <T>(path: string, method: 'PATCH' | 'DELETE', body?: unknown): Promise<T> => | |
| api<T>(path, { method, headers: { 'Content-Type': 'application/json' }, body: body === undefined ? undefined : JSON.stringify(body) }); | |