Spaces:
Running
Running
Download src/lib/api.ts from evalstate/mcp-webcam: direct link, hf CLI and curl.
- Browser
- Download file 3.29 kB
-
https://huggingface.co/spaces/evalstate/mcp-webcam/resolve/main/src/lib/api.ts
- Command line
-
hf download hf://spaces/evalstate/mcp-webcam/src/lib/api.ts
-
curl -L -o api.ts https://huggingface.co/spaces/evalstate/mcp-webcam/resolve/main/src/lib/api.ts
3.29 kB
| import type { | |
| AppConfigView, | |
| CaptureResultRequest, | |
| DashboardState, | |
| EventsConfig, | |
| FireEventRequest, | |
| FireEventResponse, | |
| } from '../../shared/api-types'; | |
| function withUser(path: string, user: string): string { | |
| const sep = path.includes('?') ? '&' : '?'; | |
| return `${path}${sep}user=${encodeURIComponent(user)}`; | |
| } | |
| async function requestJson<T>(url: string, init?: RequestInit): Promise<T> { | |
| const res = await fetch(url, { | |
| ...init, | |
| headers: { | |
| 'Content-Type': 'application/json', | |
| ...(init?.headers ?? {}), | |
| }, | |
| }); | |
| if (!res.ok) { | |
| const text = await res.text().catch(() => ''); | |
| throw new Error(text || `Request failed (${res.status})`); | |
| } | |
| if (res.status === 204) return undefined as T; | |
| const contentType = res.headers.get('content-type') ?? ''; | |
| if (contentType.includes('application/json')) { | |
| return (await res.json()) as T; | |
| } | |
| return undefined as T; | |
| } | |
| export function fetchConfig(): Promise<AppConfigView> { | |
| return requestJson<AppConfigView>('/api/config'); | |
| } | |
| export function fetchState(user: string): Promise<DashboardState> { | |
| return requestJson<DashboardState>(withUser('/api/state', user)); | |
| } | |
| export function postCaptureResult( | |
| requestId: string, | |
| user: string, | |
| body: CaptureResultRequest, | |
| ): Promise<void> { | |
| return requestJson<void>(withUser(`/api/capture/${encodeURIComponent(requestId)}`, user), { | |
| method: 'POST', | |
| body: JSON.stringify(body), | |
| }); | |
| } | |
| export function makeTabPrimary(tabId: string, user: string): Promise<void> { | |
| return requestJson<void>(withUser(`/api/tabs/${encodeURIComponent(tabId)}/primary`, user), { | |
| method: 'POST', | |
| }); | |
| } | |
| export interface TabStatusPayload { | |
| frozen?: boolean; | |
| deviceLabel?: string; | |
| width?: number; | |
| height?: number; | |
| } | |
| export function postTabStatus(tabId: string, user: string, status: TabStatusPayload): Promise<void> { | |
| return requestJson<void>(withUser(`/api/tabs/${encodeURIComponent(tabId)}/status`, user), { | |
| method: 'POST', | |
| body: JSON.stringify(status), | |
| }); | |
| } | |
| export function fireEventApi(user: string, payload: FireEventRequest): Promise<FireEventResponse> { | |
| return requestJson<FireEventResponse>(withUser('/api/events/fire', user), { | |
| method: 'POST', | |
| body: JSON.stringify(payload), | |
| }); | |
| } | |
| export function patchEventsConfig( | |
| user: string, | |
| patch: Partial<EventsConfig>, | |
| ): Promise<EventsConfig> { | |
| return requestJson<EventsConfig>(withUser('/api/events/config', user), { | |
| method: 'PATCH', | |
| body: JSON.stringify(patch), | |
| }); | |
| } | |
| export function dropPushStream(streamId: string, user: string): Promise<void> { | |
| return requestJson<void>(withUser(`/api/push/${encodeURIComponent(streamId)}/drop`, user), { | |
| method: 'POST', | |
| }); | |
| } | |
| export function revokePushStream(streamId: string, user: string): Promise<void> { | |
| return requestJson<void>(withUser(`/api/push/${encodeURIComponent(streamId)}/revoke`, user), { | |
| method: 'POST', | |
| }); | |
| } | |
| export function revokePoller(pollerId: string, user: string): Promise<void> { | |
| return requestJson<void>(withUser(`/api/pollers/${encodeURIComponent(pollerId)}/revoke`, user), { | |
| method: 'POST', | |
| }); | |
| } | |
| export function clearActivityApi(user: string): Promise<void> { | |
| return requestJson<void>(withUser('/api/activity/clear', user), { | |
| method: 'POST', | |
| }); | |
| } | |