export interface OrgClientOptions { baseUrl: string; productId: string; getAccessToken: () => string; } export interface OrgDoc { id: string; name: string; slug: string; memberCount: number; plan: string; metadata?: Record; } function joinUrl(base: string, path: string): string { const b = base.replace(/\/$/, ""); const p = path.startsWith("/") ? path : `/${path}`; return `${b}${p}`; } function headers(opts: OrgClientOptions): HeadersInit { return { Authorization: `Bearer ${opts.getAccessToken()}`, "X-Product-Id": opts.productId, Accept: "application/json", }; } async function parseJson(res: Response): Promise { if (!res.ok) { const text = await res.text(); throw new Error(`HTTP ${res.status}: ${text || res.statusText}`); } return res.json() as Promise; } export function createOrgClient(opts: OrgClientOptions) { const { baseUrl } = opts; return { async listOrgs(): Promise { const res = await fetch(joinUrl(baseUrl, "/organizations"), { method: "GET", headers: headers(opts), }); return parseJson(res); }, async getOrg(orgId: string): Promise { const res = await fetch( joinUrl(baseUrl, `/organizations/${encodeURIComponent(orgId)}`), { method: "GET", headers: headers(opts) } ); return parseJson(res); }, }; }