@doswiftly/storefront-sdk 4.4.0 → 4.5.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 (89) hide show
  1. package/dist/core/cart/types.d.ts +53 -20
  2. package/dist/core/cart/types.d.ts.map +1 -1
  3. package/dist/core/cart/types.js +3 -0
  4. package/dist/core/operations/cart.d.ts +15 -9
  5. package/dist/core/operations/cart.d.ts.map +1 -1
  6. package/dist/core/operations/cart.js +130 -58
  7. package/dist/index.d.ts +1 -1
  8. package/dist/index.js +1 -1
  9. package/package.json +9 -4
  10. package/src/__tests__/contract/storefront-api.contract.test.ts +0 -450
  11. package/src/__tests__/unit/auth-client.test.ts +0 -210
  12. package/src/__tests__/unit/bot-protection.test.ts +0 -461
  13. package/src/__tests__/unit/cart-client.test.ts +0 -233
  14. package/src/__tests__/unit/cart-store.test.ts +0 -349
  15. package/src/__tests__/unit/create-client.test.ts +0 -356
  16. package/src/__tests__/unit/helpers.test.ts +0 -377
  17. package/src/__tests__/unit/middleware.test.ts +0 -374
  18. package/src/__tests__/unit/test-helpers.ts +0 -103
  19. package/src/core/auth/auth-client.ts +0 -123
  20. package/src/core/auth/cookie-config.ts +0 -23
  21. package/src/core/auth/handlers.ts +0 -168
  22. package/src/core/auth/routes.ts +0 -26
  23. package/src/core/auth/token-client.ts +0 -51
  24. package/src/core/auth/types.ts +0 -54
  25. package/src/core/bot-protection/abstract-manager.ts +0 -185
  26. package/src/core/bot-protection/create-manager.ts +0 -37
  27. package/src/core/bot-protection/eucaptcha-manager.ts +0 -88
  28. package/src/core/bot-protection/fallback-manager.ts +0 -43
  29. package/src/core/bot-protection/turnstile-manager.ts +0 -92
  30. package/src/core/bot-protection/types/eucaptcha.d.ts +0 -28
  31. package/src/core/bot-protection/types/turnstile.d.ts +0 -33
  32. package/src/core/cache.ts +0 -102
  33. package/src/core/cart/cart-client.ts +0 -150
  34. package/src/core/cart/cookie-config.ts +0 -13
  35. package/src/core/cart/types.ts +0 -104
  36. package/src/core/client/compose.ts +0 -15
  37. package/src/core/client/create-client.ts +0 -129
  38. package/src/core/client/dedupe.ts +0 -19
  39. package/src/core/client/execute.ts +0 -70
  40. package/src/core/client/hash.ts +0 -21
  41. package/src/core/client/operation-name.ts +0 -12
  42. package/src/core/client/types.ts +0 -171
  43. package/src/core/currency/cookie-config.ts +0 -13
  44. package/src/core/errors.ts +0 -67
  45. package/src/core/format.ts +0 -254
  46. package/src/core/helpers/assert-no-user-errors.ts +0 -21
  47. package/src/core/helpers/normalize-connection.ts +0 -48
  48. package/src/core/helpers/sanitize-html.ts +0 -42
  49. package/src/core/image.ts +0 -22
  50. package/src/core/index.ts +0 -174
  51. package/src/core/language/cookie-config.ts +0 -13
  52. package/src/core/middleware/auth.ts +0 -27
  53. package/src/core/middleware/bot-protection.ts +0 -140
  54. package/src/core/middleware/currency.ts +0 -27
  55. package/src/core/middleware/errors.ts +0 -86
  56. package/src/core/middleware/language.ts +0 -30
  57. package/src/core/middleware/retry.ts +0 -75
  58. package/src/core/middleware/timeout.ts +0 -61
  59. package/src/core/operations/auth.ts +0 -123
  60. package/src/core/operations/cart.ts +0 -185
  61. package/src/index.ts +0 -25
  62. package/src/react/bot-protection/bot-protection-context.ts +0 -17
  63. package/src/react/bot-protection/bot-protection-widget.tsx +0 -46
  64. package/src/react/cookies.ts +0 -89
  65. package/src/react/helpers/create-store-context.ts +0 -56
  66. package/src/react/hooks/use-auth.ts +0 -218
  67. package/src/react/hooks/use-bot-protection.ts +0 -31
  68. package/src/react/hooks/use-cart-manager.ts +0 -236
  69. package/src/react/hooks/use-currency.ts +0 -23
  70. package/src/react/hooks/use-debounced-value.ts +0 -30
  71. package/src/react/hooks/use-hydrated.ts +0 -20
  72. package/src/react/hooks/use-storefront-client.ts +0 -12
  73. package/src/react/index.ts +0 -71
  74. package/src/react/providers/currency-provider.tsx +0 -30
  75. package/src/react/providers/language-provider.tsx +0 -34
  76. package/src/react/providers/storefront-client-provider.tsx +0 -107
  77. package/src/react/providers/storefront-provider.tsx +0 -99
  78. package/src/react/server/get-storefront-client.ts +0 -60
  79. package/src/react/server/index.ts +0 -1
  80. package/src/react/stores/auth.store.ts +0 -112
  81. package/src/react/stores/cart.context.ts +0 -10
  82. package/src/react/stores/cart.store.ts +0 -254
  83. package/src/react/stores/currency.store.ts +0 -93
  84. package/src/react/stores/index.ts +0 -17
  85. package/src/react/stores/language.store.ts +0 -90
  86. package/src/react/stores/store-context.tsx +0 -103
  87. package/src/react/types/shop-config.ts +0 -22
  88. package/tsconfig.json +0 -20
  89. package/vitest.config.ts +0 -14
