learning_ai_common_plat/packages/referral-client/src/index.ts
Saravana Achu Mac 1ee97327ee feat(packages): create 9 NomGap-required platform packages
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
2026-03-29 22:24:02 -07:00

73 lines
1.8 KiB
TypeScript

export interface ReferralClientOptions {
baseUrl: string;
productId: string;
getAccessToken: () => string;
}
export interface ReferralStats {
totalReferrals: number;
successfulReferrals: number;
pendingReferrals: number;
rewardsEarned: number;
referralCode: string;
}
export interface ReferralInfo {
code: string;
referrerEmail: string;
status: string;
}
function joinUrl(base: string, path: string): string {
const b = base.replace(/\/$/, "");
const p = path.startsWith("/") ? path : `/${path}`;
return `${b}${p}`;
}
function headers(opts: ReferralClientOptions): 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 createReferralClient(opts: ReferralClientOptions) {
const { baseUrl } = opts;
return {
async getReferralStats(): Promise<ReferralStats> {
const res = await fetch(joinUrl(baseUrl, "/referrals/stats"), {
method: "GET",
headers: headers(opts),
});
return parseJson<ReferralStats>(res);
},
buildShareLink(code: string): string {
const b = baseUrl.replace(/\/$/, "");
return `${b}/r/${code}`;
},
buildShareMessage(code: string, productName: string): string {
return `Try ${productName}! Use my referral code: ${code}`;
},
async getByEmail(code: string): Promise<ReferralInfo> {
const res = await fetch(
joinUrl(baseUrl, `/referrals/${encodeURIComponent(code)}`),
{ method: "GET", headers: headers(opts) }
);
return parseJson<ReferralInfo>(res);
},
};
}