@genlook/storefront 0.1.0

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 (60) hide show
  1. package/README.md +180 -0
  2. package/dist/analytics.d.ts +26 -0
  3. package/dist/analytics.js +7 -0
  4. package/dist/anonymous-id.d.ts +30 -0
  5. package/dist/anonymous-id.js +33 -0
  6. package/dist/client.d.ts +289 -0
  7. package/dist/client.js +465 -0
  8. package/dist/consent.d.ts +17 -0
  9. package/dist/consent.js +34 -0
  10. package/dist/create-client.d.ts +84 -0
  11. package/dist/create-client.js +131 -0
  12. package/dist/default-tracking.d.ts +64 -0
  13. package/dist/default-tracking.js +101 -0
  14. package/dist/email.d.ts +5 -0
  15. package/dist/email.js +24 -0
  16. package/dist/entities.d.ts +186 -0
  17. package/dist/entities.js +47 -0
  18. package/dist/erasure.d.ts +36 -0
  19. package/dist/erasure.js +23 -0
  20. package/dist/events.d.ts +226 -0
  21. package/dist/events.js +41 -0
  22. package/dist/fetch-transport.d.ts +65 -0
  23. package/dist/fetch-transport.js +112 -0
  24. package/dist/generation.d.ts +132 -0
  25. package/dist/generation.js +382 -0
  26. package/dist/history.d.ts +76 -0
  27. package/dist/history.js +68 -0
  28. package/dist/memory-storage.d.ts +10 -0
  29. package/dist/memory-storage.js +12 -0
  30. package/dist/pending-upload.d.ts +32 -0
  31. package/dist/pending-upload.js +13 -0
  32. package/dist/persistence.d.ts +12 -0
  33. package/dist/persistence.js +101 -0
  34. package/dist/policy.d.ts +24 -0
  35. package/dist/policy.js +45 -0
  36. package/dist/ports.d.ts +187 -0
  37. package/dist/ports.js +1 -0
  38. package/dist/public-api.d.ts +235 -0
  39. package/dist/public-api.js +1 -0
  40. package/dist/public.d.ts +33 -0
  41. package/dist/public.js +12 -0
  42. package/dist/settings.d.ts +38 -0
  43. package/dist/settings.js +70 -0
  44. package/dist/sharing.d.ts +26 -0
  45. package/dist/sharing.js +42 -0
  46. package/dist/storage-adapters.d.ts +69 -0
  47. package/dist/storage-adapters.js +82 -0
  48. package/dist/store.d.ts +9 -0
  49. package/dist/store.js +23 -0
  50. package/dist/tracker.d.ts +124 -0
  51. package/dist/tracker.js +229 -0
  52. package/dist/types.d.ts +91 -0
  53. package/dist/types.js +1 -0
  54. package/dist/upload.d.ts +123 -0
  55. package/dist/upload.js +176 -0
  56. package/dist/usage.d.ts +45 -0
  57. package/dist/usage.js +110 -0
  58. package/dist/version.d.ts +10 -0
  59. package/dist/version.js +1 -0
  60. package/package.json +30 -0
