Repository navigation
Expand file tree
/
Copy pathclient.ts
More file actions
142 lines (126 loc) · 3.33 KB
/
Copy pathclient.ts
File metadata and controls
142 lines (126 loc) · 3.33 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/** Thin typed fetch wrapper over the control-plane API (proxied at /api in dev). */
import { getAccessToken } from '../auth/AuthContext';
export class ApiError extends Error {
constructor(
public readonly status: number,
public readonly code: string,
message: string,
) {
super(message);
}
}
async function request<T>(method: string, path: string, body?: unknown): Promise<T> {
const token = await getAccessToken();
const res = await fetch(`/api${path}`, {
method,
headers: {
authorization: `Bearer ${token}`,
...(body !== undefined && { 'content-type': 'application/json' }),
},
body: body !== undefined ? JSON.stringify(body) : undefined,
});
if (res.status === 204) return undefined as T;
const payload = (await res.json().catch(() => null)) as {
error?: string;
message?: string;
} | null;
if (!res.ok) {
throw new ApiError(
res.status,
payload?.error ?? 'request_failed',
payload?.message ?? `HTTP ${res.status}`,
);
}
return payload as T;
}
export const api = {
get: <T>(path: string) => request<T>('GET', path),
post: <T>(path: string, body?: unknown) => request<T>('POST', path, body),
put: <T>(path: string, body?: unknown) => request<T>('PUT', path, body),
patch: <T>(path: string, body?: unknown) => request<T>('PATCH', path, body),
delete: <T>(path: string) => request<T>('DELETE', path),
};
// ── API types (mirrors the control-plane responses) ───────────────────────────
export type Role = 'admin' | 'developer' | 'viewer';
export interface Me {
user: { id: string; email: string; displayName: string | null };
orgs: { id: string; name: string; role: Role }[];
}
export interface Environment {
id: string;
key: string;
name: string;
}
export interface Project {
id: string;
name: string;
environments?: Environment[];
}
export interface Tool {
id: string;
key: string;
name: string;
description: string | null;
tags: string[];
metadata: Record<string, unknown>;
archived: boolean;
updatedAt: string;
}
export interface FlagRow {
toolId: string;
toolKey: string;
toolName: string;
archived: boolean;
enabled: boolean;
rolloutPercent: number | null;
targetingRules: unknown[];
updatedAt: string;
}
export interface Segment {
id: string;
key: string;
name: string;
description: string | null;
rules: unknown[];
}
export interface ToolConfig {
value: Record<string, unknown> | null;
version: number;
updatedAt?: string;
}
export interface ConfigVersion {
id: string;
version: number;
value: Record<string, unknown>;
authorId: string | null;
restoredFromVersion: number | null;
createdAt: string;
}
export interface ApiKey {
id: string;
environmentId: string;
kind: 'server' | 'client';
name: string;
prefix: string;
createdAt: string;
revokedAt: string | null;
/** Present only in the creation response — shown exactly once. */
token?: string;
}
export interface AuditEntry {
id: string;
actorId: string | null;
action: string;
entityType: string;
entityId: string | null;
before: Record<string, unknown> | null;
after: Record<string, unknown> | null;
createdAt: string;
}
export interface Member {
userId: string;
email: string;
displayName: string | null;
role: Role;
createdAt: string;
}