lib/JoyProvider
Interfaces
JoyConfig
Resolved config a ★2 block runs with.
Properties
apiUrl
apiUrl: string | null;Base URL of the Headless API; null when the merchant env is unset.
locale?
locale?: string;Language Joy renders its own text in — program titles, tier names, earn
descriptions. Hydrogen owns the URL and therefore the locale, so this is
its value (storefront.i18n.language, normalised by toJoyLocale)
travelling down; nothing here reads navigator.language, which would
contradict the route the shopper is on. Undefined -> the shop's primary
language.
loginUrl
loginUrl: string;Where the CTA-login link points for guests.
shopDomain?
shopDomain?: string;The shop's myshopify domain — needed only by getInfo() (?d= shop scope,
beat 2). Threaded into the shared client at construction so whichever hook
creates the singleton first carries it. Must be the MYSHOPIFY domain, not the
Hydrogen host hostname.
tokenRoute
tokenRoute: string;Same-origin token route (defaults to /api/joy-token).
JoyProviderCartHandlers
Host cart hooks a JoyProvider can hand its blocks — see JoyLoyaltyPage's props of the same names.
Properties
onCartUpdated?
onCartUpdated?: (cart) => void;Receives the cart each Joy cart mutation returns (e.g. push it into a client cart store).
Parameters
| Parameter | Type |
|---|---|
cart | unknown |
Returns
void
onOpenCartDrawer?
onOpenCartDrawer?: () => void;Opens the host's cart drawer (the open_cart_drawer apply behavior).
Returns
void
Type Aliases
JoyCustomerState
type JoyCustomerState<T> =
| {
status: "loading";
}
| {
status: "guest";
}
| {
data: T;
status: "ready";
}
| {
message: string;
status: "error";
};Lifecycle state of a personalised fetch.
Type Parameters
JoyProviderCart
type JoyProviderCart = unknown;The storefront cart a host hands its Joy blocks (Storefront API Cart, or a
getter for it). Loosely typed so this module stays free of
hydrogen-cart-port.ts (which pulls @shopify/hydrogen at runtime).
Functions
ensureClient()
function ensureClient(
apiUrl,
tokenRoute,
locale?,
shopDomain?
): JoyClient;Get (or lazily initialise) the shared browser client. All blocks use the
same JOY_API_URL, so a single singleton per page is correct. Exported so
lib/headless-source.ts builds its live JoyDataSource around the exact
same client — one place that knows how to construct it, not a second copy.
Re-inits when locale differs from the singleton's: on a headless SPA the
shopper can switch market/language without a reload, and a reused client would
keep sending the first locale's ?locale=/?l= forever (the "only F5 fixes
the language" bug). The SDK has no setLocale, so a new client is the only way
to change it. Callers re-set the token on the returned client (useJoyCustomer +
createHeadlessSource both do), so re-init never loses the session.
Parameters
| Parameter | Type |
|---|---|
apiUrl | string |
tokenRoute | string |
locale? | string |
shopDomain? | string |
Returns
JoyClient
JoyProvider()
function JoyProvider(__namedParameters): Element;Optional provider to set shared config once for all descendant Joy blocks.
cart is for blocks that apply rewards to the cart (the Weaverse
joy-loyalty-page): a host whose cart is not on its root loader data (e.g. a
client-side cart store) passes it here, and onCartUpdated /
onOpenCartDrawer let those blocks update that store and open its drawer.
Parameters
| Parameter | Type |
|---|---|
__namedParameters | Partial<Omit<JoyConfig, "apiUrl">> & JoyProviderCartHandlers & object |
Returns
Element
useJoyConfig()
function useJoyConfig(fallback): JoyConfig;Merge the optional JoyProvider context over a block's own fallbacks
(its loader apiUrl + its loginUrl setting). Works with or without a
provider in the tree.
Parameters
| Parameter | Type |
|---|---|
fallback | { apiUrl: string | null; locale?: string; loginUrl: string; shopDomain?: string; } |
fallback.apiUrl | string | null |
fallback.locale? | string |
fallback.loginUrl | string |
fallback.shopDomain? | string |
Returns
useJoyCustomer()
function useJoyCustomer<T>(fetcher, options): JoyCustomerState<T>;Fetch personalised data after hydration. Returns loading first (matching
SSR), then guest (no token), ready (data), or error.
fetcher is read through a ref so an inline arrow does not re-trigger the
effect; the fetch re-runs when apiUrl/tokenRoute change, plus whenever a
value in options.deps changes (e.g. a pagination cursor) — the ref always
holds the latest fetcher, so the re-run reads fresh closure values.
Type Parameters
Parameters
| Parameter | Type | Description |
|---|---|---|
fetcher | (client) => Promise<T> | - |
options | { apiUrl: string | null; deps?: readonly unknown[]; enabled?: boolean; locale?: string; shopDomain?: string; tokenRoute: string; } | - |
options.apiUrl | string | null | - |
options.deps? | readonly unknown[] | - |
options.enabled? | boolean | false → do not fetch (state stays 'loading'); flips to true to start. Lets a caller that only needs the data on demand (e.g. <JoyWidget/>'s fallback panel) avoid an authed GET /customer on every page view. Defaults to true. |
options.locale? | string | - |
options.shopDomain? | string | Threaded into the shared client so getInfo() carries ?d= — see ensureClient. |
options.tokenRoute | string | - |
Returns
useJoyProviderCart()
function useJoyProviderCart(): unknown;The JoyProvider cart, or undefined without a provider / cart.
Returns
unknown
useJoyProviderCartHandlers()
function useJoyProviderCartHandlers(): JoyProviderCartHandlers;The JoyProvider cart handlers — empty without a provider.