arky-sdk 0.7.89 → 0.7.91

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.91";
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,63 +2961,77 @@ 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
+ let resolveReady;
2966
+ const ready = new Promise((r) => {
2967
+ resolveReady = r;
2968
+ });
2969
+ function session(market2) {
2970
+ if (market2 !== void 0) apiConfig.market = market2;
2971
+ if (sessionPromise) return sessionPromise;
2972
+ sessionPromise = (async () => {
2992
2973
  stores.loading.set(true);
2993
2974
  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);
2975
+ try {
2976
+ const result = await storefrontApi.crm.customer.session({
2977
+ market: apiConfig.market
2978
+ });
2979
+ const s = {
2980
+ customer: result.customer,
2981
+ business: result.business,
2982
+ market: result.market || null
2983
+ };
2984
+ populateStores(stores, s);
2985
+ resolveReady();
2986
+ return s;
2987
+ } catch (err) {
2988
+ const status = err?.status || err?.response?.status;
2989
+ if (status === 401) {
2990
+ await setToken({ access_token: "", refresh_token: "", access_expires_at: 0 });
2991
+ const result = await storefrontApi.crm.customer.session({
2992
+ market: apiConfig.market
2993
+ });
2994
+ const s = {
2995
+ customer: result.customer,
2996
+ business: result.business,
2997
+ market: result.market || null
2998
+ };
2999
+ populateStores(stores, s);
3000
+ resolveReady();
3001
+ return s;
3002
+ }
3003
+ throw err;
3004
+ } finally {
3011
3005
  stores.loading.set(false);
3012
- return session2;
3013
3006
  }
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
3007
  })().catch((err) => {
3026
- stores.error.set(err.message || "Initialization failed");
3027
- stores.loading.set(false);
3028
- initPromise = null;
3008
+ stores.error.set(err.message || "Session failed");
3009
+ sessionPromise = null;
3029
3010
  throw err;
3030
3011
  });
3031
- return initPromise;
3012
+ return sessionPromise;
3032
3013
  }
3014
+ const activity = {
3015
+ ...storefrontApi.activity,
3016
+ async track(params) {
3017
+ await ready;
3018
+ return storefrontApi.activity.track(params);
3019
+ }
3020
+ };
3033
3021
  function setMarket(key) {
3034
- apiConfig.market = key;
3022
+ sessionPromise = null;
3023
+ return session(key);
3035
3024
  }
3036
3025
  return {
3037
- init,
3026
+ session,
3038
3027
  stores,
3039
3028
  business: storefrontApi.business,
3040
3029
  cms: storefrontApi.cms,
3041
3030
  eshop: storefrontApi.eshop,
3042
3031
  booking: storefrontApi.booking,
3043
3032
  crm: storefrontApi.crm,
3044
- activity: storefrontApi.activity,
3033
+ activity,
3034
+ ready,
3045
3035
  automation: storefrontApi.automation,
3046
3036
  setBusinessId: (businessId) => {
3047
3037
  refresh_business_id = businessId;