arky-sdk 0.9.0 → 0.9.6
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/README.md +2 -2
- package/dist/{admin-CfHen7c5.d.cts → admin-DjYydKeB.d.ts} +477 -124
- package/dist/{admin-ru7pX5bd.d.ts → admin-DlL8mCxL.d.cts} +477 -124
- package/dist/admin.cjs +592 -202
- package/dist/admin.cjs.map +1 -1
- package/dist/admin.d.cts +3 -3
- package/dist/admin.d.ts +3 -3
- package/dist/admin.js +592 -202
- package/dist/admin.js.map +1 -1
- package/dist/api-BbBHcd4p.d.cts +3289 -0
- package/dist/api-BbBHcd4p.d.ts +3289 -0
- package/dist/{index-C5gikdBg.d.cts → index-CZxubTDA.d.ts} +1 -1
- package/dist/{index-MFMjlIfS.d.ts → index-nCF3Z6Af.d.cts} +1 -1
- package/dist/index.cjs +666 -319
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +3 -3
- package/dist/index.d.ts +3 -3
- package/dist/index.js +666 -319
- package/dist/index.js.map +1 -1
- package/dist/storefront-store.cjs +149 -134
- package/dist/storefront-store.cjs.map +1 -1
- package/dist/storefront-store.d.cts +3 -3
- package/dist/storefront-store.d.ts +3 -3
- package/dist/storefront-store.js +149 -134
- package/dist/storefront-store.js.map +1 -1
- package/dist/storefront.cjs +149 -134
- package/dist/storefront.cjs.map +1 -1
- package/dist/storefront.d.cts +45 -102
- package/dist/storefront.d.ts +45 -102
- package/dist/storefront.js +149 -134
- package/dist/storefront.js.map +1 -1
- package/dist/types.cjs.map +1 -1
- package/dist/types.d.cts +1 -2666
- package/dist/types.d.ts +1 -2666
- package/dist/types.js.map +1 -1
- package/dist/utils.d.cts +2 -2
- package/dist/utils.d.ts +2 -2
- package/package.json +3 -2
|
@@ -179,7 +179,7 @@ var createActivityApi = (apiConfig) => ({
|
|
|
179
179
|
}
|
|
180
180
|
}
|
|
181
181
|
});
|
|
182
|
-
var createStorefrontApi = (apiConfig,
|
|
182
|
+
var createStorefrontApi = (apiConfig, updateProfileSession) => {
|
|
183
183
|
const base = (storeId = apiConfig.storeId) => `/v1/storefront/${storeId}`;
|
|
184
184
|
return {
|
|
185
185
|
store: {
|
|
@@ -500,11 +500,11 @@ var createStorefrontApi = (apiConfig, updateCustomerSession) => {
|
|
|
500
500
|
}
|
|
501
501
|
},
|
|
502
502
|
crm: {
|
|
503
|
-
|
|
503
|
+
profile: {
|
|
504
504
|
async identify(params, options) {
|
|
505
505
|
const store_id = apiConfig.storeId;
|
|
506
506
|
const result = await apiConfig.httpClient.post(
|
|
507
|
-
`${base(store_id)}/
|
|
507
|
+
`${base(store_id)}/profiles/identify`,
|
|
508
508
|
{
|
|
509
509
|
store_id,
|
|
510
510
|
market: params?.market || apiConfig.market || null,
|
|
@@ -514,9 +514,9 @@ var createStorefrontApi = (apiConfig, updateCustomerSession) => {
|
|
|
514
514
|
options
|
|
515
515
|
);
|
|
516
516
|
if (result?.token?.token) {
|
|
517
|
-
|
|
517
|
+
updateProfileSession(() => ({
|
|
518
518
|
access_token: result.token.token,
|
|
519
|
-
|
|
519
|
+
profile: result.profile,
|
|
520
520
|
store: result.store,
|
|
521
521
|
market: result.market
|
|
522
522
|
}));
|
|
@@ -526,12 +526,12 @@ var createStorefrontApi = (apiConfig, updateCustomerSession) => {
|
|
|
526
526
|
async verify(params, options) {
|
|
527
527
|
const store_id = apiConfig.storeId;
|
|
528
528
|
const result = await apiConfig.httpClient.post(
|
|
529
|
-
`${base(store_id)}/
|
|
529
|
+
`${base(store_id)}/profiles/verify`,
|
|
530
530
|
{ store_id, code: params.code },
|
|
531
531
|
options
|
|
532
532
|
);
|
|
533
533
|
if (result?.token) {
|
|
534
|
-
|
|
534
|
+
updateProfileSession(
|
|
535
535
|
(prev) => prev ? { ...prev, access_token: result.token } : null
|
|
536
536
|
);
|
|
537
537
|
}
|
|
@@ -541,132 +541,63 @@ var createStorefrontApi = (apiConfig, updateCustomerSession) => {
|
|
|
541
541
|
const store_id = apiConfig.storeId;
|
|
542
542
|
try {
|
|
543
543
|
await apiConfig.httpClient.post(
|
|
544
|
-
`${base(store_id)}/
|
|
544
|
+
`${base(store_id)}/profiles/logout`,
|
|
545
545
|
{},
|
|
546
546
|
options
|
|
547
547
|
);
|
|
548
548
|
} finally {
|
|
549
|
-
|
|
549
|
+
updateProfileSession(() => null);
|
|
550
550
|
}
|
|
551
551
|
},
|
|
552
552
|
getMe(options) {
|
|
553
|
-
return apiConfig.httpClient.get(`${base()}/
|
|
553
|
+
return apiConfig.httpClient.get(`${base()}/profiles/me`, options);
|
|
554
554
|
}
|
|
555
555
|
},
|
|
556
|
-
|
|
556
|
+
profileList: {
|
|
557
557
|
get(params, options) {
|
|
558
|
-
|
|
559
|
-
if (params.id) {
|
|
560
|
-
identifier = params.id;
|
|
561
|
-
} else if (params.key) {
|
|
562
|
-
identifier = `${apiConfig.storeId}:${params.key}`;
|
|
563
|
-
} else {
|
|
564
|
-
throw new Error("GetAudienceParams requires id or key");
|
|
565
|
-
}
|
|
558
|
+
const store_id = params.store_id || apiConfig.storeId;
|
|
566
559
|
return apiConfig.httpClient.get(
|
|
567
|
-
`${base(
|
|
560
|
+
`${base(store_id)}/profile-lists/${params.id}`,
|
|
568
561
|
options
|
|
569
562
|
);
|
|
570
563
|
},
|
|
571
564
|
find(params, options) {
|
|
572
|
-
|
|
565
|
+
const { store_id, ...queryParams } = params || {};
|
|
566
|
+
return apiConfig.httpClient.get(`${base(store_id)}/profile-lists`, {
|
|
573
567
|
...options,
|
|
574
|
-
params
|
|
568
|
+
params: queryParams
|
|
575
569
|
});
|
|
576
570
|
},
|
|
577
571
|
subscribe(params, options) {
|
|
572
|
+
const { store_id, id, ...payload } = params;
|
|
578
573
|
return apiConfig.httpClient.post(
|
|
579
|
-
`${base()}/
|
|
580
|
-
|
|
581
|
-
customer_id: params.customer_id,
|
|
582
|
-
price_id: params.price_id,
|
|
583
|
-
success_url: params.success_url,
|
|
584
|
-
cancel_url: params.cancel_url,
|
|
585
|
-
confirm_url: params.confirm_url
|
|
586
|
-
},
|
|
574
|
+
`${base(store_id)}/profile-lists/${id}/subscribe`,
|
|
575
|
+
payload,
|
|
587
576
|
options
|
|
588
577
|
);
|
|
589
578
|
},
|
|
590
579
|
checkAccess(params, options) {
|
|
580
|
+
const store_id = params.store_id || apiConfig.storeId;
|
|
591
581
|
return apiConfig.httpClient.get(
|
|
592
|
-
`${base()}/
|
|
582
|
+
`${base(store_id)}/profile-lists/${params.id}/access`,
|
|
593
583
|
options
|
|
594
584
|
);
|
|
595
585
|
},
|
|
596
586
|
unsubscribe(token, options) {
|
|
597
|
-
return apiConfig.httpClient.get(`${base()}/
|
|
587
|
+
return apiConfig.httpClient.get(`${base()}/profile-lists/unsubscribe`, {
|
|
598
588
|
...options,
|
|
599
589
|
params: { token }
|
|
600
590
|
});
|
|
601
591
|
},
|
|
602
592
|
confirm(token, options) {
|
|
603
|
-
return apiConfig.httpClient.get(`${base()}/
|
|
593
|
+
return apiConfig.httpClient.get(`${base()}/profile-lists/confirm`, {
|
|
604
594
|
...options,
|
|
605
595
|
params: { token }
|
|
606
596
|
});
|
|
607
597
|
}
|
|
608
598
|
}
|
|
609
599
|
},
|
|
610
|
-
activity: createActivityApi(apiConfig)
|
|
611
|
-
automation: {
|
|
612
|
-
agent: {
|
|
613
|
-
getAgents(params, options) {
|
|
614
|
-
const store_id = params?.store_id || apiConfig.storeId;
|
|
615
|
-
const queryParams = { ...params || {} };
|
|
616
|
-
delete queryParams.store_id;
|
|
617
|
-
return apiConfig.httpClient.get(`${base(store_id)}/agents`, {
|
|
618
|
-
...options,
|
|
619
|
-
params: Object.keys(queryParams).length > 0 ? queryParams : void 0
|
|
620
|
-
});
|
|
621
|
-
},
|
|
622
|
-
getAgent(params, options) {
|
|
623
|
-
const store_id = params.store_id || apiConfig.storeId;
|
|
624
|
-
return apiConfig.httpClient.get(
|
|
625
|
-
`${base(store_id)}/agents/${params.id}`,
|
|
626
|
-
options
|
|
627
|
-
);
|
|
628
|
-
},
|
|
629
|
-
sendMessage(params, options) {
|
|
630
|
-
const store_id = params.store_id || apiConfig.storeId;
|
|
631
|
-
const body = { message: params.message };
|
|
632
|
-
if (params.chat_id) body.chat_id = params.chat_id;
|
|
633
|
-
return apiConfig.httpClient.post(
|
|
634
|
-
`${base(store_id)}/agents/${params.id}/chats/messages`,
|
|
635
|
-
body,
|
|
636
|
-
options
|
|
637
|
-
);
|
|
638
|
-
},
|
|
639
|
-
getChat(params, options) {
|
|
640
|
-
const store_id = params.store_id || apiConfig.storeId;
|
|
641
|
-
return apiConfig.httpClient.get(
|
|
642
|
-
`${base(store_id)}/agents/${params.id}/chats/${params.chat_id}`,
|
|
643
|
-
options
|
|
644
|
-
);
|
|
645
|
-
},
|
|
646
|
-
getChatMessages(params, options) {
|
|
647
|
-
const store_id = params.store_id || apiConfig.storeId;
|
|
648
|
-
const queryParams = {};
|
|
649
|
-
if (params.limit) queryParams.limit = String(params.limit);
|
|
650
|
-
return apiConfig.httpClient.get(
|
|
651
|
-
`${base(store_id)}/agents/${params.id}/chats/${params.chat_id}/messages`,
|
|
652
|
-
{
|
|
653
|
-
...options,
|
|
654
|
-
params: Object.keys(queryParams).length > 0 ? queryParams : void 0
|
|
655
|
-
}
|
|
656
|
-
);
|
|
657
|
-
},
|
|
658
|
-
rateChat(params, options) {
|
|
659
|
-
const store_id = params.store_id || apiConfig.storeId;
|
|
660
|
-
const body = { rating: params.rating };
|
|
661
|
-
if (params.comment) body.comment = params.comment;
|
|
662
|
-
return apiConfig.httpClient.post(
|
|
663
|
-
`${base(store_id)}/agents/${params.id}/chats/${params.chat_id}/rate`,
|
|
664
|
-
body,
|
|
665
|
-
options
|
|
666
|
-
);
|
|
667
|
-
}
|
|
668
|
-
}
|
|
669
|
-
}
|
|
600
|
+
activity: createActivityApi(apiConfig)
|
|
670
601
|
};
|
|
671
602
|
};
|
|
672
603
|
|
|
@@ -1001,6 +932,41 @@ function createHttpClient(cfg) {
|
|
|
1001
932
|
};
|
|
1002
933
|
}
|
|
1003
934
|
|
|
935
|
+
// src/api/support.ts
|
|
936
|
+
function supportConversationQuery(params) {
|
|
937
|
+
const qs = new URLSearchParams({ store_id: params.store_id });
|
|
938
|
+
if (params.message_limit) qs.set("message_limit", String(params.message_limit));
|
|
939
|
+
if (typeof params.after_created_at === "number") qs.set("after_created_at", String(params.after_created_at));
|
|
940
|
+
if (params.after_id) qs.set("after_id", params.after_id);
|
|
941
|
+
return qs.toString();
|
|
942
|
+
}
|
|
943
|
+
function createStorefrontSupportApi(config) {
|
|
944
|
+
const { httpClient, storeId } = config;
|
|
945
|
+
return {
|
|
946
|
+
async startConversation(params = {}, opts) {
|
|
947
|
+
return httpClient.post(
|
|
948
|
+
`/v1/storefront/${storeId}/support/conversations`,
|
|
949
|
+
{ store_id: storeId, ...params },
|
|
950
|
+
opts
|
|
951
|
+
);
|
|
952
|
+
},
|
|
953
|
+
async sendMessage(params, opts) {
|
|
954
|
+
return httpClient.post(
|
|
955
|
+
`/v1/storefront/${storeId}/support/conversations/${params.conversation_id}/messages`,
|
|
956
|
+
{ store_id: storeId, ...params },
|
|
957
|
+
opts
|
|
958
|
+
);
|
|
959
|
+
},
|
|
960
|
+
async getConversation(params, opts) {
|
|
961
|
+
const qs = supportConversationQuery({ store_id: storeId, ...params });
|
|
962
|
+
return httpClient.get(
|
|
963
|
+
`/v1/storefront/${storeId}/support/conversations/${params.conversation_id}?${qs}`,
|
|
964
|
+
opts
|
|
965
|
+
);
|
|
966
|
+
}
|
|
967
|
+
};
|
|
968
|
+
}
|
|
969
|
+
|
|
1004
970
|
// src/utils/price.ts
|
|
1005
971
|
function formatCurrency(amount, currencyCode, locale = "en") {
|
|
1006
972
|
if (!currencyCode) return "";
|
|
@@ -1290,22 +1256,22 @@ function createUtilitySurface(apiConfig) {
|
|
|
1290
1256
|
getFirstAvailableFCId
|
|
1291
1257
|
};
|
|
1292
1258
|
}
|
|
1293
|
-
var
|
|
1294
|
-
function
|
|
1259
|
+
var PROFILE_STORAGE_KEY = "arky_profile_session";
|
|
1260
|
+
function readProfileSession() {
|
|
1295
1261
|
if (typeof window === "undefined") return null;
|
|
1296
1262
|
try {
|
|
1297
|
-
const raw = localStorage.getItem(
|
|
1263
|
+
const raw = localStorage.getItem(PROFILE_STORAGE_KEY);
|
|
1298
1264
|
return raw ? JSON.parse(raw) : null;
|
|
1299
1265
|
} catch {
|
|
1300
1266
|
return null;
|
|
1301
1267
|
}
|
|
1302
1268
|
}
|
|
1303
|
-
function
|
|
1269
|
+
function writeProfileSession(s) {
|
|
1304
1270
|
if (typeof window === "undefined") return;
|
|
1305
1271
|
if (s) {
|
|
1306
|
-
localStorage.setItem(
|
|
1272
|
+
localStorage.setItem(PROFILE_STORAGE_KEY, JSON.stringify(s));
|
|
1307
1273
|
} else {
|
|
1308
|
-
localStorage.removeItem(
|
|
1274
|
+
localStorage.removeItem(PROFILE_STORAGE_KEY);
|
|
1309
1275
|
}
|
|
1310
1276
|
}
|
|
1311
1277
|
function createStorefront(config) {
|
|
@@ -1314,10 +1280,10 @@ function createStorefront(config) {
|
|
|
1314
1280
|
const listeners = /* @__PURE__ */ new Set();
|
|
1315
1281
|
let bareIdentifyPromise = null;
|
|
1316
1282
|
function toPublic(s) {
|
|
1317
|
-
return s ? {
|
|
1283
|
+
return s ? { profile: s.profile, store: s.store, market: s.market } : null;
|
|
1318
1284
|
}
|
|
1319
1285
|
function emit() {
|
|
1320
|
-
const pub = toPublic(
|
|
1286
|
+
const pub = toPublic(readProfileSession());
|
|
1321
1287
|
for (const l of listeners) {
|
|
1322
1288
|
Promise.resolve().then(() => l(pub)).catch(() => {
|
|
1323
1289
|
});
|
|
@@ -1325,9 +1291,9 @@ function createStorefront(config) {
|
|
|
1325
1291
|
}
|
|
1326
1292
|
const updateSession = (updater) => {
|
|
1327
1293
|
if (config.apiToken) return;
|
|
1328
|
-
const prev =
|
|
1294
|
+
const prev = readProfileSession();
|
|
1329
1295
|
const next = updater(prev);
|
|
1330
|
-
|
|
1296
|
+
writeProfileSession(next);
|
|
1331
1297
|
emit();
|
|
1332
1298
|
};
|
|
1333
1299
|
const authStorage = config.apiToken ? {
|
|
@@ -1338,7 +1304,7 @@ function createStorefront(config) {
|
|
|
1338
1304
|
}
|
|
1339
1305
|
} : {
|
|
1340
1306
|
getTokens() {
|
|
1341
|
-
const s =
|
|
1307
|
+
const s = readProfileSession();
|
|
1342
1308
|
return s ? { access_token: s.access_token } : null;
|
|
1343
1309
|
},
|
|
1344
1310
|
onTokensRefreshed() {
|
|
@@ -1365,20 +1331,20 @@ function createStorefront(config) {
|
|
|
1365
1331
|
authStorage
|
|
1366
1332
|
};
|
|
1367
1333
|
const storefrontApi = createStorefrontApi(apiConfig, updateSession);
|
|
1368
|
-
const
|
|
1334
|
+
const profileApi = storefrontApi.crm.profile;
|
|
1369
1335
|
function identify(params) {
|
|
1370
1336
|
if (params?.market !== void 0) apiConfig.market = params.market;
|
|
1371
1337
|
const isBareCall = !params?.email && !params?.verify;
|
|
1372
1338
|
if (isBareCall && bareIdentifyPromise) return bareIdentifyPromise;
|
|
1373
1339
|
const promise = (async () => {
|
|
1374
1340
|
try {
|
|
1375
|
-
const result = await
|
|
1341
|
+
const result = await profileApi.identify({
|
|
1376
1342
|
market: apiConfig.market,
|
|
1377
1343
|
email: params?.email,
|
|
1378
1344
|
verify: params?.verify
|
|
1379
1345
|
});
|
|
1380
1346
|
return {
|
|
1381
|
-
|
|
1347
|
+
profile: result.profile,
|
|
1382
1348
|
store: result.store,
|
|
1383
1349
|
market: result.market
|
|
1384
1350
|
};
|
|
@@ -1387,9 +1353,9 @@ function createStorefront(config) {
|
|
|
1387
1353
|
const status = e?.statusCode || e?.status || e?.response?.status;
|
|
1388
1354
|
if (isBareCall && status === 401) {
|
|
1389
1355
|
updateSession(() => null);
|
|
1390
|
-
const result = await
|
|
1356
|
+
const result = await profileApi.identify({ market: apiConfig.market });
|
|
1391
1357
|
return {
|
|
1392
|
-
|
|
1358
|
+
profile: result.profile,
|
|
1393
1359
|
store: result.store,
|
|
1394
1360
|
market: result.market
|
|
1395
1361
|
};
|
|
@@ -1404,7 +1370,7 @@ function createStorefront(config) {
|
|
|
1404
1370
|
return promise;
|
|
1405
1371
|
}
|
|
1406
1372
|
async function verify(params) {
|
|
1407
|
-
const result = await
|
|
1373
|
+
const result = await profileApi.verify(params);
|
|
1408
1374
|
bareIdentifyPromise = null;
|
|
1409
1375
|
return result;
|
|
1410
1376
|
}
|
|
@@ -1412,7 +1378,7 @@ function createStorefront(config) {
|
|
|
1412
1378
|
if (config.apiToken) return;
|
|
1413
1379
|
bareIdentifyPromise = null;
|
|
1414
1380
|
try {
|
|
1415
|
-
await
|
|
1381
|
+
await profileApi.logout();
|
|
1416
1382
|
} catch {
|
|
1417
1383
|
updateSession(() => null);
|
|
1418
1384
|
}
|
|
@@ -1421,19 +1387,19 @@ function createStorefront(config) {
|
|
|
1421
1387
|
identify,
|
|
1422
1388
|
verify,
|
|
1423
1389
|
logout,
|
|
1424
|
-
me: () =>
|
|
1390
|
+
me: () => profileApi.getMe(),
|
|
1425
1391
|
get session() {
|
|
1426
1392
|
if (config.apiToken) return null;
|
|
1427
|
-
return toPublic(
|
|
1393
|
+
return toPublic(readProfileSession());
|
|
1428
1394
|
},
|
|
1429
1395
|
get isAuthenticated() {
|
|
1430
1396
|
if (config.apiToken) return true;
|
|
1431
|
-
const s =
|
|
1397
|
+
const s = readProfileSession();
|
|
1432
1398
|
return s !== null && !!s.access_token;
|
|
1433
1399
|
},
|
|
1434
1400
|
onAuthStateChanged(listener) {
|
|
1435
1401
|
listeners.add(listener);
|
|
1436
|
-
const current = toPublic(
|
|
1402
|
+
const current = toPublic(readProfileSession());
|
|
1437
1403
|
if (current) {
|
|
1438
1404
|
Promise.resolve().then(() => listener(current)).catch(() => {
|
|
1439
1405
|
});
|
|
@@ -1448,7 +1414,7 @@ function createStorefront(config) {
|
|
|
1448
1414
|
eshop: storefrontApi.eshop,
|
|
1449
1415
|
crm: storefrontApi.crm,
|
|
1450
1416
|
activity: storefrontApi.activity,
|
|
1451
|
-
|
|
1417
|
+
support: createStorefrontSupportApi(apiConfig),
|
|
1452
1418
|
setStoreId: (storeId) => {
|
|
1453
1419
|
apiConfig.storeId = storeId;
|
|
1454
1420
|
bareIdentifyPromise = null;
|
|
@@ -1519,7 +1485,7 @@ function priceForMarket(prices, market, fallbackCurrency) {
|
|
|
1519
1485
|
market: price?.market || market,
|
|
1520
1486
|
currency: price?.currency || fallbackCurrency || "",
|
|
1521
1487
|
compare_at: price?.compare_at,
|
|
1522
|
-
|
|
1488
|
+
profile_list_id: price?.profile_list_id
|
|
1523
1489
|
};
|
|
1524
1490
|
}
|
|
1525
1491
|
function availableStock(client, variant) {
|
|
@@ -1714,18 +1680,46 @@ function createArkyStore(config) {
|
|
|
1714
1680
|
selected_shipping_method_id: null,
|
|
1715
1681
|
user_token: null
|
|
1716
1682
|
});
|
|
1683
|
+
function rawProductItemCount(value) {
|
|
1684
|
+
return (value?.items || []).reduce((total, item) => {
|
|
1685
|
+
if (item.type !== "product") return total;
|
|
1686
|
+
return total + (item.quantity || 0);
|
|
1687
|
+
}, 0);
|
|
1688
|
+
}
|
|
1689
|
+
function rawServiceItemCount(value) {
|
|
1690
|
+
return (value?.items || []).reduce((total, item) => {
|
|
1691
|
+
if (item.type !== "service") return total;
|
|
1692
|
+
return total + Math.max(1, item.slots?.length || 0);
|
|
1693
|
+
}, 0);
|
|
1694
|
+
}
|
|
1717
1695
|
const product_item_count = nanostores.computed(
|
|
1718
|
-
product_items,
|
|
1719
|
-
(items) =>
|
|
1696
|
+
[cart, product_items],
|
|
1697
|
+
(cartValue, items) => Math.max(
|
|
1698
|
+
rawProductItemCount(cartValue),
|
|
1699
|
+
items.reduce((total, item) => total + (item.quantity || 0), 0)
|
|
1700
|
+
)
|
|
1701
|
+
);
|
|
1702
|
+
const service_item_count = nanostores.computed(
|
|
1703
|
+
[cart, service_items],
|
|
1704
|
+
(cartValue, items) => Math.max(rawServiceItemCount(cartValue), items.length)
|
|
1705
|
+
);
|
|
1706
|
+
const item_count = nanostores.computed(
|
|
1707
|
+
[cart, product_item_count, service_item_count],
|
|
1708
|
+
(cartValue, products, services) => Math.max(cartValue?.item_count || 0, products + services)
|
|
1720
1709
|
);
|
|
1721
|
-
const service_item_count = nanostores.computed(service_items, (items) => items.length);
|
|
1722
|
-
const item_count = nanostores.computed([product_item_count, service_item_count], (products, services) => products + services);
|
|
1723
1710
|
const snapshot = nanostores.computed([cart, product_items, service_items, item_count], (cartValue, products, services, count) => ({
|
|
1724
1711
|
cart: cartValue,
|
|
1725
1712
|
product_items: products,
|
|
1726
1713
|
service_items: services,
|
|
1727
1714
|
item_count: count
|
|
1728
1715
|
}));
|
|
1716
|
+
let cartWriteRevision = 0;
|
|
1717
|
+
let sessionRequest = null;
|
|
1718
|
+
let cartRequest = null;
|
|
1719
|
+
function nextCartWriteRevision() {
|
|
1720
|
+
cartWriteRevision += 1;
|
|
1721
|
+
return cartWriteRevision;
|
|
1722
|
+
}
|
|
1729
1723
|
const cms_state = nanostores.map({
|
|
1730
1724
|
nodes: {},
|
|
1731
1725
|
forms: {},
|
|
@@ -1773,7 +1767,12 @@ function createArkyStore(config) {
|
|
|
1773
1767
|
const current = session.get();
|
|
1774
1768
|
const marketKey = currentMarketKey();
|
|
1775
1769
|
if (current && (!marketKey || current.market?.key === marketKey)) return current;
|
|
1776
|
-
|
|
1770
|
+
if (!sessionRequest) {
|
|
1771
|
+
sessionRequest = identify({ market: marketKey }).finally(() => {
|
|
1772
|
+
sessionRequest = null;
|
|
1773
|
+
});
|
|
1774
|
+
}
|
|
1775
|
+
return sessionRequest;
|
|
1777
1776
|
}
|
|
1778
1777
|
async function identify(params = {}) {
|
|
1779
1778
|
if (params.market) setMarket(params.market);
|
|
@@ -1799,17 +1798,23 @@ function createArkyStore(config) {
|
|
|
1799
1798
|
if (context.market) setMarket(context.market);
|
|
1800
1799
|
}
|
|
1801
1800
|
async function ensureCart() {
|
|
1801
|
+
if (cartRequest) return cartRequest;
|
|
1802
1802
|
cart_status.setKey("loading", true);
|
|
1803
1803
|
cart_status.setKey("error", null);
|
|
1804
|
-
|
|
1804
|
+
const refreshRevision = cartWriteRevision;
|
|
1805
|
+
cartRequest = (async () => {
|
|
1805
1806
|
await ensureSession();
|
|
1806
1807
|
const response = await client.cart.refresh({ market: currentMarketKey() });
|
|
1807
|
-
await hydrateCart(response);
|
|
1808
|
+
await hydrateCart(response, { ifRevision: refreshRevision });
|
|
1808
1809
|
return response;
|
|
1810
|
+
})();
|
|
1811
|
+
try {
|
|
1812
|
+
return await cartRequest;
|
|
1809
1813
|
} catch (error) {
|
|
1810
1814
|
cart_status.setKey("error", readErrorMessage(error, "Failed to load cart."));
|
|
1811
1815
|
throw error;
|
|
1812
1816
|
} finally {
|
|
1817
|
+
cartRequest = null;
|
|
1813
1818
|
cart_status.setKey("loading", false);
|
|
1814
1819
|
}
|
|
1815
1820
|
}
|
|
@@ -1862,7 +1867,10 @@ function createArkyStore(config) {
|
|
|
1862
1867
|
}
|
|
1863
1868
|
return rows;
|
|
1864
1869
|
}
|
|
1865
|
-
async function hydrateCart(response) {
|
|
1870
|
+
async function hydrateCart(response, options = {}) {
|
|
1871
|
+
if (options.ifRevision !== void 0 && options.ifRevision !== cartWriteRevision) {
|
|
1872
|
+
return cart.get() || response;
|
|
1873
|
+
}
|
|
1866
1874
|
cart.set(response);
|
|
1867
1875
|
cart_status.setKey("user_token", response.token || null);
|
|
1868
1876
|
cart_status.setKey("selected_shipping_method_id", response.shipping_method_id || null);
|
|
@@ -1885,7 +1893,7 @@ function createArkyStore(config) {
|
|
|
1885
1893
|
...toServiceCheckoutItems(input.service_items || service_items.get())
|
|
1886
1894
|
];
|
|
1887
1895
|
}
|
|
1888
|
-
async function syncCart(input = {}) {
|
|
1896
|
+
async function syncCart(input = {}, writeRevision = nextCartWriteRevision()) {
|
|
1889
1897
|
cart_status.setKey("syncing", true);
|
|
1890
1898
|
cart_status.setKey("error", null);
|
|
1891
1899
|
try {
|
|
@@ -1905,7 +1913,7 @@ function createArkyStore(config) {
|
|
|
1905
1913
|
if (input.shipping_method_id !== void 0) {
|
|
1906
1914
|
cart_status.setKey("selected_shipping_method_id", input.shipping_method_id);
|
|
1907
1915
|
}
|
|
1908
|
-
await hydrateCart(response);
|
|
1916
|
+
await hydrateCart(response, { ifRevision: writeRevision });
|
|
1909
1917
|
return response;
|
|
1910
1918
|
} catch (error) {
|
|
1911
1919
|
cart_status.setKey("error", readErrorMessage(error, "Failed to sync cart."));
|
|
@@ -1916,6 +1924,7 @@ function createArkyStore(config) {
|
|
|
1916
1924
|
}
|
|
1917
1925
|
async function addProduct(product, variant, quantity = 1) {
|
|
1918
1926
|
cart_status.setKey("error", null);
|
|
1927
|
+
const writeRevision = nextCartWriteRevision();
|
|
1919
1928
|
try {
|
|
1920
1929
|
const current = cart.get() || await ensureCart();
|
|
1921
1930
|
const response = await client.cart.addItem({
|
|
@@ -1927,7 +1936,7 @@ function createArkyStore(config) {
|
|
|
1927
1936
|
quantity
|
|
1928
1937
|
}
|
|
1929
1938
|
});
|
|
1930
|
-
await hydrateCart(response);
|
|
1939
|
+
await hydrateCart(response, { ifRevision: writeRevision });
|
|
1931
1940
|
await client.activity.track({ type: "cart_added", payload: { product_id: product.id, variant_id: variant.id, quantity } });
|
|
1932
1941
|
return response;
|
|
1933
1942
|
} catch (error) {
|
|
@@ -1936,15 +1945,17 @@ function createArkyStore(config) {
|
|
|
1936
1945
|
}
|
|
1937
1946
|
}
|
|
1938
1947
|
async function setProductQuantity(itemId, quantity) {
|
|
1948
|
+
const writeRevision = nextCartWriteRevision();
|
|
1939
1949
|
const next = product_items.get().map((item) => {
|
|
1940
1950
|
if (item.id !== itemId) return item;
|
|
1941
1951
|
const bounded = item.max_stock ? Math.min(Math.max(1, quantity), item.max_stock) : Math.max(1, quantity);
|
|
1942
1952
|
return { ...item, quantity: bounded };
|
|
1943
1953
|
});
|
|
1944
1954
|
product_items.set(next);
|
|
1945
|
-
return syncCart({ product_items: next });
|
|
1955
|
+
return syncCart({ product_items: next }, writeRevision);
|
|
1946
1956
|
}
|
|
1947
1957
|
async function removeProduct(itemId) {
|
|
1958
|
+
const writeRevision = nextCartWriteRevision();
|
|
1948
1959
|
const item = product_items.get().find((candidate) => candidate.id === itemId);
|
|
1949
1960
|
product_items.set(product_items.get().filter((candidate) => candidate.id !== itemId));
|
|
1950
1961
|
const current = cart.get();
|
|
@@ -1955,21 +1966,24 @@ function createArkyStore(config) {
|
|
|
1955
1966
|
product_id: item.product_id,
|
|
1956
1967
|
variant_id: item.variant_id
|
|
1957
1968
|
});
|
|
1958
|
-
await hydrateCart(response);
|
|
1969
|
+
await hydrateCart(response, { ifRevision: writeRevision });
|
|
1959
1970
|
await client.activity.track({ type: "cart_removed", payload: { product_id: item.product_id, variant_id: item.variant_id } });
|
|
1960
1971
|
return response;
|
|
1961
1972
|
}
|
|
1962
1973
|
async function addServiceItem(item) {
|
|
1974
|
+
const writeRevision = nextCartWriteRevision();
|
|
1963
1975
|
const next = [...service_items.get(), item];
|
|
1964
1976
|
service_items.set(next);
|
|
1965
|
-
return syncCart({ service_items: next });
|
|
1977
|
+
return syncCart({ service_items: next }, writeRevision);
|
|
1966
1978
|
}
|
|
1967
1979
|
async function removeServiceItem(itemId) {
|
|
1980
|
+
const writeRevision = nextCartWriteRevision();
|
|
1968
1981
|
const next = service_items.get().filter((item) => item.id !== itemId);
|
|
1969
1982
|
service_items.set(next);
|
|
1970
|
-
return syncCart({ service_items: next });
|
|
1983
|
+
return syncCart({ service_items: next }, writeRevision);
|
|
1971
1984
|
}
|
|
1972
1985
|
async function clearCart() {
|
|
1986
|
+
const writeRevision = nextCartWriteRevision();
|
|
1973
1987
|
product_items.set([]);
|
|
1974
1988
|
service_items.set([]);
|
|
1975
1989
|
quote.set(null);
|
|
@@ -1978,7 +1992,7 @@ function createArkyStore(config) {
|
|
|
1978
1992
|
const current = cart.get();
|
|
1979
1993
|
if (!current) return null;
|
|
1980
1994
|
const response = await client.cart.clear({ id: current.id });
|
|
1981
|
-
await hydrateCart(response);
|
|
1995
|
+
await hydrateCart(response, { ifRevision: writeRevision });
|
|
1982
1996
|
return response;
|
|
1983
1997
|
}
|
|
1984
1998
|
async function fetchQuote(input = {}) {
|
|
@@ -2746,6 +2760,7 @@ function createArkyStore(config) {
|
|
|
2746
2760
|
setMarket,
|
|
2747
2761
|
setLocale,
|
|
2748
2762
|
setContext,
|
|
2763
|
+
getStoreId: client.getStoreId,
|
|
2749
2764
|
getMarket: currentMarketKey,
|
|
2750
2765
|
getLocale: currentLocale,
|
|
2751
2766
|
cms: {
|
|
@@ -2783,8 +2798,8 @@ function createArkyStore(config) {
|
|
|
2783
2798
|
},
|
|
2784
2799
|
state: nanostores.atom(null)
|
|
2785
2800
|
},
|
|
2801
|
+
support: client.support,
|
|
2786
2802
|
store: client.store,
|
|
2787
|
-
automation: client.automation,
|
|
2788
2803
|
utils: client.utils
|
|
2789
2804
|
};
|
|
2790
2805
|
}
|