package/dist/usage.js ADDED
@@ -0,0 +1,110 @@
1
+ const DAY_MS = 24 * 60 * 60 * 1000;
2
+ const MAX_RETENTION_MS = 7 * DAY_MS;
3
+ export const LEGACY_LIMITS_KEY = "genlook-widget-user-limits";
4
+ function usageKey(storeId) {
5
+ return `tryon-usage:${storeId || "default"}`;
6
+ }
7
+ function asRecord(raw) {
8
+ if (!raw)
9
+ return null;
10
+ try {
11
+ const parsed = JSON.parse(raw);
12
+ return parsed && typeof parsed === "object" ? parsed : null;
13
+ }
14
+ catch {
15
+ return null;
16
+ }
17
+ }
18
+ function numericEmail(value) {
19
+ return typeof value === "string" ? value : null;
20
+ }
21
+ export function pruneTryOns(tryOns, now) {
22
+ const cutoff = now - MAX_RETENTION_MS;
23
+ return tryOns.filter((ts) => ts > cutoff);
24
+ }
25
+ function pruneUnique(values, now) {
26
+ return [...new Set(pruneTryOns(values, now))].sort((a, b) => a - b);
27
+ }
28
+ function numberArray(value) {
29
+ return Array.isArray(value) ? value.filter((t) => typeof t === "number") : [];
30
+ }
31
+ export function saveUsage(storage, storeId, usage, now) {
32
+ try {
33
+ storage.setItem(usageKey(storeId), JSON.stringify({
34
+ tryOns: pruneTryOns(usage.tryOns, now),
35
+ refunded: pruneTryOns(usage.refunded ?? [], now),
36
+ totalTryOns: usage.totalTryOns,
37
+ email: usage.email ?? null,
38
+ }));
39
+ }
40
+ catch {
41
+ }
42
+ }
43
+ export function mergeUsage(persisted, current, now) {
44
+ const refunded = pruneUnique(persisted ? [...persisted.refunded, ...current.refunded] : current.refunded, now);
45
+ const refundedSet = new Set(refunded);
46
+ const tryOns = pruneUnique(persisted ? [...persisted.tryOns, ...current.tryOns] : current.tryOns, now).filter((ts) => !refundedSet.has(ts));
47
+ const grossOf = (u) => u.totalTryOns + pruneUnique(u.refunded, now).length;
48
+ const gross = persisted ? Math.max(grossOf(persisted), grossOf(current)) : grossOf(current);
49
+ return {
50
+ tryOns,
51
+ refunded,
52
+ totalTryOns: Math.max(0, gross - refunded.length),
53
+ email: current.email ?? persisted?.email ?? null,
54
+ };
55
+ }
56
+ export function loadUsage(storage, storeId, now) {
57
+ let raw;
58
+ try {
59
+ raw = storage.getItem(usageKey(storeId));
60
+ }
61
+ catch {
62
+ return null;
63
+ }
64
+ const p = asRecord(raw);
65
+ if (!p || !Array.isArray(p.tryOns))
66
+ return null;
67
+ const rawTryOns = p.tryOns;
68
+ const tryOns = rawTryOns.filter((t) => typeof t === "number");
69
+ return {
70
+ tryOns: pruneTryOns(tryOns, now),
71
+ refunded: pruneTryOns(numberArray(p.refunded), now),
72
+ totalTryOns: typeof p.totalTryOns === "number" ? p.totalTryOns : rawTryOns.length,
73
+ email: numericEmail(p.email),
74
+ };
75
+ }
76
+ export function migrateLegacyUsage(storage, now) {
77
+ let raw;
78
+ try {
79
+ raw = storage.getItem(LEGACY_LIMITS_KEY);
80
+ }
81
+ catch {
82
+ return null;
83
+ }
84
+ const p = asRecord(raw);
85
+ if (!p)
86
+ return null;
87
+ if (Array.isArray(p.tryOns)) {
88
+ const rawTryOns = p.tryOns;
89
+ const tryOns = rawTryOns.filter((t) => typeof t === "number");
90
+ return {
91
+ tryOns: pruneTryOns(tryOns, now),
92
+ refunded: [],
93
+ totalTryOns: typeof p.totalTryOns === "number" ? p.totalTryOns : rawTryOns.length,
94
+ email: numericEmail(p.email),
95
+ };
96
+ }
97
+ if (typeof p.generationCount === "number" && p.generationCount > 0) {
98
+ const base = typeof p.firstGenerationAt === "number" ? p.firstGenerationAt : now;
99
+ const tryOns = [];
100
+ for (let i = 0; i < p.generationCount; i++)
101
+ tryOns.push(base + i);
102
+ return {
103
+ tryOns: pruneTryOns(tryOns, now),
104
+ refunded: [],
105
+ totalTryOns: p.generationCount,
106
+ email: numericEmail(p.email),
107
+ };
108
+ }
109
+ return { tryOns: [], refunded: [], totalTryOns: 0, email: numericEmail(p.email) };
110
+ }
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Published version of this package, emitted on every API call
3
+ * (`x-genlook-integration-version`) and every tracking batch
4
+ * (`context.integration_version`).
5
+ *
6
+ * Kept as a literal rather than read from package.json: the published build is
7
+ * plain ESM with no JSON import. The publish guard fails if it drifts from
8
+ * `package.json.version`, so bumping one means bumping the other.
9
+ */
10
+ export declare const SDK_VERSION = "0.1.0";
@@ -0,0 +1 @@
1
+ export const SDK_VERSION = "0.1.0";
package/package.json ADDED
@@ -0,0 +1,30 @@
1
+ {
2
+ "name": "@genlook/storefront",
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "sideEffects": false,
6
+ "files": [
7
+ "dist",
8
+ "README.md"
9
+ ],
10
+ "exports": {
11
+ ".": {
12
+ "types": "./dist/public.d.ts",
13
+ "default": "./dist/public.js"
14
+ }
15
+ },
16
+ "publishConfig": {
17
+ "access": "public"
18
+ },
19
+ "devDependencies": {
20
+ "typescript": "^5.9.3"
21
+ },
22
+ "scripts": {
23
+ "build": "tsc",
24
+ "build:publish": "rm -rf dist && tsc -p tsconfig.publish.types.json && tsc -p tsconfig.publish.js.json",
25
+ "typecheck": "tsc --noEmit",
26
+ "test": "node --import ./ts-resolve-hook.mjs --test"
27
+ },
28
+ "main": "./dist/public.js",
29
+ "types": "./dist/public.d.ts"
30
+ }