@doswiftly/storefront-sdk 21.0.1 → 22.0.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.
@@ -67,6 +67,7 @@ export { AUTH_COOKIE_NAME, AUTH_COOKIE_DEFAULTS, type AuthCookieConfig, REFRESH_
67
67
  export { LANGUAGE_COOKIE_NAME, LANGUAGE_COOKIE_MAX_AGE, LANGUAGE_HEADER_NAME } from './language/cookie-config';
68
68
  export { CURRENCY_COOKIE_NAME, CURRENCY_COOKIE_MAX_AGE, CURRENCY_HEADER_NAME } from './currency/cookie-config';
69
69
  export { CART_COOKIE_NAME, CART_COOKIE_MAX_AGE, parseCartCookieValue, formatCartCookieValue, type CartCredentials, } from './cart/cookie-config';
70
+ export { REFERRAL_COOKIE_NAME, REFERRAL_COOKIE_MAX_AGE, REFERRAL_CODE_MAX_LENGTH, REFERRAL_CODE_PATTERN, extractReferralCodeFromUrl, } from './referral/cookie-config';
70
71
  export { matchesRoute, type RouteProtectionConfig } from './auth/routes';
71
72
  export { createSetTokenHandler, createClearTokenHandler, createWhoamiHandler, originAllowlistValidator, trustedForwardedHostValidator, } from './auth/handlers';
72
73
  export type { OriginValidator, OriginValidatorContext } from './auth/handlers';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/core/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AAGH,OAAO,EAAE,sBAAsB,EAAE,MAAM,wBAAwB,CAAC;AAGhE,YAAY,EACV,gBAAgB,EAChB,sBAAsB,EACtB,UAAU,EACV,SAAS,EACT,cAAc,EACd,eAAe,EACf,gBAAgB,EAChB,SAAS,EACT,aAAa,EACb,YAAY,EACZ,mBAAmB,EACnB,YAAY,EACZ,UAAU,EACV,kBAAkB,EAClB,eAAe,GAChB,MAAM,gBAAgB,CAAC;AAIxB,OAAO,EAAE,0BAA0B,EAAE,MAAM,iCAAiC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,qBAAqB,EACrB,uBAAuB,EACvB,mBAAmB,EACnB,wBAAwB,EACxB,6BAA6B,GAC9B,MAAM,iBAAiB,CAAC;AAGzB,YAAY,EAAE,eAAe,EAAE,sBAAsB,EAAE,MAAM,iBAAiB,CAAC;AAG/E,OAAO,EAAE,qBAAqB,EAAE,KAAK,eAAe,EAAE,MAAM,qBAAqB,CAAC;AAGlF,OAAO,EAAE,KAAK,SAAS,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAC;AAG7D,OAAO,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AAC3D,OAAO,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC"}
@@ -84,6 +84,8 @@ export { LANGUAGE_COOKIE_NAME, LANGUAGE_COOKIE_MAX_AGE, LANGUAGE_HEADER_NAME } f
84
84
  export { CURRENCY_COOKIE_NAME, CURRENCY_COOKIE_MAX_AGE, CURRENCY_HEADER_NAME } from './currency/cookie-config';
85
85
  // Cart cookie config + composite value parser/formatter (`<cartId>.<secret>`)
86
86
  export { CART_COOKIE_NAME, CART_COOKIE_MAX_AGE, parseCartCookieValue, formatCartCookieValue, } from './cart/cookie-config';
87
+ // Referral cookie config + URL extractor (loyalty referral program capture)
88
+ export { REFERRAL_COOKIE_NAME, REFERRAL_COOKIE_MAX_AGE, REFERRAL_CODE_MAX_LENGTH, REFERRAL_CODE_PATTERN, extractReferralCodeFromUrl, } from './referral/cookie-config';
87
89
  // Auth route matching
88
90
  export { matchesRoute } from './auth/routes';
89
91
  // Auth cookie handlers (API route factories)
@@ -0,0 +1,56 @@
1
+ /**
2
+ * Referral cookie configuration — platform contract.
3
+ *
4
+ * When a visitor lands on the storefront through a referral link
5
+ * (`https://shop.example/register?ref=REF-AB12CD34`), the code must survive
6
+ * navigation until the visitor actually signs up — possibly minutes or days
7
+ * later. The SDK persists it in a readable first-party cookie and the signup
8
+ * form passes it to `customerSignup(input: { referralCode })`.
9
+ *
10
+ * Used by:
11
+ * - `captureReferralCode` / `useReferralCapture` (client-side capture on landing)
12
+ * - `readReferralCodeCookie` (client + server readers at signup time)
13
+ */
14
+ export declare const REFERRAL_COOKIE_NAME = "referral-code";
15
+ /**
16
+ * Default cookie lifetime: 30 days.
17
+ *
18
+ * This is the "landing → signup" window only. It is intentionally NOT tied to
19
+ * the shop's referral validity setting (days the referred customer has to
20
+ * place their first order) — that window starts at signup and is validated
21
+ * exclusively by the backend. Coupling the two would be wrong: a shop giving
22
+ * referred customers 7 days to order would silently lose visitors who sign up
23
+ * 8 days after clicking the link. Override per call via
24
+ * `captureReferralCode({ maxAge })` if you need a different capture window.
25
+ */
26
+ export declare const REFERRAL_COOKIE_MAX_AGE: number;
27
+ /**
28
+ * Maximum accepted code length — longer values are ignored as garbage.
29
+ * Mirrors the `customerSignup` input contract (the API rejects longer codes).
30
+ */
31
+ export declare const REFERRAL_CODE_MAX_LENGTH = 50;
32
+ /**
33
+ * Accepted code shape: letters, digits and dashes (codes are case-insensitive —
34
+ * the API uppercases on lookup). Mirrors the platform referral-code charset;
35
+ * a non-conforming value is never persisted to the cookie, because the API
36
+ * would silently ignore it at signup anyway — storing it for 30 days would
37
+ * just carry dead weight (or someone's injection attempt) around. Widen this
38
+ * pattern only together with the `customerSignup.referralCode` API contract.
39
+ */
40
+ export declare const REFERRAL_CODE_PATTERN: RegExp;
41
+ /**
42
+ * Extract a referral code from a URL or a search string.
43
+ *
44
+ * Accepts a full or relative URL (`https://shop.example/register?ref=CODE`,
45
+ * `/register?ref=CODE`), a search string starting with `?` (`?ref=CODE`) or a
46
+ * `URL` instance. Returns the trimmed code, or `null` when the parameter is
47
+ * absent, empty, longer than {@link REFERRAL_CODE_MAX_LENGTH} or outside
48
+ * {@link REFERRAL_CODE_PATTERN} (such values would be ignored by the API
49
+ * anyway, so they are never persisted).
50
+ *
51
+ * @param input - URL, `URL` instance or `?`-prefixed search string to inspect.
52
+ * @param paramName - Query parameter carrying the code (default `'ref'` —
53
+ * matches the share links generated by the platform).
54
+ */
55
+ export declare function extractReferralCodeFromUrl(input: string | URL, paramName?: string): string | null;
56
+ //# sourceMappingURL=cookie-config.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cookie-config.d.ts","sourceRoot":"","sources":["../../../src/core/referral/cookie-config.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,eAAO,MAAM,oBAAoB,kBAAkB,CAAC;AAEpD;;;;;;;;;;GAUG;AACH,eAAO,MAAM,uBAAuB,QAAoB,CAAC;AAEzD;;;GAGG;AACH,eAAO,MAAM,wBAAwB,KAAK,CAAC;AAE3C;;;;;;;GAOG;AACH,eAAO,MAAM,qBAAqB,QAAoB,CAAC;AAEvD;;;;;;;;;;;;;GAaG;AACH,wBAAgB,0BAA0B,CAAC,KAAK,EAAE,MAAM,GAAG,GAAG,EAAE,SAAS,SAAQ,GAAG,MAAM,GAAG,IAAI,CAyBhG"}
@@ -0,0 +1,83 @@
1
+ /**
2
+ * Referral cookie configuration — platform contract.
3
+ *
4
+ * When a visitor lands on the storefront through a referral link
5
+ * (`https://shop.example/register?ref=REF-AB12CD34`), the code must survive
6
+ * navigation until the visitor actually signs up — possibly minutes or days
7
+ * later. The SDK persists it in a readable first-party cookie and the signup
8
+ * form passes it to `customerSignup(input: { referralCode })`.
9
+ *
10
+ * Used by:
11
+ * - `captureReferralCode` / `useReferralCapture` (client-side capture on landing)
12
+ * - `readReferralCodeCookie` (client + server readers at signup time)
13
+ */
14
+ export const REFERRAL_COOKIE_NAME = 'referral-code';
15
+ /**
16
+ * Default cookie lifetime: 30 days.
17
+ *
18
+ * This is the "landing → signup" window only. It is intentionally NOT tied to
19
+ * the shop's referral validity setting (days the referred customer has to
20
+ * place their first order) — that window starts at signup and is validated
21
+ * exclusively by the backend. Coupling the two would be wrong: a shop giving
22
+ * referred customers 7 days to order would silently lose visitors who sign up
23
+ * 8 days after clicking the link. Override per call via
24
+ * `captureReferralCode({ maxAge })` if you need a different capture window.
25
+ */
26
+ export const REFERRAL_COOKIE_MAX_AGE = 30 * 24 * 60 * 60; // 30 days
27
+ /**
28
+ * Maximum accepted code length — longer values are ignored as garbage.
29
+ * Mirrors the `customerSignup` input contract (the API rejects longer codes).
30
+ */
31
+ export const REFERRAL_CODE_MAX_LENGTH = 50;
32
+ /**
33
+ * Accepted code shape: letters, digits and dashes (codes are case-insensitive —
34
+ * the API uppercases on lookup). Mirrors the platform referral-code charset;
35
+ * a non-conforming value is never persisted to the cookie, because the API
36
+ * would silently ignore it at signup anyway — storing it for 30 days would
37
+ * just carry dead weight (or someone's injection attempt) around. Widen this
38
+ * pattern only together with the `customerSignup.referralCode` API contract.
39
+ */
40
+ export const REFERRAL_CODE_PATTERN = /^[A-Za-z0-9-]+$/;
41
+ /**
42
+ * Extract a referral code from a URL or a search string.
43
+ *
44
+ * Accepts a full or relative URL (`https://shop.example/register?ref=CODE`,
45
+ * `/register?ref=CODE`), a search string starting with `?` (`?ref=CODE`) or a
46
+ * `URL` instance. Returns the trimmed code, or `null` when the parameter is
47
+ * absent, empty, longer than {@link REFERRAL_CODE_MAX_LENGTH} or outside
48
+ * {@link REFERRAL_CODE_PATTERN} (such values would be ignored by the API
49
+ * anyway, so they are never persisted).
50
+ *
51
+ * @param input - URL, `URL` instance or `?`-prefixed search string to inspect.
52
+ * @param paramName - Query parameter carrying the code (default `'ref'` —
53
+ * matches the share links generated by the platform).
54
+ */
55
+ export function extractReferralCodeFromUrl(input, paramName = 'ref') {
56
+ let params;
57
+ if (input instanceof URL) {
58
+ params = input.searchParams;
59
+ }
60
+ else {
61
+ const raw = input.trim();
62
+ if (!raw)
63
+ return null;
64
+ if (raw.startsWith('?')) {
65
+ // Search-string form: "?ref=CODE&utm_source=x"
66
+ params = new URLSearchParams(raw.slice(1));
67
+ }
68
+ else {
69
+ // Full or relative URL — the base argument makes relative paths
70
+ // ("/register?ref=CODE") parse too.
71
+ try {
72
+ params = new URL(raw, 'http://localhost').searchParams;
73
+ }
74
+ catch {
75
+ return null;
76
+ }
77
+ }
78
+ }
79
+ const value = params.get(paramName)?.trim() ?? '';
80
+ if (!value || value.length > REFERRAL_CODE_MAX_LENGTH || !REFERRAL_CODE_PATTERN.test(value))
81
+ return null;
82
+ return value;
83
+ }
@@ -0,0 +1,9 @@
1
+ import { type CaptureReferralCodeOptions } from '../referral';
2
+ export interface UseReferralCaptureOptions extends Pick<CaptureReferralCodeOptions, 'paramName' | 'maxAge'> {
3
+ }
4
+ /**
5
+ * Capture the referral code from the current URL into the `referral-code`
6
+ * cookie and return the active code (`null` when none).
7
+ */
8
+ export declare function useReferralCapture(options?: UseReferralCaptureOptions): string | null;
9
+ //# sourceMappingURL=use-referral-capture.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"use-referral-capture.d.ts","sourceRoot":"","sources":["../../../src/react/hooks/use-referral-capture.ts"],"names":[],"mappings":"AA4BA,OAAO,EAAuB,KAAK,0BAA0B,EAAE,MAAM,aAAa,CAAC;AAEnF,MAAM,WAAW,yBACf,SAAQ,IAAI,CAAC,0BAA0B,EAAE,WAAW,GAAG,QAAQ,CAAC;CAAG;AAErE;;;GAGG;AACH,wBAAgB,kBAAkB,CAAC,OAAO,CAAC,EAAE,yBAAyB,GAAG,MAAM,GAAG,IAAI,CAWrF"}
@@ -0,0 +1,40 @@
1
+ 'use client';
2
+ /**
3
+ * useReferralCapture — capture a referral code from the landing URL.
4
+ *
5
+ * Mount the hook once near the top of the app (layout or a dedicated client
6
+ * component). On mount it inspects the current URL for the referral parameter
7
+ * (`?ref=CODE` by default), persists it in the `referral-code` cookie and
8
+ * returns the active code (from the URL, or a previously stored one when the
9
+ * visitor navigated away from the landing URL before signing up).
10
+ *
11
+ * At signup time read the code with `readReferralCodeCookie()` (or use the
12
+ * value returned here), pass it as `customerSignup(input: { referralCode })`
13
+ * and call `clearReferralCodeCookie()` after success.
14
+ *
15
+ * @example
16
+ * ```tsx
17
+ * 'use client';
18
+ * import { useReferralCapture } from '@doswiftly/storefront-sdk/react';
19
+ *
20
+ * export function ReferralCapture() {
21
+ * useReferralCapture();
22
+ * return null;
23
+ * }
24
+ * ```
25
+ */
26
+ import { useEffect, useState } from 'react';
27
+ import { captureReferralCode } from '../referral';
28
+ /**
29
+ * Capture the referral code from the current URL into the `referral-code`
30
+ * cookie and return the active code (`null` when none).
31
+ */
32
+ export function useReferralCapture(options) {
33
+ const [code, setCode] = useState(null);
34
+ const paramName = options?.paramName;
35
+ const maxAge = options?.maxAge;
36
+ useEffect(() => {
37
+ setCode(captureReferralCode({ paramName, maxAge }));
38
+ }, [paramName, maxAge]);
39
+ return code;
40
+ }
@@ -37,6 +37,8 @@ export type { ShopConfig } from './types/shop-config';
37
37
  export { selectCurrency, selectBaseCurrency, selectSupportedCurrencies, selectIsLoaded } from './stores/currency.store';
38
38
  export { selectLanguage, selectDefaultLanguage, selectSupportedLanguages, selectLanguageIsLoaded } from './stores/language.store';
39
39
  export { getCookie, setCookie, deleteCookie, createBrowserCartCookieStore, } from './cookies';
40
+ export { captureReferralCode, getReferralCodeCookie, clearReferralCodeCookie, type CaptureReferralCodeOptions, } from './referral';
41
+ export { useReferralCapture, type UseReferralCaptureOptions } from './hooks/use-referral-capture';
40
42
  export { useBotProtection } from './hooks/use-bot-protection';
41
43
  export { useHydrated } from './hooks/use-hydrated';
42
44
  export { useDebouncedValue } from './hooks/use-debounced-value';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/react/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAGH,OAAO,EAAE,kBAAkB,EAAE,KAAK,uBAAuB,EAAE,MAAM,iCAAiC,CAAC;AACnG,OAAO,EAAE,wBAAwB,EAAE,KAAK,6BAA6B,EAAE,MAAM,wCAAwC,CAAC;AACtH,OAAO,EAAE,gBAAgB,EAAE,KAAK,qBAAqB,EAAE,MAAM,+BAA+B,CAAC;AAC7F,OAAO,EAAE,gBAAgB,EAAE,KAAK,qBAAqB,EAAE,MAAM,+BAA+B,CAAC;AAC7F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AO,EACL,wBAAwB,EACxB,4BAA4B,EAC5B,KAAK,kBAAkB,GACxB,MAAM,wBAAwB,CAAC"}
@@ -36,8 +36,12 @@ export { useLanguageStore, useLanguageStoreApi } from './stores/store-context';
36
36
  export { selectCurrency, selectBaseCurrency, selectSupportedCurrencies, selectIsLoaded } from './stores/currency.store';
37
37
  export { selectLanguage, selectDefaultLanguage, selectSupportedLanguages, selectLanguageIsLoaded } from './stores/language.store';
38
38
  // Cookie utilities (client-side). Server-side readers (readCartIdCookie,
39
- // readCurrencyCookie) live in `@doswiftly/storefront-sdk/react/server`.
39
+ // readCurrencyCookie, readReferralCodeCookie) live in
40
+ // `@doswiftly/storefront-sdk/react/server`.
40
41
  export { getCookie, setCookie, deleteCookie, createBrowserCartCookieStore, } from './cookies';
42
+ // Referral capture (loyalty referral program — landing-link `?ref=CODE` → cookie)
43
+ export { captureReferralCode, getReferralCodeCookie, clearReferralCodeCookie, } from './referral';
44
+ export { useReferralCapture } from './hooks/use-referral-capture';
41
45
  // Bot protection
42
46
  export { useBotProtection } from './hooks/use-bot-protection';
43
47
  // Generic hooks
@@ -0,0 +1,53 @@
1
+ /**
2
+ * Client-side referral capture helpers.
3
+ *
4
+ * Flow: a visitor lands through a referral share link (`/register?ref=CODE`),
5
+ * `captureReferralCode()` persists the code in the readable `referral-code`
6
+ * cookie, and the signup form reads it back with `getReferralCodeCookie()`
7
+ * and passes it as `customerSignup(input: { referralCode })`. After a
8
+ * successful signup call `clearReferralCodeCookie()` so the code is not
9
+ * re-submitted by other browser tabs or future registrations.
10
+ *
11
+ * All helpers are SSR-safe (no-op / `null` without a `document`). For Server
12
+ * Components use the async `readReferralCodeCookie` from
13
+ * `@doswiftly/storefront-sdk/react/server` instead — the client helper is
14
+ * deliberately named differently (`get…`, synchronous) so the two cannot be
15
+ * confused in auto-imports.
16
+ */
17
+ export interface CaptureReferralCodeOptions {
18
+ /**
19
+ * URL (or search string) to inspect. Defaults to the current
20
+ * `window.location.href`.
21
+ */
22
+ url?: string | URL;
23
+ /** Query parameter carrying the code. Default `'ref'`. */
24
+ paramName?: string;
25
+ /**
26
+ * Cookie lifetime in seconds. Default {@link REFERRAL_COOKIE_MAX_AGE}
27
+ * (30 days) — the "landing → signup" window, independent of the shop's
28
+ * referral validity setting (see the constant's docs).
29
+ */
30
+ maxAge?: number;
31
+ }
32
+ /**
33
+ * Capture a referral code from the current URL (or an explicit one) into the
34
+ * `referral-code` cookie.
35
+ *
36
+ * Returns the captured code, or the previously stored one when the URL carries
37
+ * no code (so a visitor who navigated away from the landing URL still counts),
38
+ * or `null` when there is nothing to capture. A code present in the URL always
39
+ * wins over a previously stored one — the most recent referral link decides.
40
+ */
41
+ export declare function captureReferralCode(options?: CaptureReferralCodeOptions): string | null;
42
+ /**
43
+ * Read the stored referral code (client-side, synchronous). Returns `null`
44
+ * when absent or outside a browser environment. Server Components use the
45
+ * async `readReferralCodeCookie` from `@doswiftly/storefront-sdk/react/server`.
46
+ */
47
+ export declare function getReferralCodeCookie(): string | null;
48
+ /**
49
+ * Remove the stored referral code. Call after a successful signup so the code
50
+ * is not re-submitted by future registrations from the same browser.
51
+ */
52
+ export declare function clearReferralCodeCookie(): void;
53
+ //# sourceMappingURL=referral.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"referral.d.ts","sourceRoot":"","sources":["../../src/react/referral.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AASH,MAAM,WAAW,0BAA0B;IACzC;;;OAGG;IACH,GAAG,CAAC,EAAE,MAAM,GAAG,GAAG,CAAC;IACnB,0DAA0D;IAC1D,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB;;;;OAIG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED;;;;;;;;GAQG;AACH,wBAAgB,mBAAmB,CAAC,OAAO,GAAE,0BAA+B,GAAG,MAAM,GAAG,IAAI,CAU3F;AAED;;;;GAIG;AACH,wBAAgB,qBAAqB,IAAI,MAAM,GAAG,IAAI,CAErD;AAED;;;GAGG;AACH,wBAAgB,uBAAuB,IAAI,IAAI,CAE9C"}
@@ -0,0 +1,51 @@
1
+ /**
2
+ * Client-side referral capture helpers.
3
+ *
4
+ * Flow: a visitor lands through a referral share link (`/register?ref=CODE`),
5
+ * `captureReferralCode()` persists the code in the readable `referral-code`
6
+ * cookie, and the signup form reads it back with `getReferralCodeCookie()`
7
+ * and passes it as `customerSignup(input: { referralCode })`. After a
8
+ * successful signup call `clearReferralCodeCookie()` so the code is not
9
+ * re-submitted by other browser tabs or future registrations.
10
+ *
11
+ * All helpers are SSR-safe (no-op / `null` without a `document`). For Server
12
+ * Components use the async `readReferralCodeCookie` from
13
+ * `@doswiftly/storefront-sdk/react/server` instead — the client helper is
14
+ * deliberately named differently (`get…`, synchronous) so the two cannot be
15
+ * confused in auto-imports.
16
+ */
17
+ import { REFERRAL_COOKIE_NAME, REFERRAL_COOKIE_MAX_AGE, extractReferralCodeFromUrl, } from '../core/referral/cookie-config';
18
+ import { getCookie, setCookie, deleteCookie } from './cookies';
19
+ /**
20
+ * Capture a referral code from the current URL (or an explicit one) into the
21
+ * `referral-code` cookie.
22
+ *
23
+ * Returns the captured code, or the previously stored one when the URL carries
24
+ * no code (so a visitor who navigated away from the landing URL still counts),
25
+ * or `null` when there is nothing to capture. A code present in the URL always
26
+ * wins over a previously stored one — the most recent referral link decides.
27
+ */
28
+ export function captureReferralCode(options = {}) {
29
+ const url = options.url ?? (typeof window !== 'undefined' ? window.location.href : null);
30
+ const fromUrl = url ? extractReferralCodeFromUrl(url, options.paramName) : null;
31
+ if (fromUrl) {
32
+ setCookie(REFERRAL_COOKIE_NAME, fromUrl, { maxAge: options.maxAge ?? REFERRAL_COOKIE_MAX_AGE });
33
+ return fromUrl;
34
+ }
35
+ return getReferralCodeCookie();
36
+ }
37
+ /**
38
+ * Read the stored referral code (client-side, synchronous). Returns `null`
39
+ * when absent or outside a browser environment. Server Components use the
40
+ * async `readReferralCodeCookie` from `@doswiftly/storefront-sdk/react/server`.
41
+ */
42
+ export function getReferralCodeCookie() {
43
+ return getCookie(REFERRAL_COOKIE_NAME);
44
+ }
45
+ /**
46
+ * Remove the stored referral code. Call after a successful signup so the code
47
+ * is not re-submitted by future registrations from the same browser.
48
+ */
49
+ export function clearReferralCodeCookie() {
50
+ deleteCookie(REFERRAL_COOKIE_NAME);
51
+ }
@@ -46,4 +46,11 @@ export declare function readCartIdCookie(): Promise<string | null>;
46
46
  export declare function readCartCredentials(): Promise<CartCredentials | null>;
47
47
  /** Read the first-party `preferred-currency` cookie (server-first, client fallback). */
48
48
  export declare function readCurrencyCookie(): Promise<string | null>;
49
+ /**
50
+ * Read the first-party `referral-code` cookie (server-first, client fallback) —
51
+ * the referral code captured when the visitor landed through a referral share
52
+ * link. Useful for SSR-rendered signup forms that pre-fill the code field;
53
+ * pass it as `customerSignup(input: { referralCode })`.
54
+ */
55
+ export declare function readReferralCodeCookie(): Promise<string | null>;
49
56
  //# sourceMappingURL=cookie-readers.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"cookie-readers.d.ts","sourceRoot":"","sources":["../../../src/react/server/cookie-readers.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAEH,OAAO,EAGL,KAAK,eAAe,EACrB,MAAM,+BAA+B,CAAC;AAqBvC,6EAA6E;AAC7E,wBAAsB,gBAAgB,IAAI,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAE/D;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAsB,mBAAmB,IAAI,OAAO,CAAC,eAAe,GAAG,IAAI,CAAC,CAE3E;AAED,wFAAwF;AACxF,wBAAsB,kBAAkB,IAAI,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAEjE"}
1
+ {"version":3,"file":"cookie-readers.d.ts","sourceRoot":"","sources":["../../../src/react/server/cookie-readers.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAEH,OAAO,EAGL,KAAK,eAAe,EACrB,MAAM,+BAA+B,CAAC;AAsBvC,6EAA6E;AAC7E,wBAAsB,gBAAgB,IAAI,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAE/D;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAsB,mBAAmB,IAAI,OAAO,CAAC,eAAe,GAAG,IAAI,CAAC,CAE3E;AAED,wFAAwF;AACxF,wBAAsB,kBAAkB,IAAI,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAEjE;AAED;;;;;GAKG;AACH,wBAAsB,sBAAsB,IAAI,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAErE"}
@@ -24,6 +24,7 @@
24
24
  */
25
25
  import { CART_COOKIE_NAME, parseCartCookieValue, } from '../../core/cart/cookie-config';
26
26
  import { CURRENCY_COOKIE_NAME } from '../../core/currency/cookie-config';
27
+ import { REFERRAL_COOKIE_NAME } from '../../core/referral/cookie-config';
27
28
  import { getCookie } from '../cookies';
28
29
  /**
29
30
  * Read a cookie server-first: the request cookies via `next/headers` in a Server
@@ -71,3 +72,12 @@ export async function readCartCredentials() {
71
72
  export async function readCurrencyCookie() {
72
73
  return readCookieIsomorphic(CURRENCY_COOKIE_NAME);
73
74
  }
75
+ /**
76
+ * Read the first-party `referral-code` cookie (server-first, client fallback) —
77
+ * the referral code captured when the visitor landed through a referral share
78
+ * link. Useful for SSR-rendered signup forms that pre-fill the code field;
79
+ * pass it as `customerSignup(input: { referralCode })`.
80
+ */
81
+ export async function readReferralCodeCookie() {
82
+ return readCookieIsomorphic(REFERRAL_COOKIE_NAME);
83
+ }
@@ -1,7 +1,7 @@
1
1
  export { getStorefrontClient, type ServerClientOptions } from './get-storefront-client';
2
2
  export { createStorefrontAuthRoute, type StorefrontAuthRouteOptions, type StorefrontAuthRouteHandlers, } from './create-storefront-auth-route';
3
3
  export { getInitialAuth, type InitialAuth } from './get-initial-auth';
4
- export { readCartIdCookie, readCartCredentials, readCurrencyCookie } from './cookie-readers';
4
+ export { readCartIdCookie, readCartCredentials, readCurrencyCookie, readReferralCodeCookie } from './cookie-readers';
5
5
  export { serverCartSecretMiddleware } from '../../core/middleware/cart-secret';
6
6
  export { trustedForwardedHostValidator, originAllowlistValidator, type OriginValidator, type OriginValidatorContext, } from '../../core/auth/handlers';
7
7
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/react/server/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,KAAK,mBAAmB,EAAE,MAAM,yBAAyB,CAAC;AAGxF,OAAO,EACL,yBAAyB,EACzB,KAAK,0BAA0B,EAC/B,KAAK,2BAA2B,GACjC,MAAM,gCAAgC,CAAC;AAGxC,OAAO,EAAE,cAAc,EAAE,KAAK,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAGtE,OAAO,EAAE,gBAAgB,EAAE,mBAAmB,EAAE,kBAAkB,EAAE,MAAM,kBAAkB,CAAC;AAI7F,OAAO,EAAE,0BAA0B,EAAE,MAAM,mCAAmC,CAAC;AAG/E,OAAO,EACL,6BAA6B,EAC7B,wBAAwB,EACxB,KAAK,eAAe,EACpB,KAAK,sBAAsB,GAC5B,MAAM,0BAA0B,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/react/server/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,KAAK,mBAAmB,EAAE,MAAM,yBAAyB,CAAC;AAGxF,OAAO,EACL,yBAAyB,EACzB,KAAK,0BAA0B,EAC/B,KAAK,2BAA2B,GACjC,MAAM,gCAAgC,CAAC;AAGxC,OAAO,EAAE,cAAc,EAAE,KAAK,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAGtE,OAAO,EAAE,gBAAgB,EAAE,mBAAmB,EAAE,kBAAkB,EAAE,sBAAsB,EAAE,MAAM,kBAAkB,CAAC;AAIrH,OAAO,EAAE,0BAA0B,EAAE,MAAM,mCAAmC,CAAC;AAG/E,OAAO,EACL,6BAA6B,EAC7B,wBAAwB,EACxB,KAAK,eAAe,EACpB,KAAK,sBAAsB,GAC5B,MAAM,0BAA0B,CAAC"}
@@ -3,8 +3,8 @@ export { getStorefrontClient } from './get-storefront-client';
3
3
  export { createStorefrontAuthRoute, } from './create-storefront-auth-route';
4
4
  // Server-only cold-start auth seed from the first-party cookies.
5
5
  export { getInitialAuth } from './get-initial-auth';
6
- // Server-first readers for the readable first-party cookies (cart-id, currency).
7
- export { readCartIdCookie, readCartCredentials, readCurrencyCookie } from './cookie-readers';
6
+ // Server-first readers for the readable first-party cookies (cart-id, currency, referral).
7
+ export { readCartIdCookie, readCartCredentials, readCurrencyCookie, readReferralCodeCookie } from './cookie-readers';
8
8
  // Server cart-secret middleware — prepend to `getStorefrontClient({ middleware })`
9
9
  // with `await readCartCredentials()` so SSR / edge cart reads carry the secret.
10
10
  export { serverCartSecretMiddleware } from '../../core/middleware/cart-secret';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@doswiftly/storefront-sdk",
3
- "version": "21.0.1",
3
+ "version": "22.0.0",
4
4
  "description": "Storefront runtime SDK for DoSwiftly Commerce — layered transport, middleware pipeline, React providers, Zustand stores, cache strategies. 0 runtime dependencies in core.",
5
5
  "type": "module",
6
6
  "sideEffects": false,