arky-sdk 0.7.89 → 0.7.90

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/dist/index.cjs CHANGED
@@ -505,10 +505,10 @@ var createStorefrontApi = (apiConfig) => {
505
505
  },
506
506
  crm: {
507
507
  customer: {
508
- async initialize(params, options) {
508
+ async session(params, options) {
509
509
  const business_id = params?.business_id || apiConfig.businessId;
510
510
  const result = await apiConfig.httpClient.post(
511
- `${base(business_id)}/customers/initialize`,
511
+ `${base(business_id)}/customers/session`,
512
512
  { business_id, market: params?.market || apiConfig.market || null },
513
513
  options
514
514
  );
@@ -2462,11 +2462,12 @@ function validatePhoneNumber(phone) {
2462
2462
  return { isValid: true };
2463
2463
  }
2464
2464
  function createStores() {
2465
+ const $customer = nanostores.atom(null);
2465
2466
  const $business = nanostores.atom(null);
2466
2467
  const $market = nanostores.atom(null);
2467
2468
  const $loading = nanostores.atom(false);
2468
2469
  const $error = nanostores.atom(null);
2469
- const $initialized = nanostores.atom(false);
2470
+ const $isLoggedIn = nanostores.computed($customer, (c) => !!c?.emails?.length);
2470
2471
  const $currency = nanostores.computed($market, (m) => m?.currency);
2471
2472
  const $currencySymbol = nanostores.computed($market, (m) => {
2472
2473
  if (!m?.currency) return void 0;
@@ -2494,11 +2495,12 @@ function createStores() {
2494
2495
  });
2495
2496
  const $zones = nanostores.computed($market, (m) => m?.zones || []);
2496
2497
  return {
2498
+ customer: $customer,
2497
2499
  business: $business,
2498
2500
  market: $market,
2499
2501
  loading: $loading,
2500
2502
  error: $error,
2501
- initialized: $initialized,
2503
+ isLoggedIn: $isLoggedIn,
2502
2504
  currency: $currency,
2503
2505
  currencySymbol: $currencySymbol,
2504
2506
  paymentMethods: $paymentMethods,
@@ -2508,9 +2510,9 @@ function createStores() {
2508
2510
  };
2509
2511
  }
2510
2512
  function populateStores(stores, data) {
2513
+ stores.customer.set(data.customer);
2511
2514
  stores.business.set(data.business);
2512
2515
  stores.market.set(data.market);
2513
- stores.initialized.set(true);
2514
2516
  }
2515
2517
 
2516
2518
  // src/utils/timezone.ts
@@ -2686,7 +2688,7 @@ function getFirstAvailableFCId(variant, quantity = 1) {
2686
2688
  }
2687
2689
 
2688
2690
  // src/index.ts
2689
- var SDK_VERSION = "0.7.89";
2691
+ var SDK_VERSION = "0.7.90";
2690
2692
  var SUPPORTED_FRAMEWORKS = [
2691
2693
  "astro",
2692
2694
  "react",
@@ -2933,32 +2935,6 @@ async function createAdmin(config) {
2933
2935
  };
2934
2936
  return sdk;
2935
2937
  }
2936
- var SESSION_CACHE_TTL = 36e5;
2937
- function getCachedSession(businessId) {
2938
- if (typeof window === "undefined") return null;
2939
- try {
2940
- const raw = localStorage.getItem(`arky_session_${businessId}`);
2941
- if (!raw) return null;
2942
- const parsed = JSON.parse(raw);
2943
- if (Date.now() - (parsed.timestamp || 0) > SESSION_CACHE_TTL) {
2944
- localStorage.removeItem(`arky_session_${businessId}`);
2945
- return null;
2946
- }
2947
- return { business: parsed.business, market: parsed.market };
2948
- } catch {
2949
- return null;
2950
- }
2951
- }
2952
- function setCachedSession(businessId, data) {
2953
- if (typeof window === "undefined") return;
2954
- try {
2955
- localStorage.setItem(
2956
- `arky_session_${businessId}`,
2957
- JSON.stringify({ ...data, timestamp: Date.now() })
2958
- );
2959
- } catch {
2960
- }
2961
- }
2962
2938
  function createStorefront(config) {
2963
2939
  const locale = config.locale || "en";
2964
2940
  const market = config.market || "";
@@ -2985,56 +2961,56 @@ function createStorefront(config) {
2985
2961
  };
2986
2962
  const storefrontApi = createStorefrontApi(apiConfig);
2987
2963
  const stores = createStores();
2988
- let initPromise = null;
2989
- function init() {
2990
- if (initPromise) return initPromise;
2991
- initPromise = (async () => {
2964
+ let sessionPromise = null;
2965
+ function session(market2) {
2966
+ if (market2 !== void 0) apiConfig.market = market2;
2967
+ if (sessionPromise) return sessionPromise;
2968
+ sessionPromise = (async () => {
2992
2969
  stores.loading.set(true);
2993
2970
  stores.error.set(null);
2994
- const cached = getCachedSession(apiConfig.businessId);
2995
- const tokens = await getToken();
2996
- const hasToken = !!tokens.access_token;
2997
- if (hasToken && cached) {
2998
- populateStores(stores, cached);
2999
- stores.loading.set(false);
3000
- return cached;
3001
- }
3002
- if (hasToken) {
3003
- const [business, allMarkets] = await Promise.all([
3004
- storefrontApi.business.getBusiness(),
3005
- storefrontApi.business.market.list().catch(() => [])
3006
- ]);
3007
- const marketData = apiConfig.market ? allMarkets.find((m) => m.key === apiConfig.market) || null : null;
3008
- const session2 = { business, market: marketData };
3009
- populateStores(stores, session2);
3010
- setCachedSession(apiConfig.businessId, session2);
2971
+ try {
2972
+ const result = await storefrontApi.crm.customer.session({
2973
+ market: apiConfig.market
2974
+ });
2975
+ const s = {
2976
+ customer: result.customer,
2977
+ business: result.business,
2978
+ market: result.market || null
2979
+ };
2980
+ populateStores(stores, s);
2981
+ return s;
2982
+ } catch (err) {
2983
+ const status = err?.status || err?.response?.status;
2984
+ if (status === 401) {
2985
+ await setToken({ access_token: "", refresh_token: "", access_expires_at: 0 });
2986
+ const result = await storefrontApi.crm.customer.session({
2987
+ market: apiConfig.market
2988
+ });
2989
+ const s = {
2990
+ customer: result.customer,
2991
+ business: result.business,
2992
+ market: result.market || null
2993
+ };
2994
+ populateStores(stores, s);
2995
+ return s;
2996
+ }
2997
+ throw err;
2998
+ } finally {
3011
2999
  stores.loading.set(false);
3012
- return session2;
3013
3000
  }
3014
- const result = await storefrontApi.crm.customer.initialize({
3015
- market: apiConfig.market
3016
- });
3017
- const session = {
3018
- business: result.business,
3019
- market: result.market || null
3020
- };
3021
- populateStores(stores, session);
3022
- setCachedSession(apiConfig.businessId, session);
3023
- stores.loading.set(false);
3024
- return session;
3025
3001
  })().catch((err) => {
3026
- stores.error.set(err.message || "Initialization failed");
3027
- stores.loading.set(false);
3028
- initPromise = null;
3002
+ stores.error.set(err.message || "Session failed");
3003
+ sessionPromise = null;
3029
3004
  throw err;
3030
3005
  });
3031
- return initPromise;
3006
+ return sessionPromise;
3032
3007
  }
3033
3008
  function setMarket(key) {
3034
- apiConfig.market = key;
3009
+ sessionPromise = null;
3010
+ return session(key);
3035
3011
  }
3036
3012
  return {
3037
- init,
3013
+ session,
3038
3014
  stores,
3039
3015
  business: storefrontApi.business,
3040
3016
  cms: storefrontApi.cms,