arky-sdk 0.7.63 → 0.7.64

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
@@ -89,9 +89,9 @@ function createHttpClient(cfg) {
89
89
  "Content-Type": "application/json",
90
90
  ...options?.headers || {}
91
91
  };
92
- let { accessToken, expiresAt } = await cfg.getToken();
92
+ let { accessToken, accessExpiresAt } = await cfg.getToken();
93
93
  const nowSec = Date.now() / 1e3;
94
- if (expiresAt && nowSec > expiresAt) {
94
+ if (accessExpiresAt && nowSec > accessExpiresAt) {
95
95
  await ensureFreshToken();
96
96
  const tokens = await cfg.getToken();
97
97
  accessToken = tokens.accessToken;
@@ -2714,7 +2714,7 @@ function getFirstAvailableFCId(variant, quantity = 1) {
2714
2714
  }
2715
2715
 
2716
2716
  // src/index.ts
2717
- var SDK_VERSION = "0.7.63";
2717
+ var SDK_VERSION = "0.7.64";
2718
2718
  var SUPPORTED_FRAMEWORKS = [
2719
2719
  "astro",
2720
2720
  "react",
@@ -2985,16 +2985,36 @@ async function createStorefront(config) {
2985
2985
  getToken: config.getToken
2986
2986
  };
2987
2987
  const storefrontApi = createStorefrontApi(apiConfig);
2988
- if (typeof window !== "undefined" && apiConfig.businessId) {
2989
- storefrontApi.business.getIntegrationConfig({ businessId: apiConfig.businessId, type: "analytics" }).then((configs) => {
2990
- if (!configs) return;
2991
- for (const c of Array.isArray(configs) ? configs : [configs]) {
2992
- if (c.measurementId) {
2993
- injectGA4Script(c.measurementId);
2994
- }
2988
+ let sessionPromise = null;
2989
+ function ensureSession() {
2990
+ if (typeof window === "undefined") return Promise.resolve();
2991
+ if (sessionPromise) return sessionPromise;
2992
+ sessionPromise = (async () => {
2993
+ const tokens = await apiConfig.getToken();
2994
+ if (tokens.accessToken) return;
2995
+ const result = await storefrontApi.crm.customer.initialize();
2996
+ if (result?.accessToken) {
2997
+ apiConfig.setToken(result);
2995
2998
  }
2996
- }).catch(() => {
2999
+ })().finally(() => {
3000
+ sessionPromise = null;
3001
+ });
3002
+ return sessionPromise;
3003
+ }
3004
+ if (typeof window !== "undefined") {
3005
+ ensureSession().catch(() => {
2997
3006
  });
3007
+ if (apiConfig.businessId) {
3008
+ storefrontApi.business.getIntegrationConfig({ businessId: apiConfig.businessId, type: "analytics" }).then((configs) => {
3009
+ if (!configs) return;
3010
+ for (const c of Array.isArray(configs) ? configs : [configs]) {
3011
+ if (c.measurementId) {
3012
+ injectGA4Script(c.measurementId);
3013
+ }
3014
+ }
3015
+ }).catch(() => {
3016
+ });
3017
+ }
2998
3018
  }
2999
3019
  return {
3000
3020
  business: storefrontApi.business,
@@ -3006,6 +3026,7 @@ async function createStorefront(config) {
3006
3026
  analytics: {
3007
3027
  track
3008
3028
  },
3029
+ ensureSession,
3009
3030
  setBusinessId: (businessId) => {
3010
3031
  apiConfig.businessId = businessId;
3011
3032
  },