Create source implementations for packages imported by NomGap: - @bytelyst/accessibility — ARIA helper functions (alertLabel, progressLabel, etc.) - @bytelyst/celebrations — celebration engine for milestones - @bytelyst/gentle-notifications — guilt-free notification filtering - @bytelyst/time-references — human-friendly fasting time references - @bytelyst/subscription-client — billing/subscription HTTP client - @bytelyst/quick-actions — progressive disclosure UI helpers - @bytelyst/referral-client — referral program client - @bytelyst/marketplace-client — influencer marketplace client - @bytelyst/org-client — B2B org management client Made-with: Cursor
59 lines
1.4 KiB
TypeScript
59 lines
1.4 KiB
TypeScript
export interface OrgClientOptions {
|
|
baseUrl: string;
|
|
productId: string;
|
|
getAccessToken: () => string;
|
|
}
|
|
|
|
export interface OrgDoc {
|
|
id: string;
|
|
name: string;
|
|
slug: string;
|
|
memberCount: number;
|
|
plan: string;
|
|
metadata?: Record<string, unknown>;
|
|
}
|
|
|
|
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<T>(res: Response): Promise<T> {
|
|
if (!res.ok) {
|
|
const text = await res.text();
|
|
throw new Error(`HTTP ${res.status}: ${text || res.statusText}`);
|
|
}
|
|
return res.json() as Promise<T>;
|
|
}
|
|
|
|
export function createOrgClient(opts: OrgClientOptions) {
|
|
const { baseUrl } = opts;
|
|
|
|
return {
|
|
async listOrgs(): Promise<OrgDoc[]> {
|
|
const res = await fetch(joinUrl(baseUrl, "/organizations"), {
|
|
method: "GET",
|
|
headers: headers(opts),
|
|
});
|
|
return parseJson<OrgDoc[]>(res);
|
|
},
|
|
|
|
async getOrg(orgId: string): Promise<OrgDoc> {
|
|
const res = await fetch(
|
|
joinUrl(baseUrl, `/organizations/${encodeURIComponent(orgId)}`),
|
|
{ method: "GET", headers: headers(opts) }
|
|
);
|
|
return parseJson<OrgDoc>(res);
|
|
},
|
|
};
|
|
}
|