@kotao/storefront 0.2.7 → 0.2.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/cart/CartForm.ts","../src/react/nonce.ts","../src/analytics/attribution.ts","../src/analytics/emitter.ts","../src/analytics/analytics-bus.ts","../src/analytics/pixel-transport.ts"],"names":["CartForm"],"mappings":";;;;;AAaA,SAAS,eAAe,KAAA,EAAyB;AAC/C,EAAA,IAAI,OAAO,KAAA,KAAU,QAAA,EAAU,OAAO,KAAA;AACtC,EAAA,MAAM,OAAA,GAAU,MAAM,IAAA,EAAK;AAC3B,EAAA,IAAI,QAAQ,UAAA,CAAW,GAAG,KAAK,OAAA,CAAQ,UAAA,CAAW,GAAG,CAAA,EAAG;AACtD,IAAA,IAAI;AACF,MAAA,OAAO,IAAA,CAAK,MAAM,OAAO,CAAA;AAAA,IAC3B,CAAA,CAAA,MAAQ;AACN,MAAA,OAAO,KAAA;AAAA,IACT;AAAA,EACF;AACA,EAAA,OAAO,KAAA;AACT;AAEO,SAAS,iBAAiB,QAAA,EAAyC;AACxE,EAAA,MAAM,MAAA,GAAS,QAAA,CAAS,GAAA,CAAI,YAAY,CAAA;AACxC,EAAA,MAAM,SAA8B,EAAC;AACrC,EAAA,KAAA,MAAW,OAAO,IAAI,GAAA,CAAI,QAAA,CAAS,IAAA,EAAM,CAAA,EAAG;AAC1C,IAAA,IAAI,QAAQ,YAAA,EAAc;AAC1B,IAAA,MAAM,SAAS,QAAA,CAAS,MAAA,CAAO,GAAG,CAAA,CAAE,IAAI,cAAc,CAAA;AACtD,IAAA,MAAA,CAAO,GAAG,CAAA,GAAI,MAAA,CAAO,WAAW,CAAA,GAAI,MAAA,CAAO,CAAC,CAAA,GAAI,MAAA;AAAA,EAClD;AACA,EAAA,OAAO;AAAA,IACL,MAAA,EAAQ,OAAO,MAAA,KAAW,QAAA,GAAY,MAAA,GAA4B,IAAA;AA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AACN,MAAA,OAAA,CAAQ,UAAU,KAAA,EAAM;AAAA,IAC1B;AAAA,GACF;AACF;;;AClCA,IAAM,gBAAA,GAAmB,uBAAA;AACzB,IAAM,kBAAA,GAAqB,EAAA;AAOpB,SAAS,qBAAqB,OAAA,EAAgD;AACnF,EAAA,MAAM,QAAA,GAAW,QAAQ,QAAA,IAAY,gBAAA;AAGrC,EAAA,IAAI,CAAC,WAAA,CAAY,IAAA,CAAK,QAAQ,CAAA,EAAG;AAC/B,IAAA,MAAM,IAAI,KAAA,CAAM,CAAA,kEAAA,EAAqE,QAAQ,CAAA,CAAE,CAAA;AAAA,EACjG;AACA,EAAA,MAAM,SAAA,GAAY,QAAQ,SAAA,IAAa,kBAAA;AACvC,EAAA,MAAM,OAAO,OAAA,CAAQ,IAAA;AACrB,EAAA,IAAI,QAAsB,EAAC;AAE3B,EAAA,MAAM,QAAQ,MAAY;AACxB,IAAA,IAAI,KAAA,CAAM,WAAW,CAAA,EAAG;AACxB,IAAA,MAAM,KAAA,GAAQ,KAAA;AACd,IAAA,KAAA,GAAQ,EAAC;AACT,IAAA,IAAA,CAAK,UAAU,IAAA,CAAK,SAAA,CAAU,EAAE,MAAA,EAAQ,KAAA,EAAO,CAAC,CAAA;AAAA,EAClD,CAAA;AAEA,EAAA,OAAO;AAAA,IACL,OAAO,KAAA,EAAO;AACZ,MAAA,KAAA,CAAM,KAAK,KAAK,CAAA;AAChB,MAAA,IAAI,KAAA,CAAM,MAAA,IAAU,SAAA,EAAW,KAAA,EAAM;AAAA,IACvC,CAAA;AAAA,IACA;AAAA,GACF;AACF","file":"chunk-RFM27BKZ.js","sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any */\n\nimport { CART_FORM_ACTIONS, type CartFormAction } from './actions'\n\nexport interface CartFormProps {\n readonly action: CartFormAction\n}\n\nexport interface CartFormInputResult {\n readonly action: CartFormAction | null\n readonly inputs: Record<string, any>\n}\n\nfunction parseFormValue(value: unknown): unknown {\n if (typeof value !== 'string') return value\n const trimmed = value.trim()\n if (trimmed.startsWith('{') || trimmed.startsWith('[')) {\n try {\n return JSON.parse(trimmed) as unknown\n } catch {\n return value\n }\n }\n return value\n}\n\nexport function getCartFormInput(formData: FormData): CartFormInputResult {\n const action = formData.get('cartAction')\n const inputs: Record<string, any> = {}\n for (const key of new Set(formData.keys())) {\n if (key === 'cartAction') continue\n const values = formData.getAll(key).map(parseFormValue)\n inputs[key] = values.length === 1 ? values[0] : values\n }\n return {\n action: typeof action === 'string' ? (action as CartFormAction) : null,\n inputs,\n }\n}\n\n/**\n * Lightweight CartForm surface marker for Hydrogen-style imports. React render integration can wrap\n * this constant surface; the load-bearing API today is the stable ACTIONS contract used by handlers.\n */\nexport const CartForm = Object.assign(\n function CartForm(_props: CartFormProps): null {\n return null\n },\n { ACTIONS: CART_FORM_ACTIONS, getFormInput: getCartFormInput },\n)\n","import * as ReactRuntime from 'react'\nimport { createContext, createElement, useContext, type ReactNode } from 'react'\n\nconst NonceContext = createContext<string | undefined>(undefined)\n\nexport interface NonceProviderProps {\n readonly children?: ReactNode\n readonly nonce?: string\n}\n\nexport type BoundNonceProvider = (props: { readonly children?: ReactNode }) => ReactNode\n\nexport function NonceProvider(props: NonceProviderProps): ReactNode {\n return createElement(NonceContext.Provider, { value: props.nonce }, props.children)\n}\n\nexport function createNonceProvider(nonce: string): BoundNonceProvider {\n return function BoundNonceProvider(props: { readonly children?: ReactNode }): ReactNode {\n return createElement(NonceContext.Provider, { value: nonce }, props.children)\n }\n}\n\n/** Hydrogen-compatible nonce hook. */\nexport function useNonce(): string | undefined {\n if (!hasReactDispatcher()) return undefined\n return useContext(NonceContext)\n}\n\nfunction hasReactDispatcher(): boolean {\n const runtime = ReactRuntime as unknown as {\n __CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE?: { H?: unknown }\n __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED?: {\n ReactCurrentDispatcher?: { current?: unknown }\n }\n }\n return Boolean(\n runtime.__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE?.H ??\n runtime.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED?.ReactCurrentDispatcher?.current,\n )\n}\n","import type { AnalyticsContext, AnalyticsDeviceClass } from './events'\n\nexport interface AnalyticsAttributionEnvironment {\n readonly referrer?: string\n readonly search?: string\n readonly matchMedia?: (query: string) => { readonly matches: boolean }\n}\n\ntype AttributionContext = Pick<\n AnalyticsContext,\n 'referrer' | 'utmSource' | 'utmMedium' | 'utmCampaign' | 'deviceClass'\n>\n\nfunction deviceClass(\n matchMedia: AnalyticsAttributionEnvironment['matchMedia'],\n): AnalyticsDeviceClass | undefined {\n if (matchMedia === undefined) return undefined\n if (matchMedia('(max-width: 767px)').matches) return 'mobile'\n if (matchMedia('(max-width: 1023px)').matches) return 'tablet'\n return 'desktop'\n}\n\n/** Read static landing attribution plus the current responsive device class without retaining state\n * or identifiers. All values stay optional so non-DOM/SSR emitters keep their existing payload. */\nexport function collectAnalyticsAttribution(\n environment: AnalyticsAttributionEnvironment | null,\n): AttributionContext {\n if (environment === null) return {}\n const params = new URLSearchParams(environment.search ?? '')\n const referrer = environment.referrer || undefined\n const utmSource = params.get('utm_source') || undefined\n const utmMedium = params.get('utm_medium') || undefined\n const utmCampaign = params.get('utm_campaign') || undefined\n const classifiedDevice = deviceClass(environment.matchMedia)\n\n return {\n ...(referrer !== undefined ? { referrer } : {}),\n ...(utmSource !== undefined ? { utmSource } : {}),\n ...(utmMedium !== undefined ? { utmMedium } : {}),\n ...(utmCampaign !== undefined ? { utmCampaign } : {}),\n ...(classifiedDevice !== undefined ? { deviceClass: classifiedDevice } : {}),\n }\n}\n","import type { AnalyticsEventMap, AnalyticsEventName } from './events'\n\nexport type EventHandler<E extends AnalyticsEventName> = (payload: AnalyticsEventMap[E]) => void\n\n/** A framework-agnostic typed pub/sub bus (Hydrogen `useAnalytics` shape, minus React). */\nexport interface AnalyticsEmitter {\n subscribe<E extends AnalyticsEventName>(event: E, handler: EventHandler<E>): () => void\n publish<E extends AnalyticsEventName>(event: E, payload: AnalyticsEventMap[E]): void\n}\n\nexport function createAnalyticsEmitter(): AnalyticsEmitter {\n const handlers = new Map<AnalyticsEventName, Set<(payload: unknown) => void>>()\n return {\n subscribe(event, handler) {\n const set = handlers.get(event) ?? new Set<(payload: unknown) => void>()\n const erased = handler as (payload: unknown) => void\n set.add(erased)\n handlers.set(event, set)\n return () => {\n set.delete(erased)\n }\n },\n publish(event, payload) {\n const set = handlers.get(event)\n if (set === undefined) return\n // Snapshot so a handler unsubscribing mid-dispatch doesn't skip a sibling.\n for (const handler of [...set]) {\n try {\n handler(payload)\n } catch {\n // Isolate: a throwing subscriber must not break sibling handlers or the downstream pixel send.\n }\n }\n },\n }\n}\n","import type { AnonymousAnalyticsContext } from './anonymous-context'\nimport type { ConsentState } from './consent'\nimport { createAnalyticsEmitter, type AnalyticsEmitter } from './emitter'\nimport type { AnalyticsContext, AnalyticsEventMap, AnalyticsEventName } from './events'\nimport type { PixelTransport } from './pixel-transport'\n\nexport interface AnalyticsBusOptions {\n readonly transport: PixelTransport\n /** Legacy consent seam retained for API compatibility while Storefront consent UI is disabled. */\n readonly getConsent: () => ConsentState\n /** Ambient context (including attribution and device class) attached to consented events. */\n readonly getContext?: () => AnalyticsContext\n /** Sanitized same-origin page URL used only for the anonymous page-view fallback. */\n readonly getAnonymousContext?: () => AnonymousAnalyticsContext\n /** Clock seam for deterministic timestamps in tests. */\n readonly now?: () => string\n}\n\nexport interface AnalyticsBus {\n readonly emitter: AnalyticsEmitter\n publish<E extends AnalyticsEventName>(event: E, payload: AnalyticsEventMap[E]): void\n flush(): void\n}\n\n/**\n * Wire the emitter to the pixel. Every published event reaches theme subscribers (via the emitter).\n * First-party recording is consent-strict: denied visits get only an anonymous page_viewed row and\n * never evaluate ambient context, which may read attribution or device-class signals.\n */\nexport function createAnalyticsBus(options: AnalyticsBusOptions): AnalyticsBus {\n const emitter = createAnalyticsEmitter()\n const now = options.now ?? ((): string => new Date().toISOString())\n return {\n emitter,\n publish(event, payload) {\n emitter.publish(event, payload)\n const consented = options.getConsent().analytics === true\n if (consented) {\n const context = options.getContext?.() ?? {}\n options.transport.record({ event, payload, context, timestamp: now(), consent: true })\n return\n }\n if (event === 'page_viewed') {\n options.transport.record({\n event,\n payload: {} as AnalyticsEventMap[typeof event],\n context: options.getAnonymousContext?.() ?? {},\n timestamp: now(),\n consent: false,\n })\n }\n },\n flush() {\n options.transport.flush()\n },\n }\n}\n","import type { PixelEvent } from './events'\n\n/** The actual network send. Injectable so tests don't touch the network. */\nexport type PixelSend = (endpoint: string, body: string) => void\n\nexport interface PixelTransportOptions {\n /** Same-origin carve-out endpoint (default '/api/storefront/pixel') — never cross-origin. */\n readonly endpoint?: string\n readonly batchSize?: number\n /**\n * The actual network send. Required + injected: the browser sender (`sendBeacon`/keepalive `fetch`)\n * is DOM-specific and lives in the React layer, keeping this batching core framework-agnostic and\n * unit-testable with a spy. The provider wires the real sender.\n */\n readonly send: PixelSend\n}\n\nexport interface PixelTransport {\n record(event: PixelEvent): void\n flush(): void\n}\n\nconst DEFAULT_ENDPOINT = '/api/storefront/pixel'\nconst DEFAULT_BATCH_SIZE = 10\n\n/**\n * Batches pixel events and POSTs them to the same-origin carve-out. Flushes on `batchSize` or an\n * explicit `flush()` (the provider flushes on page-hide). The endpoint is same-origin so themes need\n * no CORS and no token is exposed in the browser.\n */\nexport function createPixelTransport(options: PixelTransportOptions): PixelTransport {\n const endpoint = options.endpoint ?? DEFAULT_ENDPOINT\n // SECURITY: the pixel must stay same-origin (no CORS, no token leak). Reject a non-relative or\n // protocol-relative endpoint so a misconfigured theme can't exfiltrate visitor data (e.g. clientIp).\n if (!/^\\/(?!\\/)/.test(endpoint)) {\n throw new Error(`pixel endpoint must be a same-origin path starting with '/'; got: ${endpoint}`)\n }\n const batchSize = options.batchSize ?? DEFAULT_BATCH_SIZE\n const send = options.send\n let queue: PixelEvent[] = []\n\n const flush = (): void => {\n if (queue.length === 0) return\n const batch = queue\n queue = []\n send(endpoint, JSON.stringify({ events: batch }))\n }\n\n return {\n record(event) {\n queue.push(event)\n if (queue.length >= batchSize) flush()\n },\n flush,\n }\n}\n"]}