arky-sdk 0.7.95 → 0.7.102

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.
@@ -392,7 +392,7 @@ var createStorefrontApi = (apiConfig) => {
392
392
  const items = paramItems || groupCartToItems(cart);
393
393
  return apiConfig.httpClient.post(
394
394
  `${base(target_store_id)}/bookings/checkout`,
395
- { market: "booking", ...payload, items },
395
+ { market: apiConfig.market, ...payload, items },
396
396
  options
397
397
  );
398
398
  },
@@ -415,7 +415,7 @@ var createStorefrontApi = (apiConfig) => {
415
415
  const target_store_id = store_id || apiConfig.storeId;
416
416
  return apiConfig.httpClient.post(
417
417
  `${base(target_store_id)}/bookings/quote`,
418
- { market: "booking", ...payload },
418
+ { market: apiConfig.market, ...payload },
419
419
  options
420
420
  );
421
421
  },
@@ -494,61 +494,46 @@ var createStorefrontApi = (apiConfig) => {
494
494
  },
495
495
  crm: {
496
496
  customer: {
497
- async session(params, options) {
498
- const store_id = params?.store_id || apiConfig.storeId;
499
- const result = await apiConfig.httpClient.post(
500
- `${base(store_id)}/customers/session`,
501
- { store_id, market: params?.market || apiConfig.market || null },
502
- options
503
- );
504
- if (result?.token?.access_token) {
505
- apiConfig.setToken(result.token);
506
- }
507
- return result;
508
- },
509
- async connect(params, options) {
510
- const store_id = params.store_id || apiConfig.storeId;
497
+ async identify(params, options) {
498
+ const store_id = apiConfig.storeId;
511
499
  const result = await apiConfig.httpClient.post(
512
- `${base(store_id)}/customers/connect`,
513
- { email: params.email, store_id },
500
+ `${base(store_id)}/customers/identify`,
501
+ {
502
+ store_id,
503
+ market: params?.market || apiConfig.market || null,
504
+ email: params?.email,
505
+ verify: params?.verify ?? false
506
+ },
514
507
  options
515
508
  );
516
- if (result?.access_token) {
517
- apiConfig.setToken(result);
509
+ if (result?.token?.token) {
510
+ apiConfig.setToken({ access_token: result.token.token });
518
511
  }
519
512
  return result;
520
513
  },
521
- requestCode(params, options) {
522
- const store_id = params.store_id || apiConfig.storeId;
523
- return apiConfig.httpClient.post(
524
- `${base(store_id)}/customers/auth/code`,
525
- { email: params.email, store_id },
526
- options
527
- );
528
- },
529
514
  async verify(params, options) {
530
- const store_id = params.store_id || apiConfig.storeId;
515
+ const store_id = apiConfig.storeId;
531
516
  const result = await apiConfig.httpClient.post(
532
- `${base(store_id)}/customers/auth/verify`,
533
- { email: params.email, code: params.code, store_id },
517
+ `${base(store_id)}/customers/verify`,
518
+ { store_id, code: params.code },
534
519
  options
535
520
  );
536
- if (result?.access_token) {
537
- apiConfig.setToken(result);
521
+ if (result?.token) {
522
+ apiConfig.setToken({ access_token: result.token });
538
523
  }
539
524
  return result;
540
525
  },
541
- async refreshToken(params, options) {
542
- const store_id = params.store_id || apiConfig.storeId;
543
- const result = await apiConfig.httpClient.post(
544
- `${base(store_id)}/customers/auth/refresh`,
545
- { refresh_token: params.refresh_token },
546
- options
547
- );
548
- if (result?.access_token) {
549
- apiConfig.setToken(result);
526
+ async logout(options) {
527
+ const store_id = apiConfig.storeId;
528
+ try {
529
+ await apiConfig.httpClient.post(
530
+ `${base(store_id)}/customers/logout`,
531
+ {},
532
+ options
533
+ );
534
+ } finally {
535
+ apiConfig.setToken({ access_token: "" });
550
536
  }
551
- return result;
552
537
  },
553
538
  getMe(options) {
554
539
  return apiConfig.httpClient.get(`${base()}/customers/me`, options);
@@ -709,36 +694,31 @@ function buildQueryString(params) {
709
694
  }
710
695
 
711
696
  // src/services/createHttpClient.ts
712
- var STORAGE_KEYS = {
713
- access_token: "arky_token",
714
- refresh_token: "arky_refresh",
715
- access_expires_at: "arky_expires_at"
716
- };
697
+ var TOKEN_KEY = "arky_token";
698
+ var LEGACY_KEYS = ["arky_refresh", "arky_expires_at"];
717
699
  function defaultGetToken() {
718
700
  if (typeof window === "undefined") return { access_token: "" };
719
701
  return {
720
- access_token: localStorage.getItem(STORAGE_KEYS.access_token) || "",
721
- refresh_token: localStorage.getItem(STORAGE_KEYS.refresh_token) || "",
722
- access_expires_at: parseInt(localStorage.getItem(STORAGE_KEYS.access_expires_at) || "0", 10)
702
+ access_token: localStorage.getItem(TOKEN_KEY) || ""
723
703
  };
724
704
  }
725
705
  function defaultSetToken(tokens) {
726
706
  if (typeof window === "undefined") return;
727
707
  if (tokens.access_token) {
728
- localStorage.setItem(STORAGE_KEYS.access_token, tokens.access_token);
729
- localStorage.setItem(STORAGE_KEYS.refresh_token, tokens.refresh_token || "");
730
- localStorage.setItem(STORAGE_KEYS.access_expires_at, (tokens.access_expires_at || 0).toString());
708
+ localStorage.setItem(TOKEN_KEY, tokens.access_token);
731
709
  } else {
732
- Object.values(STORAGE_KEYS).forEach((key) => localStorage.removeItem(key));
710
+ localStorage.removeItem(TOKEN_KEY);
733
711
  }
712
+ LEGACY_KEYS.forEach((key) => localStorage.removeItem(key));
734
713
  }
735
714
  function defaultLogout() {
736
715
  if (typeof window === "undefined") return;
737
- Object.values(STORAGE_KEYS).forEach((key) => localStorage.removeItem(key));
716
+ localStorage.removeItem(TOKEN_KEY);
717
+ LEGACY_KEYS.forEach((key) => localStorage.removeItem(key));
738
718
  }
739
719
  function defaultIsAuthenticated() {
740
720
  if (typeof window === "undefined") return false;
741
- const token = localStorage.getItem(STORAGE_KEYS.access_token) || "";
721
+ const token = localStorage.getItem(TOKEN_KEY) || "";
742
722
  return token.startsWith("customer_") || token.startsWith("account_");
743
723
  }
744
724
  function createHttpClient(cfg) {
@@ -1206,13 +1186,11 @@ function createStorefront(config) {
1206
1186
  const setToken = config.setToken || defaultSetToken;
1207
1187
  const logout = config.logout || defaultLogout;
1208
1188
  const isAuthenticated = config.isAuthenticated || defaultIsAuthenticated;
1209
- let refresh_store_id = config.storeId;
1210
1189
  const httpClient = createHttpClient({
1211
1190
  ...config,
1212
1191
  getToken,
1213
1192
  setToken,
1214
- logout,
1215
- refreshPath: () => `/v1/storefront/${refresh_store_id}/customers/auth/refresh`
1193
+ logout
1216
1194
  });
1217
1195
  const apiConfig = {
1218
1196
  httpClient,
@@ -1228,9 +1206,9 @@ function createStorefront(config) {
1228
1206
  let currentSession = null;
1229
1207
  const sessionListeners = /* @__PURE__ */ new Set();
1230
1208
  const customerApi = storefrontApi.crm.customer;
1231
- function emitSessionChange(session2) {
1209
+ function emitSessionChange(session) {
1232
1210
  for (const listener of sessionListeners) {
1233
- Promise.resolve().then(() => listener(session2)).catch(() => {
1211
+ Promise.resolve().then(() => listener(session)).catch(() => {
1234
1212
  });
1235
1213
  }
1236
1214
  }
@@ -1254,22 +1232,26 @@ function createStorefront(config) {
1254
1232
  clearSession();
1255
1233
  return result;
1256
1234
  }
1257
- function session(market2) {
1258
- if (market2 !== void 0) apiConfig.market = market2;
1259
- if (sessionPromise) return sessionPromise;
1260
- sessionPromise = (async () => {
1235
+ function identify(params) {
1236
+ if (params?.market !== void 0) apiConfig.market = params.market;
1237
+ const isBareCall = !params?.email && !params?.verify;
1238
+ if (isBareCall && sessionPromise) return sessionPromise;
1239
+ const promise = (async () => {
1261
1240
  try {
1262
- const result = await customerApi.session({
1263
- market: apiConfig.market
1241
+ const result = await customerApi.identify({
1242
+ market: apiConfig.market,
1243
+ email: params?.email,
1244
+ verify: params?.verify
1264
1245
  });
1265
1246
  return setCurrentSessionFromResult(result);
1266
1247
  } catch (err) {
1267
- const status = err?.statusCode || err?.status || err?.response?.status;
1268
- if (status === 401) {
1248
+ const e = err;
1249
+ const status = e?.statusCode || e?.status || e?.response?.status;
1250
+ if (isBareCall && status === 401) {
1269
1251
  currentSession = null;
1270
1252
  emitSessionChange(null);
1271
- await setToken({ access_token: "", refresh_token: "", access_expires_at: 0 });
1272
- const result = await customerApi.session({
1253
+ await setToken({ access_token: "" });
1254
+ const result = await customerApi.identify({
1273
1255
  market: apiConfig.market
1274
1256
  });
1275
1257
  return setCurrentSessionFromResult(result);
@@ -1277,10 +1259,11 @@ function createStorefront(config) {
1277
1259
  throw err;
1278
1260
  }
1279
1261
  })().catch((err) => {
1280
- sessionPromise = null;
1262
+ if (isBareCall) sessionPromise = null;
1281
1263
  throw err;
1282
1264
  });
1283
- return sessionPromise;
1265
+ if (isBareCall) sessionPromise = promise;
1266
+ return promise;
1284
1267
  }
1285
1268
  function setMarket(key) {
1286
1269
  apiConfig.market = key;
@@ -1299,10 +1282,6 @@ function createStorefront(config) {
1299
1282
  function getSession() {
1300
1283
  return currentSession;
1301
1284
  }
1302
- function logoutAndClearSession() {
1303
- clearSession();
1304
- return logout();
1305
- }
1306
1285
  function setTokenAndClearSession(tokens) {
1307
1286
  setToken(tokens);
1308
1287
  clearSession();
@@ -1311,17 +1290,33 @@ function createStorefront(config) {
1311
1290
  ...storefrontApi.crm,
1312
1291
  customer: {
1313
1292
  ...customerApi,
1314
- async session(params, options) {
1315
- const result = await customerApi.session(params, options);
1293
+ async identify(params, options) {
1294
+ const result = await customerApi.identify(params, options);
1316
1295
  setCurrentSessionFromResult(result);
1317
1296
  return result;
1318
1297
  },
1319
- connect: (params, options) => invalidateAfterAuth(customerApi.connect(params, options)),
1320
1298
  verify: (params, options) => invalidateAfterAuth(customerApi.verify(params, options))
1321
1299
  }
1322
1300
  };
1301
+ async function verify(params) {
1302
+ const result = await invalidateAfterAuth(customerApi.verify(params));
1303
+ return result;
1304
+ }
1305
+ async function me() {
1306
+ return customerApi.getMe();
1307
+ }
1308
+ async function logoutCustomer() {
1309
+ try {
1310
+ await customerApi.logout();
1311
+ } finally {
1312
+ clearSession();
1313
+ }
1314
+ }
1323
1315
  return {
1324
- session,
1316
+ identify,
1317
+ verify,
1318
+ me,
1319
+ logout: logoutCustomer,
1325
1320
  getSession,
1326
1321
  onSessionChange,
1327
1322
  store: storefrontApi.store,
@@ -1332,7 +1327,6 @@ function createStorefront(config) {
1332
1327
  activity: storefrontApi.activity,
1333
1328
  automation: storefrontApi.automation,
1334
1329
  setStoreId: (storeId) => {
1335
- refresh_store_id = storeId;
1336
1330
  apiConfig.storeId = storeId;
1337
1331
  clearSession();
1338
1332
  },
@@ -1344,7 +1338,6 @@ function createStorefront(config) {
1344
1338
  },
1345
1339
  getLocale: () => apiConfig.locale,
1346
1340
  isAuthenticated,
1347
- logout: logoutAndClearSession,
1348
1341
  setToken: setTokenAndClearSession,
1349
1342
  extractBlockValues,
1350
1343
  utils: createUtilitySurface(apiConfig)