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