@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/dist/advanced.js CHANGED
@@ -20,7 +20,8 @@ function currencyCode(value) {
20
20
  }
21
21
  var ErrorCode = {
22
22
  // General
23
- UNKNOWN_ERROR: "UNKNOWN_ERROR"};
23
+ UNKNOWN_ERROR: "UNKNOWN_ERROR",
24
+ NOT_FOUND: "NOT_FOUND"};
24
25
  var DOCS_ERROR_BASE_URL = "https://docs.cimplify.io/reference/error-codes";
25
26
  function docsUrlForCode(code) {
26
27
  return `${DOCS_ERROR_BASE_URL}#${code.toLowerCase().replace(/_/g, "-")}`;
@@ -44,6 +45,10 @@ var ERROR_SUGGESTIONS = {
44
45
  CHECKOUT_VALIDATION_FAILED: "Checkout payload failed validation. Verify customer, order type, and address fields are complete.",
45
46
  DELIVERY_ADDRESS_REQUIRED: "Delivery orders require an address. Collect and pass address info before processing checkout.",
46
47
  CUSTOMER_INFO_REQUIRED: "Customer details are required. Ensure name/email/phone are available before checkout.",
48
+ QUOTE_NOT_FOUND: "Quote could not be found. Refresh pricing and create a new quote before checkout.",
49
+ QUOTE_EXPIRED: "Quote has expired. Re-fetch pricing to generate a new quote with a valid expiry window.",
50
+ QUOTE_CONSUMED: "Quote has already been used. Request a fresh quote to prevent duplicate checkout attempts.",
51
+ QUOTE_STORAGE_UNAVAILABLE: "Quote storage is temporarily unavailable. Retry shortly and avoid charging until quote fetch succeeds.",
47
52
  PAYMENT_FAILED: "Payment provider rejected or failed processing. Show retry/change-method options to the shopper.",
48
53
  PAYMENT_CANCELLED: "Payment was cancelled by the shopper or provider flow. Allow a safe retry path.",
49
54
  INSUFFICIENT_FUNDS: "Payment method has insufficient funds. Prompt shopper to use another method.",
@@ -200,6 +205,23 @@ async function safe(promise) {
200
205
  return err(toCimplifyError(error));
201
206
  }
202
207
  }
208
+ async function safeWithFallback(primary, fallback) {
209
+ const primaryResult = await safe(primary());
210
+ if (primaryResult.ok) return primaryResult;
211
+ if (primaryResult.error.code !== "HTTP_404" && primaryResult.error.code !== "API_ERROR") {
212
+ return primaryResult;
213
+ }
214
+ return safe(fallback());
215
+ }
216
+ function withQuery(path, params) {
217
+ const query2 = new URLSearchParams();
218
+ for (const [key, value] of Object.entries(params)) {
219
+ if (value === void 0) continue;
220
+ query2.set(key, String(value));
221
+ }
222
+ const queryString = query2.toString();
223
+ return queryString ? `${path}?${queryString}` : path;
224
+ }
203
225
  function isRecord(value) {
204
226
  return typeof value === "object" && value !== null;
205
227
  }
@@ -257,6 +279,69 @@ function findProductBySlug(products, slug) {
257
279
  return typeof value === "string" && value === slug;
258
280
  });
259
281
  }
