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