File size: 1,182 Bytes
4fdaf19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
export interface TabEventPayload {
  source: string;
  target?: string;
  action: string;
  payload?: Record<string, unknown>;
}

export interface ApiResult<T = unknown> {
  ok: boolean;
  data?: T;
  error?: string;
}

const jsonHeaders = { 'Content-Type': 'application/json' };

export async function publishTabEvent(event: TabEventPayload): Promise<ApiResult> {
  try {
    const response = await fetch('/api/tabs/events', {
      method: 'POST',
      headers: jsonHeaders,
      body: JSON.stringify(event),
    });
    const data = await response.json();
    return response.ok ? { ok: true, data } : { ok: false, error: data?.error || response.statusText };
  } catch (error) {
    return { ok: false, error: error instanceof Error ? error.message : 'Unknown tab bus error' };
  }
}

export async function getApiSpec(): Promise<ApiResult> {
  try {
    const response = await fetch('/api/openapi.json');
    const data = await response.json();
    return response.ok ? { ok: true, data } : { ok: false, error: data?.error || response.statusText };
  } catch (error) {
    return { ok: false, error: error instanceof Error ? error.message : 'Unable to load API spec' };
  }
}