282
+ function findCategoryBySlug(categories, slug) {
283
+ return categories.find((category) => {
284
+ const value = category["slug"];
285
+ return typeof value === "string" && value === slug;
286
+ });
287
+ }
288
+ function hasCategorySlug(category) {
289
+ const value = category["slug"];
290
+ return typeof value === "string" && value.trim().length > 0;
291
+ }
292
+ function toFiniteNumber(value) {
293
+ if (typeof value === "number" && Number.isFinite(value)) {
294
+ return value;
295
+ }
296
+ if (typeof value === "string" && value.trim().length > 0) {
297
+ const parsed = Number(value);
298
+ if (Number.isFinite(parsed)) {
299
+ return parsed;
300
+ }
301
+ }
302
+ return void 0;
303
+ }
304
+ function normalizePagination(value) {
305
+ if (!isRecord(value)) {
306
+ return void 0;
307
+ }
308
+ const totalCount = toFiniteNumber(value.total_count);
309
+ const currentPage = toFiniteNumber(value.current_page);
310
+ const pageSize = toFiniteNumber(value.page_size);
311
+ const totalPages = toFiniteNumber(value.total_pages);
312
+ if (totalCount === void 0 || currentPage === void 0 || pageSize === void 0 || totalPages === void 0) {
313
+ return void 0;
314
+ }
315
+ return {
316
+ total_count: totalCount,
317
+ current_page: currentPage,
318
+ page_size: pageSize,
319
+ total_pages: totalPages,
320
+ has_more: value.has_more === true,
321
+ next_cursor: typeof value.next_cursor === "string" ? value.next_cursor : void 0
322
+ };
323
+ }
324
+ function normalizeCatalogueResult(payload) {
325
+ if (Array.isArray(payload)) {
326
+ return {
327
+ items: payload.map((product) => normalizeCatalogueProductPayload(product)),
328
+ is_complete: true
329
+ };
330
+ }
331
+ if (!isRecord(payload)) {
332
+ return {
333
+ items: [],
334
+ is_complete: true
335
+ };
336
+ }
337
+ const rawItems = Array.isArray(payload.products) ? payload.products : Array.isArray(payload.items) ? payload.items : [];
338
+ return {
339
+ items: rawItems.map((product) => normalizeCatalogueProductPayload(product)),
340
+ is_complete: typeof payload.is_complete === "boolean" ? payload.is_complete : true,
341
+ total_available: toFiniteNumber(payload.total_available),
342
+ pagination: normalizePagination(payload.pagination)
343
+ };
344
+ }
260
345
  var CatalogueQueries = class {
261
346
  constructor(client) {
262
347
  this.client = client;
@@ -276,6 +361,13 @@ var CatalogueQueries = class {
276
361
  if (options?.search) {
277
362
  filters.push(`@.name contains '${escapeQueryValue(options.search)}'`);
278
363
  }
364
+ if (options?.tags?.length) {
365
+ for (const tag of options.tags) {
366
+ if (tag.trim().length > 0) {
367
+ filters.push(`@.tags contains '${escapeQueryValue(tag)}'`);
368
+ }
369
+ }
370
+ }
279
371
  if (options?.min_price !== void 0) {
280
372
  filters.push(`@.price>=${options.min_price}`);
281
373
  }
@@ -288,25 +380,57 @@ var CatalogueQueries = class {
288
380
  if (options?.sort_by) {
289
381
  query2 += `#sort(${options.sort_by},${options.sort_order || "asc"})`;
290
382
  }
291
- if (options?.limit) {
383
+ if (options?.limit !== void 0) {
292
384
  query2 += `#limit(${options.limit})`;
293
385
  }
294
- if (options?.offset) {
386
+ if (options?.offset !== void 0) {
295
387
  query2 += `#offset(${options.offset})`;
296
388
  }
297
- const result = await safe(this.client.query(query2));
389
+ const path = withQuery("/api/v1/catalogue/products", {
390
+ category_id: options?.category,
391
+ search: options?.search,
392
+ page: options?.page,
393
+ tags: options?.tags?.join(","),
394
+ featured: options?.featured,
395
+ in_stock: options?.in_stock,
396
+ min_price: options?.min_price,
397
+ max_price: options?.max_price,
398
+ sort_by: options?.sort_by,
399
+ sort_order: options?.sort_order,
400
+ limit: options?.limit,
401
+ offset: options?.offset,
402
+ cursor: options?.cursor
403
+ });
404
+ const result = await safeWithFallback(
405
+ () => this.client.get(path),
406
+ () => this.client.query(query2)
407
+ );
298
408
  if (!result.ok) return result;
299
- return ok(result.value.map((product) => normalizeCatalogueProductPayload(product)));
409
+ return ok(normalizeCatalogueResult(result.value));
300
410
  }
301
411
  async getProduct(id) {
302
- const result = await safe(this.client.query(`products.${id}`));
412
+ const encodedId = encodeURIComponent(id);
413
+ const result = await safeWithFallback(
414
+ () => this.client.get(`/api/v1/catalogue/products/${encodedId}`),
415
+ () => this.client.query(`products.${id}`)
416
+ );
303
417
  if (!result.ok) return result;
304
418
  return ok(normalizeCatalogueProductPayload(result.value));
305
419
  }
306
420
  async getProductBySlug(slug) {
421
+ const encodedSlug = encodeURIComponent(slug);
422
+ const restResult = await safe(
423
+ this.client.get(`/api/v1/catalogue/products/slug/${encodedSlug}`)
424
+ );
425
+ if (restResult.ok) {
426
+ return ok(normalizeCatalogueProductPayload(restResult.value));
427
+ }
428
+ if (restResult.error.code !== "HTTP_404" && restResult.error.code !== "API_ERROR") {
429
+ return restResult;
430
+ }
307
431
  const filteredResult = await safe(
308
432
  this.client.query(
309
- `products[?(@.slug=='${escapeQueryValue(slug)}')]`
433
+ `products[?(@.slug=='${escapeQueryValue(slug)}')]#limit(50)`
310
434
  )
311
435
  );
312
436
  if (!filteredResult.ok) return filteredResult;
@@ -317,7 +441,9 @@ var CatalogueQueries = class {
317
441
  if (filteredResult.value.length === 1) {
318
442
  return ok(normalizeCatalogueProductPayload(filteredResult.value[0]));
319
443
  }
320
- const unfilteredResult = await safe(this.client.query("products"));
444
+ const unfilteredResult = await safe(
445
+ this.client.query("products#limit(200)")
446
+ );
321
447
  if (!unfilteredResult.ok) return unfilteredResult;
322
448
  const fallbackMatch = findProductBySlug(unfilteredResult.value, slug);
323
449
  if (!fallbackMatch) {
@@ -326,18 +452,33 @@ var CatalogueQueries = class {
326
452
  return ok(normalizeCatalogueProductPayload(fallbackMatch));
327
453
  }
328
454
  async getVariants(productId) {
329
- return safe(this.client.query(`products.${productId}.variants`));
455
+ const encodedId = encodeURIComponent(productId);
456
+ return safeWithFallback(
457
+ () => this.client.get(`/api/v1/catalogue/products/${encodedId}/variants`),
458
+ () => this.client.query(`products.${productId}.variants`)
459
+ );
330
460
  }
331
461
  async getVariantAxes(productId) {
332
- return safe(this.client.query(`products.${productId}.variant_axes`));
462
+ const encodedId = encodeURIComponent(productId);
463
+ return safeWithFallback(
464
+ () => this.client.get(`/api/v1/catalogue/products/${encodedId}/variant-axes`),
465
+ () => this.client.query(`products.${productId}.variant_axes`)
466
+ );
333
467
  }
334
468
  /**
335
469
  * Find a variant by axis selections (e.g., { "Size": "Large", "Color": "Red" })
336
470
  * Returns the matching variant or null if no match found.
337
471
  */
338
472
  async getVariantByAxisSelections(productId, selections) {
339
- return safe(
340
- this.client.query(`products.${productId}.variant`, {
473
+ const encodedId = encodeURIComponent(productId);
474
+ return safeWithFallback(
475
+ () => this.client.post(
476
+ `/api/v1/catalogue/products/${encodedId}/variants/find`,
477
+ {
478
+ axis_selections: selections
479
+ }
480
+ ),
481
+ () => this.client.query(`products.${productId}.variant`, {
341
482
  axis_selections: selections
342
483
  })
343
484
  );
@@ -346,45 +487,107 @@ var CatalogueQueries = class {
346
487
  * Get a specific variant by its ID
347
488
  */
348
489
  async getVariantById(productId, variantId) {
349
- return safe(this.client.query(`products.${productId}.variant.${variantId}`));
490
+ const encodedProductId = encodeURIComponent(productId);
491
+ const encodedVariantId = encodeURIComponent(variantId);
492
+ return safeWithFallback(
493
+ () => this.client.get(
494
+ `/api/v1/catalogue/products/${encodedProductId}/variants/${encodedVariantId}`
495
+ ),
496
+ () => this.client.query(`products.${productId}.variant.${variantId}`)
497
+ );
350
498
  }
351
499
  async getAddOns(productId) {
352
- return safe(this.client.query(`products.${productId}.add_ons`));
500
+ const encodedId = encodeURIComponent(productId);
501
+ return safeWithFallback(
502
+ () => this.client.get(`/api/v1/catalogue/products/${encodedId}/add-ons`),
503
+ () => this.client.query(`products.${productId}.add_ons`)
504
+ );
353
505
  }
354
506
  async getCategories() {
355
- return safe(this.client.query("categories"));
507
+ const result = await safeWithFallback(
508
+ () => this.client.get("/api/v1/catalogue/categories"),
509
+ () => this.client.query("categories")
510
+ );
511
+ if (!result.ok) return result;
512
+ if (result.value.some(hasCategorySlug)) {
513
+ return result;
514
+ }
515
+ const catalogueResult = await safe(
516
+ this.client.query("catalogue#limit(1)")
517
+ );
518
+ if (!catalogueResult.ok) {
519
+ return result;
520
+ }
521
+ const fallbackCategories = Array.isArray(catalogueResult.value.categories) ? catalogueResult.value.categories : [];
522
+ return fallbackCategories.length > 0 ? ok(fallbackCategories) : result;
356
523
  }
357
524
  async getCategory(id) {
358
- return safe(this.client.query(`categories.${id}`));
525
+ const encodedId = encodeURIComponent(id);
526
+ return safeWithFallback(
527
+ () => this.client.get(`/api/v1/catalogue/categories/${encodedId}`),
528
+ () => this.client.query(`categories.${id}`)
529
+ );
359
530
  }
360
531
  async getCategoryBySlug(slug) {
532
+ const encodedSlug = encodeURIComponent(slug);
533
+ const restResult = await safe(this.client.get(`/api/v1/catalogue/categories/slug/${encodedSlug}`));
534
+ if (restResult.ok) {
535
+ return restResult;
536
+ }
537
+ if (restResult.error.code !== "HTTP_404" && restResult.error.code !== "API_ERROR") {
538
+ return restResult;
539
+ }
361
540
  const result = await safe(
362
541
  this.client.query(`categories[?(@.slug=='${escapeQueryValue(slug)}')]`)
363
542
  );
364
543
  if (!result.ok) return result;
365
- if (!result.value.length) {
544
+ const exactMatch = findCategoryBySlug(result.value, slug);
545
+ if (exactMatch) {
546
+ return ok(exactMatch);
547
+ }
548
+ const categoriesResult = await this.getCategories();
549
+ if (!categoriesResult.ok) {
550
+ return categoriesResult;
551
+ }
552
+ const fallbackMatch = findCategoryBySlug(categoriesResult.value, slug);
553
+ if (!fallbackMatch) {
366
554
  return err(new CimplifyError("NOT_FOUND", `Category not found: ${slug}`, false));
367
555
  }
368
- return ok(result.value[0]);
556
+ return ok(fallbackMatch);
369
557
  }
370
558
  async getCategoryProducts(categoryId) {
371
- return safe(
372
- this.client.query(
559
+ const encodedId = encodeURIComponent(categoryId);
560
+ return safeWithFallback(
561
+ () => this.client.get(`/api/v1/catalogue/categories/${encodedId}/products`),
562
+ () => this.client.query(
373
563
  `products[?(@.category_id=='${escapeQueryValue(categoryId)}')]`
374
564
  )
375
565
  );
376
566
  }
377
567
  async getCollections() {
378
- return safe(this.client.query("collections"));
568
+ return safeWithFallback(
569
+ () => this.client.get("/api/v1/catalogue/collections"),
570
+ () => this.client.query("collections")
571
+ );
379
572
  }
380
573
  async getCollection(id) {
381
- return safe(this.client.query(`collections.${id}`));
574
+ const encodedId = encodeURIComponent(id);
575
+ return safeWithFallback(
576
+ () => this.client.get(`/api/v1/catalogue/collections/${encodedId}`),
577
+ () => this.client.query(`collections.${id}`)
578
+ );
382
579
  }
383
580
  async getCollectionBySlug(slug) {
581
+ const encodedSlug = encodeURIComponent(slug);
582
+ const restResult = await safe(
583
+ this.client.get(`/api/v1/catalogue/collections/slug/${encodedSlug}`)
584
+ );
585
+ if (restResult.ok) return restResult;
586
+ if (restResult.error.code !== "HTTP_404" && restResult.error.code !== "API_ERROR") {
587
+ return restResult;
588
+ }
384
589
  const result = await safe(
385
- this.client.query(
386
- `collections[?(@.slug=='${escapeQueryValue(slug)}')]`
387
- )
590
+ this.client.query(`collections[?(@.slug=='${escapeQueryValue(slug)}')]`)
388
591
  );
389
592
  if (!result.ok) return result;
390
593
  if (!result.value.length) {
@@ -393,22 +596,43 @@ var CatalogueQueries = class {
393
596
  return ok(result.value[0]);
394
597
  }
395
598
  async getCollectionProducts(collectionId) {
396
- return safe(this.client.query(`collections.${collectionId}.products`));
599
+ const encodedId = encodeURIComponent(collectionId);
600
+ return safeWithFallback(
601
+ () => this.client.get(`/api/v1/catalogue/collections/${encodedId}/products`),
602
+ () => this.client.query(`collections.${collectionId}.products`)
603
+ );
397
604
  }
398
605
  async searchCollections(query2, limit = 20) {
399
- return safe(
400
- this.client.query(
606
+ const path = withQuery("/api/v1/catalogue/collections", { search: query2, limit });
607
+ return safeWithFallback(
608
+ () => this.client.get(path),
609
+ () => this.client.query(
401
610
  `collections[?(@.name contains '${escapeQueryValue(query2)}')]#limit(${limit})`
402
611
  )
403
612
  );
404
613
  }
405
614
  async getBundles() {
406
- return safe(this.client.query("bundles"));
615
+ return safeWithFallback(
616
+ () => this.client.get("/api/v1/catalogue/bundles"),
617
+ () => this.client.query("bundles")
618
+ );
407
619
  }
408
620
  async getBundle(id) {
409
- return safe(this.client.query(`bundles.${id}`));
621
+ const encodedId = encodeURIComponent(id);
622
+ return safeWithFallback(
623
+ () => this.client.get(`/api/v1/catalogue/bundles/${encodedId}`),
624
+ () => this.client.query(`bundles.${id}`)
625
+ );
410
626
  }
411
627
  async getBundleBySlug(slug) {
628
+ const encodedSlug = encodeURIComponent(slug);
629
+ const restResult = await safe(
630
+ this.client.get(`/api/v1/catalogue/bundles/slug/${encodedSlug}`)
631
+ );
632
+ if (restResult.ok) return restResult;
633
+ if (restResult.error.code !== "HTTP_404" && restResult.error.code !== "API_ERROR") {
634
+ return restResult;
635
+ }
412
636
  const result = await safe(
413
637
  this.client.query(
414
638
  `bundles[?(@.slug=='${escapeQueryValue(slug)}')]`
@@ -421,8 +645,10 @@ var CatalogueQueries = class {
421
645
  return ok(result.value[0]);
422
646
  }
423
647
  async searchBundles(query2, limit = 20) {
424
- return safe(
425
- this.client.query(
648
+ const path = withQuery("/api/v1/catalogue/bundles", { search: query2, limit });
649
+ return safeWithFallback(
650
+ () => this.client.get(path),
651
+ () => this.client.query(
426
652
  `bundles[?(@.name contains '${escapeQueryValue(query2)}')]#limit(${limit})`
427
653
  )
428
654
  );
@@ -432,17 +658,39 @@ var CatalogueQueries = class {
432
658
  if (options?.limit) {
433
659
  query2 += `#limit(${options.limit})`;
434
660
  }
435
- return safe(this.client.query(query2));
661
+ const path = withQuery("/api/v1/catalogue/composites", { limit: options?.limit });
662
+ return safeWithFallback(
663
+ () => this.client.get(path),
664
+ () => this.client.query(query2)
665
+ );
436
666
  }
437
667
  async getComposite(id) {
438
- return safe(this.client.query(`composites.${id}`));
668
+ const encodedId = encodeURIComponent(id);
669
+ return safeWithFallback(
670
+ () => this.client.get(`/api/v1/catalogue/composites/${encodedId}`),
671
+ () => this.client.query(`composites.${id}`)
672
+ );
439
673
  }
440
674
  async getCompositeByProductId(productId) {
441
- return safe(this.client.query(`composites.by_product.${productId}`));
675
+ const encodedId = encodeURIComponent(productId);
676
+ return safeWithFallback(
677
+ () => this.client.get(
678
+ `/api/v1/catalogue/composites/by-product/${encodedId}`
679
+ ),
680
+ () => this.client.query(`composites.by_product.${productId}`)
681
+ );
442
682
  }
443
683
  async calculateCompositePrice(compositeId, selections, locationId) {
444
- return safe(
445
- this.client.call("composite.calculatePrice", {
684
+ const encodedId = encodeURIComponent(compositeId);
685
+ return safeWithFallback(
686
+ () => this.client.post(
687
+ `/api/v1/catalogue/composites/${encodedId}/calculate-price`,
688
+ {
689
+ selections,
690
+ location_id: locationId
691
+ }
692
+ ),
693
+ () => this.client.call("composite.calculatePrice", {
446
694
  composite_id: compositeId,
447
695
  selections,
448
696
  location_id: locationId
@@ -450,35 +698,41 @@ var CatalogueQueries = class {
450
698
  );
451
699
  }
452
700
  async fetchQuote(input) {
453
- return safe(this.client.call("catalogue.createQuote", input));
701
+ return safeWithFallback(
702
+ () => this.client.post("/api/v1/catalogue/quotes", input),
703
+ () => this.client.call("catalogue.createQuote", input)
704
+ );
454
705
  }
455
706
  async getQuote(quoteId) {
456
- return safe(
457
- this.client.call("catalogue.getQuote", {
707
+ const encodedQuoteId = encodeURIComponent(quoteId);
708
+ return safeWithFallback(
709
+ () => this.client.get(`/api/v1/catalogue/quotes/${encodedQuoteId}`),
710
+ () => this.client.call("catalogue.getQuote", {
458
711
  quote_id: quoteId
459
712
  })
460
713
  );
461
714
  }
462
715
  async refreshQuote(input) {
463
- return safe(this.client.call("catalogue.refreshQuote", input));
716
+ const encodedQuoteId = encodeURIComponent(input.quote_id);
717
+ return safeWithFallback(
718
+ () => this.client.post(
719
+ `/api/v1/catalogue/quotes/${encodedQuoteId}/refresh`,
720
+ input
721
+ ),
722
+ () => this.client.call("catalogue.refreshQuote", input)
723
+ );
464
724
  }
465
725
  async search(query2, options) {
466
- const limit = options?.limit ?? 20;
467
- let searchQuery = `products[?(@.name contains '${escapeQueryValue(query2)}')]`;
468
- if (options?.category) {
469
- searchQuery = `products[?(@.name contains '${escapeQueryValue(query2)}' && @.category_id=='${escapeQueryValue(options.category)}')]`;
470
- }
471
- searchQuery += `#limit(${limit})`;
472
- return safe(this.client.query(searchQuery));
726
+ const result = await this.getProducts({
727
+ search: query2,
728
+ category: options?.category,
729
+ limit: options?.limit ?? 20
730
+ });
731
+ if (!result.ok) return result;
732
+ return ok(result.value.items);
473
733
  }
474
734
  async searchProducts(query2, options) {
475
- return safe(
476
- this.client.call("catalogue.search", {
477
- query: query2,
478
- limit: options?.limit ?? 20,
479
- category: options?.category
480
- })
481
- );
735
+ return this.search(query2, options);
482
736
  }
483
737
  async getMenu(options) {
484
738
  let query2 = "menu";
@@ -488,13 +742,28 @@ var CatalogueQueries = class {
488
742
  if (options?.limit) {
489
743
  query2 += `#limit(${options.limit})`;
490
744
  }
491
- return safe(this.client.query(query2));
745
+ const path = withQuery("/api/v1/catalogue/menu", {
746
+ category_id: options?.category,
747
+ limit: options?.limit
748
+ });
749
+ return safeWithFallback(
750
+ () => this.client.get(path),
751
+ () => this.client.query(query2)
752
+ );
492
753
  }
493
754
  async getMenuCategory(categoryId) {
494
- return safe(this.client.query(`menu.category.${categoryId}`));
755
+ const encodedId = encodeURIComponent(categoryId);
756
+ return safeWithFallback(
757
+ () => this.client.get(`/api/v1/catalogue/menu/categories/${encodedId}`),
758
+ () => this.client.query(`menu.category.${categoryId}`)
759
+ );
495
760
  }
496
761
  async getMenuItem(itemId) {
497
- return safe(this.client.query(`menu.${itemId}`));
762
+ const encodedId = encodeURIComponent(itemId);
763
+ return safeWithFallback(
764
+ () => this.client.get(`/api/v1/catalogue/menu/items/${encodedId}`),
765
+ () => this.client.query(`menu.${itemId}`)
766
+ );
498
767
  }
499
768
  };
500
769
 
@@ -513,6 +782,14 @@ async function safe2(promise) {
513
782
  return err(toCimplifyError2(error));
514
783
  }
515
784
  }
785
+ async function safeWithFallback2(primary, fallback) {
786
+ const primaryResult = await safe2(primary());
787
+ if (primaryResult.ok) return primaryResult;
788
+ if (primaryResult.error.code !== "HTTP_404" && primaryResult.error.code !== "API_ERROR") {
789
+ return primaryResult;
790
+ }
791
+ return safe2(fallback());
792
+ }
516
793
  function isUICartResponse(value) {
517
794
  return "cart" in value;
518
795
  }
@@ -524,21 +801,36 @@ var CartOperations = class {
524
801
  this.client = client;
525
802
  }
526
803
  async get() {
527
- const result = await safe2(this.client.query("cart#enriched"));
804
+ const result = await safeWithFallback2(
805
+ () => this.client.get("/api/v1/cart"),
806
+ () => this.client.query("cart#enriched")
807
+ );
528
808
  if (!result.ok) return result;
529
809
  return ok(unwrapEnrichedCart(result.value));
530
810
  }
531
811
  async getRaw() {
532
- return safe2(this.client.query("cart"));
812
+ return safeWithFallback2(
813
+ () => this.client.get("/api/v1/cart"),
814
+ () => this.client.query("cart")
815
+ );
533
816
  }
534
817
  async getItems() {
535
- return safe2(this.client.query("cart_items"));
818
+ return safeWithFallback2(
819
+ () => this.client.get("/api/v1/cart/items"),
820
+ () => this.client.query("cart_items")
821
+ );
536
822
  }
537
823
  async getCount() {
538
- return safe2(this.client.query("cart#count"));
824
+ return safeWithFallback2(
825
+ () => this.client.get("/api/v1/cart/count"),
826
+ () => this.client.query("cart#count")
827
+ );
539
828
  }
540
829
  async getTotal() {
541
- return safe2(this.client.query("cart#total"));
830
+ return safeWithFallback2(
831
+ () => this.client.get("/api/v1/cart/total"),
832
+ () => this.client.query("cart#total")
833
+ );
542
834
  }
543
835
  async getSummary() {
544
836
  const cartResult = await this.get();
@@ -555,43 +847,66 @@ var CartOperations = class {
555
847
  });
556
848
  }
557
849
  async addItem(input) {
558
- return safe2(this.client.call("cart.addItem", input));
850
+ return safeWithFallback2(
851
+ () => this.client.post("/api/v1/cart/items", input),
852
+ () => this.client.call("cart.addItem", input)
853
+ );
559
854
  }
560
855
  async updateItem(cartItemId, updates) {
561
- return safe2(
562
- this.client.call("cart.updateItem", {
856
+ if (typeof updates.quantity === "number") {
857
+ return this.updateQuantity(cartItemId, updates.quantity);
858
+ }
859
+ const encodedId = encodeURIComponent(cartItemId);
860
+ return safeWithFallback2(
861
+ () => this.client.patch(`/api/v1/cart/items/${encodedId}`, updates),
862
+ () => this.client.call("cart.updateItem", {
563
863
  cart_item_id: cartItemId,
564
864
  ...updates
565
865
  })
566
866
  );
567
867
  }
568
868
  async updateQuantity(cartItemId, quantity) {
569
- return safe2(
570
- this.client.call("cart.updateItemQuantity", {
869
+ const encodedId = encodeURIComponent(cartItemId);
870
+ return safeWithFallback2(
871
+ () => this.client.patch(`/api/v1/cart/items/${encodedId}`, {
872
+ quantity
873
+ }),
874
+ () => this.client.call("cart.updateItemQuantity", {
571
875
  cart_item_id: cartItemId,
572
876
  quantity
573
877
  })
574
878
  );
575
879
  }
576
880
  async removeItem(cartItemId) {
577
- return safe2(
578
- this.client.call("cart.removeItem", {
881
+ const encodedId = encodeURIComponent(cartItemId);
882
+ return safeWithFallback2(
883
+ () => this.client.delete(`/api/v1/cart/items/${encodedId}`),
884
+ () => this.client.call("cart.removeItem", {
579
885
  cart_item_id: cartItemId
580
886
  })
581
887
  );
582
888
  }
583
889
  async clear() {
584
- return safe2(this.client.call("cart.clearCart"));
890
+ return safeWithFallback2(
891
+ () => this.client.delete("/api/v1/cart"),
892
+ () => this.client.call("cart.clearCart")
893
+ );
585
894
  }
586
895
  async applyCoupon(code) {
587
- return safe2(
588
- this.client.call("cart.applyCoupon", {
896
+ return safeWithFallback2(
897
+ () => this.client.post("/api/v1/cart/coupons", {
898
+ coupon_code: code
899
+ }),
900
+ () => this.client.call("cart.applyCoupon", {
589
901
  coupon_code: code
590
902
  })
591
903
  );
592
904
  }
593
905
  async removeCoupon() {
594
- return safe2(this.client.call("cart.removeCoupon"));
906
+ return safeWithFallback2(
907
+ () => this.client.delete("/api/v1/cart/coupons/current"),
908
+ () => this.client.call("cart.removeCoupon")
909
+ );
595
910
  }
596
911
  async isEmpty() {
597
912
  const countResult = await this.getCount();
@@ -1351,6 +1666,14 @@ async function safe3(promise) {
1351
1666
  return err(toCimplifyError3(error));
1352
1667
  }
1353
1668
  }
1669
+ async function safeWithFallback3(primary, fallback) {
1670
+ const primaryResult = await safe3(primary());
1671
+ if (primaryResult.ok) return primaryResult;
1672
+ if (primaryResult.error.code !== "HTTP_404" && primaryResult.error.code !== "API_ERROR") {
1673
+ return primaryResult;
1674
+ }
1675
+ return safe3(fallback());
1676
+ }
1354
1677
  function toTerminalFailure(code, message, recoverable) {
1355
1678
  return {
1356
1679
  success: false,
@@ -1378,8 +1701,11 @@ var CheckoutService = class {
1378
1701
  ...data,
1379
1702
  idempotency_key: data.idempotency_key || generateIdempotencyKey()
1380
1703
  };
1381
- return safe3(
1382
- this.client.call(CHECKOUT_MUTATION.PROCESS, {
1704
+ return safeWithFallback3(
1705
+ () => this.client.post("/api/v1/checkout", {
1706
+ checkout_data: checkoutData
1707
+ }),
1708
+ () => this.client.call(CHECKOUT_MUTATION.PROCESS, {
1383
1709
  checkout_data: checkoutData
1384
1710
  })
1385
1711
  );
@@ -1393,18 +1719,23 @@ var CheckoutService = class {
1393
1719
  );
1394
1720
  }
1395
1721
  async submitAuthorization(input) {
1396
- return safe3(
1397
- this.client.call(PAYMENT_MUTATION.SUBMIT_AUTHORIZATION, input)
1722
+ return safeWithFallback3(
1723
+ () => this.client.post("/api/v1/payments/authorization", input),
1724
+ () => this.client.call(PAYMENT_MUTATION.SUBMIT_AUTHORIZATION, input)
1398
1725
  );
1399
1726
  }
1400
1727
  async pollPaymentStatus(orderId) {
1401
- return safe3(
1402
- this.client.call(PAYMENT_MUTATION.CHECK_STATUS, orderId)
1728
+ const encodedId = encodeURIComponent(orderId);
1729
+ return safeWithFallback3(
1730
+ () => this.client.get(`/api/v1/orders/${encodedId}/payment-status`),
1731
+ () => this.client.call(PAYMENT_MUTATION.CHECK_STATUS, orderId)
1403
1732
  );
1404
1733
  }
1405
1734
  async updateOrderCustomer(orderId, customer) {
1406
- return safe3(
1407
- this.client.call(ORDER_MUTATION.UPDATE_CUSTOMER, {
1735
+ const encodedId = encodeURIComponent(orderId);
1736
+ return safeWithFallback3(
1737
+ () => this.client.post(`/api/v1/orders/${encodedId}/customer`, customer),
1738
+ () => this.client.call(ORDER_MUTATION.UPDATE_CUSTOMER, {
1408
1739
  order_id: orderId,
1409
1740
  ...customer
1410
1741
  })
@@ -1534,6 +1865,14 @@ async function safe4(promise) {
1534
1865
  return err(toCimplifyError4(error));
1535
1866
  }
1536
1867
  }
1868
+ async function safeWithFallback4(primary, fallback) {
1869
+ const primaryResult = await safe4(primary());
1870
+ if (primaryResult.ok) return primaryResult;
1871
+ if (primaryResult.error.code !== "HTTP_404" && primaryResult.error.code !== "API_ERROR") {
1872
+ return primaryResult;
1873
+ }
1874
+ return safe4(fallback());
1875
+ }
1537
1876
  var OrderQueries = class {
1538
1877
  constructor(client) {
1539
1878
  this.client = client;
@@ -1550,20 +1889,36 @@ var OrderQueries = class {
1550
1889
  if (options?.offset) {
1551
1890
  query2 += `#offset(${options.offset})`;
1552
1891
  }
1553
- return safe4(this.client.query(query2));
1892
+ const params = new URLSearchParams();
1893
+ if (options?.status) params.set("status", options.status);
1894
+ if (options?.limit) params.set("limit", String(options.limit));
1895
+ if (options?.offset) params.set("offset", String(options.offset));
1896
+ const path = params.toString() ? `/api/v1/orders?${params.toString()}` : "/api/v1/orders";
1897
+ return safeWithFallback4(
1898
+ () => this.client.get(path),
1899
+ () => this.client.query(query2)
1900
+ );
1554
1901
  }
1555
1902
  async get(orderId) {
1556
- return safe4(this.client.query(`orders.${orderId}`));
1903
+ const encodedId = encodeURIComponent(orderId);
1904
+ return safeWithFallback4(
1905
+ () => this.client.get(`/api/v1/orders/${encodedId}`),
1906
+ () => this.client.query(`orders.${orderId}`)
1907
+ );
1557
1908
  }
1558
1909
  async getRecent(limit = 5) {
1559
1910
  return safe4(this.client.query(`orders#sort(created_at,desc)#limit(${limit})`));
1560
1911
  }
1561
1912
  async getByStatus(status) {
1562
- return safe4(this.client.query(`orders[?(@.status=='${status}')]`));
1913
+ return this.list({ status });
1563
1914
  }
1564
1915
  async cancel(orderId, reason) {
1565
- return safe4(
1566
- this.client.call("order.cancelOrder", {
1916
+ const encodedId = encodeURIComponent(orderId);
1917
+ return safeWithFallback4(
1918
+ () => this.client.post(`/api/v1/orders/${encodedId}/cancel`, {
1919
+ reason
1920
+ }),
1921
+ () => this.client.call("order.cancelOrder", {
1567
1922
  order_id: orderId,
1568
1923
  reason
1569
1924
  })
@@ -1738,12 +2093,23 @@ async function safe6(promise) {
1738
2093
  return err(toCimplifyError6(error));
1739
2094
  }
1740
2095
  }
2096
+ async function safeWithFallback5(primary, fallback) {
2097
+ const primaryResult = await safe6(primary());
2098
+ if (primaryResult.ok) return primaryResult;
2099
+ if (primaryResult.error.code !== "HTTP_404" && primaryResult.error.code !== "API_ERROR") {
2100
+ return primaryResult;
2101
+ }
2102
+ return safe6(fallback());
2103
+ }
1741
2104
  var AuthService = class {
1742
2105
  constructor(client) {
1743
2106
  this.client = client;
1744
2107
  }
1745
2108
  async getStatus() {
1746
- return safe6(this.client.query("auth"));
2109
+ return safeWithFallback5(
2110
+ () => this.client.get("/api/v1/auth/status"),
2111
+ () => this.client.query("auth")
2112
+ );
1747
2113
  }
1748
2114
  async getCurrentUser() {
1749
2115
  const result = await this.getStatus();
@@ -1756,23 +2122,34 @@ var AuthService = class {
1756
2122
  return ok(result.value.is_authenticated);
1757
2123
  }
1758
2124
  async requestOtp(contact, contactType) {
1759
- return safe6(
1760
- this.client.call(AUTH_MUTATION.REQUEST_OTP, {
2125
+ return safeWithFallback5(
2126
+ () => this.client.post("/api/v1/auth/request-otp", {
2127
+ contact,
2128
+ contact_type: contactType
2129
+ }),
2130
+ () => this.client.call(AUTH_MUTATION.REQUEST_OTP, {
1761
2131
  contact,
1762
2132
  contact_type: contactType
1763
2133
  })
1764
2134
  );
1765
2135
  }
1766
2136
  async verifyOtp(code, contact) {
1767
- return safe6(
1768
- this.client.call(AUTH_MUTATION.VERIFY_OTP, {
2137
+ return safeWithFallback5(
2138
+ () => this.client.post("/api/v1/auth/verify-otp", {
2139
+ otp_code: code,
2140
+ contact
2141
+ }),
2142
+ () => this.client.call(AUTH_MUTATION.VERIFY_OTP, {
1769
2143
  otp_code: code,
1770
2144
  contact
1771
2145
  })
1772
2146
  );
1773
2147
  }
1774
2148
  async logout() {
1775
- return safe6(this.client.call("auth.logout"));
2149
+ return safeWithFallback5(
2150
+ () => this.client.post("/api/v1/auth/logout"),
2151
+ () => this.client.call("auth.logout")
2152
+ );
1776
2153
  }
1777
2154
  async updateProfile(input) {
1778
2155
  return safe6(this.client.call("auth.update_profile", input));
@@ -1800,12 +2177,23 @@ async function safe7(promise) {
1800
2177
  return err(toCimplifyError7(error));
1801
2178
  }
1802
2179
  }
2180
+ async function safeWithFallback6(primary, fallback) {
2181
+ const primaryResult = await safe7(primary());
2182
+ if (primaryResult.ok) return primaryResult;
2183
+ if (primaryResult.error.code !== "HTTP_404" && primaryResult.error.code !== "API_ERROR") {
2184
+ return primaryResult;
2185
+ }
2186
+ return safe7(fallback());
2187
+ }
1803
2188
  var BusinessService = class {
1804
2189
  constructor(client) {
1805
2190
  this.client = client;
1806
2191
  }
1807
2192
  async getInfo() {
1808
- return safe7(this.client.query("business.info"));
2193
+ return safeWithFallback6(
2194
+ () => this.client.get("/api/v1/business"),
2195
+ () => this.client.query("business.info")
2196
+ );
1809
2197
  }
1810
2198
  async getByHandle(handle) {
1811
2199
  return safe7(this.client.query(`business.handle.${handle}`));
@@ -1814,24 +2202,48 @@ var BusinessService = class {
1814
2202
  return safe7(this.client.query("business.domain", { domain }));
1815
2203
  }
1816
2204
  async getSettings() {
1817
- return safe7(this.client.query("business.settings"));
2205
+ return safeWithFallback6(
2206
+ () => this.client.get("/api/v1/business/settings"),
2207
+ () => this.client.query("business.settings")
2208
+ );
1818
2209
  }
1819
2210
  async getTheme() {
1820
- return safe7(this.client.query("business.theme"));
2211
+ return safeWithFallback6(
2212
+ () => this.client.get("/api/v1/business/theme"),
2213
+ () => this.client.query("business.theme")
2214
+ );
1821
2215
  }
1822
2216
  async getLocations() {
1823
- return safe7(this.client.query("business.locations"));
2217
+ return safeWithFallback6(
2218
+ () => this.client.get("/api/v1/business/locations"),
2219
+ () => this.client.query("business.locations")
2220
+ );
1824
2221
  }
1825
2222
  async getLocation(locationId) {
1826
- return safe7(this.client.query(`business.locations.${locationId}`));
2223
+ const result = await this.getLocations();
2224
+ if (!result.ok) return err(result.error);
2225
+ const location = result.value.find((item) => item.id === locationId);
2226
+ if (!location) {
2227
+ return err(new CimplifyError(ErrorCode.NOT_FOUND, `Location not found: ${locationId}`, false));
2228
+ }
2229
+ return ok(location);
1827
2230
  }
1828
2231
  async getHours() {
1829
- return safe7(this.client.query("business.hours"));
2232
+ return safeWithFallback6(
2233
+ () => this.client.get("/api/v1/business/hours"),
2234
+ () => this.client.query("business.hours")
2235
+ );
1830
2236
  }
1831
2237
  async getLocationHours(locationId) {
1832
- return safe7(this.client.query(`business.locations.${locationId}.hours`));
2238
+ const result = await this.getHours();
2239
+ if (!result.ok) return result;
2240
+ return ok(result.value.filter((hour) => hour.location_id === locationId));
1833
2241
  }
1834
2242
  async getBootstrap() {
2243
+ const restBootstrap = await safe7(this.client.get("/api/v1/bootstrap"));
2244
+ if (restBootstrap.ok) {
2245
+ return restBootstrap;
2246
+ }
1835
2247
  const [businessResult, locationsResult, categoriesResult] = await Promise.all([
1836
2248
  this.getInfo(),
1837
2249
  this.getLocations(),
@@ -2124,15 +2536,30 @@ async function safe11(promise) {
2124
2536
  return err(toCimplifyError11(error));
2125
2537
  }
2126
2538
  }
2539
+ async function safeWithFallback7(primary, fallback) {
2540
+ const primaryResult = await safe11(primary());
2541
+ if (primaryResult.ok) return primaryResult;
2542
+ if (primaryResult.error.code !== "HTTP_404" && primaryResult.error.code !== "API_ERROR") {
2543
+ return primaryResult;
2544
+ }
2545
+ return safe11(fallback());
2546
+ }
2127
2547
  var FxService = class {
2128
2548
  constructor(client) {
2129
2549
  this.client = client;
2130
2550
  }
2131
2551
  async getRate(from, to) {
2132
- return safe11(this.client.call("fx.getRate", { from, to }));
2552
+ const path = `/api/v1/fx/rate?from=${encodeURIComponent(from)}&to=${encodeURIComponent(to)}`;
2553
+ return safeWithFallback7(
2554
+ () => this.client.get(path),
2555
+ () => this.client.call("fx.getRate", { from, to })
2556
+ );
2133
2557
  }
2134
2558
  async lockQuote(request) {
2135
- return safe11(this.client.call("fx.lockQuote", request));
2559
+ return safeWithFallback7(
2560
+ () => this.client.post("/api/v1/fx/quotes", request),
2561
+ () => this.client.call("fx.lockQuote", request)
2562
+ );
2136
2563
  }
2137
2564
  };
2138
2565