@cimplify/sdk 0.6.11 → 0.7.0
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 +8 -3
- package/dist/advanced.d.mts +2 -2
- package/dist/advanced.d.ts +2 -2
- package/dist/advanced.js +529 -102
- package/dist/advanced.mjs +529 -102
- package/dist/{client-DM3-ZG29.d.ts → client-CyBNao15.d.ts} +15 -927
- package/dist/{client-BngAGN0v.d.mts → client-hccS_JMl.d.mts} +15 -927
- package/dist/{index-DaKJxoEh.d.mts → index-B_JHO-ER.d.ts} +29 -35
- package/dist/{index-pztT_bcJ.d.ts → index-mrKi_VbR.d.mts} +29 -35
- package/dist/index.d.mts +4 -4
- package/dist/index.d.ts +4 -4
- package/dist/index.js +585 -105
- package/dist/index.mjs +582 -106
- package/dist/payment-CLIWNMaP.d.mts +1133 -0
- package/dist/payment-CLIWNMaP.d.ts +1133 -0
- package/dist/react.d.mts +5 -2
- package/dist/react.d.ts +5 -2
- package/dist/react.js +582 -108
- package/dist/react.mjs +582 -108
- package/dist/utils.d.mts +2 -2
- package/dist/utils.d.ts +2 -2
- package/dist/utils.js +27 -0
- package/dist/utils.mjs +24 -1
- package/package.json +1 -1
- package/dist/payment-Cu75tmUc.d.mts +0 -196
- package/dist/payment-Cu75tmUc.d.ts +0 -196
package/dist/index.mjs
CHANGED
|
@@ -31,6 +31,11 @@ var ErrorCode = {
|
|
|
31
31
|
CHECKOUT_VALIDATION_FAILED: "CHECKOUT_VALIDATION_FAILED",
|
|
32
32
|
DELIVERY_ADDRESS_REQUIRED: "DELIVERY_ADDRESS_REQUIRED",
|
|
33
33
|
CUSTOMER_INFO_REQUIRED: "CUSTOMER_INFO_REQUIRED",
|
|
34
|
+
// Quote
|
|
35
|
+
QUOTE_NOT_FOUND: "QUOTE_NOT_FOUND",
|
|
36
|
+
QUOTE_EXPIRED: "QUOTE_EXPIRED",
|
|
37
|
+
QUOTE_CONSUMED: "QUOTE_CONSUMED",
|
|
38
|
+
QUOTE_STORAGE_UNAVAILABLE: "QUOTE_STORAGE_UNAVAILABLE",
|
|
34
39
|
// Payment
|
|
35
40
|
PAYMENT_FAILED: "PAYMENT_FAILED",
|
|
36
41
|
PAYMENT_CANCELLED: "PAYMENT_CANCELLED",
|
|
@@ -71,6 +76,10 @@ var ERROR_SUGGESTIONS = {
|
|
|
71
76
|
CHECKOUT_VALIDATION_FAILED: "Checkout payload failed validation. Verify customer, order type, and address fields are complete.",
|
|
72
77
|
DELIVERY_ADDRESS_REQUIRED: "Delivery orders require an address. Collect and pass address info before processing checkout.",
|
|
73
78
|
CUSTOMER_INFO_REQUIRED: "Customer details are required. Ensure name/email/phone are available before checkout.",
|
|
79
|
+
QUOTE_NOT_FOUND: "Quote could not be found. Refresh pricing and create a new quote before checkout.",
|
|
80
|
+
QUOTE_EXPIRED: "Quote has expired. Re-fetch pricing to generate a new quote with a valid expiry window.",
|
|
81
|
+
QUOTE_CONSUMED: "Quote has already been used. Request a fresh quote to prevent duplicate checkout attempts.",
|
|
82
|
+
QUOTE_STORAGE_UNAVAILABLE: "Quote storage is temporarily unavailable. Retry shortly and avoid charging until quote fetch succeeds.",
|
|
74
83
|
PAYMENT_FAILED: "Payment provider rejected or failed processing. Show retry/change-method options to the shopper.",
|
|
75
84
|
PAYMENT_CANCELLED: "Payment was cancelled by the shopper or provider flow. Allow a safe retry path.",
|
|
76
85
|
INSUFFICIENT_FUNDS: "Payment method has insufficient funds. Prompt shopper to use another method.",
|
|
@@ -310,6 +319,23 @@ async function safe(promise) {
|
|
|
310
319
|
return err(toCimplifyError(error));
|
|
311
320
|
}
|
|
312
321
|
}
|
|
322
|
+
async function safeWithFallback(primary, fallback) {
|
|
323
|
+
const primaryResult = await safe(primary());
|
|
324
|
+
if (primaryResult.ok) return primaryResult;
|
|
325
|
+
if (primaryResult.error.code !== "HTTP_404" && primaryResult.error.code !== "API_ERROR") {
|
|
326
|
+
return primaryResult;
|
|
327
|
+
}
|
|
328
|
+
return safe(fallback());
|
|
329
|
+
}
|
|
330
|
+
function withQuery(path, params) {
|
|
331
|
+
const query2 = new URLSearchParams();
|
|
332
|
+
for (const [key, value] of Object.entries(params)) {
|
|
333
|
+
if (value === void 0) continue;
|
|
334
|
+
query2.set(key, String(value));
|
|
335
|
+
}
|
|
336
|
+
const queryString = query2.toString();
|
|
337
|
+
return queryString ? `${path}?${queryString}` : path;
|
|
338
|
+
}
|
|
313
339
|
function isRecord(value) {
|
|
314
340
|
return typeof value === "object" && value !== null;
|
|
315
341
|
}
|
|
@@ -367,6 +393,69 @@ function findProductBySlug(products, slug) {
|
|
|
367
393
|
return typeof value === "string" && value === slug;
|
|
368
394
|
});
|
|
369
395
|
}
|
|
396
|
+
function findCategoryBySlug(categories, slug) {
|
|
397
|
+
return categories.find((category) => {
|
|
398
|
+
const value = category["slug"];
|
|
399
|
+
return typeof value === "string" && value === slug;
|
|
400
|
+
});
|
|
401
|
+
}
|
|
402
|
+
function hasCategorySlug(category) {
|
|
403
|
+
const value = category["slug"];
|
|
404
|
+
return typeof value === "string" && value.trim().length > 0;
|
|
405
|
+
}
|
|
406
|
+
function toFiniteNumber(value) {
|
|
407
|
+
if (typeof value === "number" && Number.isFinite(value)) {
|
|
408
|
+
return value;
|
|
409
|
+
}
|
|
410
|
+
if (typeof value === "string" && value.trim().length > 0) {
|
|
411
|
+
const parsed = Number(value);
|
|
412
|
+
if (Number.isFinite(parsed)) {
|
|
413
|
+
return parsed;
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
return void 0;
|
|
417
|
+
}
|
|
418
|
+
function normalizePagination(value) {
|
|
419
|
+
if (!isRecord(value)) {
|
|
420
|
+
return void 0;
|
|
421
|
+
}
|
|
422
|
+
const totalCount = toFiniteNumber(value.total_count);
|
|
423
|
+
const currentPage = toFiniteNumber(value.current_page);
|
|
424
|
+
const pageSize = toFiniteNumber(value.page_size);
|
|
425
|
+
const totalPages = toFiniteNumber(value.total_pages);
|
|
426
|
+
if (totalCount === void 0 || currentPage === void 0 || pageSize === void 0 || totalPages === void 0) {
|
|
427
|
+
return void 0;
|
|
428
|
+
}
|
|
429
|
+
return {
|
|
430
|
+
total_count: totalCount,
|
|
431
|
+
current_page: currentPage,
|
|
432
|
+
page_size: pageSize,
|
|
433
|
+
total_pages: totalPages,
|
|
434
|
+
has_more: value.has_more === true,
|
|
435
|
+
next_cursor: typeof value.next_cursor === "string" ? value.next_cursor : void 0
|
|
436
|
+
};
|
|
437
|
+
}
|
|
438
|
+
function normalizeCatalogueResult(payload) {
|
|
439
|
+
if (Array.isArray(payload)) {
|
|
440
|
+
return {
|
|
441
|
+
items: payload.map((product) => normalizeCatalogueProductPayload(product)),
|
|
442
|
+
is_complete: true
|
|
443
|
+
};
|
|
444
|
+
}
|
|
445
|
+
if (!isRecord(payload)) {
|
|
446
|
+
return {
|
|
447
|
+
items: [],
|
|
448
|
+
is_complete: true
|
|
449
|
+
};
|
|
450
|
+
}
|
|
451
|
+
const rawItems = Array.isArray(payload.products) ? payload.products : Array.isArray(payload.items) ? payload.items : [];
|
|
452
|
+
return {
|
|
453
|
+
items: rawItems.map((product) => normalizeCatalogueProductPayload(product)),
|
|
454
|
+
is_complete: typeof payload.is_complete === "boolean" ? payload.is_complete : true,
|
|
455
|
+
total_available: toFiniteNumber(payload.total_available),
|
|
456
|
+
pagination: normalizePagination(payload.pagination)
|
|
457
|
+
};
|
|
458
|
+
}
|
|
370
459
|
var CatalogueQueries = class {
|
|
371
460
|
constructor(client) {
|
|
372
461
|
this.client = client;
|
|
@@ -386,6 +475,13 @@ var CatalogueQueries = class {
|
|
|
386
475
|
if (options?.search) {
|
|
387
476
|
filters.push(`@.name contains '${escapeQueryValue(options.search)}'`);
|
|
388
477
|
}
|
|
478
|
+
if (options?.tags?.length) {
|
|
479
|
+
for (const tag of options.tags) {
|
|
480
|
+
if (tag.trim().length > 0) {
|
|
481
|
+
filters.push(`@.tags contains '${escapeQueryValue(tag)}'`);
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
}
|
|
389
485
|
if (options?.min_price !== void 0) {
|
|
390
486
|
filters.push(`@.price>=${options.min_price}`);
|
|
391
487
|
}
|
|
@@ -398,25 +494,57 @@ var CatalogueQueries = class {
|
|
|
398
494
|
if (options?.sort_by) {
|
|
399
495
|
query2 += `#sort(${options.sort_by},${options.sort_order || "asc"})`;
|
|
400
496
|
}
|
|
401
|
-
if (options?.limit) {
|
|
497
|
+
if (options?.limit !== void 0) {
|
|
402
498
|
query2 += `#limit(${options.limit})`;
|
|
403
499
|
}
|
|
404
|
-
if (options?.offset) {
|
|
500
|
+
if (options?.offset !== void 0) {
|
|
405
501
|
query2 += `#offset(${options.offset})`;
|
|
406
502
|
}
|
|
407
|
-
const
|
|
503
|
+
const path = withQuery("/api/v1/catalogue/products", {
|
|
504
|
+
category_id: options?.category,
|
|
505
|
+
search: options?.search,
|
|
506
|
+
page: options?.page,
|
|
507
|
+
tags: options?.tags?.join(","),
|
|
508
|
+
featured: options?.featured,
|
|
509
|
+
in_stock: options?.in_stock,
|
|
510
|
+
min_price: options?.min_price,
|
|
511
|
+
max_price: options?.max_price,
|
|
512
|
+
sort_by: options?.sort_by,
|
|
513
|
+
sort_order: options?.sort_order,
|
|
514
|
+
limit: options?.limit,
|
|
515
|
+
offset: options?.offset,
|
|
516
|
+
cursor: options?.cursor
|
|
517
|
+
});
|
|
518
|
+
const result = await safeWithFallback(
|
|
519
|
+
() => this.client.get(path),
|
|
520
|
+
() => this.client.query(query2)
|
|
521
|
+
);
|
|
408
522
|
if (!result.ok) return result;
|
|
409
|
-
return ok(result.value
|
|
523
|
+
return ok(normalizeCatalogueResult(result.value));
|
|
410
524
|
}
|
|
411
525
|
async getProduct(id) {
|
|
412
|
-
const
|
|
526
|
+
const encodedId = encodeURIComponent(id);
|
|
527
|
+
const result = await safeWithFallback(
|
|
528
|
+
() => this.client.get(`/api/v1/catalogue/products/${encodedId}`),
|
|
529
|
+
() => this.client.query(`products.${id}`)
|
|
530
|
+
);
|
|
413
531
|
if (!result.ok) return result;
|
|
414
532
|
return ok(normalizeCatalogueProductPayload(result.value));
|
|
415
533
|
}
|
|
416
534
|
async getProductBySlug(slug) {
|
|
535
|
+
const encodedSlug = encodeURIComponent(slug);
|
|
536
|
+
const restResult = await safe(
|
|
537
|
+
this.client.get(`/api/v1/catalogue/products/slug/${encodedSlug}`)
|
|
538
|
+
);
|
|
539
|
+
if (restResult.ok) {
|
|
540
|
+
return ok(normalizeCatalogueProductPayload(restResult.value));
|
|
541
|
+
}
|
|
542
|
+
if (restResult.error.code !== "HTTP_404" && restResult.error.code !== "API_ERROR") {
|
|
543
|
+
return restResult;
|
|
544
|
+
}
|
|
417
545
|
const filteredResult = await safe(
|
|
418
546
|
this.client.query(
|
|
419
|
-
`products[?(@.slug=='${escapeQueryValue(slug)}')]`
|
|
547
|
+
`products[?(@.slug=='${escapeQueryValue(slug)}')]#limit(50)`
|
|
420
548
|
)
|
|
421
549
|
);
|
|
422
550
|
if (!filteredResult.ok) return filteredResult;
|
|
@@ -427,7 +555,9 @@ var CatalogueQueries = class {
|
|
|
427
555
|
if (filteredResult.value.length === 1) {
|
|
428
556
|
return ok(normalizeCatalogueProductPayload(filteredResult.value[0]));
|
|
429
557
|
}
|
|
430
|
-
const unfilteredResult = await safe(
|
|
558
|
+
const unfilteredResult = await safe(
|
|
559
|
+
this.client.query("products#limit(200)")
|
|
560
|
+
);
|
|
431
561
|
if (!unfilteredResult.ok) return unfilteredResult;
|
|
432
562
|
const fallbackMatch = findProductBySlug(unfilteredResult.value, slug);
|
|
433
563
|
if (!fallbackMatch) {
|
|
@@ -436,18 +566,33 @@ var CatalogueQueries = class {
|
|
|
436
566
|
return ok(normalizeCatalogueProductPayload(fallbackMatch));
|
|
437
567
|
}
|
|
438
568
|
async getVariants(productId) {
|
|
439
|
-
|
|
569
|
+
const encodedId = encodeURIComponent(productId);
|
|
570
|
+
return safeWithFallback(
|
|
571
|
+
() => this.client.get(`/api/v1/catalogue/products/${encodedId}/variants`),
|
|
572
|
+
() => this.client.query(`products.${productId}.variants`)
|
|
573
|
+
);
|
|
440
574
|
}
|
|
441
575
|
async getVariantAxes(productId) {
|
|
442
|
-
|
|
576
|
+
const encodedId = encodeURIComponent(productId);
|
|
577
|
+
return safeWithFallback(
|
|
578
|
+
() => this.client.get(`/api/v1/catalogue/products/${encodedId}/variant-axes`),
|
|
579
|
+
() => this.client.query(`products.${productId}.variant_axes`)
|
|
580
|
+
);
|
|
443
581
|
}
|
|
444
582
|
/**
|
|
445
583
|
* Find a variant by axis selections (e.g., { "Size": "Large", "Color": "Red" })
|
|
446
584
|
* Returns the matching variant or null if no match found.
|
|
447
585
|
*/
|
|
448
586
|
async getVariantByAxisSelections(productId, selections) {
|
|
449
|
-
|
|
450
|
-
|
|
587
|
+
const encodedId = encodeURIComponent(productId);
|
|
588
|
+
return safeWithFallback(
|
|
589
|
+
() => this.client.post(
|
|
590
|
+
`/api/v1/catalogue/products/${encodedId}/variants/find`,
|
|
591
|
+
{
|
|
592
|
+
axis_selections: selections
|
|
593
|
+
}
|
|
594
|
+
),
|
|
595
|
+
() => this.client.query(`products.${productId}.variant`, {
|
|
451
596
|
axis_selections: selections
|
|
452
597
|
})
|
|
453
598
|
);
|
|
@@ -456,45 +601,107 @@ var CatalogueQueries = class {
|
|
|
456
601
|
* Get a specific variant by its ID
|
|
457
602
|
*/
|
|
458
603
|
async getVariantById(productId, variantId) {
|
|
459
|
-
|
|
604
|
+
const encodedProductId = encodeURIComponent(productId);
|
|
605
|
+
const encodedVariantId = encodeURIComponent(variantId);
|
|
606
|
+
return safeWithFallback(
|
|
607
|
+
() => this.client.get(
|
|
608
|
+
`/api/v1/catalogue/products/${encodedProductId}/variants/${encodedVariantId}`
|
|
609
|
+
),
|
|
610
|
+
() => this.client.query(`products.${productId}.variant.${variantId}`)
|
|
611
|
+
);
|
|
460
612
|
}
|
|
461
613
|
async getAddOns(productId) {
|
|
462
|
-
|
|
614
|
+
const encodedId = encodeURIComponent(productId);
|
|
615
|
+
return safeWithFallback(
|
|
616
|
+
() => this.client.get(`/api/v1/catalogue/products/${encodedId}/add-ons`),
|
|
617
|
+
() => this.client.query(`products.${productId}.add_ons`)
|
|
618
|
+
);
|
|
463
619
|
}
|
|
464
620
|
async getCategories() {
|
|
465
|
-
|
|
621
|
+
const result = await safeWithFallback(
|
|
622
|
+
() => this.client.get("/api/v1/catalogue/categories"),
|
|
623
|
+
() => this.client.query("categories")
|
|
624
|
+
);
|
|
625
|
+
if (!result.ok) return result;
|
|
626
|
+
if (result.value.some(hasCategorySlug)) {
|
|
627
|
+
return result;
|
|
628
|
+
}
|
|
629
|
+
const catalogueResult = await safe(
|
|
630
|
+
this.client.query("catalogue#limit(1)")
|
|
631
|
+
);
|
|
632
|
+
if (!catalogueResult.ok) {
|
|
633
|
+
return result;
|
|
634
|
+
}
|
|
635
|
+
const fallbackCategories = Array.isArray(catalogueResult.value.categories) ? catalogueResult.value.categories : [];
|
|
636
|
+
return fallbackCategories.length > 0 ? ok(fallbackCategories) : result;
|
|
466
637
|
}
|
|
467
638
|
async getCategory(id) {
|
|
468
|
-
|
|
639
|
+
const encodedId = encodeURIComponent(id);
|
|
640
|
+
return safeWithFallback(
|
|
641
|
+
() => this.client.get(`/api/v1/catalogue/categories/${encodedId}`),
|
|
642
|
+
() => this.client.query(`categories.${id}`)
|
|
643
|
+
);
|
|
469
644
|
}
|
|
470
645
|
async getCategoryBySlug(slug) {
|
|
646
|
+
const encodedSlug = encodeURIComponent(slug);
|
|
647
|
+
const restResult = await safe(this.client.get(`/api/v1/catalogue/categories/slug/${encodedSlug}`));
|
|
648
|
+
if (restResult.ok) {
|
|
649
|
+
return restResult;
|
|
650
|
+
}
|
|
651
|
+
if (restResult.error.code !== "HTTP_404" && restResult.error.code !== "API_ERROR") {
|
|
652
|
+
return restResult;
|
|
653
|
+
}
|
|
471
654
|
const result = await safe(
|
|
472
655
|
this.client.query(`categories[?(@.slug=='${escapeQueryValue(slug)}')]`)
|
|
473
656
|
);
|
|
474
657
|
if (!result.ok) return result;
|
|
475
|
-
|
|
658
|
+
const exactMatch = findCategoryBySlug(result.value, slug);
|
|
659
|
+
if (exactMatch) {
|
|
660
|
+
return ok(exactMatch);
|
|
661
|
+
}
|
|
662
|
+
const categoriesResult = await this.getCategories();
|
|
663
|
+
if (!categoriesResult.ok) {
|
|
664
|
+
return categoriesResult;
|
|
665
|
+
}
|
|
666
|
+
const fallbackMatch = findCategoryBySlug(categoriesResult.value, slug);
|
|
667
|
+
if (!fallbackMatch) {
|
|
476
668
|
return err(new CimplifyError("NOT_FOUND", `Category not found: ${slug}`, false));
|
|
477
669
|
}
|
|
478
|
-
return ok(
|
|
670
|
+
return ok(fallbackMatch);
|
|
479
671
|
}
|
|
480
672
|
async getCategoryProducts(categoryId) {
|
|
481
|
-
|
|
482
|
-
|
|
673
|
+
const encodedId = encodeURIComponent(categoryId);
|
|
674
|
+
return safeWithFallback(
|
|
675
|
+
() => this.client.get(`/api/v1/catalogue/categories/${encodedId}/products`),
|
|
676
|
+
() => this.client.query(
|
|
483
677
|
`products[?(@.category_id=='${escapeQueryValue(categoryId)}')]`
|
|
484
678
|
)
|
|
485
679
|
);
|
|
486
680
|
}
|
|
487
681
|
async getCollections() {
|
|
488
|
-
return
|
|
682
|
+
return safeWithFallback(
|
|
683
|
+
() => this.client.get("/api/v1/catalogue/collections"),
|
|
684
|
+
() => this.client.query("collections")
|
|
685
|
+
);
|
|
489
686
|
}
|
|
490
687
|
async getCollection(id) {
|
|
491
|
-
|
|
688
|
+
const encodedId = encodeURIComponent(id);
|
|
689
|
+
return safeWithFallback(
|
|
690
|
+
() => this.client.get(`/api/v1/catalogue/collections/${encodedId}`),
|
|
691
|
+
() => this.client.query(`collections.${id}`)
|
|
692
|
+
);
|
|
492
693
|
}
|
|
493
694
|
async getCollectionBySlug(slug) {
|
|
695
|
+
const encodedSlug = encodeURIComponent(slug);
|
|
696
|
+
const restResult = await safe(
|
|
697
|
+
this.client.get(`/api/v1/catalogue/collections/slug/${encodedSlug}`)
|
|
698
|
+
);
|
|
699
|
+
if (restResult.ok) return restResult;
|
|
700
|
+
if (restResult.error.code !== "HTTP_404" && restResult.error.code !== "API_ERROR") {
|
|
701
|
+
return restResult;
|
|
702
|
+
}
|
|
494
703
|
const result = await safe(
|
|
495
|
-
this.client.query(
|
|
496
|
-
`collections[?(@.slug=='${escapeQueryValue(slug)}')]`
|
|
497
|
-
)
|
|
704
|
+
this.client.query(`collections[?(@.slug=='${escapeQueryValue(slug)}')]`)
|
|
498
705
|
);
|
|
499
706
|
if (!result.ok) return result;
|
|
500
707
|
if (!result.value.length) {
|
|
@@ -503,22 +710,43 @@ var CatalogueQueries = class {
|
|
|
503
710
|
return ok(result.value[0]);
|
|
504
711
|
}
|
|
505
712
|
async getCollectionProducts(collectionId) {
|
|
506
|
-
|
|
713
|
+
const encodedId = encodeURIComponent(collectionId);
|
|
714
|
+
return safeWithFallback(
|
|
715
|
+
() => this.client.get(`/api/v1/catalogue/collections/${encodedId}/products`),
|
|
716
|
+
() => this.client.query(`collections.${collectionId}.products`)
|
|
717
|
+
);
|
|
507
718
|
}
|
|
508
719
|
async searchCollections(query2, limit = 20) {
|
|
509
|
-
|
|
510
|
-
|
|
720
|
+
const path = withQuery("/api/v1/catalogue/collections", { search: query2, limit });
|
|
721
|
+
return safeWithFallback(
|
|
722
|
+
() => this.client.get(path),
|
|
723
|
+
() => this.client.query(
|
|
511
724
|
`collections[?(@.name contains '${escapeQueryValue(query2)}')]#limit(${limit})`
|
|
512
725
|
)
|
|
513
726
|
);
|
|
514
727
|
}
|
|
515
728
|
async getBundles() {
|
|
516
|
-
return
|
|
729
|
+
return safeWithFallback(
|
|
730
|
+
() => this.client.get("/api/v1/catalogue/bundles"),
|
|
731
|
+
() => this.client.query("bundles")
|
|
732
|
+
);
|
|
517
733
|
}
|
|
518
734
|
async getBundle(id) {
|
|
519
|
-
|
|
735
|
+
const encodedId = encodeURIComponent(id);
|
|
736
|
+
return safeWithFallback(
|
|
737
|
+
() => this.client.get(`/api/v1/catalogue/bundles/${encodedId}`),
|
|
738
|
+
() => this.client.query(`bundles.${id}`)
|
|
739
|
+
);
|
|
520
740
|
}
|
|
521
741
|
async getBundleBySlug(slug) {
|
|
742
|
+
const encodedSlug = encodeURIComponent(slug);
|
|
743
|
+
const restResult = await safe(
|
|
744
|
+
this.client.get(`/api/v1/catalogue/bundles/slug/${encodedSlug}`)
|
|
745
|
+
);
|
|
746
|
+
if (restResult.ok) return restResult;
|
|
747
|
+
if (restResult.error.code !== "HTTP_404" && restResult.error.code !== "API_ERROR") {
|
|
748
|
+
return restResult;
|
|
749
|
+
}
|
|
522
750
|
const result = await safe(
|
|
523
751
|
this.client.query(
|
|
524
752
|
`bundles[?(@.slug=='${escapeQueryValue(slug)}')]`
|
|
@@ -531,8 +759,10 @@ var CatalogueQueries = class {
|
|
|
531
759
|
return ok(result.value[0]);
|
|
532
760
|
}
|
|
533
761
|
async searchBundles(query2, limit = 20) {
|
|
534
|
-
|
|
535
|
-
|
|
762
|
+
const path = withQuery("/api/v1/catalogue/bundles", { search: query2, limit });
|
|
763
|
+
return safeWithFallback(
|
|
764
|
+
() => this.client.get(path),
|
|
765
|
+
() => this.client.query(
|
|
536
766
|
`bundles[?(@.name contains '${escapeQueryValue(query2)}')]#limit(${limit})`
|
|
537
767
|
)
|
|
538
768
|
);
|
|
@@ -542,17 +772,39 @@ var CatalogueQueries = class {
|
|
|
542
772
|
if (options?.limit) {
|
|
543
773
|
query2 += `#limit(${options.limit})`;
|
|
544
774
|
}
|
|
545
|
-
|
|
775
|
+
const path = withQuery("/api/v1/catalogue/composites", { limit: options?.limit });
|
|
776
|
+
return safeWithFallback(
|
|
777
|
+
() => this.client.get(path),
|
|
778
|
+
() => this.client.query(query2)
|
|
779
|
+
);
|
|
546
780
|
}
|
|
547
781
|
async getComposite(id) {
|
|
548
|
-
|
|
782
|
+
const encodedId = encodeURIComponent(id);
|
|
783
|
+
return safeWithFallback(
|
|
784
|
+
() => this.client.get(`/api/v1/catalogue/composites/${encodedId}`),
|
|
785
|
+
() => this.client.query(`composites.${id}`)
|
|
786
|
+
);
|
|
549
787
|
}
|
|
550
788
|
async getCompositeByProductId(productId) {
|
|
551
|
-
|
|
789
|
+
const encodedId = encodeURIComponent(productId);
|
|
790
|
+
return safeWithFallback(
|
|
791
|
+
() => this.client.get(
|
|
792
|
+
`/api/v1/catalogue/composites/by-product/${encodedId}`
|
|
793
|
+
),
|
|
794
|
+
() => this.client.query(`composites.by_product.${productId}`)
|
|
795
|
+
);
|
|
552
796
|
}
|
|
553
797
|
async calculateCompositePrice(compositeId, selections, locationId) {
|
|
554
|
-
|
|
555
|
-
|
|
798
|
+
const encodedId = encodeURIComponent(compositeId);
|
|
799
|
+
return safeWithFallback(
|
|
800
|
+
() => this.client.post(
|
|
801
|
+
`/api/v1/catalogue/composites/${encodedId}/calculate-price`,
|
|
802
|
+
{
|
|
803
|
+
selections,
|
|
804
|
+
location_id: locationId
|
|
805
|
+
}
|
|
806
|
+
),
|
|
807
|
+
() => this.client.call("composite.calculatePrice", {
|
|
556
808
|
composite_id: compositeId,
|
|
557
809
|
selections,
|
|
558
810
|
location_id: locationId
|
|
@@ -560,35 +812,41 @@ var CatalogueQueries = class {
|
|
|
560
812
|
);
|
|
561
813
|
}
|
|
562
814
|
async fetchQuote(input) {
|
|
563
|
-
return
|
|
815
|
+
return safeWithFallback(
|
|
816
|
+
() => this.client.post("/api/v1/catalogue/quotes", input),
|
|
817
|
+
() => this.client.call("catalogue.createQuote", input)
|
|
818
|
+
);
|
|
564
819
|
}
|
|
565
820
|
async getQuote(quoteId) {
|
|
566
|
-
|
|
567
|
-
|
|
821
|
+
const encodedQuoteId = encodeURIComponent(quoteId);
|
|
822
|
+
return safeWithFallback(
|
|
823
|
+
() => this.client.get(`/api/v1/catalogue/quotes/${encodedQuoteId}`),
|
|
824
|
+
() => this.client.call("catalogue.getQuote", {
|
|
568
825
|
quote_id: quoteId
|
|
569
826
|
})
|
|
570
827
|
);
|
|
571
828
|
}
|
|
572
829
|
async refreshQuote(input) {
|
|
573
|
-
|
|
830
|
+
const encodedQuoteId = encodeURIComponent(input.quote_id);
|
|
831
|
+
return safeWithFallback(
|
|
832
|
+
() => this.client.post(
|
|
833
|
+
`/api/v1/catalogue/quotes/${encodedQuoteId}/refresh`,
|
|
834
|
+
input
|
|
835
|
+
),
|
|
836
|
+
() => this.client.call("catalogue.refreshQuote", input)
|
|
837
|
+
);
|
|
574
838
|
}
|
|
575
839
|
async search(query2, options) {
|
|
576
|
-
const
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
}
|
|
581
|
-
|
|
582
|
-
return
|
|
840
|
+
const result = await this.getProducts({
|
|
841
|
+
search: query2,
|
|
842
|
+
category: options?.category,
|
|
843
|
+
limit: options?.limit ?? 20
|
|
844
|
+
});
|
|
845
|
+
if (!result.ok) return result;
|
|
846
|
+
return ok(result.value.items);
|
|
583
847
|
}
|
|
584
848
|
async searchProducts(query2, options) {
|
|
585
|
-
return
|
|
586
|
-
this.client.call("catalogue.search", {
|
|
587
|
-
query: query2,
|
|
588
|
-
limit: options?.limit ?? 20,
|
|
589
|
-
category: options?.category
|
|
590
|
-
})
|
|
591
|
-
);
|
|
849
|
+
return this.search(query2, options);
|
|
592
850
|
}
|
|
593
851
|
async getMenu(options) {
|
|
594
852
|
let query2 = "menu";
|
|
@@ -598,13 +856,28 @@ var CatalogueQueries = class {
|
|
|
598
856
|
if (options?.limit) {
|
|
599
857
|
query2 += `#limit(${options.limit})`;
|
|
600
858
|
}
|
|
601
|
-
|
|
859
|
+
const path = withQuery("/api/v1/catalogue/menu", {
|
|
860
|
+
category_id: options?.category,
|
|
861
|
+
limit: options?.limit
|
|
862
|
+
});
|
|
863
|
+
return safeWithFallback(
|
|
864
|
+
() => this.client.get(path),
|
|
865
|
+
() => this.client.query(query2)
|
|
866
|
+
);
|
|
602
867
|
}
|
|
603
868
|
async getMenuCategory(categoryId) {
|
|
604
|
-
|
|
869
|
+
const encodedId = encodeURIComponent(categoryId);
|
|
870
|
+
return safeWithFallback(
|
|
871
|
+
() => this.client.get(`/api/v1/catalogue/menu/categories/${encodedId}`),
|
|
872
|
+
() => this.client.query(`menu.category.${categoryId}`)
|
|
873
|
+
);
|
|
605
874
|
}
|
|
606
875
|
async getMenuItem(itemId) {
|
|
607
|
-
|
|
876
|
+
const encodedId = encodeURIComponent(itemId);
|
|
877
|
+
return safeWithFallback(
|
|
878
|
+
() => this.client.get(`/api/v1/catalogue/menu/items/${encodedId}`),
|
|
879
|
+
() => this.client.query(`menu.${itemId}`)
|
|
880
|
+
);
|
|
608
881
|
}
|
|
609
882
|
};
|
|
610
883
|
|
|
@@ -623,6 +896,14 @@ async function safe2(promise) {
|
|
|
623
896
|
return err(toCimplifyError2(error));
|
|
624
897
|
}
|
|
625
898
|
}
|
|
899
|
+
async function safeWithFallback2(primary, fallback) {
|
|
900
|
+
const primaryResult = await safe2(primary());
|
|
901
|
+
if (primaryResult.ok) return primaryResult;
|
|
902
|
+
if (primaryResult.error.code !== "HTTP_404" && primaryResult.error.code !== "API_ERROR") {
|
|
903
|
+
return primaryResult;
|
|
904
|
+
}
|
|
905
|
+
return safe2(fallback());
|
|
906
|
+
}
|
|
626
907
|
function isUICartResponse(value) {
|
|
627
908
|
return "cart" in value;
|
|
628
909
|
}
|
|
@@ -634,21 +915,36 @@ var CartOperations = class {
|
|
|
634
915
|
this.client = client;
|
|
635
916
|
}
|
|
636
917
|
async get() {
|
|
637
|
-
const result = await
|
|
918
|
+
const result = await safeWithFallback2(
|
|
919
|
+
() => this.client.get("/api/v1/cart"),
|
|
920
|
+
() => this.client.query("cart#enriched")
|
|
921
|
+
);
|
|
638
922
|
if (!result.ok) return result;
|
|
639
923
|
return ok(unwrapEnrichedCart(result.value));
|
|
640
924
|
}
|
|
641
925
|
async getRaw() {
|
|
642
|
-
return
|
|
926
|
+
return safeWithFallback2(
|
|
927
|
+
() => this.client.get("/api/v1/cart"),
|
|
928
|
+
() => this.client.query("cart")
|
|
929
|
+
);
|
|
643
930
|
}
|
|
644
931
|
async getItems() {
|
|
645
|
-
return
|
|
932
|
+
return safeWithFallback2(
|
|
933
|
+
() => this.client.get("/api/v1/cart/items"),
|
|
934
|
+
() => this.client.query("cart_items")
|
|
935
|
+
);
|
|
646
936
|
}
|
|
647
937
|
async getCount() {
|
|
648
|
-
return
|
|
938
|
+
return safeWithFallback2(
|
|
939
|
+
() => this.client.get("/api/v1/cart/count"),
|
|
940
|
+
() => this.client.query("cart#count")
|
|
941
|
+
);
|
|
649
942
|
}
|
|
650
943
|
async getTotal() {
|
|
651
|
-
return
|
|
944
|
+
return safeWithFallback2(
|
|
945
|
+
() => this.client.get("/api/v1/cart/total"),
|
|
946
|
+
() => this.client.query("cart#total")
|
|
947
|
+
);
|
|
652
948
|
}
|
|
653
949
|
async getSummary() {
|
|
654
950
|
const cartResult = await this.get();
|
|
@@ -665,43 +961,66 @@ var CartOperations = class {
|
|
|
665
961
|
});
|
|
666
962
|
}
|
|
667
963
|
async addItem(input) {
|
|
668
|
-
return
|
|
964
|
+
return safeWithFallback2(
|
|
965
|
+
() => this.client.post("/api/v1/cart/items", input),
|
|
966
|
+
() => this.client.call("cart.addItem", input)
|
|
967
|
+
);
|
|
669
968
|
}
|
|
670
969
|
async updateItem(cartItemId, updates) {
|
|
671
|
-
|
|
672
|
-
this.
|
|
970
|
+
if (typeof updates.quantity === "number") {
|
|
971
|
+
return this.updateQuantity(cartItemId, updates.quantity);
|
|
972
|
+
}
|
|
973
|
+
const encodedId = encodeURIComponent(cartItemId);
|
|
974
|
+
return safeWithFallback2(
|
|
975
|
+
() => this.client.patch(`/api/v1/cart/items/${encodedId}`, updates),
|
|
976
|
+
() => this.client.call("cart.updateItem", {
|
|
673
977
|
cart_item_id: cartItemId,
|
|
674
978
|
...updates
|
|
675
979
|
})
|
|
676
980
|
);
|
|
677
981
|
}
|
|
678
982
|
async updateQuantity(cartItemId, quantity) {
|
|
679
|
-
|
|
680
|
-
|
|
983
|
+
const encodedId = encodeURIComponent(cartItemId);
|
|
984
|
+
return safeWithFallback2(
|
|
985
|
+
() => this.client.patch(`/api/v1/cart/items/${encodedId}`, {
|
|
986
|
+
quantity
|
|
987
|
+
}),
|
|
988
|
+
() => this.client.call("cart.updateItemQuantity", {
|
|
681
989
|
cart_item_id: cartItemId,
|
|
682
990
|
quantity
|
|
683
991
|
})
|
|
684
992
|
);
|
|
685
993
|
}
|
|
686
994
|
async removeItem(cartItemId) {
|
|
687
|
-
|
|
688
|
-
|
|
995
|
+
const encodedId = encodeURIComponent(cartItemId);
|
|
996
|
+
return safeWithFallback2(
|
|
997
|
+
() => this.client.delete(`/api/v1/cart/items/${encodedId}`),
|
|
998
|
+
() => this.client.call("cart.removeItem", {
|
|
689
999
|
cart_item_id: cartItemId
|
|
690
1000
|
})
|
|
691
1001
|
);
|
|
692
1002
|
}
|
|
693
1003
|
async clear() {
|
|
694
|
-
return
|
|
1004
|
+
return safeWithFallback2(
|
|
1005
|
+
() => this.client.delete("/api/v1/cart"),
|
|
1006
|
+
() => this.client.call("cart.clearCart")
|
|
1007
|
+
);
|
|
695
1008
|
}
|
|
696
1009
|
async applyCoupon(code) {
|
|
697
|
-
return
|
|
698
|
-
this.client.
|
|
1010
|
+
return safeWithFallback2(
|
|
1011
|
+
() => this.client.post("/api/v1/cart/coupons", {
|
|
1012
|
+
coupon_code: code
|
|
1013
|
+
}),
|
|
1014
|
+
() => this.client.call("cart.applyCoupon", {
|
|
699
1015
|
coupon_code: code
|
|
700
1016
|
})
|
|
701
1017
|
);
|
|
702
1018
|
}
|
|
703
1019
|
async removeCoupon() {
|
|
704
|
-
return
|
|
1020
|
+
return safeWithFallback2(
|
|
1021
|
+
() => this.client.delete("/api/v1/cart/coupons/current"),
|
|
1022
|
+
() => this.client.call("cart.removeCoupon")
|
|
1023
|
+
);
|
|
705
1024
|
}
|
|
706
1025
|
async isEmpty() {
|
|
707
1026
|
const countResult = await this.getCount();
|
|
@@ -982,6 +1301,25 @@ function parsePrice(value) {
|
|
|
982
1301
|
const parsed = parseFloat(cleaned);
|
|
983
1302
|
return isNaN(parsed) ? 0 : parsed;
|
|
984
1303
|
}
|
|
1304
|
+
function hasTaxInfo(priceInfo) {
|
|
1305
|
+
return priceInfo.tax_info !== void 0 && priceInfo.tax_info !== null;
|
|
1306
|
+
}
|
|
1307
|
+
function getTaxAmount(priceInfo) {
|
|
1308
|
+
return parsePrice(priceInfo.tax_info?.tax_amount);
|
|
1309
|
+
}
|
|
1310
|
+
function isTaxInclusive(priceInfo) {
|
|
1311
|
+
return priceInfo.tax_info?.is_inclusive === true;
|
|
1312
|
+
}
|
|
1313
|
+
function formatPriceWithTax(priceInfo, currency = "GHS") {
|
|
1314
|
+
const finalPrice = formatPrice(priceInfo.final_price, currency);
|
|
1315
|
+
if (!hasTaxInfo(priceInfo)) {
|
|
1316
|
+
return finalPrice;
|
|
1317
|
+
}
|
|
1318
|
+
if (isTaxInclusive(priceInfo)) {
|
|
1319
|
+
return `${finalPrice} (incl. tax)`;
|
|
1320
|
+
}
|
|
1321
|
+
return `${finalPrice} + ${formatPrice(getTaxAmount(priceInfo), currency)} tax`;
|
|
1322
|
+
}
|
|
985
1323
|
function getDisplayPrice(product) {
|
|
986
1324
|
if (product.price_info) {
|
|
987
1325
|
return parsePrice(product.price_info.final_price);
|
|
@@ -1844,6 +2182,14 @@ async function safe3(promise) {
|
|
|
1844
2182
|
return err(toCimplifyError3(error));
|
|
1845
2183
|
}
|
|
1846
2184
|
}
|
|
2185
|
+
async function safeWithFallback3(primary, fallback) {
|
|
2186
|
+
const primaryResult = await safe3(primary());
|
|
2187
|
+
if (primaryResult.ok) return primaryResult;
|
|
2188
|
+
if (primaryResult.error.code !== "HTTP_404" && primaryResult.error.code !== "API_ERROR") {
|
|
2189
|
+
return primaryResult;
|
|
2190
|
+
}
|
|
2191
|
+
return safe3(fallback());
|
|
2192
|
+
}
|
|
1847
2193
|
function toTerminalFailure(code, message, recoverable) {
|
|
1848
2194
|
return {
|
|
1849
2195
|
success: false,
|
|
@@ -1871,8 +2217,11 @@ var CheckoutService = class {
|
|
|
1871
2217
|
...data,
|
|
1872
2218
|
idempotency_key: data.idempotency_key || generateIdempotencyKey()
|
|
1873
2219
|
};
|
|
1874
|
-
return
|
|
1875
|
-
this.client.
|
|
2220
|
+
return safeWithFallback3(
|
|
2221
|
+
() => this.client.post("/api/v1/checkout", {
|
|
2222
|
+
checkout_data: checkoutData
|
|
2223
|
+
}),
|
|
2224
|
+
() => this.client.call(CHECKOUT_MUTATION.PROCESS, {
|
|
1876
2225
|
checkout_data: checkoutData
|
|
1877
2226
|
})
|
|
1878
2227
|
);
|
|
@@ -1886,18 +2235,23 @@ var CheckoutService = class {
|
|
|
1886
2235
|
);
|
|
1887
2236
|
}
|
|
1888
2237
|
async submitAuthorization(input) {
|
|
1889
|
-
return
|
|
1890
|
-
this.client.
|
|
2238
|
+
return safeWithFallback3(
|
|
2239
|
+
() => this.client.post("/api/v1/payments/authorization", input),
|
|
2240
|
+
() => this.client.call(PAYMENT_MUTATION.SUBMIT_AUTHORIZATION, input)
|
|
1891
2241
|
);
|
|
1892
2242
|
}
|
|
1893
2243
|
async pollPaymentStatus(orderId) {
|
|
1894
|
-
|
|
1895
|
-
|
|
2244
|
+
const encodedId = encodeURIComponent(orderId);
|
|
2245
|
+
return safeWithFallback3(
|
|
2246
|
+
() => this.client.get(`/api/v1/orders/${encodedId}/payment-status`),
|
|
2247
|
+
() => this.client.call(PAYMENT_MUTATION.CHECK_STATUS, orderId)
|
|
1896
2248
|
);
|
|
1897
2249
|
}
|
|
1898
2250
|
async updateOrderCustomer(orderId, customer) {
|
|
1899
|
-
|
|
1900
|
-
|
|
2251
|
+
const encodedId = encodeURIComponent(orderId);
|
|
2252
|
+
return safeWithFallback3(
|
|
2253
|
+
() => this.client.post(`/api/v1/orders/${encodedId}/customer`, customer),
|
|
2254
|
+
() => this.client.call(ORDER_MUTATION.UPDATE_CUSTOMER, {
|
|
1901
2255
|
order_id: orderId,
|
|
1902
2256
|
...customer
|
|
1903
2257
|
})
|
|
@@ -2027,6 +2381,14 @@ async function safe4(promise) {
|
|
|
2027
2381
|
return err(toCimplifyError4(error));
|
|
2028
2382
|
}
|
|
2029
2383
|
}
|
|
2384
|
+
async function safeWithFallback4(primary, fallback) {
|
|
2385
|
+
const primaryResult = await safe4(primary());
|
|
2386
|
+
if (primaryResult.ok) return primaryResult;
|
|
2387
|
+
if (primaryResult.error.code !== "HTTP_404" && primaryResult.error.code !== "API_ERROR") {
|
|
2388
|
+
return primaryResult;
|
|
2389
|
+
}
|
|
2390
|
+
return safe4(fallback());
|
|
2391
|
+
}
|
|
2030
2392
|
var OrderQueries = class {
|
|
2031
2393
|
constructor(client) {
|
|
2032
2394
|
this.client = client;
|
|
@@ -2043,20 +2405,36 @@ var OrderQueries = class {
|
|
|
2043
2405
|
if (options?.offset) {
|
|
2044
2406
|
query2 += `#offset(${options.offset})`;
|
|
2045
2407
|
}
|
|
2046
|
-
|
|
2408
|
+
const params = new URLSearchParams();
|
|
2409
|
+
if (options?.status) params.set("status", options.status);
|
|
2410
|
+
if (options?.limit) params.set("limit", String(options.limit));
|
|
2411
|
+
if (options?.offset) params.set("offset", String(options.offset));
|
|
2412
|
+
const path = params.toString() ? `/api/v1/orders?${params.toString()}` : "/api/v1/orders";
|
|
2413
|
+
return safeWithFallback4(
|
|
2414
|
+
() => this.client.get(path),
|
|
2415
|
+
() => this.client.query(query2)
|
|
2416
|
+
);
|
|
2047
2417
|
}
|
|
2048
2418
|
async get(orderId) {
|
|
2049
|
-
|
|
2419
|
+
const encodedId = encodeURIComponent(orderId);
|
|
2420
|
+
return safeWithFallback4(
|
|
2421
|
+
() => this.client.get(`/api/v1/orders/${encodedId}`),
|
|
2422
|
+
() => this.client.query(`orders.${orderId}`)
|
|
2423
|
+
);
|
|
2050
2424
|
}
|
|
2051
2425
|
async getRecent(limit = 5) {
|
|
2052
2426
|
return safe4(this.client.query(`orders#sort(created_at,desc)#limit(${limit})`));
|
|
2053
2427
|
}
|
|
2054
2428
|
async getByStatus(status) {
|
|
2055
|
-
return
|
|
2429
|
+
return this.list({ status });
|
|
2056
2430
|
}
|
|
2057
2431
|
async cancel(orderId, reason) {
|
|
2058
|
-
|
|
2059
|
-
|
|
2432
|
+
const encodedId = encodeURIComponent(orderId);
|
|
2433
|
+
return safeWithFallback4(
|
|
2434
|
+
() => this.client.post(`/api/v1/orders/${encodedId}/cancel`, {
|
|
2435
|
+
reason
|
|
2436
|
+
}),
|
|
2437
|
+
() => this.client.call("order.cancelOrder", {
|
|
2060
2438
|
order_id: orderId,
|
|
2061
2439
|
reason
|
|
2062
2440
|
})
|
|
@@ -2231,12 +2609,23 @@ async function safe6(promise) {
|
|
|
2231
2609
|
return err(toCimplifyError6(error));
|
|
2232
2610
|
}
|
|
2233
2611
|
}
|
|
2612
|
+
async function safeWithFallback5(primary, fallback) {
|
|
2613
|
+
const primaryResult = await safe6(primary());
|
|
2614
|
+
if (primaryResult.ok) return primaryResult;
|
|
2615
|
+
if (primaryResult.error.code !== "HTTP_404" && primaryResult.error.code !== "API_ERROR") {
|
|
2616
|
+
return primaryResult;
|
|
2617
|
+
}
|
|
2618
|
+
return safe6(fallback());
|
|
2619
|
+
}
|
|
2234
2620
|
var AuthService = class {
|
|
2235
2621
|
constructor(client) {
|
|
2236
2622
|
this.client = client;
|
|
2237
2623
|
}
|
|
2238
2624
|
async getStatus() {
|
|
2239
|
-
return
|
|
2625
|
+
return safeWithFallback5(
|
|
2626
|
+
() => this.client.get("/api/v1/auth/status"),
|
|
2627
|
+
() => this.client.query("auth")
|
|
2628
|
+
);
|
|
2240
2629
|
}
|
|
2241
2630
|
async getCurrentUser() {
|
|
2242
2631
|
const result = await this.getStatus();
|
|
@@ -2249,23 +2638,34 @@ var AuthService = class {
|
|
|
2249
2638
|
return ok(result.value.is_authenticated);
|
|
2250
2639
|
}
|
|
2251
2640
|
async requestOtp(contact, contactType) {
|
|
2252
|
-
return
|
|
2253
|
-
this.client.
|
|
2641
|
+
return safeWithFallback5(
|
|
2642
|
+
() => this.client.post("/api/v1/auth/request-otp", {
|
|
2643
|
+
contact,
|
|
2644
|
+
contact_type: contactType
|
|
2645
|
+
}),
|
|
2646
|
+
() => this.client.call(AUTH_MUTATION.REQUEST_OTP, {
|
|
2254
2647
|
contact,
|
|
2255
2648
|
contact_type: contactType
|
|
2256
2649
|
})
|
|
2257
2650
|
);
|
|
2258
2651
|
}
|
|
2259
2652
|
async verifyOtp(code, contact) {
|
|
2260
|
-
return
|
|
2261
|
-
this.client.
|
|
2653
|
+
return safeWithFallback5(
|
|
2654
|
+
() => this.client.post("/api/v1/auth/verify-otp", {
|
|
2655
|
+
otp_code: code,
|
|
2656
|
+
contact
|
|
2657
|
+
}),
|
|
2658
|
+
() => this.client.call(AUTH_MUTATION.VERIFY_OTP, {
|
|
2262
2659
|
otp_code: code,
|
|
2263
2660
|
contact
|
|
2264
2661
|
})
|
|
2265
2662
|
);
|
|
2266
2663
|
}
|
|
2267
2664
|
async logout() {
|
|
2268
|
-
return
|
|
2665
|
+
return safeWithFallback5(
|
|
2666
|
+
() => this.client.post("/api/v1/auth/logout"),
|
|
2667
|
+
() => this.client.call("auth.logout")
|
|
2668
|
+
);
|
|
2269
2669
|
}
|
|
2270
2670
|
async updateProfile(input) {
|
|
2271
2671
|
return safe6(this.client.call("auth.update_profile", input));
|
|
@@ -2293,12 +2693,23 @@ async function safe7(promise) {
|
|
|
2293
2693
|
return err(toCimplifyError7(error));
|
|
2294
2694
|
}
|
|
2295
2695
|
}
|
|
2696
|
+
async function safeWithFallback6(primary, fallback) {
|
|
2697
|
+
const primaryResult = await safe7(primary());
|
|
2698
|
+
if (primaryResult.ok) return primaryResult;
|
|
2699
|
+
if (primaryResult.error.code !== "HTTP_404" && primaryResult.error.code !== "API_ERROR") {
|
|
2700
|
+
return primaryResult;
|
|
2701
|
+
}
|
|
2702
|
+
return safe7(fallback());
|
|
2703
|
+
}
|
|
2296
2704
|
var BusinessService = class {
|
|
2297
2705
|
constructor(client) {
|
|
2298
2706
|
this.client = client;
|
|
2299
2707
|
}
|
|
2300
2708
|
async getInfo() {
|
|
2301
|
-
return
|
|
2709
|
+
return safeWithFallback6(
|
|
2710
|
+
() => this.client.get("/api/v1/business"),
|
|
2711
|
+
() => this.client.query("business.info")
|
|
2712
|
+
);
|
|
2302
2713
|
}
|
|
2303
2714
|
async getByHandle(handle) {
|
|
2304
2715
|
return safe7(this.client.query(`business.handle.${handle}`));
|
|
@@ -2307,24 +2718,48 @@ var BusinessService = class {
|
|
|
2307
2718
|
return safe7(this.client.query("business.domain", { domain }));
|
|
2308
2719
|
}
|
|
2309
2720
|
async getSettings() {
|
|
2310
|
-
return
|
|
2721
|
+
return safeWithFallback6(
|
|
2722
|
+
() => this.client.get("/api/v1/business/settings"),
|
|
2723
|
+
() => this.client.query("business.settings")
|
|
2724
|
+
);
|
|
2311
2725
|
}
|
|
2312
2726
|
async getTheme() {
|
|
2313
|
-
return
|
|
2727
|
+
return safeWithFallback6(
|
|
2728
|
+
() => this.client.get("/api/v1/business/theme"),
|
|
2729
|
+
() => this.client.query("business.theme")
|
|
2730
|
+
);
|
|
2314
2731
|
}
|
|
2315
2732
|
async getLocations() {
|
|
2316
|
-
return
|
|
2733
|
+
return safeWithFallback6(
|
|
2734
|
+
() => this.client.get("/api/v1/business/locations"),
|
|
2735
|
+
() => this.client.query("business.locations")
|
|
2736
|
+
);
|
|
2317
2737
|
}
|
|
2318
2738
|
async getLocation(locationId) {
|
|
2319
|
-
|
|
2739
|
+
const result = await this.getLocations();
|
|
2740
|
+
if (!result.ok) return err(result.error);
|
|
2741
|
+
const location = result.value.find((item) => item.id === locationId);
|
|
2742
|
+
if (!location) {
|
|
2743
|
+
return err(new CimplifyError(ErrorCode.NOT_FOUND, `Location not found: ${locationId}`, false));
|
|
2744
|
+
}
|
|
2745
|
+
return ok(location);
|
|
2320
2746
|
}
|
|
2321
2747
|
async getHours() {
|
|
2322
|
-
return
|
|
2748
|
+
return safeWithFallback6(
|
|
2749
|
+
() => this.client.get("/api/v1/business/hours"),
|
|
2750
|
+
() => this.client.query("business.hours")
|
|
2751
|
+
);
|
|
2323
2752
|
}
|
|
2324
2753
|
async getLocationHours(locationId) {
|
|
2325
|
-
|
|
2754
|
+
const result = await this.getHours();
|
|
2755
|
+
if (!result.ok) return result;
|
|
2756
|
+
return ok(result.value.filter((hour) => hour.location_id === locationId));
|
|
2326
2757
|
}
|
|
2327
2758
|
async getBootstrap() {
|
|
2759
|
+
const restBootstrap = await safe7(this.client.get("/api/v1/bootstrap"));
|
|
2760
|
+
if (restBootstrap.ok) {
|
|
2761
|
+
return restBootstrap;
|
|
2762
|
+
}
|
|
2328
2763
|
const [businessResult, locationsResult, categoriesResult] = await Promise.all([
|
|
2329
2764
|
this.getInfo(),
|
|
2330
2765
|
this.getLocations(),
|
|
@@ -2617,15 +3052,30 @@ async function safe11(promise) {
|
|
|
2617
3052
|
return err(toCimplifyError11(error));
|
|
2618
3053
|
}
|
|
2619
3054
|
}
|
|
3055
|
+
async function safeWithFallback7(primary, fallback) {
|
|
3056
|
+
const primaryResult = await safe11(primary());
|
|
3057
|
+
if (primaryResult.ok) return primaryResult;
|
|
3058
|
+
if (primaryResult.error.code !== "HTTP_404" && primaryResult.error.code !== "API_ERROR") {
|
|
3059
|
+
return primaryResult;
|
|
3060
|
+
}
|
|
3061
|
+
return safe11(fallback());
|
|
3062
|
+
}
|
|
2620
3063
|
var FxService = class {
|
|
2621
3064
|
constructor(client) {
|
|
2622
3065
|
this.client = client;
|
|
2623
3066
|
}
|
|
2624
3067
|
async getRate(from, to) {
|
|
2625
|
-
|
|
3068
|
+
const path = `/api/v1/fx/rate?from=${encodeURIComponent(from)}&to=${encodeURIComponent(to)}`;
|
|
3069
|
+
return safeWithFallback7(
|
|
3070
|
+
() => this.client.get(path),
|
|
3071
|
+
() => this.client.call("fx.getRate", { from, to })
|
|
3072
|
+
);
|
|
2626
3073
|
}
|
|
2627
3074
|
async lockQuote(request) {
|
|
2628
|
-
return
|
|
3075
|
+
return safeWithFallback7(
|
|
3076
|
+
() => this.client.post("/api/v1/fx/quotes", request),
|
|
3077
|
+
() => this.client.call("fx.lockQuote", request)
|
|
3078
|
+
);
|
|
2629
3079
|
}
|
|
2630
3080
|
};
|
|
2631
3081
|
|
|
@@ -3633,6 +4083,15 @@ var CimplifyClient = class {
|
|
|
3633
4083
|
});
|
|
3634
4084
|
return this.handleRestResponse(response);
|
|
3635
4085
|
}
|
|
4086
|
+
async patch(path, body) {
|
|
4087
|
+
const response = await this.resilientFetch(`${this.baseUrl}${path}`, {
|
|
4088
|
+
method: "PATCH",
|
|
4089
|
+
credentials: this.credentials,
|
|
4090
|
+
headers: this.getHeaders(),
|
|
4091
|
+
body: body ? JSON.stringify(body) : void 0
|
|
4092
|
+
});
|
|
4093
|
+
return this.handleRestResponse(response);
|
|
4094
|
+
}
|
|
3636
4095
|
async delete(path) {
|
|
3637
4096
|
const response = await this.resilientFetch(`${this.baseUrl}${path}`, {
|
|
3638
4097
|
method: "DELETE",
|
|
@@ -3672,11 +4131,14 @@ var CimplifyClient = class {
|
|
|
3672
4131
|
async handleRestResponse(response) {
|
|
3673
4132
|
const json = await response.json();
|
|
3674
4133
|
if (!response.ok) {
|
|
4134
|
+
const errorCode = typeof json.error === "object" && json.error?.error_code || typeof json.error === "object" && json.error?.code || "API_ERROR";
|
|
4135
|
+
const errorMessage = typeof json.error === "object" && json.error?.error_message || typeof json.error === "object" && json.error?.message || typeof json.error === "string" && json.error || typeof json.message === "string" && json.message || "An error occurred";
|
|
4136
|
+
const retryable = typeof json.error === "object" && typeof json.error?.retryable === "boolean" ? json.error.retryable : false;
|
|
3675
4137
|
const error = enrichError(
|
|
3676
4138
|
new CimplifyError(
|
|
3677
|
-
|
|
3678
|
-
|
|
3679
|
-
|
|
4139
|
+
errorCode,
|
|
4140
|
+
errorMessage,
|
|
4141
|
+
retryable
|
|
3680
4142
|
),
|
|
3681
4143
|
{ isTestMode: this.isTestMode() }
|
|
3682
4144
|
);
|
|
@@ -3687,7 +4149,21 @@ var CimplifyClient = class {
|
|
|
3687
4149
|
}
|
|
3688
4150
|
throw error;
|
|
3689
4151
|
}
|
|
3690
|
-
|
|
4152
|
+
if (json?.success === false || json?.error?.code || json?.error?.message) {
|
|
4153
|
+
const error = enrichError(
|
|
4154
|
+
new CimplifyError(
|
|
4155
|
+
json.error?.code || "API_ERROR",
|
|
4156
|
+
json.error?.message || "An error occurred",
|
|
4157
|
+
json.error?.retryable || false
|
|
4158
|
+
),
|
|
4159
|
+
{ isTestMode: this.isTestMode() }
|
|
4160
|
+
);
|
|
4161
|
+
throw error;
|
|
4162
|
+
}
|
|
4163
|
+
if (json?.data !== void 0) {
|
|
4164
|
+
return json.data;
|
|
4165
|
+
}
|
|
4166
|
+
return json;
|
|
3691
4167
|
}
|
|
3692
4168
|
async handleResponse(response) {
|
|
3693
4169
|
const json = await response.json();
|
|
@@ -3800,4 +4276,4 @@ function createCimplifyClient(config = {}) {
|
|
|
3800
4276
|
return new CimplifyClient(config);
|
|
3801
4277
|
}
|
|
3802
4278
|
|
|
3803
|
-
export { AUTHORIZATION_TYPE, AUTH_MUTATION, AuthService, BusinessService, CHECKOUT_MODE, CHECKOUT_MUTATION, CHECKOUT_STEP, CONTACT_TYPE, CURRENCY_SYMBOLS, CartOperations, CatalogueQueries, CheckoutService as CheckoutOperations, CheckoutService, CimplifyClient, CimplifyElement, CimplifyElements, CimplifyError, DEFAULT_COUNTRY, DEFAULT_CURRENCY, DEVICE_TYPE, ELEMENT_TYPES, ERROR_HINTS, EVENT_TYPES, ErrorCode, FxService, InventoryService, LINK_MUTATION, LINK_QUERY, LinkService, LiteService, MESSAGE_TYPES, MOBILE_MONEY_PROVIDER, MOBILE_MONEY_PROVIDERS, ORDER_MUTATION, ORDER_TYPE, OrderQueries, PAYMENT_METHOD, PAYMENT_MUTATION, PAYMENT_STATE, PICKUP_TIME_TYPE, QueryBuilder, SchedulingService, ZERO, categorizePaymentError, combine, combineObject, createCimplifyClient, createElements, currencyCode, detectMobileMoneyProvider, enrichError, err, flatMap, formatMoney, formatNumberCompact, formatPrice, formatPriceAdjustment, formatPriceCompact, formatProductPrice, fromPromise, generateIdempotencyKey, getBasePrice, getCurrencySymbol, getDiscountPercentage, getDisplayPrice, getErrorHint, getMarkupPercentage, getOrElse, getProductCurrency, isCimplifyError, isErr, isOk, isOnSale, isPaymentStatusFailure, isPaymentStatusRequiresAction, isPaymentStatusSuccess, isRetryableError, mapError, mapResult, money, moneyFromNumber, normalizePaymentResponse, normalizeStatusResponse, ok, parsePrice, query, toNullable, tryCatch, unwrap };
|
|
4279
|
+
export { AUTHORIZATION_TYPE, AUTH_MUTATION, AuthService, BusinessService, CHECKOUT_MODE, CHECKOUT_MUTATION, CHECKOUT_STEP, CONTACT_TYPE, CURRENCY_SYMBOLS, CartOperations, CatalogueQueries, CheckoutService as CheckoutOperations, CheckoutService, CimplifyClient, CimplifyElement, CimplifyElements, CimplifyError, DEFAULT_COUNTRY, DEFAULT_CURRENCY, DEVICE_TYPE, ELEMENT_TYPES, ERROR_HINTS, EVENT_TYPES, ErrorCode, FxService, InventoryService, LINK_MUTATION, LINK_QUERY, LinkService, LiteService, MESSAGE_TYPES, MOBILE_MONEY_PROVIDER, MOBILE_MONEY_PROVIDERS, ORDER_MUTATION, ORDER_TYPE, OrderQueries, PAYMENT_METHOD, PAYMENT_MUTATION, PAYMENT_STATE, PICKUP_TIME_TYPE, QueryBuilder, SchedulingService, ZERO, categorizePaymentError, combine, combineObject, createCimplifyClient, createElements, currencyCode, detectMobileMoneyProvider, enrichError, err, flatMap, formatMoney, formatNumberCompact, formatPrice, formatPriceAdjustment, formatPriceCompact, formatPriceWithTax, formatProductPrice, fromPromise, generateIdempotencyKey, getBasePrice, getCurrencySymbol, getDiscountPercentage, getDisplayPrice, getErrorHint, getMarkupPercentage, getOrElse, getProductCurrency, getTaxAmount, hasTaxInfo, isCimplifyError, isErr, isOk, isOnSale, isPaymentStatusFailure, isPaymentStatusRequiresAction, isPaymentStatusSuccess, isRetryableError, isTaxInclusive, mapError, mapResult, money, moneyFromNumber, normalizePaymentResponse, normalizeStatusResponse, ok, parsePrice, query, toNullable, tryCatch, unwrap };
|