@wapitee/typhoonx-hydrogen 0.4.0 → 0.4.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.
package/README.md CHANGED
@@ -42,11 +42,11 @@ const {nonce, header, NonceProvider} = createContentSecurityPolicy({
42
42
 
43
43
  ### Props
44
44
 
45
- | Prop | Type | Required | Description |
46
- | -------------- | -------- | -------- | ------------------------------------------------------------------------ |
47
- | `merchantId` | `string` | yes | TyphoonX merchant ID (`TPX-…`) |
48
- | `shopId` | `string` | yes | Shopify store ID |
49
- | `cookieDomain` | `string` | no | Cookie `Domain` for a first-party client id. Omit for a host-only cookie |
45
+ | Prop | Type | Required | Description |
46
+ | -------------- | -------- | -------- | ----------------------------------------------------------------------------------------------------------------- |
47
+ | `merchantId` | `string` | yes | TyphoonX merchant ID (`TPX-…`) |
48
+ | `shopId` | `string` | yes | Shopify store ID |
49
+ | `cookieDomain` | `string` | no | Cookie `Domain` for a first-party client id. Defaults to the current hostname's apex domain (e.g. `.example.com`) |
50
50
 
51
51
  ## License
52
52
 
@@ -1,18 +1,3 @@
1
- /**
2
- * Returns the TyphoonX client id from the `__typhoon_client_id` cookie, or
3
- * creates a new UUID and writes it when the cookie is missing.
4
- *
5
- * The cookie is scoped to `Path=/`, uses `SameSite=Lax`, and expires after
6
- * one year. When `cookieDomain` is set, that value is used as the cookie
7
- * `Domain` so the id can be shared across subdomains. The `Secure` flag is
8
- * added automatically on HTTPS pages.
9
- *
10
- * @remarks
11
- * This helper must run in the browser. It reads and writes `document.cookie`.
12
- *
13
- * @param cookieDomain - Optional cookie `Domain` attribute, typically the
14
- * storefront apex domain.
15
- * @returns The existing or newly created client id.
16
- */
1
+ /** Existing `__typhoon_client_id`, or a new UUID cookie on the apex domain. */
17
2
  export declare function getOrCreateClientId(cookieDomain?: string): string;
18
3
  //# sourceMappingURL=client-id.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"client-id.d.ts","sourceRoot":"","sources":["../src/client-id.ts"],"names":[],"mappings":"AAMA;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,mBAAmB,CAAC,YAAY,CAAC,EAAE,MAAM,GAAG,MAAM,CA8BjE"}
1
+ {"version":3,"file":"client-id.d.ts","sourceRoot":"","sources":["../src/client-id.ts"],"names":[],"mappings":"AAwBA,+EAA+E;AAC/E,wBAAgB,mBAAmB,CAAC,YAAY,CAAC,EAAE,MAAM,GAAG,MAAM,CAWjE"}
package/dist/client-id.js CHANGED
@@ -1,46 +1,37 @@
1
- /** Cookie name that stores the anonymous TyphoonX client identifier. */
1
+ import CookieLib from 'universal-cookie';
2
2
  const COOKIE = '__typhoon_client_id';
3
- /** Cookie lifetime in seconds (365 days). */
4
- const MAX_AGE = 31_536_000;
5
- /**
6
- * Returns the TyphoonX client id from the `__typhoon_client_id` cookie, or
7
- * creates a new UUID and writes it when the cookie is missing.
8
- *
9
- * The cookie is scoped to `Path=/`, uses `SameSite=Lax`, and expires after
10
- * one year. When `cookieDomain` is set, that value is used as the cookie
11
- * `Domain` so the id can be shared across subdomains. The `Secure` flag is
12
- * added automatically on HTTPS pages.
13
- *
14
- * @remarks
15
- * This helper must run in the browser. It reads and writes `document.cookie`.
16
- *
17
- * @param cookieDomain - Optional cookie `Domain` attribute, typically the
18
- * storefront apex domain.
19
- * @returns The existing or newly created client id.
20
- */
3
+ const MAX_AGE = 31_536_000; // 365 days
4
+ // Runtime default export is the class.
5
+ const Cookies = CookieLib;
6
+ const cookies = new Cookies(null, {
7
+ path: '/',
8
+ sameSite: 'lax',
9
+ maxAge: MAX_AGE,
10
+ });
11
+ /** Existing `__typhoon_client_id`, or a new UUID cookie on the apex domain. */
21
12
  export function getOrCreateClientId(cookieDomain) {
22
- const prefix = `${COOKIE}=`;
23
- const existing = document.cookie
24
- .split(';')
25
- .map((part) => part.trim())
26
- .find((part) => part.startsWith(prefix))
27
- ?.slice(prefix.length);
13
+ const existing = cookies.get(COOKIE, { doNotParse: true });
28
14
  if (existing) {
29
- return decodeURIComponent(existing);
15
+ return existing;
30
16
  }
31
17
  const id = crypto.randomUUID();
32
- const segments = [
33
- `${COOKIE}=${id}`,
34
- 'Path=/',
35
- 'SameSite=Lax',
36
- `Max-Age=${String(MAX_AGE)}`,
37
- ];
38
- if (cookieDomain) {
39
- segments.push(`Domain=${cookieDomain}`);
40
- }
41
- if (window.location.protocol === 'https:') {
42
- segments.push('Secure');
43
- }
44
- document.cookie = segments.join('; ');
18
+ const domain = cookieDomain ?? apexCookieDomain(window.location.hostname);
19
+ const secure = window.location.protocol === 'https:';
20
+ cookies.set(COOKIE, id, domain ? { domain, secure } : { secure });
45
21
  return id;
46
22
  }
23
+ function apexCookieDomain(hostname) {
24
+ const labels = hostname.split('.');
25
+ const secure = window.location.protocol === 'https:';
26
+ for (let n = 2; n <= labels.length; n += 1) {
27
+ const domain = `.${labels.slice(-n).join('.')}`;
28
+ const token = crypto.randomUUID();
29
+ cookies.set('__typhoon_d', token, { domain, maxAge: 60, secure });
30
+ const probe = cookies.get('__typhoon_d', { doNotParse: true });
31
+ cookies.remove('__typhoon_d', { domain, secure });
32
+ if (probe === token) {
33
+ return domain;
34
+ }
35
+ }
36
+ return undefined;
37
+ }
package/package.json CHANGED
@@ -1,7 +1,15 @@
1
1
  {
2
2
  "name": "@wapitee/typhoonx-hydrogen",
3
- "version": "0.4.0",
3
+ "version": "0.4.2",
4
4
  "description": "TyphoonX Hydrogen Analytics subscriber",
5
+ "keywords": [
6
+ "typhoonx",
7
+ "shopify",
8
+ "hydrogen",
9
+ "analytics",
10
+ "react",
11
+ "storefront"
12
+ ],
5
13
  "homepage": "https://github.com/Wapitee-Interactive-Marketing-Limited/typhoonx-js/tree/main/packages/typhoonx-hydrogen",
6
14
  "bugs": {
7
15
  "url": "https://github.com/Wapitee-Interactive-Marketing-Limited/typhoonx-js/issues"
@@ -28,6 +36,9 @@
28
36
  "README.md",
29
37
  "dist"
30
38
  ],
39
+ "dependencies": {
40
+ "universal-cookie": "^8.1.2"
41
+ },
31
42
  "devDependencies": {
32
43
  "@shopify/hydrogen": "^2026.4.5",
33
44
  "@types/react": "^18.3.31",