learning_ai_common_plat/packages/marketplace-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

76 lines
2.0 KiB
TypeScript

export interface MarketplaceClientOptions {
baseUrl: string;
productId: string;
getAccessToken: () => string;
}
export interface MarketplaceListing {
id: string;
title: string;
description: string;
category: string;
author: string;
downloads: number;
}
function joinUrl(base: string, path: string): string {
const b = base.replace(/\/$/, "");
const p = path.startsWith("/") ? path : `/${path}`;
return `${b}${p}`;
}
function headers(opts: MarketplaceClientOptions): HeadersInit {
return {
Authorization: `Bearer ${opts.getAccessToken()}`,
"X-Product-Id": opts.productId,
Accept: "application/json",
"Content-Type": "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 createMarketplaceClient(opts: MarketplaceClientOptions) {
const { baseUrl } = opts;
return {
async listListings(listOpts?: {
category?: string;
}): Promise<MarketplaceListing[]> {
const q = new URLSearchParams();
if (listOpts?.category !== undefined && listOpts.category !== "") {
q.set("category", listOpts.category);
}
const query = q.toString();
const path =
query.length > 0
? `/marketplace/listings?${query}`
: "/marketplace/listings";
const res = await fetch(joinUrl(baseUrl, path), {
method: "GET",
headers: headers(opts),
});
return parseJson<MarketplaceListing[]>(res);
},
async installListing(
listingId: string
): Promise<{ success: boolean }> {
const res = await fetch(
joinUrl(
baseUrl,
`/marketplace/listings/${encodeURIComponent(listingId)}/install`
),
{ method: "POST", headers: headers(opts), body: "{}" }
);
return parseJson<{ success: boolean }>(res);
},
};
}