@@ -1,93 +0,0 @@
1
- /**
2
- * Currency store factory — cookie persistence (SSR-safe).
3
- *
4
- * Creates a vanilla Zustand store (not a React hook singleton).
5
- * Instance is created in StorefrontProvider via useRef and provided
6
- * through React Context — eliminates module duplication in Turbopack.
7
- */
8
-
9
- import { createStore } from 'zustand/vanilla';
10
- import type { ShopConfig } from '../types/shop-config';
11
- import { CURRENCY_COOKIE_NAME, CURRENCY_COOKIE_MAX_AGE } from '../../core/currency/cookie-config';
12
- import { getCookie, setCookie } from '../cookies';
13
-
14
- /**
15
- * @deprecated Use ShopConfig instead. Kept for backward compatibility of type exports.
16
- */
17
- export type ShopCurrencyData = ShopConfig;
18
-
19
- export interface CurrencyStore {
20
- // State
21
- baseCurrency: string | null;
22
- supportedCurrencies: string[];
23
- currency: string | null;
24
- isLoaded: boolean;
25
-
26
- // Actions
27
- initialize: (shopData: ShopConfig) => void;
28
- setCurrency: (currency: string) => void;
29
- }
30
-
31
- export const createCurrencyStore = () =>
32
- createStore<CurrencyStore>()((set, get) => ({
33
- baseCurrency: null,
34
- supportedCurrencies: [],
35
- currency: null,
36
- isLoaded: false,
37
-
38
- initialize: (shopData: ShopConfig) => {
39
- // 1. Try saved currency from cookie (SSR-safe)
40
- const saved = getCookie(CURRENCY_COOKIE_NAME);
41
-
42
- // 2. Try to detect from browser locale
43
- let detected: string | undefined;
44
- if (shopData.localeToCurrencyMap && typeof navigator !== 'undefined') {
45
- const browserLocale = navigator.language;
46
- const match = shopData.localeToCurrencyMap.find(
47
- (m) => m.locale === browserLocale,
48
- );
49
- detected = match?.currency;
50
- }
51
-
52
- // 3. Determine final currency (priority: saved > detected > base)
53
- const finalCurrency =
54
- saved && shopData.supportedCurrencies.includes(saved)
55
- ? saved
56
- : detected && shopData.supportedCurrencies.includes(detected)
57
- ? detected
58
- : shopData.currencyCode;
59
-
60
- set({
61
- baseCurrency: shopData.currencyCode,
62
- supportedCurrencies: shopData.supportedCurrencies,
63
- currency: finalCurrency,
64
- isLoaded: true,
65
- });
66
-
67
- // Ensure cookie is set if we determined a currency
68
- if (finalCurrency && !saved) {
69
- setCookie(CURRENCY_COOKIE_NAME, finalCurrency, { maxAge: CURRENCY_COOKIE_MAX_AGE });
70
- }
71
- },
72
-
73
- setCurrency: (currency: string) => {
74
- const { supportedCurrencies } = get();
75
-
76
- if (!supportedCurrencies.includes(currency)) {
77
- console.warn(`[CurrencyStore] Currency ${currency} not supported`);
78
- return;
79
- }
80
-
81
- set({ currency });
82
- setCookie(CURRENCY_COOKIE_NAME, currency, { maxAge: CURRENCY_COOKIE_MAX_AGE });
83
- },
84
- }));
85
-
86
- // ---------------------------------------------------------------------------
87
- // Selectors (for use with useCurrencyStore(selector))
88
- // ---------------------------------------------------------------------------
89
-
90
- export const selectCurrency = (state: CurrencyStore) => state.currency;
91
- export const selectBaseCurrency = (state: CurrencyStore) => state.baseCurrency;
92
- export const selectSupportedCurrencies = (state: CurrencyStore) => state.supportedCurrencies;
93
- export const selectIsLoaded = (state: CurrencyStore) => state.isLoaded;
@@ -1,17 +0,0 @@
1
- /**
2
- * Zustand stores — factory functions + context hooks.
3
- */
4
-
5
- // Factory functions
6
- export { createAuthStore, type AuthStore, type CustomerInfo } from './auth.store';
7
- export { createCurrencyStore, type CurrencyStore, type ShopCurrencyData } from './currency.store';
8
-
9
- // Selectors
10
- export { selectCurrency, selectBaseCurrency, selectSupportedCurrencies, selectIsLoaded } from './currency.store';
11
-
12
- // Context hooks
13
- export {
14
- useAuthStore, useAuthStoreApi, useAuthHydrated,
15
- useCurrencyStore, useCurrencyStoreApi,
16
- AuthStoreContext, CurrencyStoreContext,
17
- } from './store-context';
@@ -1,90 +0,0 @@
1
- /**
2
- * Language store factory — tracks active language for SDK middleware.
3
- *
4
- * Creates a vanilla Zustand store (not a React hook singleton).
5
- * Instance is created in StorefrontProvider via useRef and provided
6
- * through React Context — eliminates module duplication in Turbopack.
7
- *
8
- * KEY PRINCIPLE: URL is the single source of truth for language.
9
- * - next-intl middleware uses `preferred-language` cookie (custom name via localeCookie.name)
10
- * - This store tracks the active language for SDK X-Lang header
11
- * - setLanguage() persists to `preferred-language` cookie
12
- * - initialize() sets defaultLanguage and supportedLanguages from Shop query
13
- * - setLanguage() is called by LanguageSyncProvider when URL locale changes
14
- *
15
- * Single `preferred-language` cookie serves both:
16
- * - next-intl middleware (locale detection/routing, via localeCookie.name override)
17
- * - SDK language store (X-Lang header, framework-agnostic)
18
- */
19
-
20
- import { createStore } from 'zustand/vanilla';
21
- import type { ShopConfig } from '../types/shop-config';
22
- import { LANGUAGE_COOKIE_NAME, LANGUAGE_COOKIE_MAX_AGE } from '../../core/language/cookie-config';
23
- import { getCookie, setCookie } from '../cookies';
24
-
25
- export interface LanguageStore {
26
- // State
27
- defaultLanguage: string | null;
28
- supportedLanguages: string[];
29
- language: string | null;
30
- isLoaded: boolean;
31
-
32
- // Actions
33
- initialize: (shopData: ShopConfig) => void;
34
- setLanguage: (language: string) => void;
35
- }
36
-
37
- export const createLanguageStore = (initialLanguage?: string | null) =>
38
- createStore<LanguageStore>()((set, get) => ({
39
- defaultLanguage: null,
40
- supportedLanguages: [],
41
- language: initialLanguage ?? null,
42
- isLoaded: false,
43
-
44
- initialize: (shopData: ShopConfig) => {
45
- const supported = shopData.supportedLanguages ?? [];
46
- const current = get().language;
47
- const saved = getCookie(LANGUAGE_COOKIE_NAME);
48
-
49
- // Priority: current (from initialLanguage/URL) > cookie (validated) > default
50
- const finalLanguage =
51
- current && (supported.length === 0 || supported.includes(current))
52
- ? current
53
- : saved && (supported.length === 0 || supported.includes(saved))
54
- ? saved
55
- : shopData.defaultLanguage ?? null;
56
-
57
- set({
58
- defaultLanguage: shopData.defaultLanguage ?? null,
59
- supportedLanguages: supported,
60
- language: finalLanguage,
61
- isLoaded: true,
62
- });
63
-
64
- // Persist cookie if determined from fallback (not already saved)
65
- if (finalLanguage && finalLanguage !== saved) {
66
- setCookie(LANGUAGE_COOKIE_NAME, finalLanguage, { maxAge: LANGUAGE_COOKIE_MAX_AGE });
67
- }
68
- },
69
-
70
- setLanguage: (language: string) => {
71
- const { supportedLanguages } = get();
72
- // Validate vs supported (bypass if empty — LanguageSyncProvider may call before initialize)
73
- if (supportedLanguages.length > 0 && !supportedLanguages.includes(language)) {
74
- console.warn(`[LanguageStore] Language ${language} not supported`);
75
- return;
76
- }
77
- if (get().language === language) return;
78
- setCookie(LANGUAGE_COOKIE_NAME, language, { maxAge: LANGUAGE_COOKIE_MAX_AGE });
79
- set({ language });
80
- },
81
- }));
82
-
83
- // ---------------------------------------------------------------------------
84
- // Selectors (for use with useLanguageStore(selector))
85
- // ---------------------------------------------------------------------------
86
-
87
- export const selectLanguage = (state: LanguageStore) => state.language;
88
- export const selectDefaultLanguage = (state: LanguageStore) => state.defaultLanguage;
89
- export const selectSupportedLanguages = (state: LanguageStore) => state.supportedLanguages;
90
- export const selectLanguageIsLoaded = (state: LanguageStore) => state.isLoaded;
@@ -1,103 +0,0 @@
1
- /**
2
- * Store Context — React Context + hooks for Zustand vanilla stores.
3
- *
4
- * This is the SINGLE source for useAuthStore and useCurrencyStore hooks.
5
- * Stores are created in StorefrontProvider (useRef) and provided via Context.
6
- * This eliminates module-level singletons and Turbopack duplication bugs.
7
- *
8
- * useAuthHydrated() replaces the old isHydrated store field — tracks
9
- * Zustand persist rehydration via React lifecycle (not module-level code).
10
- */
11
-
12
- 'use client';
13
-
14
- import { createContext, useContext, useState, useEffect } from 'react';
15
- import { useStore, type StoreApi } from 'zustand';
16
- import type { AuthStore } from './auth.store';
17
- import type { CurrencyStore } from './currency.store';
18
- import type { LanguageStore } from './language.store';
19
-
20
- // ---------------------------------------------------------------------------
21
- // Contexts
22
- // ---------------------------------------------------------------------------
23
-
24
- export const AuthStoreContext = createContext<StoreApi<AuthStore> | null>(null);
25
- export const CurrencyStoreContext = createContext<StoreApi<CurrencyStore> | null>(null);
26
- export const LanguageStoreContext = createContext<StoreApi<LanguageStore> | null>(null);
27
-
28
- // ---------------------------------------------------------------------------
29
- // Auth hooks
30
- // ---------------------------------------------------------------------------
31
-
32
- export function useAuthStore(): AuthStore;
33
- export function useAuthStore<T>(selector: (s: AuthStore) => T): T;
34
- export function useAuthStore<T>(selector?: (s: AuthStore) => T) {
35
- const store = useContext(AuthStoreContext);
36
- if (!store) throw new Error('useAuthStore must be used within StorefrontProvider');
37
- // eslint-disable-next-line react-hooks/rules-of-hooks
38
- return selector ? useStore(store, selector) : useStore(store);
39
- }
40
-
41
- export function useAuthStoreApi(): StoreApi<AuthStore> {
42
- const store = useContext(AuthStoreContext);
43
- if (!store) throw new Error('useAuthStoreApi must be used within StorefrontProvider');
44
- return store;
45
- }
46
-
47
- /**
48
- * Tracks persist hydration state via React lifecycle (not module-level).
49
- * Returns `true` once Zustand persist has finished rehydrating from localStorage.
50
- */
51
- export function useAuthHydrated(): boolean {
52
- const store = useAuthStoreApi();
53
- const [hydrated, setHydrated] = useState(false);
54
-
55
- useEffect(() => {
56
- const persistApi = (store as any).persist;
57
- // Subscribe to future hydration completions
58
- const unsubFinish = persistApi.onFinishHydration(() => setHydrated(true));
59
- // Check if hydration already completed before effect ran
60
- if (persistApi.hasHydrated()) setHydrated(true);
61
- return unsubFinish;
62
- }, [store]);
63
-
64
- return hydrated;
65
- }
66
-
67
- // ---------------------------------------------------------------------------
68
- // Currency hooks
69
- // ---------------------------------------------------------------------------
70
-
71
- export function useCurrencyStore(): CurrencyStore;
72
- export function useCurrencyStore<T>(selector: (s: CurrencyStore) => T): T;
73
- export function useCurrencyStore<T>(selector?: (s: CurrencyStore) => T) {
74
- const store = useContext(CurrencyStoreContext);
75
- if (!store) throw new Error('useCurrencyStore must be used within StorefrontProvider');
76
- // eslint-disable-next-line react-hooks/rules-of-hooks
77
- return selector ? useStore(store, selector) : useStore(store);
78
- }
79
-
80
- export function useCurrencyStoreApi(): StoreApi<CurrencyStore> {
81
- const store = useContext(CurrencyStoreContext);
82
- if (!store) throw new Error('useCurrencyStoreApi must be used within StorefrontProvider');
83
- return store;
84
- }
85
-
86
- // ---------------------------------------------------------------------------
87
- // Language hooks
88
- // ---------------------------------------------------------------------------
89
-
90
- export function useLanguageStore(): LanguageStore;
91
- export function useLanguageStore<T>(selector: (s: LanguageStore) => T): T;
92
- export function useLanguageStore<T>(selector?: (s: LanguageStore) => T) {
93
- const store = useContext(LanguageStoreContext);
94
- if (!store) throw new Error('useLanguageStore must be used within StorefrontProvider');
95
- // eslint-disable-next-line react-hooks/rules-of-hooks
96
- return selector ? useStore(store, selector) : useStore(store);
97
- }
98
-
99
- export function useLanguageStoreApi(): StoreApi<LanguageStore> {
100
- const store = useContext(LanguageStoreContext);
101
- if (!store) throw new Error('useLanguageStoreApi must be used within StorefrontProvider');
102
- return store;
103
- }
@@ -1,22 +0,0 @@
1
- /**
2
- * ShopConfig — full shop configuration from backend shop query.
3
- *
4
- * Flat interface — new features added here directly.
5
- * Each concern is consumed by its own provider internally.
6
- */
7
-
8
- import type { BotProtectionConfig } from '../../core/middleware/bot-protection';
9
-
10
- export interface ShopConfig {
11
- // Currency
12
- currencyCode: string;
13
- supportedCurrencies: string[];
14
- localeToCurrencyMap?: Array<{ locale: string; currency: string }>;
15
-
16
- // Language
17
- defaultLanguage?: string | null;
18
- supportedLanguages?: string[] | null;
19
-
20
- // Bot protection (platform-level, optional)
21
- botProtection?: BotProtectionConfig | null;
22
- }
package/tsconfig.json DELETED
@@ -1,20 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "target": "ES2022",
4
- "module": "ESNext",
5
- "lib": ["ES2022", "DOM"],
6
- "declaration": true,
7
- "declarationMap": true,
8
- "outDir": "./dist",
9
- "rootDir": "./src",
10
- "strict": true,
11
- "esModuleInterop": true,
12
- "skipLibCheck": true,
13
- "forceConsistentCasingInFileNames": true,
14
- "moduleResolution": "bundler",
15
- "resolveJsonModule": true,
16
- "jsx": "react-jsx"
17
- },
18
- "include": ["src/**/*"],
19
- "exclude": ["node_modules", "dist", "**/*.spec.ts", "**/*.test.ts"]
20
- }
package/vitest.config.ts DELETED
@@ -1,14 +0,0 @@
1
- import { defineConfig } from "vitest/config";
2
-
3
- export default defineConfig({
4
- // Suppress CJS deprecation warning
5
- define: {
6
- "import.meta.vitest": "undefined",
7
- },
8
- test: {
9
- environment: "node",
10
- globals: true, // Enable global test functions like describe, it, expect
11
- // Use pool threads for better compatibility
12
- pool: "threads",
13
- },
14
- });