@replohq/canopy-sdk 0.1.0 → 0.1.2

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.
Files changed (39) hide show
  1. package/_vendor/replo-utils/lib/json.mjs +2 -0
  2. package/_vendor/replo-utils/lib/misc.d.ts +2 -0
  3. package/_vendor/replo-utils/lib/misc.mjs +2 -1
  4. package/_vendor/schemas/errors.mjs +2 -1
  5. package/_vendor/schemas/generated/consent.d.ts +24 -0
  6. package/_vendor/schemas/loaderKeys.mjs +1 -5
  7. package/_vendor/schemas/selectMode.mjs +3 -10
  8. package/analytics/analytics-provider.js +28 -29
  9. package/analytics/analytics-provider.js.map +2 -2
  10. package/analytics/get-analytics-sinks.d.ts +25 -0
  11. package/analytics/get-analytics-sinks.js +46 -0
  12. package/analytics/get-analytics-sinks.js.map +7 -0
  13. package/chunk-GV5FY7ZZ.mjs +89 -0
  14. package/chunk-VKO7P3ED.mjs +41 -0
  15. package/{chunk-INHK6MYI.mjs → chunk-XK2K3263.mjs} +48 -128
  16. package/consent/consent-store.d.ts +52 -0
  17. package/consent/consent-store.js +160 -0
  18. package/consent/consent-store.js.map +7 -0
  19. package/consent/cookie.d.ts +38 -0
  20. package/consent/cookie.js +83 -0
  21. package/consent/cookie.js.map +7 -0
  22. package/consent/replo-scripts.d.ts +9 -0
  23. package/consent/replo-scripts.js +91 -0
  24. package/consent/replo-scripts.js.map +7 -0
  25. package/consent/script-registration-store.d.ts +25 -0
  26. package/consent/script-registration-store.js +61 -0
  27. package/consent/script-registration-store.js.map +7 -0
  28. package/consent/script-snippets.d.ts +19 -0
  29. package/consent/script-snippets.js +125 -0
  30. package/consent/script-snippets.js.map +7 -0
  31. package/consent/server.d.ts +12 -0
  32. package/consent/server.js +29 -0
  33. package/consent/server.js.map +7 -0
  34. package/consent/window-api.d.ts +41 -0
  35. package/consent/window-api.js +73 -0
  36. package/consent/window-api.js.map +7 -0
  37. package/loaders/prefetch-loaders.d.ts +1 -1
  38. package/loaders/prefetch-loaders.js.map +1 -1
  39. package/package.json +1 -1
