UNPKG

@rebuy/hydrogen-sdk

Version:

The React/Hydrogen wrapper for the Rebuy Core SDK.

97 lines (96 loc) 3.19 kB
import { RebuySDK, type RebuyCartContext, type SupportedCart } from '@rebuy/core-sdk'; export { RebuyContextBuilder } from '@rebuy/core-sdk'; /** * Enhanced Hydrogen context type with i18n support */ type HydrogenContext = { [key: string]: unknown; cart: { get: () => Promise<SupportedCart | null>; }; env?: { PUBLIC_REBUY_API_KEY?: string; REBUY_API_HOST?: string; REBUY_API_KEY?: string; REBUY_SDK_DEBUG?: boolean; }; storefront?: { i18n?: { country?: string; language?: string; }; }; }; /** * Enhanced options with request object */ type GetRebuyDataOptions = { apiHost?: string; apiKey?: string; context: HydrogenContext; request?: Request; }; /** * Type definition for the getRebuyData return value */ type RebuyDataResult = { rebuy: { cartContext: RebuyCartContext; sdk: RebuySDK; }; }; /** * Helper function to be called within Remix loaders to abstract Rebuy-related * data fetching and context creation. This enables server-side cart reactivity * by providing cart-aware context for Rebuy's data source APIs. * * Now with automatic context enrichment from URL, geolocation, and language data. * * @param {GetRebuyDataOptions} options - Configuration options * @param {HydrogenContext} options.context - The Hydrogen context object from the loader * @param {Request} options.request - The request object for URL context extraction * @param {string} options.apiKey - Optional Rebuy API key (defaults to env.REBUY_API_KEY) * @param {string} options.apiHost - Optional API host override (defaults to env.REBUY_API_HOST) * * @returns Object containing the initialized SDK and cart context * * @example * ```typescript * // In your Remix loader - now with automatic context enrichment * export async function loader({ context, request }: LoaderFunctionArgs) { * const { rebuy } = await getRebuyData({ context, request }); * * // The cartContext now includes: * // - Cart data (if available) * // - URL path and parameters * // - Country code and language from i18n * * // Fetch recommendations using the enriched context * const recommendations = await rebuy.sdk.products.fetchFromDataSource( * 'pdp-recommendations', * { * ...rebuy.cartContext, * shopify_product_ids: productId * } * ); * * return defer({ recommendations }); * } * ``` */ export declare const getRebuyData: (options: GetRebuyDataOptions) => Promise<RebuyDataResult>; /** * Type-safe helper for using getRebuyData in TypeScript projects * * @example * ```typescript * import type { LoaderFunctionArgs } from '@shopify/remix-oxygen'; * import { getRebuyData } from '@rebuy/hydrogen-sdk'; * * export async function loader({ context, request }: LoaderFunctionArgs) { * const rebuyData = await getRebuyData({ context, request }); * // TypeScript knows rebuyData.rebuy.sdk and rebuyData.rebuy.cartContext types * } * ``` */ export type { RebuyDataResult, GetRebuyDataOptions };