@cimplify/sdk 0.6.11 → 0.6.12

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,11 +279,79 @@ 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;
263
348
  }
264
349
  async getProducts(options) {
350
+ const result = await this.getProductsWithMeta(options);
351
+ if (!result.ok) return result;
352
+ return ok(result.value.items);
353
+ }
354
+ async getProductsWithMeta(options) {
265
355
  let query2 = "products";
266
356
  const filters = [];
267
357
  if (options?.category) {
@@ -276,6 +366,13 @@ var CatalogueQueries = class {
276
366
  if (options?.search) {
277
367
  filters.push(`@.name contains '${escapeQueryValue(options.search)}'`);
278
368
  }
369
+ if (options?.tags?.length) {
370
+ for (const tag of options.tags) {
371
+ if (tag.trim().length > 0) {
372
+ filters.push(`@.tags contains '${escapeQueryValue(tag)}'`);
373
+ }
374
+ }
375
+ }
279
376
  if (options?.min_price !== void 0) {
280
377
  filters.push(`@.price>=${options.min_price}`);
281
378
  }
@@ -288,25 +385,57 @@ var CatalogueQueries = class {
288
385
  if (options?.sort_by) {
289
386
  query2 += `#sort(${options.sort_by},${options.sort_order || "asc"})`;
290
387
  }
291
- if (options?.limit) {
388
+ if (options?.limit !== void 0) {
292
389
  query2 += `#limit(${options.limit})`;
293
390
  }
294
- if (options?.offset) {
391
+ if (options?.offset !== void 0) {
295
392
  query2 += `#offset(${options.offset})`;
296
393
  }
297
- const result = await safe(this.client.query(query2));
394
+ const path = withQuery("/api/v1/catalogue/products", {
395
+ category_id: options?.category,
396
+ search: options?.search,
397
+ page: options?.page,
398
+ tags: options?.tags?.join(","),
399
+ featured: options?.featured,
400
+ in_stock: options?.in_stock,
401
+ min_price: options?.min_price,
402
+ max_price: options?.max_price,
403
+ sort_by: options?.sort_by,
404
+ sort_order: options?.sort_order,
405
+ limit: options?.limit,
406
+ offset: options?.offset,
407
+ cursor: options?.cursor
408
+ });
409
+ const result = await safeWithFallback(
410
+ () => this.client.get(path),
411
+ () => this.client.query(query2)
412
+ );
298
413
  if (!result.ok) return result;
299
- return ok(result.value.map((product) => normalizeCatalogueProductPayload(product)));
414
+ return ok(normalizeCatalogueResult(result.value));
300
415
  }
301
416
  async getProduct(id) {
302
- const result = await safe(this.client.query(`products.${id}`));
417
+ const encodedId = encodeURIComponent(id);
418
+ const result = await safeWithFallback(
419
+ () => this.client.get(`/api/v1/catalogue/products/${encodedId}`),
420
+ () => this.client.query(`products.${id}`)
421
+ );
303
422
  if (!result.ok) return result;
304
423
  return ok(normalizeCatalogueProductPayload(result.value));
305
424
  }
306
425
  async getProductBySlug(slug) {
426
+ const encodedSlug = encodeURIComponent(slug);
427
+ const restResult = await safe(
428
+ this.client.get(`/api/v1/catalogue/products/slug/${encodedSlug}`)
429
+ );
430
+ if (restResult.ok) {
431
+ return ok(normalizeCatalogueProductPayload(restResult.value));
432
+ }
433
+ if (restResult.error.code !== "HTTP_404" && restResult.error.code !== "API_ERROR") {
434
+ return restResult;
435
+ }
307
436
  const filteredResult = await safe(
308
437
  this.client.query(
309
- `products[?(@.slug=='${escapeQueryValue(slug)}')]`
438
+ `products[?(@.slug=='${escapeQueryValue(slug)}')]#limit(50)`
310
439
  )
311
440
  );
312
441
  if (!filteredResult.ok) return filteredResult;
@@ -317,7 +446,9 @@ var CatalogueQueries = class {
317
446
  if (filteredResult.value.length === 1) {
318
447
  return ok(normalizeCatalogueProductPayload(filteredResult.value[0]));
319
448
  }
320
- const unfilteredResult = await safe(this.client.query("products"));
449
+ const unfilteredResult = await safe(
450
+ this.client.query("products#limit(200)")
451
+ );
321
452
  if (!unfilteredResult.ok) return unfilteredResult;
322
453
  const fallbackMatch = findProductBySlug(unfilteredResult.value, slug);
323
454
  if (!fallbackMatch) {
@@ -326,18 +457,33 @@ var CatalogueQueries = class {
326
457
  return ok(normalizeCatalogueProductPayload(fallbackMatch));
327
458
  }
328
459
  async getVariants(productId) {
329
- return safe(this.client.query(`products.${productId}.variants`));
460
+ const encodedId = encodeURIComponent(productId);
461
+ return safeWithFallback(
462
+ () => this.client.get(`/api/v1/catalogue/products/${encodedId}/variants`),
463
+ () => this.client.query(`products.${productId}.variants`)
464
+ );
330
465
  }
331
466
  async getVariantAxes(productId) {
332
- return safe(this.client.query(`products.${productId}.variant_axes`));
467
+ const encodedId = encodeURIComponent(productId);
468
+ return safeWithFallback(
469
+ () => this.client.get(`/api/v1/catalogue/products/${encodedId}/variant-axes`),
470
+ () => this.client.query(`products.${productId}.variant_axes`)
471
+ );
333
472
  }
334
473
  /**
335
474
  * Find a variant by axis selections (e.g., { "Size": "Large", "Color": "Red" })
336
475
  * Returns the matching variant or null if no match found.
337
476
  */
338
477
  async getVariantByAxisSelections(productId, selections) {
339
- return safe(
340
- this.client.query(`products.${productId}.variant`, {
478
+ const encodedId = encodeURIComponent(productId);
479
+ return safeWithFallback(
480
+ () => this.client.post(
481
+ `/api/v1/catalogue/products/${encodedId}/variants/find`,
482
+ {
483
+ axis_selections: selections
484
+ }
485
+ ),
486
+ () => this.client.query(`products.${productId}.variant`, {
341
487
  axis_selections: selections
342
488
  })
343
489
  );
@@ -346,45 +492,107 @@ var CatalogueQueries = class {
346
492
  * Get a specific variant by its ID
347
493
  */
348
494
  async getVariantById(productId, variantId) {
349
- return safe(this.client.query(`products.${productId}.variant.${variantId}`));
495
+ const encodedProductId = encodeURIComponent(productId);
496
+ const encodedVariantId = encodeURIComponent(variantId);
497
+ return safeWithFallback(
498
+ () => this.client.get(
499
+ `/api/v1/catalogue/products/${encodedProductId}/variants/${encodedVariantId}`
500
+ ),
501
+ () => this.client.query(`products.${productId}.variant.${variantId}`)
502
+ );
350
503
  }
351
504
  async getAddOns(productId) {
352
- return safe(this.client.query(`products.${productId}.add_ons`));
505
+ const encodedId = encodeURIComponent(productId);
506
+ return safeWithFallback(
507
+ () => this.client.get(`/api/v1/catalogue/products/${encodedId}/add-ons`),
508
+ () => this.client.query(`products.${productId}.add_ons`)
509
+ );
353
510
  }
354
511
  async getCategories() {
355
- return safe(this.client.query("categories"));
512
+ const result = await safeWithFallback(
513
+ () => this.client.get("/api/v1/catalogue/categories"),
514
+ () => this.client.query("categories")
515
+ );
516
+ if (!result.ok) return result;
517
+ if (result.value.some(hasCategorySlug)) {
518
+ return result;
519
+ }
520
+ const catalogueResult = await safe(
521
+ this.client.query("catalogue#limit(1)")
522
+ );
523
+ if (!catalogueResult.ok) {
524
+ return result;
525
+ }
526
+ const fallbackCategories = Array.isArray(catalogueResult.value.categories) ? catalogueResult.value.categories : [];
527
+ return fallbackCategories.length > 0 ? ok(fallbackCategories) : result;
356
528
  }
357
529
  async getCategory(id) {
358
- return safe(this.client.query(`categories.${id}`));
530
+ const encodedId = encodeURIComponent(id);
531
+ return safeWithFallback(
532
+ () => this.client.get(`/api/v1/catalogue/categories/${encodedId}`),
533
+ () => this.client.query(`categories.${id}`)
534
+ );
359
535
  }
360
536
  async getCategoryBySlug(slug) {
537
+ const encodedSlug = encodeURIComponent(slug);
538
+ const restResult = await safe(this.client.get(`/api/v1/catalogue/categories/slug/${encodedSlug}`));
539
+ if (restResult.ok) {
540
+ return restResult;
541
+ }
542
+ if (restResult.error.code !== "HTTP_404" && restResult.error.code !== "API_ERROR") {
543
+ return restResult;
544
+ }
361
545
  const result = await safe(
362
546
  this.client.query(`categories[?(@.slug=='${escapeQueryValue(slug)}')]`)
363
547
  );
364
548
  if (!result.ok) return result;
365
- if (!result.value.length) {
549
+ const exactMatch = findCategoryBySlug(result.value, slug);
550
+ if (exactMatch) {
551
+ return ok(exactMatch);
552
+ }
553
+ const categoriesResult = await this.getCategories();
554
+ if (!categoriesResult.ok) {
555
+ return categoriesResult;
556
+ }
557
+ const fallbackMatch = findCategoryBySlug(categoriesResult.value, slug);
558
+ if (!fallbackMatch) {
366
559
  return err(new CimplifyError("NOT_FOUND", `Category not found: ${slug}`, false));
367
560
  }
368
- return ok(result.value[0]);
561
+ return ok(fallbackMatch);
369
562
  }
370
563
  async getCategoryProducts(categoryId) {
371
- return safe(
372
- this.client.query(
564
+ const encodedId = encodeURIComponent(categoryId);
565
+ return safeWithFallback(
566
+ () => this.client.get(`/api/v1/catalogue/categories/${encodedId}/products`),
567
+ () => this.client.query(
373
568
  `products[?(@.category_id=='${escapeQueryValue(categoryId)}')]`
374
569
  )
375
570
  );
376
571
  }
377
572
  async getCollections() {
378
- return safe(this.client.query("collections"));
573
+ return safeWithFallback(
574
+ () => this.client.get("/api/v1/catalogue/collections"),
575
+ () => this.client.query("collections")
576
+ );
379
577
  }
380
578
  async getCollection(id) {
381
- return safe(this.client.query(`collections.${id}`));
579
+ const encodedId = encodeURIComponent(id);
580
+ return safeWithFallback(
581
+ () => this.client.get(`/api/v1/catalogue/collections/${encodedId}`),
582
+ () => this.client.query(`collections.${id}`)
583
+ );
382
584
  }
383
585
  async getCollectionBySlug(slug) {
586
+ const encodedSlug = encodeURIComponent(slug);
587
+ const restResult = await safe(
588
+ this.client.get(`/api/v1/catalogue/collections/slug/${encodedSlug}`)
589
+ );
590
+ if (restResult.ok) return restResult;
591
+ if (restResult.error.code !== "HTTP_404" && restResult.error.code !== "API_ERROR") {
592
+ return restResult;
593
+ }
384
594
  const result = await safe(
385
- this.client.query(
386
- `collections[?(@.slug=='${escapeQueryValue(slug)}')]`
387
- )
595
+ this.client.query(`collections[?(@.slug=='${escapeQueryValue(slug)}')]`)
388
596
  );
389
597
  if (!result.ok) return result;
390
598
  if (!result.value.length) {
@@ -393,22 +601,43 @@ var CatalogueQueries = class {
393
601
  return ok(result.value[0]);
394
602
  }
395
603
  async getCollectionProducts(collectionId) {
396
- return safe(this.client.query(`collections.${collectionId}.products`));
604
+ const encodedId = encodeURIComponent(collectionId);
605
+ return safeWithFallback(
606
+ () => this.client.get(`/api/v1/catalogue/collections/${encodedId}/products`),
607
+ () => this.client.query(`collections.${collectionId}.products`)
608
+ );
397
609
  }
398
610
  async searchCollections(query2, limit = 20) {
399
- return safe(
400
- this.client.query(
611
+ const path = withQuery("/api/v1/catalogue/collections", { search: query2, limit });
612
+ return safeWithFallback(
613
+ () => this.client.get(path),
614
+ () => this.client.query(
401
615
  `collections[?(@.name contains '${escapeQueryValue(query2)}')]#limit(${limit})`
402
616
  )
403
617
  );
404
618
  }
405
619
  async getBundles() {
406
- return safe(this.client.query("bundles"));
620
+ return safeWithFallback(
621
+ () => this.client.get("/api/v1/catalogue/bundles"),
622
+ () => this.client.query("bundles")
623
+ );
407
624
  }
408
625
  async getBundle(id) {
409
- return safe(this.client.query(`bundles.${id}`));
626
+ const encodedId = encodeURIComponent(id);
627
+ return safeWithFallback(
628
+ () => this.client.get(`/api/v1/catalogue/bundles/${encodedId}`),
629
+ () => this.client.query(`bundles.${id}`)
630
+ );
410
631
  }
411
632
  async getBundleBySlug(slug) {
633
+ const encodedSlug = encodeURIComponent(slug);
634
+ const restResult = await safe(
635
+ this.client.get(`/api/v1/catalogue/bundles/slug/${encodedSlug}`)
636
+ );
637
+ if (restResult.ok) return restResult;
638
+ if (restResult.error.code !== "HTTP_404" && restResult.error.code !== "API_ERROR") {
639
+ return restResult;
640
+ }
412
641
  const result = await safe(
413
642
  this.client.query(
414
643
  `bundles[?(@.slug=='${escapeQueryValue(slug)}')]`
@@ -421,8 +650,10 @@ var CatalogueQueries = class {
421
650
  return ok(result.value[0]);
422
651
  }
423
652
  async searchBundles(query2, limit = 20) {
424
- return safe(
425
- this.client.query(
653
+ const path = withQuery("/api/v1/catalogue/bundles", { search: query2, limit });
654
+ return safeWithFallback(
655
+ () => this.client.get(path),
656
+ () => this.client.query(
426
657
  `bundles[?(@.name contains '${escapeQueryValue(query2)}')]#limit(${limit})`
427
658
  )
428
659
  );
@@ -432,17 +663,39 @@ var CatalogueQueries = class {
432
663
  if (options?.limit) {
433
664
  query2 += `#limit(${options.limit})`;
434
665
  }
435
- return safe(this.client.query(query2));
666
+ const path = withQuery("/api/v1/catalogue/composites", { limit: options?.limit });
667
+ return safeWithFallback(
668
+ () => this.client.get(path),
669
+ () => this.client.query(query2)
670
+ );
436
671
  }
437
672
  async getComposite(id) {
438
- return safe(this.client.query(`composites.${id}`));
673
+ const encodedId = encodeURIComponent(id);
674
+ return safeWithFallback(
675
+ () => this.client.get(`/api/v1/catalogue/composites/${encodedId}`),
676
+ () => this.client.query(`composites.${id}`)
677
+ );
439
678
  }
440
679
  async getCompositeByProductId(productId) {
441
- return safe(this.client.query(`composites.by_product.${productId}`));
680
+ const encodedId = encodeURIComponent(productId);
681
+ return safeWithFallback(
682
+ () => this.client.get(
683
+ `/api/v1/catalogue/composites/by-product/${encodedId}`
684
+ ),
685
+ () => this.client.query(`composites.by_product.${productId}`)
686
+ );
442
687
  }
443
688
  async calculateCompositePrice(compositeId, selections, locationId) {
444
- return safe(
445
- this.client.call("composite.calculatePrice", {
689
+ const encodedId = encodeURIComponent(compositeId);
690
+ return safeWithFallback(
691
+ () => this.client.post(
692
+ `/api/v1/catalogue/composites/${encodedId}/calculate-price`,
693
+ {
694
+ selections,
695
+ location_id: locationId
696
+ }
697
+ ),
698
+ () => this.client.call("composite.calculatePrice", {
446
699
  composite_id: compositeId,
447
700
  selections,
448
701
  location_id: locationId
@@ -450,35 +703,41 @@ var CatalogueQueries = class {
450
703
  );
451
704
  }
452
705
  async fetchQuote(input) {
453
- return safe(this.client.call("catalogue.createQuote", input));
706
+ return safeWithFallback(
707
+ () => this.client.post("/api/v1/catalogue/quotes", input),
708
+ () => this.client.call("catalogue.createQuote", input)
709
+ );
454
710
  }
455
711
  async getQuote(quoteId) {
456
- return safe(
457
- this.client.call("catalogue.getQuote", {
712
+ const encodedQuoteId = encodeURIComponent(quoteId);
713
+ return safeWithFallback(
714
+ () => this.client.get(`/api/v1/catalogue/quotes/${encodedQuoteId}`),
715
+ () => this.client.call("catalogue.getQuote", {
458
716
  quote_id: quoteId
459
717
  })
460
718
  );
461
719
  }
462
720
  async refreshQuote(input) {
463
- return safe(this.client.call("catalogue.refreshQuote", input));
721
+ const encodedQuoteId = encodeURIComponent(input.quote_id);
722
+ return safeWithFallback(
723
+ () => this.client.post(
724
+ `/api/v1/catalogue/quotes/${encodedQuoteId}/refresh`,
725
+ input
726
+ ),
727
+ () => this.client.call("catalogue.refreshQuote", input)
728
+ );
464
729
  }
465
730
  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));
731
+ const result = await this.getProducts({
732
+ search: query2,
733
+ category: options?.category,
734
+ limit: options?.limit ?? 20
735
+ });
736
+ if (!result.ok) return result;
737
+ return ok(result.value);
473
738
  }
474
739
  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
- );
740
+ return this.search(query2, options);
482
741
  }
483
742
  async getMenu(options) {
484
743
  let query2 = "menu";
@@ -488,13 +747,28 @@ var CatalogueQueries = class {
488
747
  if (options?.limit) {
489
748
  query2 += `#limit(${options.limit})`;
490
749
  }
491
- return safe(this.client.query(query2));
750
+ const path = withQuery("/api/v1/catalogue/menu", {
751
+ category_id: options?.category,
752
+ limit: options?.limit
753
+ });
754
+ return safeWithFallback(
755
+ () => this.client.get(path),
756
+ () => this.client.query(query2)
757
+ );
492
758
  }
493
759
  async getMenuCategory(categoryId) {
494
- return safe(this.client.query(`menu.category.${categoryId}`));
760
+ const encodedId = encodeURIComponent(categoryId);
761
+ return safeWithFallback(
762
+ () => this.client.get(`/api/v1/catalogue/menu/categories/${encodedId}`),
763
+ () => this.client.query(`menu.category.${categoryId}`)
764
+ );
495
765
  }
496
766
  async getMenuItem(itemId) {
497
- return safe(this.client.query(`menu.${itemId}`));
767
+ const encodedId = encodeURIComponent(itemId);
768
+ return safeWithFallback(
769
+ () => this.client.get(`/api/v1/catalogue/menu/items/${encodedId}`),
770
+ () => this.client.query(`menu.${itemId}`)
771
+ );
498
772
  }
499
773
  };
500
774
 
@@ -513,6 +787,14 @@ async function safe2(promise) {
513
787
  return err(toCimplifyError2(error));
514
788
  }
515
789
  }
790
+ async function safeWithFallback2(primary, fallback) {
791
+ const primaryResult = await safe2(primary());
792
+ if (primaryResult.ok) return primaryResult;
793
+ if (primaryResult.error.code !== "HTTP_404" && primaryResult.error.code !== "API_ERROR") {
794
+ return primaryResult;
795
+ }
796
+ return safe2(fallback());
797
+ }
516
798
  function isUICartResponse(value) {
517
799
  return "cart" in value;
518
800
  }
@@ -524,21 +806,36 @@ var CartOperations = class {
524
806
  this.client = client;
525
807
  }
526
808
  async get() {
527
- const result = await safe2(this.client.query("cart#enriched"));
809
+ const result = await safeWithFallback2(
810
+ () => this.client.get("/api/v1/cart"),
811
+ () => this.client.query("cart#enriched")
812
+ );
528
813
  if (!result.ok) return result;
529
814
  return ok(unwrapEnrichedCart(result.value));
530
815
  }
531
816
  async getRaw() {
532
- return safe2(this.client.query("cart"));
817
+ return safeWithFallback2(
818
+ () => this.client.get("/api/v1/cart"),
819
+ () => this.client.query("cart")
820
+ );
533
821
  }
534
822
  async getItems() {
535
- return safe2(this.client.query("cart_items"));
823
+ return safeWithFallback2(
824
+ () => this.client.get("/api/v1/cart/items"),
825
+ () => this.client.query("cart_items")
826
+ );
536
827
  }
537
828
  async getCount() {
538
- return safe2(this.client.query("cart#count"));
829
+ return safeWithFallback2(
830
+ () => this.client.get("/api/v1/cart/count"),
831
+ () => this.client.query("cart#count")
832
+ );
539
833
  }
540
834
  async getTotal() {
541
- return safe2(this.client.query("cart#total"));
835
+ return safeWithFallback2(
836
+ () => this.client.get("/api/v1/cart/total"),
837
+ () => this.client.query("cart#total")
838
+ );
542
839
  }
543
840
  async getSummary() {
544
841
  const cartResult = await this.get();
@@ -555,43 +852,66 @@ var CartOperations = class {
555
852
  });
556
853
  }
557
854
  async addItem(input) {
558
- return safe2(this.client.call("cart.addItem", input));
855
+ return safeWithFallback2(
856
+ () => this.client.post("/api/v1/cart/items", input),
857
+ () => this.client.call("cart.addItem", input)
858
+ );
559
859
  }
560
860
  async updateItem(cartItemId, updates) {
561
- return safe2(
562
- this.client.call("cart.updateItem", {
861
+ if (typeof updates.quantity === "number") {
862
+ return this.updateQuantity(cartItemId, updates.quantity);
863
+ }
864
+ const encodedId = encodeURIComponent(cartItemId);
865
+ return safeWithFallback2(
866
+ () => this.client.patch(`/api/v1/cart/items/${encodedId}`, updates),
867
+ () => this.client.call("cart.updateItem", {
563
868
  cart_item_id: cartItemId,
564
869
  ...updates
565
870
  })
566
871
  );
567
872
  }
568
873
  async updateQuantity(cartItemId, quantity) {
569
- return safe2(
570
- this.client.call("cart.updateItemQuantity", {
874
+ const encodedId = encodeURIComponent(cartItemId);
875
+ return safeWithFallback2(
876
+ () => this.client.patch(`/api/v1/cart/items/${encodedId}`, {
877
+ quantity
878
+ }),
879
+ () => this.client.call("cart.updateItemQuantity", {
571
880
  cart_item_id: cartItemId,
572
881
  quantity
573
882
  })
574
883
  );
575
884
  }
576
885
  async removeItem(cartItemId) {
577
- return safe2(
578
- this.client.call("cart.removeItem", {
886
+ const encodedId = encodeURIComponent(cartItemId);
887
+ return safeWithFallback2(
888
+ () => this.client.delete(`/api/v1/cart/items/${encodedId}`),
889
+ () => this.client.call("cart.removeItem", {
579
890
  cart_item_id: cartItemId
580
891
  })
581
892
  );
582
893
  }
583
894
  async clear() {
584
- return safe2(this.client.call("cart.clearCart"));
895
+ return safeWithFallback2(
896
+ () => this.client.delete("/api/v1/cart"),
897
+ () => this.client.call("cart.clearCart")
898
+ );
585
899
  }
586
900
  async applyCoupon(code) {
587
- return safe2(
588
- this.client.call("cart.applyCoupon", {
901
+ return safeWithFallback2(
902
+ () => this.client.post("/api/v1/cart/coupons", {
903
+ coupon_code: code
904
+ }),
905
+ () => this.client.call("cart.applyCoupon", {
589
906
  coupon_code: code
590
907
  })
591
908
  );
592
909
  }
593
910
  async removeCoupon() {
594
- return safe2(this.client.call("cart.removeCoupon"));
911
+ return safeWithFallback2(
912
+ () => this.client.delete("/api/v1/cart/coupons/current"),
913
+ () => this.client.call("cart.removeCoupon")
914
+ );
595
915
  }
596
916
  async isEmpty() {
597
917
  const countResult = await this.getCount();
@@ -1351,6 +1671,14 @@ async function safe3(promise) {
1351
1671
  return err(toCimplifyError3(error));
1352
1672
  }
1353
1673
  }
1674
+ async function safeWithFallback3(primary, fallback) {
1675
+ const primaryResult = await safe3(primary());
1676
+ if (primaryResult.ok) return primaryResult;
1677
+ if (primaryResult.error.code !== "HTTP_404" && primaryResult.error.code !== "API_ERROR") {
1678
+ return primaryResult;
1679
+ }
1680
+ return safe3(fallback());
1681
+ }
1354
1682
  function toTerminalFailure(code, message, recoverable) {
1355
1683
  return {
1356
1684
  success: false,
@@ -1378,8 +1706,11 @@ var CheckoutService = class {
1378
1706
  ...data,
1379
1707
  idempotency_key: data.idempotency_key || generateIdempotencyKey()
1380
1708
  };
1381
- return safe3(
1382
- this.client.call(CHECKOUT_MUTATION.PROCESS, {
1709
+ return safeWithFallback3(
1710
+ () => this.client.post("/api/v1/checkout", {
1711
+ checkout_data: checkoutData
1712
+ }),
1713
+ () => this.client.call(CHECKOUT_MUTATION.PROCESS, {
1383
1714
  checkout_data: checkoutData
1384
1715
  })
1385
1716
  );
@@ -1393,18 +1724,23 @@ var CheckoutService = class {
1393
1724
  );
1394
1725
  }
1395
1726
  async submitAuthorization(input) {
1396
- return safe3(
1397
- this.client.call(PAYMENT_MUTATION.SUBMIT_AUTHORIZATION, input)
1727
+ return safeWithFallback3(
1728
+ () => this.client.post("/api/v1/payments/authorization", input),
1729
+ () => this.client.call(PAYMENT_MUTATION.SUBMIT_AUTHORIZATION, input)
1398
1730
  );
1399
1731
  }
1400
1732
  async pollPaymentStatus(orderId) {
1401
- return safe3(
1402
- this.client.call(PAYMENT_MUTATION.CHECK_STATUS, orderId)
1733
+ const encodedId = encodeURIComponent(orderId);
1734
+ return safeWithFallback3(
1735
+ () => this.client.get(`/api/v1/orders/${encodedId}/payment-status`),
1736
+ () => this.client.call(PAYMENT_MUTATION.CHECK_STATUS, orderId)
1403
1737
  );
1404
1738
  }
1405
1739
  async updateOrderCustomer(orderId, customer) {
1406
- return safe3(
1407
- this.client.call(ORDER_MUTATION.UPDATE_CUSTOMER, {
1740
+ const encodedId = encodeURIComponent(orderId);
1741
+ return safeWithFallback3(
1742
+ () => this.client.post(`/api/v1/orders/${encodedId}/customer`, customer),
1743
+ () => this.client.call(ORDER_MUTATION.UPDATE_CUSTOMER, {
1408
1744
  order_id: orderId,
1409
1745
  ...customer
1410
1746
  })
@@ -1534,6 +1870,14 @@ async function safe4(promise) {
1534
1870
  return err(toCimplifyError4(error));
1535
1871
  }
1536
1872
  }
1873
+ async function safeWithFallback4(primary, fallback) {
1874
+ const primaryResult = await safe4(primary());
1875
+ if (primaryResult.ok) return primaryResult;
1876
+ if (primaryResult.error.code !== "HTTP_404" && primaryResult.error.code !== "API_ERROR") {
1877
+ return primaryResult;
1878
+ }
1879
+ return safe4(fallback());
1880
+ }
1537
1881
  var OrderQueries = class {
1538
1882
  constructor(client) {
1539
1883
  this.client = client;
@@ -1550,20 +1894,36 @@ var OrderQueries = class {
1550
1894
  if (options?.offset) {
1551
1895
  query2 += `#offset(${options.offset})`;
1552
1896
  }
1553
- return safe4(this.client.query(query2));
1897
+ const params = new URLSearchParams();
1898
+ if (options?.status) params.set("status", options.status);
1899
+ if (options?.limit) params.set("limit", String(options.limit));
1900
+ if (options?.offset) params.set("offset", String(options.offset));
1901
+ const path = params.toString() ? `/api/v1/orders?${params.toString()}` : "/api/v1/orders";
1902
+ return safeWithFallback4(
1903
+ () => this.client.get(path),
1904
+ () => this.client.query(query2)
1905
+ );
1554
1906
  }
1555
1907
  async get(orderId) {
1556
- return safe4(this.client.query(`orders.${orderId}`));
1908
+ const encodedId = encodeURIComponent(orderId);
1909
+ return safeWithFallback4(
1910
+ () => this.client.get(`/api/v1/orders/${encodedId}`),
1911
+ () => this.client.query(`orders.${orderId}`)
1912
+ );
1557
1913
  }
1558
1914
  async getRecent(limit = 5) {
1559
1915
  return safe4(this.client.query(`orders#sort(created_at,desc)#limit(${limit})`));
1560
1916
  }
1561
1917
  async getByStatus(status) {
1562
- return safe4(this.client.query(`orders[?(@.status=='${status}')]`));
1918
+ return this.list({ status });
1563
1919
  }
1564
1920
  async cancel(orderId, reason) {
1565
- return safe4(
1566
- this.client.call("order.cancelOrder", {
1921
+ const encodedId = encodeURIComponent(orderId);
1922
+ return safeWithFallback4(
1923
+ () => this.client.post(`/api/v1/orders/${encodedId}/cancel`, {
1924
+ reason
1925
+ }),
1926
+ () => this.client.call("order.cancelOrder", {
1567
1927
  order_id: orderId,
1568
1928
  reason
1569
1929
  })
@@ -1738,12 +2098,23 @@ async function safe6(promise) {
1738
2098
  return err(toCimplifyError6(error));
1739
2099
  }
1740
2100
  }
2101
+ async function safeWithFallback5(primary, fallback) {
2102
+ const primaryResult = await safe6(primary());
2103
+ if (primaryResult.ok) return primaryResult;
2104
+ if (primaryResult.error.code !== "HTTP_404" && primaryResult.error.code !== "API_ERROR") {
2105
+ return primaryResult;
2106
+ }
2107
+ return safe6(fallback());
2108
+ }
1741
2109
  var AuthService = class {
1742
2110
  constructor(client) {
1743
2111
  this.client = client;
1744
2112
  }
1745
2113
  async getStatus() {
1746
- return safe6(this.client.query("auth"));
2114
+ return safeWithFallback5(
2115
+ () => this.client.get("/api/v1/auth/status"),
2116
+ () => this.client.query("auth")
2117
+ );
1747
2118
  }
1748
2119
  async getCurrentUser() {
1749
2120
  const result = await this.getStatus();
@@ -1756,23 +2127,34 @@ var AuthService = class {
1756
2127
  return ok(result.value.is_authenticated);
1757
2128
  }
1758
2129
  async requestOtp(contact, contactType) {
1759
- return safe6(
1760
- this.client.call(AUTH_MUTATION.REQUEST_OTP, {
2130
+ return safeWithFallback5(
2131
+ () => this.client.post("/api/v1/auth/request-otp", {
2132
+ contact,
2133
+ contact_type: contactType
2134
+ }),
2135
+ () => this.client.call(AUTH_MUTATION.REQUEST_OTP, {
1761
2136
  contact,
1762
2137
  contact_type: contactType
1763
2138
  })
1764
2139
  );
1765
2140
  }
1766
2141
  async verifyOtp(code, contact) {
1767
- return safe6(
1768
- this.client.call(AUTH_MUTATION.VERIFY_OTP, {
2142
+ return safeWithFallback5(
2143
+ () => this.client.post("/api/v1/auth/verify-otp", {
2144
+ otp_code: code,
2145
+ contact
2146
+ }),
2147
+ () => this.client.call(AUTH_MUTATION.VERIFY_OTP, {
1769
2148
  otp_code: code,
1770
2149
  contact
1771
2150
  })
1772
2151
  );
1773
2152
  }
1774
2153
  async logout() {
1775
- return safe6(this.client.call("auth.logout"));
2154
+ return safeWithFallback5(
2155
+ () => this.client.post("/api/v1/auth/logout"),
2156
+ () => this.client.call("auth.logout")
2157
+ );
1776
2158
  }
1777
2159
  async updateProfile(input) {
1778
2160
  return safe6(this.client.call("auth.update_profile", input));
@@ -1800,12 +2182,23 @@ async function safe7(promise) {
1800
2182
  return err(toCimplifyError7(error));
1801
2183
  }
1802
2184
  }
2185
+ async function safeWithFallback6(primary, fallback) {
2186
+ const primaryResult = await safe7(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 safe7(fallback());
2192
+ }
1803
2193
  var BusinessService = class {
1804
2194
  constructor(client) {
1805
2195
  this.client = client;
1806
2196
  }
1807
2197
  async getInfo() {
1808
- return safe7(this.client.query("business.info"));
2198
+ return safeWithFallback6(
2199
+ () => this.client.get("/api/v1/business"),
2200
+ () => this.client.query("business.info")
2201
+ );
1809
2202
  }
1810
2203
  async getByHandle(handle) {
1811
2204
  return safe7(this.client.query(`business.handle.${handle}`));
@@ -1814,24 +2207,48 @@ var BusinessService = class {
1814
2207
  return safe7(this.client.query("business.domain", { domain }));
1815
2208
  }
1816
2209
  async getSettings() {
1817
- return safe7(this.client.query("business.settings"));
2210
+ return safeWithFallback6(
2211
+ () => this.client.get("/api/v1/business/settings"),
2212
+ () => this.client.query("business.settings")
2213
+ );
1818
2214
  }
1819
2215
  async getTheme() {
1820
- return safe7(this.client.query("business.theme"));
2216
+ return safeWithFallback6(
2217
+ () => this.client.get("/api/v1/business/theme"),
2218
+ () => this.client.query("business.theme")
2219
+ );
1821
2220
  }
1822
2221
  async getLocations() {
1823
- return safe7(this.client.query("business.locations"));
2222
+ return safeWithFallback6(
2223
+ () => this.client.get("/api/v1/business/locations"),
2224
+ () => this.client.query("business.locations")
2225
+ );
1824
2226
  }
1825
2227
  async getLocation(locationId) {
1826
- return safe7(this.client.query(`business.locations.${locationId}`));
2228
+ const result = await this.getLocations();
2229
+ if (!result.ok) return err(result.error);
2230
+ const location = result.value.find((item) => item.id === locationId);
2231
+ if (!location) {
2232
+ return err(new CimplifyError(ErrorCode.NOT_FOUND, `Location not found: ${locationId}`, false));
2233
+ }
2234
+ return ok(location);
1827
2235
  }
1828
2236
  async getHours() {
1829
- return safe7(this.client.query("business.hours"));
2237
+ return safeWithFallback6(
2238
+ () => this.client.get("/api/v1/business/hours"),
2239
+ () => this.client.query("business.hours")
2240
+ );
1830
2241
  }
1831
2242
  async getLocationHours(locationId) {
1832
- return safe7(this.client.query(`business.locations.${locationId}.hours`));
2243
+ const result = await this.getHours();
2244
+ if (!result.ok) return result;
2245
+ return ok(result.value.filter((hour) => hour.location_id === locationId));
1833
2246
  }
1834
2247
  async getBootstrap() {
2248
+ const restBootstrap = await safe7(this.client.get("/api/v1/bootstrap"));
2249
+ if (restBootstrap.ok) {
2250
+ return restBootstrap;
2251
+ }
1835
2252
  const [businessResult, locationsResult, categoriesResult] = await Promise.all([
1836
2253
  this.getInfo(),
1837
2254
  this.getLocations(),
@@ -2124,15 +2541,30 @@ async function safe11(promise) {
2124
2541
  return err(toCimplifyError11(error));
2125
2542
  }
2126
2543
  }
2544
+ async function safeWithFallback7(primary, fallback) {
2545
+ const primaryResult = await safe11(primary());
2546
+ if (primaryResult.ok) return primaryResult;
2547
+ if (primaryResult.error.code !== "HTTP_404" && primaryResult.error.code !== "API_ERROR") {
2548
+ return primaryResult;
2549
+ }
2550
+ return safe11(fallback());
2551
+ }
2127
2552
  var FxService = class {
2128
2553
  constructor(client) {
2129
2554
  this.client = client;
2130
2555
  }
2131
2556
  async getRate(from, to) {
2132
- return safe11(this.client.call("fx.getRate", { from, to }));
2557
+ const path = `/api/v1/fx/rate?from=${encodeURIComponent(from)}&to=${encodeURIComponent(to)}`;
2558
+ return safeWithFallback7(
2559
+ () => this.client.get(path),
2560
+ () => this.client.call("fx.getRate", { from, to })
2561
+ );
2133
2562
  }
2134
2563
  async lockQuote(request) {
2135
- return safe11(this.client.call("fx.lockQuote", request));
2564
+ return safeWithFallback7(
2565
+ () => this.client.post("/api/v1/fx/quotes", request),
2566
+ () => this.client.call("fx.lockQuote", request)
2567
+ );
2136
2568
  }
2137
2569
  };
2138
2570