arky-sdk 0.8.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-CEVBErl_.d.ts → admin-DjYydKeB.d.ts} +510 -176
- package/dist/{admin-DCe3PEOB.d.cts → admin-DlL8mCxL.d.cts} +510 -176
- package/dist/admin.cjs +596 -216
- 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 +596 -216
- 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 +678 -335
- 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 +678 -335
- package/dist/index.js.map +1 -1
- package/dist/storefront-store.cjs +158 -139
- 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 +158 -139
- package/dist/storefront-store.js.map +1 -1
- package/dist/storefront.cjs +158 -139
- 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 +158 -139
- package/dist/storefront.js.map +1 -1
- package/dist/types.cjs.map +1 -1
- package/dist/types.d.cts +1 -2655
- package/dist/types.d.ts +1 -2655
- 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
package/dist/storefront.cjs
CHANGED
|
@@ -140,6 +140,12 @@ function normalizeOrderCheckoutItems(items) {
|
|
|
140
140
|
return { type: "service", ...item };
|
|
141
141
|
});
|
|
142
142
|
}
|
|
143
|
+
function normalizePublicCheckoutItems(items) {
|
|
144
|
+
return normalizeOrderCheckoutItems(items).map((item) => {
|
|
145
|
+
const { price: _price, ...publicItem } = item;
|
|
146
|
+
return publicItem;
|
|
147
|
+
});
|
|
148
|
+
}
|
|
143
149
|
|
|
144
150
|
// src/api/storefront.ts
|
|
145
151
|
var COMMON_ACTIVITY_TYPES = [
|
|
@@ -171,7 +177,7 @@ var createActivityApi = (apiConfig) => ({
|
|
|
171
177
|
}
|
|
172
178
|
}
|
|
173
179
|
});
|
|
174
|
-
var createStorefrontApi = (apiConfig,
|
|
180
|
+
var createStorefrontApi = (apiConfig, updateProfileSession) => {
|
|
175
181
|
const base = (storeId = apiConfig.storeId) => `/v1/storefront/${storeId}`;
|
|
176
182
|
return {
|
|
177
183
|
store: {
|
|
@@ -355,7 +361,7 @@ var createStorefrontApi = (apiConfig, updateCustomerSession) => {
|
|
|
355
361
|
{
|
|
356
362
|
...payload,
|
|
357
363
|
store_id: target,
|
|
358
|
-
...items ? { items:
|
|
364
|
+
...items ? { items: normalizePublicCheckoutItems(items) } : {}
|
|
359
365
|
},
|
|
360
366
|
options
|
|
361
367
|
);
|
|
@@ -368,7 +374,7 @@ var createStorefrontApi = (apiConfig, updateCustomerSession) => {
|
|
|
368
374
|
{
|
|
369
375
|
...payload,
|
|
370
376
|
store_id: target,
|
|
371
|
-
item:
|
|
377
|
+
item: normalizePublicCheckoutItems([item])[0]
|
|
372
378
|
},
|
|
373
379
|
options
|
|
374
380
|
);
|
|
@@ -492,11 +498,11 @@ var createStorefrontApi = (apiConfig, updateCustomerSession) => {
|
|
|
492
498
|
}
|
|
493
499
|
},
|
|
494
500
|
crm: {
|
|
495
|
-
|
|
501
|
+
profile: {
|
|
496
502
|
async identify(params, options) {
|
|
497
503
|
const store_id = apiConfig.storeId;
|
|
498
504
|
const result = await apiConfig.httpClient.post(
|
|
499
|
-
`${base(store_id)}/
|
|
505
|
+
`${base(store_id)}/profiles/identify`,
|
|
500
506
|
{
|
|
501
507
|
store_id,
|
|
502
508
|
market: params?.market || apiConfig.market || null,
|
|
@@ -506,9 +512,9 @@ var createStorefrontApi = (apiConfig, updateCustomerSession) => {
|
|
|
506
512
|
options
|
|
507
513
|
);
|
|
508
514
|
if (result?.token?.token) {
|
|
509
|
-
|
|
515
|
+
updateProfileSession(() => ({
|
|
510
516
|
access_token: result.token.token,
|
|
511
|
-
|
|
517
|
+
profile: result.profile,
|
|
512
518
|
store: result.store,
|
|
513
519
|
market: result.market
|
|
514
520
|
}));
|
|
@@ -518,12 +524,12 @@ var createStorefrontApi = (apiConfig, updateCustomerSession) => {
|
|
|
518
524
|
async verify(params, options) {
|
|
519
525
|
const store_id = apiConfig.storeId;
|
|
520
526
|
const result = await apiConfig.httpClient.post(
|
|
521
|
-
`${base(store_id)}/
|
|
527
|
+
`${base(store_id)}/profiles/verify`,
|
|
522
528
|
{ store_id, code: params.code },
|
|
523
529
|
options
|
|
524
530
|
);
|
|
525
531
|
if (result?.token) {
|
|
526
|
-
|
|
532
|
+
updateProfileSession(
|
|
527
533
|
(prev) => prev ? { ...prev, access_token: result.token } : null
|
|
528
534
|
);
|
|
529
535
|
}
|
|
@@ -533,132 +539,63 @@ var createStorefrontApi = (apiConfig, updateCustomerSession) => {
|
|
|
533
539
|
const store_id = apiConfig.storeId;
|
|
534
540
|
try {
|
|
535
541
|
await apiConfig.httpClient.post(
|
|
536
|
-
`${base(store_id)}/
|
|
542
|
+
`${base(store_id)}/profiles/logout`,
|
|
537
543
|
{},
|
|
538
544
|
options
|
|
539
545
|
);
|
|
540
546
|
} finally {
|
|
541
|
-
|
|
547
|
+
updateProfileSession(() => null);
|
|
542
548
|
}
|
|
543
549
|
},
|
|
544
550
|
getMe(options) {
|
|
545
|
-
return apiConfig.httpClient.get(`${base()}/
|
|
551
|
+
return apiConfig.httpClient.get(`${base()}/profiles/me`, options);
|
|
546
552
|
}
|
|
547
553
|
},
|
|
548
|
-
|
|
554
|
+
profileList: {
|
|
549
555
|
get(params, options) {
|
|
550
|
-
|
|
551
|
-
if (params.id) {
|
|
552
|
-
identifier = params.id;
|
|
553
|
-
} else if (params.key) {
|
|
554
|
-
identifier = `${apiConfig.storeId}:${params.key}`;
|
|
555
|
-
} else {
|
|
556
|
-
throw new Error("GetAudienceParams requires id or key");
|
|
557
|
-
}
|
|
556
|
+
const store_id = params.store_id || apiConfig.storeId;
|
|
558
557
|
return apiConfig.httpClient.get(
|
|
559
|
-
`${base(
|
|
558
|
+
`${base(store_id)}/profile-lists/${params.id}`,
|
|
560
559
|
options
|
|
561
560
|
);
|
|
562
561
|
},
|
|
563
562
|
find(params, options) {
|
|
564
|
-
|
|
563
|
+
const { store_id, ...queryParams } = params || {};
|
|
564
|
+
return apiConfig.httpClient.get(`${base(store_id)}/profile-lists`, {
|
|
565
565
|
...options,
|
|
566
|
-
params
|
|
566
|
+
params: queryParams
|
|
567
567
|
});
|
|
568
568
|
},
|
|
569
569
|
subscribe(params, options) {
|
|
570
|
+
const { store_id, id, ...payload } = params;
|
|
570
571
|
return apiConfig.httpClient.post(
|
|
571
|
-
`${base()}/
|
|
572
|
-
|
|
573
|
-
customer_id: params.customer_id,
|
|
574
|
-
price_id: params.price_id,
|
|
575
|
-
success_url: params.success_url,
|
|
576
|
-
cancel_url: params.cancel_url,
|
|
577
|
-
confirm_url: params.confirm_url
|
|
578
|
-
},
|
|
572
|
+
`${base(store_id)}/profile-lists/${id}/subscribe`,
|
|
573
|
+
payload,
|
|
579
574
|
options
|
|
580
575
|
);
|
|
581
576
|
},
|
|
582
577
|
checkAccess(params, options) {
|
|
578
|
+
const store_id = params.store_id || apiConfig.storeId;
|
|
583
579
|
return apiConfig.httpClient.get(
|
|
584
|
-
`${base()}/
|
|
580
|
+
`${base(store_id)}/profile-lists/${params.id}/access`,
|
|
585
581
|
options
|
|
586
582
|
);
|
|
587
583
|
},
|
|
588
584
|
unsubscribe(token, options) {
|
|
589
|
-
return apiConfig.httpClient.get(`${base()}/
|
|
585
|
+
return apiConfig.httpClient.get(`${base()}/profile-lists/unsubscribe`, {
|
|
590
586
|
...options,
|
|
591
587
|
params: { token }
|
|
592
588
|
});
|
|
593
589
|
},
|
|
594
590
|
confirm(token, options) {
|
|
595
|
-
return apiConfig.httpClient.get(`${base()}/
|
|
591
|
+
return apiConfig.httpClient.get(`${base()}/profile-lists/confirm`, {
|
|
596
592
|
...options,
|
|
597
593
|
params: { token }
|
|
598
594
|
});
|
|
599
595
|
}
|
|
600
596
|
}
|
|
601
597
|
},
|
|
602
|
-
activity: createActivityApi(apiConfig)
|
|
603
|
-
automation: {
|
|
604
|
-
agent: {
|
|
605
|
-
getAgents(params, options) {
|
|
606
|
-
const store_id = params?.store_id || apiConfig.storeId;
|
|
607
|
-
const queryParams = { ...params || {} };
|
|
608
|
-
delete queryParams.store_id;
|
|
609
|
-
return apiConfig.httpClient.get(`${base(store_id)}/agents`, {
|
|
610
|
-
...options,
|
|
611
|
-
params: Object.keys(queryParams).length > 0 ? queryParams : void 0
|
|
612
|
-
});
|
|
613
|
-
},
|
|
614
|
-
getAgent(params, options) {
|
|
615
|
-
const store_id = params.store_id || apiConfig.storeId;
|
|
616
|
-
return apiConfig.httpClient.get(
|
|
617
|
-
`${base(store_id)}/agents/${params.id}`,
|
|
618
|
-
options
|
|
619
|
-
);
|
|
620
|
-
},
|
|
621
|
-
sendMessage(params, options) {
|
|
622
|
-
const store_id = params.store_id || apiConfig.storeId;
|
|
623
|
-
const body = { message: params.message };
|
|
624
|
-
if (params.chat_id) body.chat_id = params.chat_id;
|
|
625
|
-
return apiConfig.httpClient.post(
|
|
626
|
-
`${base(store_id)}/agents/${params.id}/chats/messages`,
|
|
627
|
-
body,
|
|
628
|
-
options
|
|
629
|
-
);
|
|
630
|
-
},
|
|
631
|
-
getChat(params, options) {
|
|
632
|
-
const store_id = params.store_id || apiConfig.storeId;
|
|
633
|
-
return apiConfig.httpClient.get(
|
|
634
|
-
`${base(store_id)}/agents/${params.id}/chats/${params.chat_id}`,
|
|
635
|
-
options
|
|
636
|
-
);
|
|
637
|
-
},
|
|
638
|
-
getChatMessages(params, options) {
|
|
639
|
-
const store_id = params.store_id || apiConfig.storeId;
|
|
640
|
-
const queryParams = {};
|
|
641
|
-
if (params.limit) queryParams.limit = String(params.limit);
|
|
642
|
-
return apiConfig.httpClient.get(
|
|
643
|
-
`${base(store_id)}/agents/${params.id}/chats/${params.chat_id}/messages`,
|
|
644
|
-
{
|
|
645
|
-
...options,
|
|
646
|
-
params: Object.keys(queryParams).length > 0 ? queryParams : void 0
|
|
647
|
-
}
|
|
648
|
-
);
|
|
649
|
-
},
|
|
650
|
-
rateChat(params, options) {
|
|
651
|
-
const store_id = params.store_id || apiConfig.storeId;
|
|
652
|
-
const body = { rating: params.rating };
|
|
653
|
-
if (params.comment) body.comment = params.comment;
|
|
654
|
-
return apiConfig.httpClient.post(
|
|
655
|
-
`${base(store_id)}/agents/${params.id}/chats/${params.chat_id}/rate`,
|
|
656
|
-
body,
|
|
657
|
-
options
|
|
658
|
-
);
|
|
659
|
-
}
|
|
660
|
-
}
|
|
661
|
-
}
|
|
598
|
+
activity: createActivityApi(apiConfig)
|
|
662
599
|
};
|
|
663
600
|
};
|
|
664
601
|
|
|
@@ -993,6 +930,41 @@ function createHttpClient(cfg) {
|
|
|
993
930
|
};
|
|
994
931
|
}
|
|
995
932
|
|
|
933
|
+
// src/api/support.ts
|
|
934
|
+
function supportConversationQuery(params) {
|
|
935
|
+
const qs = new URLSearchParams({ store_id: params.store_id });
|
|
936
|
+
if (params.message_limit) qs.set("message_limit", String(params.message_limit));
|
|
937
|
+
if (typeof params.after_created_at === "number") qs.set("after_created_at", String(params.after_created_at));
|
|
938
|
+
if (params.after_id) qs.set("after_id", params.after_id);
|
|
939
|
+
return qs.toString();
|
|
940
|
+
}
|
|
941
|
+
function createStorefrontSupportApi(config) {
|
|
942
|
+
const { httpClient, storeId } = config;
|
|
943
|
+
return {
|
|
944
|
+
async startConversation(params = {}, opts) {
|
|
945
|
+
return httpClient.post(
|
|
946
|
+
`/v1/storefront/${storeId}/support/conversations`,
|
|
947
|
+
{ store_id: storeId, ...params },
|
|
948
|
+
opts
|
|
949
|
+
);
|
|
950
|
+
},
|
|
951
|
+
async sendMessage(params, opts) {
|
|
952
|
+
return httpClient.post(
|
|
953
|
+
`/v1/storefront/${storeId}/support/conversations/${params.conversation_id}/messages`,
|
|
954
|
+
{ store_id: storeId, ...params },
|
|
955
|
+
opts
|
|
956
|
+
);
|
|
957
|
+
},
|
|
958
|
+
async getConversation(params, opts) {
|
|
959
|
+
const qs = supportConversationQuery({ store_id: storeId, ...params });
|
|
960
|
+
return httpClient.get(
|
|
961
|
+
`/v1/storefront/${storeId}/support/conversations/${params.conversation_id}?${qs}`,
|
|
962
|
+
opts
|
|
963
|
+
);
|
|
964
|
+
}
|
|
965
|
+
};
|
|
966
|
+
}
|
|
967
|
+
|
|
996
968
|
// src/utils/price.ts
|
|
997
969
|
function formatCurrency(amount, currencyCode, locale = "en") {
|
|
998
970
|
if (!currencyCode) return "";
|
|
@@ -1282,22 +1254,22 @@ function createUtilitySurface(apiConfig) {
|
|
|
1282
1254
|
getFirstAvailableFCId
|
|
1283
1255
|
};
|
|
1284
1256
|
}
|
|
1285
|
-
var
|
|
1286
|
-
function
|
|
1257
|
+
var PROFILE_STORAGE_KEY = "arky_profile_session";
|
|
1258
|
+
function readProfileSession() {
|
|
1287
1259
|
if (typeof window === "undefined") return null;
|
|
1288
1260
|
try {
|
|
1289
|
-
const raw = localStorage.getItem(
|
|
1261
|
+
const raw = localStorage.getItem(PROFILE_STORAGE_KEY);
|
|
1290
1262
|
return raw ? JSON.parse(raw) : null;
|
|
1291
1263
|
} catch {
|
|
1292
1264
|
return null;
|
|
1293
1265
|
}
|
|
1294
1266
|
}
|
|
1295
|
-
function
|
|
1267
|
+
function writeProfileSession(s) {
|
|
1296
1268
|
if (typeof window === "undefined") return;
|
|
1297
1269
|
if (s) {
|
|
1298
|
-
localStorage.setItem(
|
|
1270
|
+
localStorage.setItem(PROFILE_STORAGE_KEY, JSON.stringify(s));
|
|
1299
1271
|
} else {
|
|
1300
|
-
localStorage.removeItem(
|
|
1272
|
+
localStorage.removeItem(PROFILE_STORAGE_KEY);
|
|
1301
1273
|
}
|
|
1302
1274
|
}
|
|
1303
1275
|
function createStorefront(config) {
|
|
@@ -1306,10 +1278,10 @@ function createStorefront(config) {
|
|
|
1306
1278
|
const listeners = /* @__PURE__ */ new Set();
|
|
1307
1279
|
let bareIdentifyPromise = null;
|
|
1308
1280
|
function toPublic(s) {
|
|
1309
|
-
return s ? {
|
|
1281
|
+
return s ? { profile: s.profile, store: s.store, market: s.market } : null;
|
|
1310
1282
|
}
|
|
1311
1283
|
function emit() {
|
|
1312
|
-
const pub = toPublic(
|
|
1284
|
+
const pub = toPublic(readProfileSession());
|
|
1313
1285
|
for (const l of listeners) {
|
|
1314
1286
|
Promise.resolve().then(() => l(pub)).catch(() => {
|
|
1315
1287
|
});
|
|
@@ -1317,9 +1289,9 @@ function createStorefront(config) {
|
|
|
1317
1289
|
}
|
|
1318
1290
|
const updateSession = (updater) => {
|
|
1319
1291
|
if (config.apiToken) return;
|
|
1320
|
-
const prev =
|
|
1292
|
+
const prev = readProfileSession();
|
|
1321
1293
|
const next = updater(prev);
|
|
1322
|
-
|
|
1294
|
+
writeProfileSession(next);
|
|
1323
1295
|
emit();
|
|
1324
1296
|
};
|
|
1325
1297
|
const authStorage = config.apiToken ? {
|
|
@@ -1330,7 +1302,7 @@ function createStorefront(config) {
|
|
|
1330
1302
|
}
|
|
1331
1303
|
} : {
|
|
1332
1304
|
getTokens() {
|
|
1333
|
-
const s =
|
|
1305
|
+
const s = readProfileSession();
|
|
1334
1306
|
return s ? { access_token: s.access_token } : null;
|
|
1335
1307
|
},
|
|
1336
1308
|
onTokensRefreshed() {
|
|
@@ -1357,20 +1329,20 @@ function createStorefront(config) {
|
|
|
1357
1329
|
authStorage
|
|
1358
1330
|
};
|
|
1359
1331
|
const storefrontApi = createStorefrontApi(apiConfig, updateSession);
|
|
1360
|
-
const
|
|
1332
|
+
const profileApi = storefrontApi.crm.profile;
|
|
1361
1333
|
function identify(params) {
|
|
1362
1334
|
if (params?.market !== void 0) apiConfig.market = params.market;
|
|
1363
1335
|
const isBareCall = !params?.email && !params?.verify;
|
|
1364
1336
|
if (isBareCall && bareIdentifyPromise) return bareIdentifyPromise;
|
|
1365
1337
|
const promise = (async () => {
|
|
1366
1338
|
try {
|
|
1367
|
-
const result = await
|
|
1339
|
+
const result = await profileApi.identify({
|
|
1368
1340
|
market: apiConfig.market,
|
|
1369
1341
|
email: params?.email,
|
|
1370
1342
|
verify: params?.verify
|
|
1371
1343
|
});
|
|
1372
1344
|
return {
|
|
1373
|
-
|
|
1345
|
+
profile: result.profile,
|
|
1374
1346
|
store: result.store,
|
|
1375
1347
|
market: result.market
|
|
1376
1348
|
};
|
|
@@ -1379,9 +1351,9 @@ function createStorefront(config) {
|
|
|
1379
1351
|
const status = e?.statusCode || e?.status || e?.response?.status;
|
|
1380
1352
|
if (isBareCall && status === 401) {
|
|
1381
1353
|
updateSession(() => null);
|
|
1382
|
-
const result = await
|
|
1354
|
+
const result = await profileApi.identify({ market: apiConfig.market });
|
|
1383
1355
|
return {
|
|
1384
|
-
|
|
1356
|
+
profile: result.profile,
|
|
1385
1357
|
store: result.store,
|
|
1386
1358
|
market: result.market
|
|
1387
1359
|
};
|
|
@@ -1396,7 +1368,7 @@ function createStorefront(config) {
|
|
|
1396
1368
|
return promise;
|
|
1397
1369
|
}
|
|
1398
1370
|
async function verify(params) {
|
|
1399
|
-
const result = await
|
|
1371
|
+
const result = await profileApi.verify(params);
|
|
1400
1372
|
bareIdentifyPromise = null;
|
|
1401
1373
|
return result;
|
|
1402
1374
|
}
|
|
@@ -1404,7 +1376,7 @@ function createStorefront(config) {
|
|
|
1404
1376
|
if (config.apiToken) return;
|
|
1405
1377
|
bareIdentifyPromise = null;
|
|
1406
1378
|
try {
|
|
1407
|
-
await
|
|
1379
|
+
await profileApi.logout();
|
|
1408
1380
|
} catch {
|
|
1409
1381
|
updateSession(() => null);
|
|
1410
1382
|
}
|
|
@@ -1413,19 +1385,19 @@ function createStorefront(config) {
|
|
|
1413
1385
|
identify,
|
|
1414
1386
|
verify,
|
|
1415
1387
|
logout,
|
|
1416
|
-
me: () =>
|
|
1388
|
+
me: () => profileApi.getMe(),
|
|
1417
1389
|
get session() {
|
|
1418
1390
|
if (config.apiToken) return null;
|
|
1419
|
-
return toPublic(
|
|
1391
|
+
return toPublic(readProfileSession());
|
|
1420
1392
|
},
|
|
1421
1393
|
get isAuthenticated() {
|
|
1422
1394
|
if (config.apiToken) return true;
|
|
1423
|
-
const s =
|
|
1395
|
+
const s = readProfileSession();
|
|
1424
1396
|
return s !== null && !!s.access_token;
|
|
1425
1397
|
},
|
|
1426
1398
|
onAuthStateChanged(listener) {
|
|
1427
1399
|
listeners.add(listener);
|
|
1428
|
-
const current = toPublic(
|
|
1400
|
+
const current = toPublic(readProfileSession());
|
|
1429
1401
|
if (current) {
|
|
1430
1402
|
Promise.resolve().then(() => listener(current)).catch(() => {
|
|
1431
1403
|
});
|
|
@@ -1440,7 +1412,7 @@ function createStorefront(config) {
|
|
|
1440
1412
|
eshop: storefrontApi.eshop,
|
|
1441
1413
|
crm: storefrontApi.crm,
|
|
1442
1414
|
activity: storefrontApi.activity,
|
|
1443
|
-
|
|
1415
|
+
support: createStorefrontSupportApi(apiConfig),
|
|
1444
1416
|
setStoreId: (storeId) => {
|
|
1445
1417
|
apiConfig.storeId = storeId;
|
|
1446
1418
|
bareIdentifyPromise = null;
|
|
@@ -1511,7 +1483,7 @@ function priceForMarket(prices, market, fallbackCurrency) {
|
|
|
1511
1483
|
market: price?.market || market,
|
|
1512
1484
|
currency: price?.currency || fallbackCurrency || "",
|
|
1513
1485
|
compare_at: price?.compare_at,
|
|
1514
|
-
|
|
1486
|
+
profile_list_id: price?.profile_list_id
|
|
1515
1487
|
};
|
|
1516
1488
|
}
|
|
1517
1489
|
function availableStock(client, variant) {
|
|
@@ -1559,8 +1531,7 @@ function toServiceCheckoutItems(items) {
|
|
|
1559
1531
|
service_id: item.service_id,
|
|
1560
1532
|
provider_id: item.provider_id,
|
|
1561
1533
|
slots: [slot],
|
|
1562
|
-
forms: item.forms || []
|
|
1563
|
-
price: item.price
|
|
1534
|
+
forms: item.forms || []
|
|
1564
1535
|
});
|
|
1565
1536
|
}
|
|
1566
1537
|
return [...groups.values()].map((item) => ({
|
|
@@ -1707,18 +1678,46 @@ function createArkyStore(config) {
|
|
|
1707
1678
|
selected_shipping_method_id: null,
|
|
1708
1679
|
user_token: null
|
|
1709
1680
|
});
|
|
1681
|
+
function rawProductItemCount(value) {
|
|
1682
|
+
return (value?.items || []).reduce((total, item) => {
|
|
1683
|
+
if (item.type !== "product") return total;
|
|
1684
|
+
return total + (item.quantity || 0);
|
|
1685
|
+
}, 0);
|
|
1686
|
+
}
|
|
1687
|
+
function rawServiceItemCount(value) {
|
|
1688
|
+
return (value?.items || []).reduce((total, item) => {
|
|
1689
|
+
if (item.type !== "service") return total;
|
|
1690
|
+
return total + Math.max(1, item.slots?.length || 0);
|
|
1691
|
+
}, 0);
|
|
1692
|
+
}
|
|
1710
1693
|
const product_item_count = nanostores.computed(
|
|
1711
|
-
product_items,
|
|
1712
|
-
(items) =>
|
|
1694
|
+
[cart, product_items],
|
|
1695
|
+
(cartValue, items) => Math.max(
|
|
1696
|
+
rawProductItemCount(cartValue),
|
|
1697
|
+
items.reduce((total, item) => total + (item.quantity || 0), 0)
|
|
1698
|
+
)
|
|
1699
|
+
);
|
|
1700
|
+
const service_item_count = nanostores.computed(
|
|
1701
|
+
[cart, service_items],
|
|
1702
|
+
(cartValue, items) => Math.max(rawServiceItemCount(cartValue), items.length)
|
|
1703
|
+
);
|
|
1704
|
+
const item_count = nanostores.computed(
|
|
1705
|
+
[cart, product_item_count, service_item_count],
|
|
1706
|
+
(cartValue, products, services) => Math.max(cartValue?.item_count || 0, products + services)
|
|
1713
1707
|
);
|
|
1714
|
-
const service_item_count = nanostores.computed(service_items, (items) => items.length);
|
|
1715
|
-
const item_count = nanostores.computed([product_item_count, service_item_count], (products, services) => products + services);
|
|
1716
1708
|
const snapshot = nanostores.computed([cart, product_items, service_items, item_count], (cartValue, products, services, count) => ({
|
|
1717
1709
|
cart: cartValue,
|
|
1718
1710
|
product_items: products,
|
|
1719
1711
|
service_items: services,
|
|
1720
1712
|
item_count: count
|
|
1721
1713
|
}));
|
|
1714
|
+
let cartWriteRevision = 0;
|
|
1715
|
+
let sessionRequest = null;
|
|
1716
|
+
let cartRequest = null;
|
|
1717
|
+
function nextCartWriteRevision() {
|
|
1718
|
+
cartWriteRevision += 1;
|
|
1719
|
+
return cartWriteRevision;
|
|
1720
|
+
}
|
|
1722
1721
|
const cms_state = nanostores.map({
|
|
1723
1722
|
nodes: {},
|
|
1724
1723
|
forms: {},
|
|
@@ -1766,7 +1765,12 @@ function createArkyStore(config) {
|
|
|
1766
1765
|
const current = session.get();
|
|
1767
1766
|
const marketKey = currentMarketKey();
|
|
1768
1767
|
if (current && (!marketKey || current.market?.key === marketKey)) return current;
|
|
1769
|
-
|
|
1768
|
+
if (!sessionRequest) {
|
|
1769
|
+
sessionRequest = identify({ market: marketKey }).finally(() => {
|
|
1770
|
+
sessionRequest = null;
|
|
1771
|
+
});
|
|
1772
|
+
}
|
|
1773
|
+
return sessionRequest;
|
|
1770
1774
|
}
|
|
1771
1775
|
async function identify(params = {}) {
|
|
1772
1776
|
if (params.market) setMarket(params.market);
|
|
@@ -1792,17 +1796,23 @@ function createArkyStore(config) {
|
|
|
1792
1796
|
if (context.market) setMarket(context.market);
|
|
1793
1797
|
}
|
|
1794
1798
|
async function ensureCart() {
|
|
1799
|
+
if (cartRequest) return cartRequest;
|
|
1795
1800
|
cart_status.setKey("loading", true);
|
|
1796
1801
|
cart_status.setKey("error", null);
|
|
1797
|
-
|
|
1802
|
+
const refreshRevision = cartWriteRevision;
|
|
1803
|
+
cartRequest = (async () => {
|
|
1798
1804
|
await ensureSession();
|
|
1799
1805
|
const response = await client.cart.refresh({ market: currentMarketKey() });
|
|
1800
|
-
await hydrateCart(response);
|
|
1806
|
+
await hydrateCart(response, { ifRevision: refreshRevision });
|
|
1801
1807
|
return response;
|
|
1808
|
+
})();
|
|
1809
|
+
try {
|
|
1810
|
+
return await cartRequest;
|
|
1802
1811
|
} catch (error) {
|
|
1803
1812
|
cart_status.setKey("error", readErrorMessage(error, "Failed to load cart."));
|
|
1804
1813
|
throw error;
|
|
1805
1814
|
} finally {
|
|
1815
|
+
cartRequest = null;
|
|
1806
1816
|
cart_status.setKey("loading", false);
|
|
1807
1817
|
}
|
|
1808
1818
|
}
|
|
@@ -1848,7 +1858,6 @@ function createArkyStore(config) {
|
|
|
1848
1858
|
from: slot.from,
|
|
1849
1859
|
to: slot.to,
|
|
1850
1860
|
forms: item.forms || [],
|
|
1851
|
-
price: item.price,
|
|
1852
1861
|
service_name: service ? serviceName(service, currentLocale()) : item.service_id,
|
|
1853
1862
|
provider_name: provider ? providerName(provider, currentLocale()) : item.provider_id
|
|
1854
1863
|
});
|
|
@@ -1856,7 +1865,10 @@ function createArkyStore(config) {
|
|
|
1856
1865
|
}
|
|
1857
1866
|
return rows;
|
|
1858
1867
|
}
|
|
1859
|
-
async function hydrateCart(response) {
|
|
1868
|
+
async function hydrateCart(response, options = {}) {
|
|
1869
|
+
if (options.ifRevision !== void 0 && options.ifRevision !== cartWriteRevision) {
|
|
1870
|
+
return cart.get() || response;
|
|
1871
|
+
}
|
|
1860
1872
|
cart.set(response);
|
|
1861
1873
|
cart_status.setKey("user_token", response.token || null);
|
|
1862
1874
|
cart_status.setKey("selected_shipping_method_id", response.shipping_method_id || null);
|
|
@@ -1879,7 +1891,7 @@ function createArkyStore(config) {
|
|
|
1879
1891
|
...toServiceCheckoutItems(input.service_items || service_items.get())
|
|
1880
1892
|
];
|
|
1881
1893
|
}
|
|
1882
|
-
async function syncCart(input = {}) {
|
|
1894
|
+
async function syncCart(input = {}, writeRevision = nextCartWriteRevision()) {
|
|
1883
1895
|
cart_status.setKey("syncing", true);
|
|
1884
1896
|
cart_status.setKey("error", null);
|
|
1885
1897
|
try {
|
|
@@ -1899,7 +1911,7 @@ function createArkyStore(config) {
|
|
|
1899
1911
|
if (input.shipping_method_id !== void 0) {
|
|
1900
1912
|
cart_status.setKey("selected_shipping_method_id", input.shipping_method_id);
|
|
1901
1913
|
}
|
|
1902
|
-
await hydrateCart(response);
|
|
1914
|
+
await hydrateCart(response, { ifRevision: writeRevision });
|
|
1903
1915
|
return response;
|
|
1904
1916
|
} catch (error) {
|
|
1905
1917
|
cart_status.setKey("error", readErrorMessage(error, "Failed to sync cart."));
|
|
@@ -1910,6 +1922,7 @@ function createArkyStore(config) {
|
|
|
1910
1922
|
}
|
|
1911
1923
|
async function addProduct(product, variant, quantity = 1) {
|
|
1912
1924
|
cart_status.setKey("error", null);
|
|
1925
|
+
const writeRevision = nextCartWriteRevision();
|
|
1913
1926
|
try {
|
|
1914
1927
|
const current = cart.get() || await ensureCart();
|
|
1915
1928
|
const response = await client.cart.addItem({
|
|
@@ -1921,7 +1934,7 @@ function createArkyStore(config) {
|
|
|
1921
1934
|
quantity
|
|
1922
1935
|
}
|
|
1923
1936
|
});
|
|
1924
|
-
await hydrateCart(response);
|
|
1937
|
+
await hydrateCart(response, { ifRevision: writeRevision });
|
|
1925
1938
|
await client.activity.track({ type: "cart_added", payload: { product_id: product.id, variant_id: variant.id, quantity } });
|
|
1926
1939
|
return response;
|
|
1927
1940
|
} catch (error) {
|
|
@@ -1930,15 +1943,17 @@ function createArkyStore(config) {
|
|
|
1930
1943
|
}
|
|
1931
1944
|
}
|
|
1932
1945
|
async function setProductQuantity(itemId, quantity) {
|
|
1946
|
+
const writeRevision = nextCartWriteRevision();
|
|
1933
1947
|
const next = product_items.get().map((item) => {
|
|
1934
1948
|
if (item.id !== itemId) return item;
|
|
1935
1949
|
const bounded = item.max_stock ? Math.min(Math.max(1, quantity), item.max_stock) : Math.max(1, quantity);
|
|
1936
1950
|
return { ...item, quantity: bounded };
|
|
1937
1951
|
});
|
|
1938
1952
|
product_items.set(next);
|
|
1939
|
-
return syncCart({ product_items: next });
|
|
1953
|
+
return syncCart({ product_items: next }, writeRevision);
|
|
1940
1954
|
}
|
|
1941
1955
|
async function removeProduct(itemId) {
|
|
1956
|
+
const writeRevision = nextCartWriteRevision();
|
|
1942
1957
|
const item = product_items.get().find((candidate) => candidate.id === itemId);
|
|
1943
1958
|
product_items.set(product_items.get().filter((candidate) => candidate.id !== itemId));
|
|
1944
1959
|
const current = cart.get();
|
|
@@ -1949,21 +1964,24 @@ function createArkyStore(config) {
|
|
|
1949
1964
|
product_id: item.product_id,
|
|
1950
1965
|
variant_id: item.variant_id
|
|
1951
1966
|
});
|
|
1952
|
-
await hydrateCart(response);
|
|
1967
|
+
await hydrateCart(response, { ifRevision: writeRevision });
|
|
1953
1968
|
await client.activity.track({ type: "cart_removed", payload: { product_id: item.product_id, variant_id: item.variant_id } });
|
|
1954
1969
|
return response;
|
|
1955
1970
|
}
|
|
1956
1971
|
async function addServiceItem(item) {
|
|
1972
|
+
const writeRevision = nextCartWriteRevision();
|
|
1957
1973
|
const next = [...service_items.get(), item];
|
|
1958
1974
|
service_items.set(next);
|
|
1959
|
-
return syncCart({ service_items: next });
|
|
1975
|
+
return syncCart({ service_items: next }, writeRevision);
|
|
1960
1976
|
}
|
|
1961
1977
|
async function removeServiceItem(itemId) {
|
|
1978
|
+
const writeRevision = nextCartWriteRevision();
|
|
1962
1979
|
const next = service_items.get().filter((item) => item.id !== itemId);
|
|
1963
1980
|
service_items.set(next);
|
|
1964
|
-
return syncCart({ service_items: next });
|
|
1981
|
+
return syncCart({ service_items: next }, writeRevision);
|
|
1965
1982
|
}
|
|
1966
1983
|
async function clearCart() {
|
|
1984
|
+
const writeRevision = nextCartWriteRevision();
|
|
1967
1985
|
product_items.set([]);
|
|
1968
1986
|
service_items.set([]);
|
|
1969
1987
|
quote.set(null);
|
|
@@ -1972,7 +1990,7 @@ function createArkyStore(config) {
|
|
|
1972
1990
|
const current = cart.get();
|
|
1973
1991
|
if (!current) return null;
|
|
1974
1992
|
const response = await client.cart.clear({ id: current.id });
|
|
1975
|
-
await hydrateCart(response);
|
|
1993
|
+
await hydrateCart(response, { ifRevision: writeRevision });
|
|
1976
1994
|
return response;
|
|
1977
1995
|
}
|
|
1978
1996
|
async function fetchQuote(input = {}) {
|
|
@@ -2740,6 +2758,7 @@ function createArkyStore(config) {
|
|
|
2740
2758
|
setMarket,
|
|
2741
2759
|
setLocale,
|
|
2742
2760
|
setContext,
|
|
2761
|
+
getStoreId: client.getStoreId,
|
|
2743
2762
|
getMarket: currentMarketKey,
|
|
2744
2763
|
getLocale: currentLocale,
|
|
2745
2764
|
cms: {
|
|
@@ -2777,8 +2796,8 @@ function createArkyStore(config) {
|
|
|
2777
2796
|
},
|
|
2778
2797
|
state: nanostores.atom(null)
|
|
2779
2798
|
},
|
|
2799
|
+
support: client.support,
|
|
2780
2800
|
store: client.store,
|
|
2781
|
-
automation: client.automation,
|
|
2782
2801
|
utils: client.utils
|
|
2783
2802
|
};
|
|
2784
2803
|
}
|