Headless API
SDK reference
hydrogen
lib
JoyProvider

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
ParameterType
cartunknown
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

Type Parameter
T

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

ParameterType
apiUrlstring
tokenRoutestring
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

ParameterType
__namedParametersPartial<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

ParameterType
fallback{ apiUrl: string | null; locale?: string; loginUrl: string; shopDomain?: string; }
fallback.apiUrlstring | null
fallback.locale?string
fallback.loginUrlstring
fallback.shopDomain?string

Returns

JoyConfig


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

Type Parameter
T

Parameters

ParameterTypeDescription
fetcher(client) => Promise<T>-
options{ apiUrl: string | null; deps?: readonly unknown[]; enabled?: boolean; locale?: string; shopDomain?: string; tokenRoute: string; }-
options.apiUrlstring | null-
options.deps?readonly unknown[]-
options.enabled?booleanfalse → 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?stringThreaded into the shared client so getInfo() carries ?d= — see ensureClient.
options.tokenRoutestring-

Returns

JoyCustomerState<T>


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.

Returns

JoyProviderCartHandlers