client.ts
This file
Path
packages/catalog/src/client.ts
39 lines · 1.0 KiB · TypeScript
Last changed by
History of this fileTake it
12345678910111213141516171819202122232425262728293031323334353637383940import { API, legacyFetch } from "@burrow/kit";import type { Transport } from "@burrow/kit";
/** A product as the catalog lists it. */export interface Product { readonly id: string; readonly name: string; readonly priceCents: number;}
/** Reads the catalog. */export interface CatalogClient { readonly category: (slug: string) => Promise<readonly Product[]>; readonly product: (id: string) => Promise<Product>;}
const clientOver = (transport?: Transport): CatalogClient => ({ category: async (slug) => JSON.parse( await legacyFetch( `${API}/categories/${encodeURIComponent(slug)}`, transport ) ), product: async (id) => JSON.parse( await legacyFetch(`${API}/products/${encodeURIComponent(id)}`, transport) ),});
// Made on first use.let client: CatalogClient | undefined;
/** The catalog client, made on first use over the transport it was first given. */export const catalog = (transport?: Transport): CatalogClient => { client ??= clientOver(transport);
return client;};