@@ -0,0 +1,73 @@
1
+ "use client";
2
+ import { consentStore, isConsentAllowed } from "./consent-store";
3
+ const CONSENT_CHANGE_EVENT = "visitorConsentCollected";
4
+ function isAllowed(category) {
5
+ return isConsentAllowed({
6
+ state: consentStore.getSnapshot(),
7
+ requiredConsent: [category]
8
+ });
9
+ }
10
+ function currentVisitorConsent() {
11
+ const state = consentStore.getSnapshot();
12
+ const signal = (category) => {
13
+ if (state.decidedAt === null) {
14
+ return "";
15
+ }
16
+ return state.categories[category] ? "yes" : "no";
17
+ };
18
+ return {
19
+ analytics: signal("analytics"),
20
+ marketing: signal("marketing"),
21
+ preferences: signal("preferences"),
22
+ sale_of_data: signal("sale_of_data")
23
+ };
24
+ }
25
+ const api = {
26
+ currentVisitorConsent,
27
+ analyticsProcessingAllowed: () => isAllowed("analytics"),
28
+ marketingAllowed: () => isAllowed("marketing"),
29
+ preferencesProcessingAllowed: () => isAllowed("preferences"),
30
+ saleOfDataAllowed: () => isAllowed("sale_of_data"),
31
+ shouldShowBanner: () => {
32
+ const state = consentStore.getSnapshot();
33
+ return state.mode !== "off" && state.decidedAt === null;
34
+ },
35
+ setTrackingConsent: (consent, callback) => {
36
+ consentStore.updateCategories(consent);
37
+ callback?.();
38
+ }
39
+ };
40
+ function consentChangeDetail() {
41
+ return {
42
+ analyticsAllowed: isAllowed("analytics"),
43
+ marketingAllowed: isAllowed("marketing"),
44
+ preferencesAllowed: isAllowed("preferences"),
45
+ saleOfDataAllowed: isAllowed("sale_of_data")
46
+ };
47
+ }
48
+ let teardown = null;
49
+ function installConsentWindowApi() {
50
+ if (typeof window === "undefined") {
51
+ return () => {
52
+ };
53
+ }
54
+ if (teardown) {
55
+ return teardown;
56
+ }
57
+ window.Replo = { ...window.Replo, customerPrivacy: api };
58
+ const unsubscribe = consentStore.subscribe(() => {
59
+ document.dispatchEvent(
60
+ new CustomEvent(CONSENT_CHANGE_EVENT, { detail: consentChangeDetail() })
61
+ );
62
+ });
63
+ teardown = () => {
64
+ unsubscribe();
65
+ teardown = null;
66
+ };
67
+ return teardown;
68
+ }
69
+ export {
70
+ CONSENT_CHANGE_EVENT,
71
+ installConsentWindowApi
72
+ };
73
+ //# sourceMappingURL=window-api.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../consent/window-api.ts"],
4
+ "sourcesContent": ["\"use client\";\n\nimport type { ConsentCategory } from \"schemas/generated/consent\";\n\nimport { consentStore, isConsentAllowed } from \"./consent-store\";\n\n/**\n * Global, framework-agnostic consent API exposed on `window.Replo.customerPrivacy`,\n * mirroring Shopify's Customer Privacy API surface so on-page (third-party)\n * scripts can query consent and react to changes without importing canopy.\n *\n * It deliberately lives under our own `window.Replo` namespace rather than\n * `window.Shopify` to avoid colliding with Shopify's real API on Shopify-backed\n * sites.\n */\n\ntype ToggleableCategory = Exclude<ConsentCategory, \"necessary\">;\n\n/** Shopify-style tri-state: declared-allowed, declared-denied, or undeclared. */\ntype ConsentSignal = \"yes\" | \"no\" | \"\";\n\nexport interface ReploCustomerPrivacyApi {\n /** Per-category declaration, or \"\" when the visitor has not decided. */\n currentVisitorConsent(): Record<ToggleableCategory, ConsentSignal>;\n analyticsProcessingAllowed(): boolean;\n marketingAllowed(): boolean;\n preferencesProcessingAllowed(): boolean;\n saleOfDataAllowed(): boolean;\n /** Whether the consent banner should still be shown (mode on + undecided). */\n shouldShowBanner(): boolean;\n /** Update a subset of categories; runs the optional callback once persisted. */\n setTrackingConsent(\n consent: Partial<Record<ToggleableCategory, boolean>>,\n callback?: () => void,\n ): void;\n}\n\ndeclare global {\n interface Window {\n Replo?: { customerPrivacy?: ReploCustomerPrivacyApi };\n }\n}\n\n/** DOM event dispatched on `document` whenever consent changes. */\nexport const CONSENT_CHANGE_EVENT = \"visitorConsentCollected\";\n\nfunction isAllowed(category: ConsentCategory): boolean {\n return isConsentAllowed({\n state: consentStore.getSnapshot(),\n requiredConsent: [category],\n });\n}\n\nfunction currentVisitorConsent(): Record<ToggleableCategory, ConsentSignal> {\n const state = consentStore.getSnapshot();\n const signal = (category: ToggleableCategory): ConsentSignal => {\n if (state.decidedAt === null) {\n return \"\";\n }\n return state.categories[category] ? \"yes\" : \"no\";\n };\n return {\n analytics: signal(\"analytics\"),\n marketing: signal(\"marketing\"),\n preferences: signal(\"preferences\"),\n sale_of_data: signal(\"sale_of_data\"),\n };\n}\n\nconst api: ReploCustomerPrivacyApi = {\n currentVisitorConsent,\n analyticsProcessingAllowed: () => isAllowed(\"analytics\"),\n marketingAllowed: () => isAllowed(\"marketing\"),\n preferencesProcessingAllowed: () => isAllowed(\"preferences\"),\n saleOfDataAllowed: () => isAllowed(\"sale_of_data\"),\n shouldShowBanner: () => {\n const state = consentStore.getSnapshot();\n return state.mode !== \"off\" && state.decidedAt === null;\n },\n setTrackingConsent: (consent, callback) => {\n consentStore.updateCategories(consent);\n callback?.();\n },\n};\n\nfunction consentChangeDetail() {\n return {\n analyticsAllowed: isAllowed(\"analytics\"),\n marketingAllowed: isAllowed(\"marketing\"),\n preferencesAllowed: isAllowed(\"preferences\"),\n saleOfDataAllowed: isAllowed(\"sale_of_data\"),\n };\n}\n\nlet teardown: (() => void) | null = null;\n\n/**\n * Installs `window.Replo.customerPrivacy` and dispatches `visitorConsentCollected`\n * on `document` whenever consent changes. Idempotent; returns a teardown that\n * removes the change subscription. Safe to call on the server (no-op).\n */\nexport function installConsentWindowApi(): () => void {\n if (typeof window === \"undefined\") {\n return () => {\n // no-op on the server\n };\n }\n if (teardown) {\n return teardown;\n }\n window.Replo = { ...window.Replo, customerPrivacy: api };\n const unsubscribe = consentStore.subscribe(() => {\n document.dispatchEvent(\n new CustomEvent(CONSENT_CHANGE_EVENT, { detail: consentChangeDetail() }),\n );\n });\n teardown = () => {\n unsubscribe();\n teardown = null;\n };\n return teardown;\n}\n"],
5
+ "mappings": ";AAIA,SAAS,cAAc,wBAAwB;AAwCxC,MAAM,uB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E,GAAG,OAAO,OAAO,iBAAiB,IAAI;AACvD,QAAM,cAAc,aAAa,UAAU,MAAM;AAC/C,aAAS;AAAA,MACP,IAAI,YAAY,sBAAsB,EAAE,QAAQ,oBAAoB,EAAE,CAAC;AAAA,IACzE;AAAA,EACF,CAAC;AACD,aAAW,MAAM;AACf,gBAAY;AACZ,eAAW;AAAA,EACb;AACA,SAAO;AACT;",
6
+ "names": []
7
+ }
@@ -14,7 +14,7 @@ type PrefetchLoaderConfig = {
14
14
  * convention used by all loader components.
15
15
  *
16
16
  * Usage:
17
- * import { DATA_LOADER_KEYS } from "../_vendor/schemas/loaderKeys";
17
+ * import { DATA_LOADER_KEYS } from "canopy-sdk/_vendor/schemas/loaderKeys";
18
18
  *
19
19
  * <PrefetchedLoaders
20
20
  * queries={[
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../loaders/prefetch-loaders.tsx"],
4
- "sourcesContent": ["import {\n dehydrate,\n HydrationBoundary,\n QueryClient,\n} from \"@tanstack/react-query\";\n\nimport { invokeLoaderServer } from \"./invoke-loader-server\";\n\ntype PrefetchLoaderConfig = {\n loaderKey: string;\n args: Record<string, unknown>;\n /** How long the server-side cache entry is fresh. Defaults to 60s. Use `false` for indefinite (tag-based invalidation only). */\n revalidateSeconds?: number | false;\n};\n\n/**\n * Server component that prefetches one or more loader queries and seeds the\n * React Query cache via HydrationBoundary. Client components that use\n * useSuspenseQuery with the same queryKey will get the data immediately\n * in the initial HTML \u2014 no loading state on first paint.\n *\n * The queryKey is derived internally as `[loaderKey, args]`, matching the\n * convention used by all loader components.\n *\n * Usage:\n * import { DATA_LOADER_KEYS } from \"schemas/loaderKeys\";\n *\n * <PrefetchedLoaders\n * queries={[\n * {\n * loaderKey: DATA_LOADER_KEYS.SHOPIFY_PRODUCT,\n * args: { productId: \"gid://shopify/Product/123\" },\n * },\n * ]}\n * >\n * <ProductPreview productId=\"gid://shopify/Product/123\" />\n * </PrefetchedLoaders>\n */\nexport async function PrefetchedLoaders({\n queries,\n children,\n}: React.PropsWithChildren<{\n queries: PrefetchLoaderConfig[];\n}>) {\n const queryClient = new QueryClient({\n defaultOptions: {\n queries: { staleTime: 60 * 1000 },\n },\n });\n\n await Promise.all(\n queries.map((query) => {\n return queryClient.prefetchQuery({\n queryKey: [query.loaderKey, query.args],\n queryFn: () => {\n return invokeLoaderServer({\n loaderKey: query.loaderKey,\n args: query.args,\n revalidateSeconds: query.revalidateSeconds,\n });\n },\n });\n }),\n );\n\n return (\n <HydrationBoundary\n state={dehydrate(queryClient, {\n shouldDehydrateQuery: (query) => {\n return (\n query.state.status === \"success\" || query.state.status === \"pending\"\n );\n },\n })}\n >\n {children}\n </HydrationBoundary>\n );\n}\n"],
4
+ "sourcesContent": ["import {\n dehydrate,\n HydrationBoundary,\n QueryClient,\n} from \"@tanstack/react-query\";\n\nimport { invokeLoaderServer } from \"./invoke-loader-server\";\n\ntype PrefetchLoaderConfig = {\n loaderKey: string;\n args: Record<string, unknown>;\n /** How long the server-side cache entry is fresh. Defaults to 60s. Use `false` for indefinite (tag-based invalidation only). */\n revalidateSeconds?: number | false;\n};\n\n/**\n * Server component that prefetches one or more loader queries and seeds the\n * React Query cache via HydrationBoundary. Client components that use\n * useSuspenseQuery with the same queryKey will get the data immediately\n * in the initial HTML \u2014 no loading state on first paint.\n *\n * The queryKey is derived internally as `[loaderKey, args]`, matching the\n * convention used by all loader components.\n *\n * Usage:\n * import { DATA_LOADER_KEYS } from \"canopy-sdk/_vendor/schemas/loaderKeys\";\n *\n * <PrefetchedLoaders\n * queries={[\n * {\n * loaderKey: DATA_LOADER_KEYS.SHOPIFY_PRODUCT,\n * args: { productId: \"gid://shopify/Product/123\" },\n * },\n * ]}\n * >\n * <ProductPreview productId=\"gid://shopify/Product/123\" />\n * </PrefetchedLoaders>\n */\nexport async function PrefetchedLoaders({\n queries,\n children,\n}: React.PropsWithChildren<{\n queries: PrefetchLoaderConfig[];\n}>) {\n const queryClient = new QueryClient({\n defaultOptions: {\n queries: { staleTime: 60 * 1000 },\n },\n });\n\n await Promise.all(\n queries.map((query) => {\n return queryClient.prefetchQuery({\n queryKey: [query.loaderKey, query.args],\n queryFn: () => {\n return invokeLoaderServer({\n loaderKey: query.loaderKey,\n args: query.args,\n revalidateSeconds: query.revalidateSeconds,\n });\n },\n });\n }),\n );\n\n return (\n <HydrationBoundary\n state={dehydrate(queryClient, {\n shouldDehydrateQuery: (query) => {\n return (\n query.state.status === \"success\" || query.state.status === \"pending\"\n );\n },\n })}\n >\n {children}\n </HydrationBoundary>\n );\n}\n"],
5
5
  "mappings": "AAkEI;AAlEJ;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAEP,SAAS,0BAA0B;AAgCnC,eAAsB,kBAAkB;AAAA,EACtC;AAAA,EACA;AACF,GAEI;AACF,QAAM,cAAc,IAAI,YAAY;AAAA,IAClC,gBAAgB;AAAA,MACd,SAAS,EAAE,WAAW,KAAK,IAAK;AAAA,IAClC;AAAA,EACF,CAAC;AAED,QAAM,QAAQ;AAAA,IACZ,QAAQ,IAAI,CAAC,UAAU;AACrB,aAAO,YAAY,cAAc;AAAA,QAC/B,UAAU,CAAC,MAAM,WAAW,MAAM,IAAI;AAAA,QACtC,SAAS,MAAM;AACb,iBAAO,mBAAmB;AAAA,YACxB,WAAW,MAAM;AAAA,YACjB,MAAM,MAAM;AAAA,YACZ,mBAAmB,MAAM;AAAA,UAC3B,CAAC;AAAA,QACH;AAAA,MACF,CAAC;AAAA,IACH,CAAC;AAAA,EACH;AAEA,SACE;AAAA,IAAC;AAAA;AAAA,MACC,OAAO,UAAU,aAAa;AAAA,QAC5B,sBAAsB,CAAC,UAAU;AAC/B,iBACE,MAAM,MAAM,WAAW,aAAa,MAAM,MAAM,WAAW;AAAA,QAE/D;AAAA,MACF,CAAC;AAAA,MAEA;AAAA;AAAA,EACH;AAEJ;",
6
6
  "names": []
7
7
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@replohq/canopy-sdk",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "Replo Canopy SDK — cart, analytics, data loaders, and editor runtime for agent-built Next.js sites.",
5
5
  "type": "module",
6
6
  "sideEffects": false,