Spaces:
Running

File size: 3,290 Bytes
6f5f15e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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',
  });
}