@cimplify/sdk 0.7.1 → 0.7.3

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
@@ -122,74 +122,6 @@ function enrichError(error, options = {}) {
122
122
  return error;
123
123
  }
124
124
 
125
- // src/query/builder.ts
126
- function escapeQueryValue(value) {
127
- return value.replace(/'/g, "\\'");
128
- }
129
- var QueryBuilder = class {
130
- constructor(entity) {
131
- this.filters = [];
132
- this.modifiers = [];
133
- this.pathSegments = [];
134
- this.entity = entity;
135
- }
136
- path(segment) {
137
- this.pathSegments.push(segment);
138
- return this;
139
- }
140
- where(field, op, value) {
141
- const v = typeof value === "string" ? `'${escapeQueryValue(value)}'` : value;
142
- if (op === "contains" || op === "startsWith") {
143
- this.filters.push(`@.${field} ${op} ${v}`);
144
- } else {
145
- this.filters.push(`@.${field}${op}${v}`);
146
- }
147
- return this;
148
- }
149
- and(field, op, value) {
150
- return this.where(field, op, value);
151
- }
152
- sort(field, order = "asc") {
153
- this.modifiers.push(`sort(${field},${order})`);
154
- return this;
155
- }
156
- limit(n) {
157
- this.modifiers.push(`limit(${n})`);
158
- return this;
159
- }
160
- offset(n) {
161
- this.modifiers.push(`offset(${n})`);
162
- return this;
163
- }
164
- count() {
165
- this.modifiers.push("count");
166
- return this;
167
- }
168
- enriched() {
169
- this.modifiers.push("enriched");
170
- return this;
171
- }
172
- build() {
173
- let query2 = this.entity;
174
- if (this.pathSegments.length > 0) {
175
- query2 += "." + this.pathSegments.join(".");
176
- }
177
- if (this.filters.length > 0) {
178
- query2 += `[?(${this.filters.join(" && ")})]`;
179
- }
180
- for (const mod of this.modifiers) {
181
- query2 += `#${mod}`;
182
- }
183
- return query2;
184
- }
185
- toString() {
186
- return this.build();
187
- }
188
- };
189
- function query(entity) {
190
- return new QueryBuilder(entity);
191
- }
192
-
193
125
  // src/catalogue.ts
194
126
  function toCimplifyError(error) {
195
127
  if (error instanceof CimplifyError) return enrichError(error);
@@ -205,14 +137,6 @@ async function safe(promise) {
205
137
  return err(toCimplifyError(error));
206
138
  }
207
139
  }
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
140
  function withQuery(path, params) {
217
141
  const query2 = new URLSearchParams();
218
142
  for (const [key, value] of Object.entries(params)) {
@@ -274,22 +198,6 @@ function normalizeCatalogueProductPayload(product) {
274
198
  }
275
199
  return normalized;
276
200
  }
277
- function findProductBySlug(products, slug) {
278
- return products.find((product) => {
279
- const value = product["slug"];
280
- return typeof value === "string" && value === slug;
281
- });
282
- }
283
- function findCategoryBySlug(categories, slug) {
284
- return categories.find((category) => {
285
- const value = category["slug"];
286
- return typeof value === "string" && value === slug;
287
- });
288
- }
289
- function hasCategorySlug(category) {
290
- const value = category["slug"];
291
- return typeof value === "string" && value.trim().length > 0;
292
- }
293
201
  function toFiniteNumber(value) {
294
202
  if (typeof value === "number" && Number.isFinite(value)) {
295
203
  return value;
@@ -377,53 +285,11 @@ var CatalogueQueries = class {
377
285
  this.client = client;
378
286
  }
379
287
  async getCatalogue() {
380
- const result = await safeWithFallback(
381
- () => this.client.get("/api/v1/catalogue"),
382
- () => this.client.query("catalogue")
383
- );
288
+ const result = await safe(this.client.get("/api/v1/catalogue"));
384
289
  if (!result.ok) return result;
385
290
  return ok(normalizeCatalogueSnapshot(result.value));
386
291
  }
387
292
  async getProducts(options) {
388
- let query2 = "products";
389
- const filters = [];
390
- if (options?.category) {
391
- filters.push(`@.category_id=='${escapeQueryValue(options.category)}'`);
392
- }
393
- if (options?.featured !== void 0) {
394
- filters.push(`@.featured==${options.featured}`);
395
- }
396
- if (options?.in_stock !== void 0) {
397
- filters.push(`@.in_stock==${options.in_stock}`);
398
- }
399
- if (options?.search) {
400
- filters.push(`@.name contains '${escapeQueryValue(options.search)}'`);
401
- }
402
- if (options?.tags?.length) {
403
- for (const tag of options.tags) {
404
- if (tag.trim().length > 0) {
405
- filters.push(`@.tags contains '${escapeQueryValue(tag)}'`);
406
- }
407
- }
408
- }
409
- if (options?.min_price !== void 0) {
410
- filters.push(`@.price>=${options.min_price}`);
411
- }
412
- if (options?.max_price !== void 0) {
413
- filters.push(`@.price<=${options.max_price}`);
414
- }
415
- if (filters.length > 0) {
416
- query2 += `[?(${filters.join(" && ")})]`;
417
- }
418
- if (options?.sort_by) {
419
- query2 += `#sort(${options.sort_by},${options.sort_order || "asc"})`;
420
- }
421
- if (options?.limit !== void 0) {
422
- query2 += `#limit(${options.limit})`;
423
- }
424
- if (options?.offset !== void 0) {
425
- query2 += `#offset(${options.offset})`;
426
- }
427
293
  const path = withQuery("/api/v1/catalogue/products", {
428
294
  category_id: options?.category,
429
295
  search: options?.search,
@@ -439,325 +305,145 @@ var CatalogueQueries = class {
439
305
  offset: options?.offset,
440
306
  cursor: options?.cursor
441
307
  });
442
- const result = await safeWithFallback(
443
- () => this.client.get(path),
444
- () => this.client.query(query2)
445
- );
308
+ const result = await safe(this.client.get(path));
446
309
  if (!result.ok) return result;
447
310
  return ok(normalizeCatalogueResult(result.value));
448
311
  }
449
312
  async getProduct(id) {
450
313
  const encodedId = encodeURIComponent(id);
451
- const result = await safeWithFallback(
452
- () => this.client.get(`/api/v1/catalogue/products/${encodedId}`),
453
- () => this.client.query(`products.${id}`)
454
- );
314
+ const result = await safe(this.client.get(`/api/v1/catalogue/products/${encodedId}`));
455
315
  if (!result.ok) return result;
456
316
  return ok(normalizeCatalogueProductPayload(result.value));
457
317
  }
458
318
  async getProductBySlug(slug) {
459
319
  const encodedSlug = encodeURIComponent(slug);
460
- const restResult = await safe(
461
- this.client.get(`/api/v1/catalogue/products/slug/${encodedSlug}`)
462
- );
463
- if (restResult.ok) {
464
- return ok(normalizeCatalogueProductPayload(restResult.value));
465
- }
466
- if (restResult.error.code !== "HTTP_404" && restResult.error.code !== "API_ERROR") {
467
- return restResult;
468
- }
469
- const filteredResult = await safe(
470
- this.client.query(
471
- `products[?(@.slug=='${escapeQueryValue(slug)}')]#limit(50)`
472
- )
473
- );
474
- if (!filteredResult.ok) return filteredResult;
475
- const exactMatch = findProductBySlug(filteredResult.value, slug);
476
- if (exactMatch) {
477
- return ok(normalizeCatalogueProductPayload(exactMatch));
478
- }
479
- if (filteredResult.value.length === 1) {
480
- return ok(normalizeCatalogueProductPayload(filteredResult.value[0]));
481
- }
482
- const unfilteredResult = await safe(
483
- this.client.query("products#limit(200)")
484
- );
485
- if (!unfilteredResult.ok) return unfilteredResult;
486
- const fallbackMatch = findProductBySlug(unfilteredResult.value, slug);
487
- if (!fallbackMatch) {
488
- return err(new CimplifyError("NOT_FOUND", `Product not found: ${slug}`, false));
489
- }
490
- return ok(normalizeCatalogueProductPayload(fallbackMatch));
320
+ const result = await safe(this.client.get(`/api/v1/catalogue/products/slug/${encodedSlug}`));
321
+ if (!result.ok) return result;
322
+ return ok(normalizeCatalogueProductPayload(result.value));
491
323
  }
492
324
  async getVariants(productId) {
493
325
  const encodedId = encodeURIComponent(productId);
494
- return safeWithFallback(
495
- () => this.client.get(`/api/v1/catalogue/products/${encodedId}/variants`),
496
- () => this.client.query(`products.${productId}.variants`)
497
- );
326
+ return safe(this.client.get(`/api/v1/catalogue/products/${encodedId}/variants`));
498
327
  }
499
328
  async getVariantAxes(productId) {
500
329
  const encodedId = encodeURIComponent(productId);
501
- return safeWithFallback(
502
- () => this.client.get(`/api/v1/catalogue/products/${encodedId}/variant-axes`),
503
- () => this.client.query(`products.${productId}.variant_axes`)
504
- );
330
+ return safe(this.client.get(`/api/v1/catalogue/products/${encodedId}/variant-axes`));
505
331
  }
506
- /**
507
- * Find a variant by axis selections (e.g., { "Size": "Large", "Color": "Red" })
508
- * Returns the matching variant or null if no match found.
509
- */
510
332
  async getVariantByAxisSelections(productId, selections) {
511
333
  const encodedId = encodeURIComponent(productId);
512
- return safeWithFallback(
513
- () => this.client.post(
334
+ return safe(
335
+ this.client.post(
514
336
  `/api/v1/catalogue/products/${encodedId}/variants/find`,
515
337
  {
516
338
  axis_selections: selections
517
339
  }
518
- ),
519
- () => this.client.query(`products.${productId}.variant`, {
520
- axis_selections: selections
521
- })
340
+ )
522
341
  );
523
342
  }
524
- /**
525
- * Get a specific variant by its ID
526
- */
527
343
  async getVariantById(productId, variantId) {
528
344
  const encodedProductId = encodeURIComponent(productId);
529
345
  const encodedVariantId = encodeURIComponent(variantId);
530
- return safeWithFallback(
531
- () => this.client.get(
346
+ return safe(
347
+ this.client.get(
532
348
  `/api/v1/catalogue/products/${encodedProductId}/variants/${encodedVariantId}`
533
- ),
534
- () => this.client.query(`products.${productId}.variant.${variantId}`)
349
+ )
535
350
  );
536
351
  }
537
352
  async getAddOns(productId) {
538
353
  const encodedId = encodeURIComponent(productId);
539
- return safeWithFallback(
540
- () => this.client.get(`/api/v1/catalogue/products/${encodedId}/add-ons`),
541
- () => this.client.query(`products.${productId}.add_ons`)
542
- );
354
+ return safe(this.client.get(`/api/v1/catalogue/products/${encodedId}/add-ons`));
543
355
  }
544
356
  async getCategories() {
545
- const result = await safeWithFallback(
546
- () => this.client.get("/api/v1/catalogue/categories"),
547
- () => this.client.query("categories")
548
- );
549
- if (!result.ok) return result;
550
- if (result.value.some(hasCategorySlug)) {
551
- return result;
552
- }
553
- const catalogueResult = await safe(
554
- this.client.query("catalogue#limit(1)")
555
- );
556
- if (!catalogueResult.ok) {
557
- return result;
558
- }
559
- const fallbackCategories = Array.isArray(catalogueResult.value.categories) ? catalogueResult.value.categories : [];
560
- return fallbackCategories.length > 0 ? ok(fallbackCategories) : result;
357
+ return safe(this.client.get("/api/v1/catalogue/categories"));
561
358
  }
562
359
  async getCategory(id) {
563
360
  const encodedId = encodeURIComponent(id);
564
- return safeWithFallback(
565
- () => this.client.get(`/api/v1/catalogue/categories/${encodedId}`),
566
- () => this.client.query(`categories.${id}`)
567
- );
361
+ return safe(this.client.get(`/api/v1/catalogue/categories/${encodedId}`));
568
362
  }
569
363
  async getCategoryBySlug(slug) {
570
364
  const encodedSlug = encodeURIComponent(slug);
571
- const restResult = await safe(this.client.get(`/api/v1/catalogue/categories/slug/${encodedSlug}`));
572
- if (restResult.ok) {
573
- return restResult;
574
- }
575
- if (restResult.error.code !== "HTTP_404" && restResult.error.code !== "API_ERROR") {
576
- return restResult;
577
- }
578
- const result = await safe(
579
- this.client.query(`categories[?(@.slug=='${escapeQueryValue(slug)}')]`)
580
- );
581
- if (!result.ok) return result;
582
- const exactMatch = findCategoryBySlug(result.value, slug);
583
- if (exactMatch) {
584
- return ok(exactMatch);
585
- }
586
- const categoriesResult = await this.getCategories();
587
- if (!categoriesResult.ok) {
588
- return categoriesResult;
589
- }
590
- const fallbackMatch = findCategoryBySlug(categoriesResult.value, slug);
591
- if (!fallbackMatch) {
592
- return err(new CimplifyError("NOT_FOUND", `Category not found: ${slug}`, false));
593
- }
594
- return ok(fallbackMatch);
365
+ return safe(this.client.get(`/api/v1/catalogue/categories/slug/${encodedSlug}`));
595
366
  }
596
367
  async getCategoryProducts(categoryId) {
597
368
  const encodedId = encodeURIComponent(categoryId);
598
- return safeWithFallback(
599
- () => this.client.get(`/api/v1/catalogue/categories/${encodedId}/products`),
600
- () => this.client.query(
601
- `products[?(@.category_id=='${escapeQueryValue(categoryId)}')]`
602
- )
603
- );
369
+ return safe(this.client.get(`/api/v1/catalogue/categories/${encodedId}/products`));
604
370
  }
605
371
  async getCollections() {
606
- return safeWithFallback(
607
- () => this.client.get("/api/v1/catalogue/collections"),
608
- () => this.client.query("collections")
609
- );
372
+ return safe(this.client.get("/api/v1/catalogue/collections"));
610
373
  }
611
374
  async getCollection(id) {
612
375
  const encodedId = encodeURIComponent(id);
613
- return safeWithFallback(
614
- () => this.client.get(`/api/v1/catalogue/collections/${encodedId}`),
615
- () => this.client.query(`collections.${id}`)
616
- );
376
+ return safe(this.client.get(`/api/v1/catalogue/collections/${encodedId}`));
617
377
  }
618
378
  async getCollectionBySlug(slug) {
619
379
  const encodedSlug = encodeURIComponent(slug);
620
- const restResult = await safe(
621
- this.client.get(`/api/v1/catalogue/collections/slug/${encodedSlug}`)
622
- );
623
- if (restResult.ok) return restResult;
624
- if (restResult.error.code !== "HTTP_404" && restResult.error.code !== "API_ERROR") {
625
- return restResult;
626
- }
627
- const result = await safe(
628
- this.client.query(`collections[?(@.slug=='${escapeQueryValue(slug)}')]`)
629
- );
630
- if (!result.ok) return result;
631
- if (!result.value.length) {
632
- return err(new CimplifyError("NOT_FOUND", `Collection not found: ${slug}`, false));
633
- }
634
- return ok(result.value[0]);
380
+ return safe(this.client.get(`/api/v1/catalogue/collections/slug/${encodedSlug}`));
635
381
  }
636
382
  async getCollectionProducts(collectionId) {
637
383
  const encodedId = encodeURIComponent(collectionId);
638
- return safeWithFallback(
639
- () => this.client.get(`/api/v1/catalogue/collections/${encodedId}/products`),
640
- () => this.client.query(`collections.${collectionId}.products`)
641
- );
384
+ return safe(this.client.get(`/api/v1/catalogue/collections/${encodedId}/products`));
642
385
  }
643
386
  async searchCollections(query2, limit = 20) {
644
387
  const path = withQuery("/api/v1/catalogue/collections", { search: query2, limit });
645
- return safeWithFallback(
646
- () => this.client.get(path),
647
- () => this.client.query(
648
- `collections[?(@.name contains '${escapeQueryValue(query2)}')]#limit(${limit})`
649
- )
650
- );
388
+ return safe(this.client.get(path));
651
389
  }
652
390
  async getBundles() {
653
- return safeWithFallback(
654
- () => this.client.get("/api/v1/catalogue/bundles"),
655
- () => this.client.query("bundles")
656
- );
391
+ return safe(this.client.get("/api/v1/catalogue/bundles"));
657
392
  }
658
393
  async getBundle(id) {
659
394
  const encodedId = encodeURIComponent(id);
660
- return safeWithFallback(
661
- () => this.client.get(`/api/v1/catalogue/bundles/${encodedId}`),
662
- () => this.client.query(`bundles.${id}`)
663
- );
395
+ return safe(this.client.get(`/api/v1/catalogue/bundles/${encodedId}`));
664
396
  }
665
397
  async getBundleBySlug(slug) {
666
398
  const encodedSlug = encodeURIComponent(slug);
667
- const restResult = await safe(
668
- this.client.get(`/api/v1/catalogue/bundles/slug/${encodedSlug}`)
669
- );
670
- if (restResult.ok) return restResult;
671
- if (restResult.error.code !== "HTTP_404" && restResult.error.code !== "API_ERROR") {
672
- return restResult;
673
- }
674
- const result = await safe(
675
- this.client.query(
676
- `bundles[?(@.slug=='${escapeQueryValue(slug)}')]`
677
- )
678
- );
679
- if (!result.ok) return result;
680
- if (!result.value.length) {
681
- return err(new CimplifyError("NOT_FOUND", `Bundle not found: ${slug}`, false));
682
- }
683
- return ok(result.value[0]);
399
+ return safe(this.client.get(`/api/v1/catalogue/bundles/slug/${encodedSlug}`));
684
400
  }
685
401
  async searchBundles(query2, limit = 20) {
686
402
  const path = withQuery("/api/v1/catalogue/bundles", { search: query2, limit });
687
- return safeWithFallback(
688
- () => this.client.get(path),
689
- () => this.client.query(
690
- `bundles[?(@.name contains '${escapeQueryValue(query2)}')]#limit(${limit})`
691
- )
692
- );
403
+ return safe(this.client.get(path));
693
404
  }
694
405
  async getComposites(options) {
695
- let query2 = "composites";
696
- if (options?.limit) {
697
- query2 += `#limit(${options.limit})`;
698
- }
699
406
  const path = withQuery("/api/v1/catalogue/composites", { limit: options?.limit });
700
- return safeWithFallback(
701
- () => this.client.get(path),
702
- () => this.client.query(query2)
703
- );
407
+ return safe(this.client.get(path));
704
408
  }
705
409
  async getComposite(id) {
706
410
  const encodedId = encodeURIComponent(id);
707
- return safeWithFallback(
708
- () => this.client.get(`/api/v1/catalogue/composites/${encodedId}`),
709
- () => this.client.query(`composites.${id}`)
710
- );
411
+ return safe(this.client.get(`/api/v1/catalogue/composites/${encodedId}`));
711
412
  }
712
413
  async getCompositeByProductId(productId) {
713
414
  const encodedId = encodeURIComponent(productId);
714
- return safeWithFallback(
715
- () => this.client.get(
415
+ return safe(
416
+ this.client.get(
716
417
  `/api/v1/catalogue/composites/by-product/${encodedId}`
717
- ),
718
- () => this.client.query(`composites.by_product.${productId}`)
418
+ )
719
419
  );
720
420
  }
721
421
  async calculateCompositePrice(compositeId, selections, locationId) {
722
422
  const encodedId = encodeURIComponent(compositeId);
723
- return safeWithFallback(
724
- () => this.client.post(
423
+ return safe(
424
+ this.client.post(
725
425
  `/api/v1/catalogue/composites/${encodedId}/calculate-price`,
726
426
  {
727
427
  selections,
728
428
  location_id: locationId
729
429
  }
730
- ),
731
- () => this.client.call("composite.calculatePrice", {
732
- composite_id: compositeId,
733
- selections,
734
- location_id: locationId
735
- })
430
+ )
736
431
  );
737
432
  }
738
433
  async fetchQuote(input) {
739
- return safeWithFallback(
740
- () => this.client.post("/api/v1/catalogue/quotes", input),
741
- () => this.client.call("catalogue.createQuote", input)
742
- );
434
+ return safe(this.client.post("/api/v1/catalogue/quotes", input));
743
435
  }
744
436
  async getQuote(quoteId) {
745
437
  const encodedQuoteId = encodeURIComponent(quoteId);
746
- return safeWithFallback(
747
- () => this.client.get(`/api/v1/catalogue/quotes/${encodedQuoteId}`),
748
- () => this.client.call("catalogue.getQuote", {
749
- quote_id: quoteId
750
- })
751
- );
438
+ return safe(this.client.get(`/api/v1/catalogue/quotes/${encodedQuoteId}`));
752
439
  }
753
440
  async refreshQuote(input) {
754
441
  const encodedQuoteId = encodeURIComponent(input.quote_id);
755
- return safeWithFallback(
756
- () => this.client.post(
442
+ return safe(
443
+ this.client.post(
757
444
  `/api/v1/catalogue/quotes/${encodedQuoteId}/refresh`,
758
445
  input
759
- ),
760
- () => this.client.call("catalogue.refreshQuote", input)
446
+ )
761
447
  );
762
448
  }
763
449
  async search(query2, options) {
@@ -773,35 +459,19 @@ var CatalogueQueries = class {
773
459
  return this.search(query2, options);
774
460
  }
775
461
  async getMenu(options) {
776
- let query2 = "menu";
777
- if (options?.category) {
778
- query2 = `menu[?(@.category=='${escapeQueryValue(options.category)}')]`;
779
- }
780
- if (options?.limit) {
781
- query2 += `#limit(${options.limit})`;
782
- }
783
462
  const path = withQuery("/api/v1/catalogue/menu", {
784
463
  category_id: options?.category,
785
464
  limit: options?.limit
786
465
  });
787
- return safeWithFallback(
788
- () => this.client.get(path),
789
- () => this.client.query(query2)
790
- );
466
+ return safe(this.client.get(path));
791
467
  }
792
468
  async getMenuCategory(categoryId) {
793
469
  const encodedId = encodeURIComponent(categoryId);
794
- return safeWithFallback(
795
- () => this.client.get(`/api/v1/catalogue/menu/categories/${encodedId}`),
796
- () => this.client.query(`menu.category.${categoryId}`)
797
- );
470
+ return safe(this.client.get(`/api/v1/catalogue/menu/categories/${encodedId}`));
798
471
  }
799
472
  async getMenuItem(itemId) {
800
473
  const encodedId = encodeURIComponent(itemId);
801
- return safeWithFallback(
802
- () => this.client.get(`/api/v1/catalogue/menu/items/${encodedId}`),
803
- () => this.client.query(`menu.${itemId}`)
804
- );
474
+ return safe(this.client.get(`/api/v1/catalogue/menu/items/${encodedId}`));
805
475
  }
806
476
  };
807
477
 
@@ -820,14 +490,6 @@ async function safe2(promise) {
820
490
  return err(toCimplifyError2(error));
821
491
  }
822
492
  }
823
- async function safeWithFallback2(primary, fallback) {
824
- const primaryResult = await safe2(primary());
825
- if (primaryResult.ok) return primaryResult;
826
- if (primaryResult.error.code !== "HTTP_404" && primaryResult.error.code !== "API_ERROR") {
827
- return primaryResult;
828
- }
829
- return safe2(fallback());
830
- }
831
493
  function isUICartResponse(value) {
832
494
  return "cart" in value;
833
495
  }
@@ -839,36 +501,21 @@ var CartOperations = class {
839
501
  this.client = client;
840
502
  }
841
503
  async get() {
842
- const result = await safeWithFallback2(
843
- () => this.client.get("/api/v1/cart"),
844
- () => this.client.query("cart#enriched")
845
- );
504
+ const result = await safe2(this.client.get("/api/v1/cart"));
846
505
  if (!result.ok) return result;
847
506
  return ok(unwrapEnrichedCart(result.value));
848
507
  }
849
508
  async getRaw() {
850
- return safeWithFallback2(
851
- () => this.client.get("/api/v1/cart"),
852
- () => this.client.query("cart")
853
- );
509
+ return safe2(this.client.get("/api/v1/cart"));
854
510
  }
855
511
  async getItems() {
856
- return safeWithFallback2(
857
- () => this.client.get("/api/v1/cart/items"),
858
- () => this.client.query("cart_items")
859
- );
512
+ return safe2(this.client.get("/api/v1/cart/items"));
860
513
  }
861
514
  async getCount() {
862
- return safeWithFallback2(
863
- () => this.client.get("/api/v1/cart/count"),
864
- () => this.client.query("cart#count")
865
- );
515
+ return safe2(this.client.get("/api/v1/cart/count"));
866
516
  }
867
517
  async getTotal() {
868
- return safeWithFallback2(
869
- () => this.client.get("/api/v1/cart/total"),
870
- () => this.client.query("cart#total")
871
- );
518
+ return safe2(this.client.get("/api/v1/cart/total"));
872
519
  }
873
520
  async getSummary() {
874
521
  const cartResult = await this.get();
@@ -885,66 +532,39 @@ var CartOperations = class {
885
532
  });
886
533
  }
887
534
  async addItem(input) {
888
- return safeWithFallback2(
889
- () => this.client.post("/api/v1/cart/items", input),
890
- () => this.client.call("cart.addItem", input)
891
- );
535
+ return safe2(this.client.post("/api/v1/cart/items", input));
892
536
  }
893
537
  async updateItem(cartItemId, updates) {
894
538
  if (typeof updates.quantity === "number") {
895
539
  return this.updateQuantity(cartItemId, updates.quantity);
896
540
  }
897
541
  const encodedId = encodeURIComponent(cartItemId);
898
- return safeWithFallback2(
899
- () => this.client.patch(`/api/v1/cart/items/${encodedId}`, updates),
900
- () => this.client.call("cart.updateItem", {
901
- cart_item_id: cartItemId,
902
- ...updates
903
- })
904
- );
542
+ return safe2(this.client.patch(`/api/v1/cart/items/${encodedId}`, updates));
905
543
  }
906
544
  async updateQuantity(cartItemId, quantity) {
907
545
  const encodedId = encodeURIComponent(cartItemId);
908
- return safeWithFallback2(
909
- () => this.client.patch(`/api/v1/cart/items/${encodedId}`, {
910
- quantity
911
- }),
912
- () => this.client.call("cart.updateItemQuantity", {
913
- cart_item_id: cartItemId,
546
+ return safe2(
547
+ this.client.patch(`/api/v1/cart/items/${encodedId}`, {
914
548
  quantity
915
549
  })
916
550
  );
917
551
  }
918
552
  async removeItem(cartItemId) {
919
553
  const encodedId = encodeURIComponent(cartItemId);
920
- return safeWithFallback2(
921
- () => this.client.delete(`/api/v1/cart/items/${encodedId}`),
922
- () => this.client.call("cart.removeItem", {
923
- cart_item_id: cartItemId
924
- })
925
- );
554
+ return safe2(this.client.delete(`/api/v1/cart/items/${encodedId}`));
926
555
  }
927
556
  async clear() {
928
- return safeWithFallback2(
929
- () => this.client.delete("/api/v1/cart"),
930
- () => this.client.call("cart.clearCart")
931
- );
557
+ return safe2(this.client.delete("/api/v1/cart"));
932
558
  }
933
559
  async applyCoupon(code) {
934
- return safeWithFallback2(
935
- () => this.client.post("/api/v1/cart/coupons", {
936
- coupon_code: code
937
- }),
938
- () => this.client.call("cart.applyCoupon", {
560
+ return safe2(
561
+ this.client.post("/api/v1/cart/coupons", {
939
562
  coupon_code: code
940
563
  })
941
564
  );
942
565
  }
943
566
  async removeCoupon() {
944
- return safeWithFallback2(
945
- () => this.client.delete("/api/v1/cart/coupons/current"),
946
- () => this.client.call("cart.removeCoupon")
947
- );
567
+ return safe2(this.client.delete("/api/v1/cart/coupons/current"));
948
568
  }
949
569
  async isEmpty() {
950
570
  const countResult = await this.getCount();
@@ -981,35 +601,6 @@ var CartOperations = class {
981
601
  }
982
602
  };
983
603
 
984
- // src/constants.ts
985
- var LINK_QUERY = {
986
- PREFERENCES: "link.preferences"};
987
- var LINK_MUTATION = {
988
- CHECK_STATUS: "link.check_status",
989
- ENROLL: "link.enroll",
990
- ENROLL_AND_LINK_ORDER: "link.enroll_and_link_order",
991
- UPDATE_PREFERENCES: "link.update_preferences",
992
- CREATE_ADDRESS: "link.create_address",
993
- UPDATE_ADDRESS: "link.update_address",
994
- TRACK_ADDRESS_USAGE: "link.track_address_usage",
995
- CREATE_MOBILE_MONEY: "link.create_mobile_money",
996
- TRACK_MOBILE_MONEY_USAGE: "link.track_mobile_money_usage",
997
- VERIFY_MOBILE_MONEY: "link.verify_mobile_money"};
998
- var AUTH_MUTATION = {
999
- REQUEST_OTP: "auth.request_otp",
1000
- VERIFY_OTP: "auth.verify_otp"
1001
- };
1002
- var CHECKOUT_MUTATION = {
1003
- PROCESS: "checkout.process"
1004
- };
1005
- var PAYMENT_MUTATION = {
1006
- SUBMIT_AUTHORIZATION: "payment.submit_authorization",
1007
- CHECK_STATUS: "order.poll_payment_status"
1008
- };
1009
- var ORDER_MUTATION = {
1010
- UPDATE_CUSTOMER: "order.update_order_customer"
1011
- };
1012
-
1013
604
  // src/utils/payment.ts
1014
605
  var PAYMENT_SUCCESS_STATUSES = /* @__PURE__ */ new Set([
1015
606
  "success",
@@ -1704,14 +1295,6 @@ async function safe3(promise) {
1704
1295
  return err(toCimplifyError3(error));
1705
1296
  }
1706
1297
  }
1707
- async function safeWithFallback3(primary, fallback) {
1708
- const primaryResult = await safe3(primary());
1709
- if (primaryResult.ok) return primaryResult;
1710
- if (primaryResult.error.code !== "HTTP_404" && primaryResult.error.code !== "API_ERROR") {
1711
- return primaryResult;
1712
- }
1713
- return safe3(fallback());
1714
- }
1715
1298
  function toTerminalFailure(code, message, recoverable) {
1716
1299
  return {
1717
1300
  success: false,
@@ -1734,57 +1317,51 @@ var CheckoutService = class {
1734
1317
  constructor(client) {
1735
1318
  this.client = client;
1736
1319
  }
1320
+ orderTokenParam(orderId) {
1321
+ const token = this.client.getOrderToken(orderId);
1322
+ return token ? `?token=${encodeURIComponent(token)}` : "";
1323
+ }
1737
1324
  async process(data) {
1738
1325
  const checkoutData = {
1739
1326
  ...data,
1740
1327
  idempotency_key: data.idempotency_key || generateIdempotencyKey()
1741
1328
  };
1742
- return safeWithFallback3(
1743
- () => this.client.post("/api/v1/checkout", {
1744
- checkout_data: checkoutData
1745
- }),
1746
- () => this.client.call(CHECKOUT_MUTATION.PROCESS, {
1329
+ const result = await safe3(
1330
+ this.client.post("/api/v1/checkout", {
1747
1331
  checkout_data: checkoutData
1748
1332
  })
1749
1333
  );
1334
+ if (result.ok && result.value.bill_token) {
1335
+ this.client.setOrderToken(result.value.order_id, result.value.bill_token);
1336
+ }
1337
+ return result;
1750
1338
  }
1751
- async initializePayment(orderId, method) {
1752
- return safe3(
1753
- this.client.call("order.initializePayment", {
1754
- order_id: orderId,
1755
- payment_method: method
1756
- })
1339
+ async initializePayment(_orderId, _method) {
1340
+ return err(
1341
+ new CimplifyError(
1342
+ "NOT_IMPLEMENTED",
1343
+ "initializePayment is not available in REST mode.",
1344
+ false
1345
+ )
1757
1346
  );
1758
1347
  }
1759
1348
  async submitAuthorization(input) {
1760
- return safeWithFallback3(
1761
- () => this.client.post("/api/v1/payments/authorization", input),
1762
- () => this.client.call(PAYMENT_MUTATION.SUBMIT_AUTHORIZATION, input)
1763
- );
1349
+ return safe3(this.client.post("/api/v1/payments/authorization", input));
1764
1350
  }
1765
1351
  async pollPaymentStatus(orderId) {
1766
1352
  const encodedId = encodeURIComponent(orderId);
1767
- return safeWithFallback3(
1768
- () => this.client.get(`/api/v1/orders/${encodedId}/payment-status`),
1769
- () => this.client.call(PAYMENT_MUTATION.CHECK_STATUS, orderId)
1770
- );
1353
+ const tokenParam = this.orderTokenParam(orderId);
1354
+ return safe3(this.client.get(`/api/v1/orders/${encodedId}/payment-status${tokenParam}`));
1771
1355
  }
1772
1356
  async updateOrderCustomer(orderId, customer) {
1773
1357
  const encodedId = encodeURIComponent(orderId);
1774
- return safeWithFallback3(
1775
- () => this.client.post(`/api/v1/orders/${encodedId}/customer`, customer),
1776
- () => this.client.call(ORDER_MUTATION.UPDATE_CUSTOMER, {
1777
- order_id: orderId,
1778
- ...customer
1779
- })
1780
- );
1358
+ const tokenParam = this.orderTokenParam(orderId);
1359
+ return safe3(this.client.post(`/api/v1/orders/${encodedId}/customer${tokenParam}`, customer));
1781
1360
  }
1782
1361
  async verifyPayment(orderId) {
1783
- return safe3(
1784
- this.client.call("order.verifyPayment", {
1785
- order_id: orderId
1786
- })
1787
- );
1362
+ const encodedId = encodeURIComponent(orderId);
1363
+ const tokenParam = this.orderTokenParam(orderId);
1364
+ return safe3(this.client.post(`/api/v1/orders/${encodedId}/verify-payment${tokenParam}`));
1788
1365
  }
1789
1366
  async processAndResolve(data) {
1790
1367
  data.on_status_change?.("preparing", {});
@@ -1903,61 +1480,38 @@ async function safe4(promise) {
1903
1480
  return err(toCimplifyError4(error));
1904
1481
  }
1905
1482
  }
1906
- async function safeWithFallback4(primary, fallback) {
1907
- const primaryResult = await safe4(primary());
1908
- if (primaryResult.ok) return primaryResult;
1909
- if (primaryResult.error.code !== "HTTP_404" && primaryResult.error.code !== "API_ERROR") {
1910
- return primaryResult;
1911
- }
1912
- return safe4(fallback());
1913
- }
1914
1483
  var OrderQueries = class {
1915
1484
  constructor(client) {
1916
1485
  this.client = client;
1917
1486
  }
1487
+ orderTokenParam(orderId) {
1488
+ const token = this.client.getOrderToken(orderId);
1489
+ return token ? `?token=${encodeURIComponent(token)}` : "";
1490
+ }
1918
1491
  async list(options) {
1919
- let query2 = "orders";
1920
- if (options?.status) {
1921
- query2 += `[?(@.status=='${options.status}')]`;
1922
- }
1923
- query2 += "#sort(created_at,desc)";
1924
- if (options?.limit) {
1925
- query2 += `#limit(${options.limit})`;
1926
- }
1927
- if (options?.offset) {
1928
- query2 += `#offset(${options.offset})`;
1929
- }
1930
1492
  const params = new URLSearchParams();
1931
1493
  if (options?.status) params.set("status", options.status);
1932
1494
  if (options?.limit) params.set("limit", String(options.limit));
1933
1495
  if (options?.offset) params.set("offset", String(options.offset));
1934
1496
  const path = params.toString() ? `/api/v1/orders?${params.toString()}` : "/api/v1/orders";
1935
- return safeWithFallback4(
1936
- () => this.client.get(path),
1937
- () => this.client.query(query2)
1938
- );
1497
+ return safe4(this.client.get(path));
1939
1498
  }
1940
1499
  async get(orderId) {
1941
1500
  const encodedId = encodeURIComponent(orderId);
1942
- return safeWithFallback4(
1943
- () => this.client.get(`/api/v1/orders/${encodedId}`),
1944
- () => this.client.query(`orders.${orderId}`)
1945
- );
1501
+ const tokenParam = this.orderTokenParam(orderId);
1502
+ return safe4(this.client.get(`/api/v1/orders/${encodedId}${tokenParam}`));
1946
1503
  }
1947
1504
  async getRecent(limit = 5) {
1948
- return safe4(this.client.query(`orders#sort(created_at,desc)#limit(${limit})`));
1505
+ return this.list({ limit });
1949
1506
  }
1950
1507
  async getByStatus(status) {
1951
1508
  return this.list({ status });
1952
1509
  }
1953
1510
  async cancel(orderId, reason) {
1954
1511
  const encodedId = encodeURIComponent(orderId);
1955
- return safeWithFallback4(
1956
- () => this.client.post(`/api/v1/orders/${encodedId}/cancel`, {
1957
- reason
1958
- }),
1959
- () => this.client.call("order.cancelOrder", {
1960
- order_id: orderId,
1512
+ const tokenParam = this.orderTokenParam(orderId);
1513
+ return safe4(
1514
+ this.client.post(`/api/v1/orders/${encodedId}/cancel${tokenParam}`, {
1961
1515
  reason
1962
1516
  })
1963
1517
  );
@@ -1979,6 +1533,38 @@ async function safe5(promise) {
1979
1533
  return err(toCimplifyError5(error));
1980
1534
  }
1981
1535
  }
1536
+ function encodePathSegment(value) {
1537
+ return encodeURIComponent(value);
1538
+ }
1539
+ function toCreateAddressPayload(input) {
1540
+ return {
1541
+ label: input.label,
1542
+ street_address: input.address_line1,
1543
+ apartment: input.address_line2,
1544
+ city: input.city,
1545
+ region: input.state ?? "",
1546
+ postal_code: input.postal_code,
1547
+ country: input.country
1548
+ };
1549
+ }
1550
+ function toUpdateAddressPayload(input) {
1551
+ return {
1552
+ label: input.label,
1553
+ street_address: input.address_line1,
1554
+ apartment: input.address_line2,
1555
+ city: input.city,
1556
+ region: input.state,
1557
+ postal_code: input.postal_code,
1558
+ country: input.country
1559
+ };
1560
+ }
1561
+ function toCreateMobileMoneyPayload(input) {
1562
+ return {
1563
+ phone_number: input.phone_number,
1564
+ provider: input.provider,
1565
+ label: input.account_name
1566
+ };
1567
+ }
1982
1568
  var LinkService = class {
1983
1569
  constructor(client) {
1984
1570
  this.client = client;
@@ -2006,11 +1592,7 @@ var LinkService = class {
2006
1592
  return result;
2007
1593
  }
2008
1594
  async checkStatus(contact) {
2009
- return safe5(
2010
- this.client.call(LINK_MUTATION.CHECK_STATUS, {
2011
- contact
2012
- })
2013
- );
1595
+ return safe5(this.client.linkPost("/v1/link/check-status", { contact }));
2014
1596
  }
2015
1597
  async getLinkData() {
2016
1598
  return safe5(this.client.linkGet("/v1/link/data"));
@@ -2022,61 +1604,85 @@ var LinkService = class {
2022
1604
  return safe5(this.client.linkGet("/v1/link/mobile-money"));
2023
1605
  }
2024
1606
  async getPreferences() {
2025
- return safe5(this.client.query(LINK_QUERY.PREFERENCES));
1607
+ return safe5(this.client.linkGet("/v1/link/preferences"));
2026
1608
  }
2027
1609
  async enroll(data) {
2028
- return safe5(this.client.call(LINK_MUTATION.ENROLL, data));
1610
+ return safe5(this.client.linkPost("/v1/link/enroll", data));
2029
1611
  }
2030
1612
  async enrollAndLinkOrder(data) {
2031
1613
  return safe5(
2032
- this.client.call(LINK_MUTATION.ENROLL_AND_LINK_ORDER, data)
1614
+ this.client.linkPost("/v1/link/enroll-and-link-order", data)
2033
1615
  );
2034
1616
  }
2035
1617
  async updatePreferences(preferences) {
2036
- return safe5(this.client.call(LINK_MUTATION.UPDATE_PREFERENCES, preferences));
1618
+ return safe5(this.client.linkPost("/v1/link/preferences", preferences));
2037
1619
  }
2038
1620
  async createAddress(input) {
2039
- return safe5(this.client.call(LINK_MUTATION.CREATE_ADDRESS, input));
1621
+ return safe5(
1622
+ this.client.linkPost("/v1/link/addresses", toCreateAddressPayload(input))
1623
+ );
2040
1624
  }
2041
1625
  async updateAddress(input) {
2042
- return safe5(this.client.call(LINK_MUTATION.UPDATE_ADDRESS, input));
1626
+ return safe5(
1627
+ this.client.linkPost(
1628
+ `/v1/link/addresses/${encodePathSegment(input.address_id)}`,
1629
+ toUpdateAddressPayload(input)
1630
+ )
1631
+ );
2043
1632
  }
2044
1633
  async deleteAddress(addressId) {
2045
- return safe5(this.client.linkDelete(`/v1/link/addresses/${addressId}`));
1634
+ return safe5(
1635
+ this.client.linkDelete(`/v1/link/addresses/${encodePathSegment(addressId)}`)
1636
+ );
2046
1637
  }
2047
1638
  async setDefaultAddress(addressId) {
2048
- return safe5(this.client.linkPost(`/v1/link/addresses/${addressId}/default`));
1639
+ return safe5(
1640
+ this.client.linkPost(
1641
+ `/v1/link/addresses/${encodePathSegment(addressId)}/default`
1642
+ )
1643
+ );
2049
1644
  }
2050
1645
  async trackAddressUsage(addressId) {
2051
1646
  return safe5(
2052
- this.client.call(LINK_MUTATION.TRACK_ADDRESS_USAGE, {
2053
- address_id: addressId
2054
- })
1647
+ this.client.linkPost(
1648
+ `/v1/link/addresses/${encodePathSegment(addressId)}/track-usage`
1649
+ )
2055
1650
  );
2056
1651
  }
2057
1652
  async createMobileMoney(input) {
2058
- return safe5(this.client.call(LINK_MUTATION.CREATE_MOBILE_MONEY, input));
1653
+ return safe5(
1654
+ this.client.linkPost(
1655
+ "/v1/link/mobile-money",
1656
+ toCreateMobileMoneyPayload(input)
1657
+ )
1658
+ );
2059
1659
  }
2060
1660
  async deleteMobileMoney(mobileMoneyId) {
2061
1661
  return safe5(
2062
- this.client.linkDelete(`/v1/link/mobile-money/${mobileMoneyId}`)
1662
+ this.client.linkDelete(
1663
+ `/v1/link/mobile-money/${encodePathSegment(mobileMoneyId)}`
1664
+ )
2063
1665
  );
2064
1666
  }
2065
1667
  async setDefaultMobileMoney(mobileMoneyId) {
2066
1668
  return safe5(
2067
- this.client.linkPost(`/v1/link/mobile-money/${mobileMoneyId}/default`)
1669
+ this.client.linkPost(
1670
+ `/v1/link/mobile-money/${encodePathSegment(mobileMoneyId)}/default`
1671
+ )
2068
1672
  );
2069
1673
  }
2070
1674
  async trackMobileMoneyUsage(mobileMoneyId) {
2071
1675
  return safe5(
2072
- this.client.call(LINK_MUTATION.TRACK_MOBILE_MONEY_USAGE, {
2073
- mobile_money_id: mobileMoneyId
2074
- })
1676
+ this.client.linkPost(
1677
+ `/v1/link/mobile-money/${encodePathSegment(mobileMoneyId)}/track-usage`
1678
+ )
2075
1679
  );
2076
1680
  }
2077
1681
  async verifyMobileMoney(mobileMoneyId) {
2078
1682
  return safe5(
2079
- this.client.call(LINK_MUTATION.VERIFY_MOBILE_MONEY, mobileMoneyId)
1683
+ this.client.linkPost(
1684
+ `/v1/link/mobile-money/${encodePathSegment(mobileMoneyId)}/verify`
1685
+ )
2080
1686
  );
2081
1687
  }
2082
1688
  async getSessions() {
@@ -2089,30 +1695,28 @@ var LinkService = class {
2089
1695
  return safe5(this.client.linkDelete("/v1/link/sessions"));
2090
1696
  }
2091
1697
  async getAddressesRest() {
2092
- return safe5(this.client.linkGet("/v1/link/addresses"));
1698
+ return this.getAddresses();
2093
1699
  }
2094
1700
  async createAddressRest(input) {
2095
- return safe5(this.client.linkPost("/v1/link/addresses", input));
1701
+ return this.createAddress(input);
2096
1702
  }
2097
1703
  async deleteAddressRest(addressId) {
2098
- return safe5(this.client.linkDelete(`/v1/link/addresses/${addressId}`));
1704
+ return this.deleteAddress(addressId);
2099
1705
  }
2100
1706
  async setDefaultAddressRest(addressId) {
2101
- return safe5(this.client.linkPost(`/v1/link/addresses/${addressId}/default`));
1707
+ return this.setDefaultAddress(addressId);
2102
1708
  }
2103
1709
  async getMobileMoneyRest() {
2104
- return safe5(this.client.linkGet("/v1/link/mobile-money"));
1710
+ return this.getMobileMoney();
2105
1711
  }
2106
1712
  async createMobileMoneyRest(input) {
2107
- return safe5(this.client.linkPost("/v1/link/mobile-money", input));
1713
+ return this.createMobileMoney(input);
2108
1714
  }
2109
1715
  async deleteMobileMoneyRest(mobileMoneyId) {
2110
- return safe5(this.client.linkDelete(`/v1/link/mobile-money/${mobileMoneyId}`));
1716
+ return this.deleteMobileMoney(mobileMoneyId);
2111
1717
  }
2112
1718
  async setDefaultMobileMoneyRest(mobileMoneyId) {
2113
- return safe5(
2114
- this.client.linkPost(`/v1/link/mobile-money/${mobileMoneyId}/default`)
2115
- );
1719
+ return this.setDefaultMobileMoney(mobileMoneyId);
2116
1720
  }
2117
1721
  };
2118
1722
 
@@ -2131,23 +1735,12 @@ async function safe6(promise) {
2131
1735
  return err(toCimplifyError6(error));
2132
1736
  }
2133
1737
  }
2134
- async function safeWithFallback5(primary, fallback) {
2135
- const primaryResult = await safe6(primary());
2136
- if (primaryResult.ok) return primaryResult;
2137
- if (primaryResult.error.code !== "HTTP_404" && primaryResult.error.code !== "API_ERROR") {
2138
- return primaryResult;
2139
- }
2140
- return safe6(fallback());
2141
- }
2142
1738
  var AuthService = class {
2143
1739
  constructor(client) {
2144
1740
  this.client = client;
2145
1741
  }
2146
1742
  async getStatus() {
2147
- return safeWithFallback5(
2148
- () => this.client.get("/api/v1/auth/status"),
2149
- () => this.client.query("auth")
2150
- );
1743
+ return safe6(this.client.get("/api/v1/auth/status"));
2151
1744
  }
2152
1745
  async getCurrentUser() {
2153
1746
  const result = await this.getStatus();
@@ -2160,43 +1753,26 @@ var AuthService = class {
2160
1753
  return ok(result.value.is_authenticated);
2161
1754
  }
2162
1755
  async requestOtp(contact, contactType) {
2163
- return safeWithFallback5(
2164
- () => this.client.post("/api/v1/auth/request-otp", {
2165
- contact,
2166
- contact_type: contactType
2167
- }),
2168
- () => this.client.call(AUTH_MUTATION.REQUEST_OTP, {
1756
+ return safe6(
1757
+ this.client.post("/api/v1/auth/request-otp", {
2169
1758
  contact,
2170
1759
  contact_type: contactType
2171
1760
  })
2172
1761
  );
2173
1762
  }
2174
1763
  async verifyOtp(code, contact) {
2175
- return safeWithFallback5(
2176
- () => this.client.post("/api/v1/auth/verify-otp", {
2177
- otp_code: code,
2178
- contact
2179
- }),
2180
- () => this.client.call(AUTH_MUTATION.VERIFY_OTP, {
1764
+ return safe6(
1765
+ this.client.post("/api/v1/auth/verify-otp", {
2181
1766
  otp_code: code,
2182
1767
  contact
2183
1768
  })
2184
1769
  );
2185
1770
  }
2186
1771
  async logout() {
2187
- return safeWithFallback5(
2188
- () => this.client.post("/api/v1/auth/logout"),
2189
- () => this.client.call("auth.logout")
2190
- );
1772
+ return safe6(this.client.post("/api/v1/auth/logout"));
2191
1773
  }
2192
1774
  async updateProfile(input) {
2193
- return safe6(this.client.call("auth.update_profile", input));
2194
- }
2195
- async changePassword(input) {
2196
- return safe6(this.client.call("auth.change_password", input));
2197
- }
2198
- async resetPassword(email) {
2199
- return safe6(this.client.call("auth.reset_password", { email }));
1775
+ return safe6(this.client.post("/api/v1/auth/profile", input));
2200
1776
  }
2201
1777
  };
2202
1778
 
@@ -2215,47 +1791,29 @@ async function safe7(promise) {
2215
1791
  return err(toCimplifyError7(error));
2216
1792
  }
2217
1793
  }
2218
- async function safeWithFallback6(primary, fallback) {
2219
- const primaryResult = await safe7(primary());
2220
- if (primaryResult.ok) return primaryResult;
2221
- if (primaryResult.error.code !== "HTTP_404" && primaryResult.error.code !== "API_ERROR") {
2222
- return primaryResult;
2223
- }
2224
- return safe7(fallback());
2225
- }
2226
1794
  var BusinessService = class {
2227
1795
  constructor(client) {
2228
1796
  this.client = client;
2229
1797
  }
2230
1798
  async getInfo() {
2231
- return safeWithFallback6(
2232
- () => this.client.get("/api/v1/business"),
2233
- () => this.client.query("business.info")
2234
- );
1799
+ return safe7(this.client.get("/api/v1/business"));
2235
1800
  }
2236
1801
  async getByHandle(handle) {
2237
- return safe7(this.client.query(`business.handle.${handle}`));
1802
+ const encodedHandle = encodeURIComponent(handle);
1803
+ return safe7(this.client.get(`/api/v1/business/by-handle/${encodedHandle}`));
2238
1804
  }
2239
1805
  async getByDomain(domain) {
2240
- return safe7(this.client.query("business.domain", { domain }));
1806
+ const encodedDomain = encodeURIComponent(domain);
1807
+ return safe7(this.client.get(`/api/v1/business/by-domain?domain=${encodedDomain}`));
2241
1808
  }
2242
1809
  async getSettings() {
2243
- return safeWithFallback6(
2244
- () => this.client.get("/api/v1/business/settings"),
2245
- () => this.client.query("business.settings")
2246
- );
1810
+ return safe7(this.client.get("/api/v1/business/settings"));
2247
1811
  }
2248
1812
  async getTheme() {
2249
- return safeWithFallback6(
2250
- () => this.client.get("/api/v1/business/theme"),
2251
- () => this.client.query("business.theme")
2252
- );
1813
+ return safe7(this.client.get("/api/v1/business/theme"));
2253
1814
  }
2254
1815
  async getLocations() {
2255
- return safeWithFallback6(
2256
- () => this.client.get("/api/v1/business/locations"),
2257
- () => this.client.query("business.locations")
2258
- );
1816
+ return safe7(this.client.get("/api/v1/business/locations"));
2259
1817
  }
2260
1818
  async getLocation(locationId) {
2261
1819
  const result = await this.getLocations();
@@ -2267,10 +1825,7 @@ var BusinessService = class {
2267
1825
  return ok(location);
2268
1826
  }
2269
1827
  async getHours() {
2270
- return safeWithFallback6(
2271
- () => this.client.get("/api/v1/business/hours"),
2272
- () => this.client.query("business.hours")
2273
- );
1828
+ return safe7(this.client.get("/api/v1/business/hours"));
2274
1829
  }
2275
1830
  async getLocationHours(locationId) {
2276
1831
  const result = await this.getHours();
@@ -2278,31 +1833,7 @@ var BusinessService = class {
2278
1833
  return ok(result.value.filter((hour) => hour.location_id === locationId));
2279
1834
  }
2280
1835
  async getBootstrap() {
2281
- const restBootstrap = await safe7(this.client.get("/api/v1/bootstrap"));
2282
- if (restBootstrap.ok) {
2283
- return restBootstrap;
2284
- }
2285
- const [businessResult, locationsResult, categoriesResult] = await Promise.all([
2286
- this.getInfo(),
2287
- this.getLocations(),
2288
- safe7(this.client.query("categories#select(id,name,slug)"))
2289
- ]);
2290
- if (!businessResult.ok) return businessResult;
2291
- if (!locationsResult.ok) return locationsResult;
2292
- if (!categoriesResult.ok) return categoriesResult;
2293
- const business = businessResult.value;
2294
- const locations = locationsResult.value;
2295
- const categories = categoriesResult.value;
2296
- const defaultLocation = locations[0];
2297
- return ok({
2298
- business,
2299
- location: defaultLocation,
2300
- locations,
2301
- categories,
2302
- currency: business.default_currency,
2303
- is_open: defaultLocation?.accepts_online_orders ?? false,
2304
- accepts_orders: defaultLocation?.accepts_online_orders ?? false
2305
- });
1836
+ return safe7(this.client.get("/api/v1/bootstrap"));
2306
1837
  }
2307
1838
  };
2308
1839
 
@@ -2325,49 +1856,47 @@ var InventoryService = class {
2325
1856
  constructor(client) {
2326
1857
  this.client = client;
2327
1858
  }
1859
+ withQuery(path, params) {
1860
+ const searchParams = new URLSearchParams();
1861
+ for (const [key, value] of Object.entries(params)) {
1862
+ if (value === void 0) continue;
1863
+ searchParams.set(key, String(value));
1864
+ }
1865
+ const query2 = searchParams.toString();
1866
+ return query2 ? `${path}?${query2}` : path;
1867
+ }
2328
1868
  async getStockLevels() {
2329
- return safe8(this.client.query("inventory.stock_levels"));
1869
+ return safe8(this.client.get("/api/v1/inventory/stock-levels"));
2330
1870
  }
2331
1871
  async getProductStock(productId, locationId) {
2332
- if (locationId) {
2333
- return safe8(
2334
- this.client.query("inventory.product", {
2335
- product_id: productId,
2336
- location_id: locationId
2337
- })
2338
- );
2339
- }
2340
- return safe8(
2341
- this.client.query("inventory.product", {
2342
- product_id: productId
2343
- })
2344
- );
1872
+ const encodedId = encodeURIComponent(productId);
1873
+ const path = this.withQuery(`/api/v1/inventory/products/${encodedId}/stock`, {
1874
+ location_id: locationId
1875
+ });
1876
+ return safe8(this.client.get(path));
2345
1877
  }
2346
1878
  async getVariantStock(variantId, locationId) {
2347
- return safe8(
2348
- this.client.query("inventory.variant", {
2349
- variant_id: variantId,
2350
- location_id: locationId
2351
- })
2352
- );
1879
+ const encodedId = encodeURIComponent(variantId);
1880
+ const path = this.withQuery(`/api/v1/inventory/variants/${encodedId}/stock`, {
1881
+ location_id: locationId
1882
+ });
1883
+ return safe8(this.client.get(path));
2353
1884
  }
2354
1885
  async checkProductAvailability(productId, quantity, locationId) {
2355
- return safe8(
2356
- this.client.query("inventory.check_availability", {
2357
- product_id: productId,
2358
- quantity,
2359
- location_id: locationId
2360
- })
2361
- );
1886
+ const encodedId = encodeURIComponent(productId);
1887
+ const path = this.withQuery(`/api/v1/inventory/products/${encodedId}/availability`, {
1888
+ quantity,
1889
+ location_id: locationId
1890
+ });
1891
+ return safe8(this.client.get(path));
2362
1892
  }
2363
1893
  async checkVariantAvailability(variantId, quantity, locationId) {
2364
- return safe8(
2365
- this.client.query("inventory.check_availability", {
2366
- variant_id: variantId,
2367
- quantity,
2368
- location_id: locationId
2369
- })
2370
- );
1894
+ const encodedId = encodeURIComponent(variantId);
1895
+ const path = this.withQuery(`/api/v1/inventory/variants/${encodedId}/availability`, {
1896
+ quantity,
1897
+ location_id: locationId
1898
+ });
1899
+ return safe8(this.client.get(path));
2371
1900
  }
2372
1901
  async checkMultipleAvailability(items, locationId) {
2373
1902
  const results = await Promise.all(
@@ -2381,7 +1910,7 @@ var InventoryService = class {
2381
1910
  return ok(results.map((r) => r.value));
2382
1911
  }
2383
1912
  async getSummary() {
2384
- return safe8(this.client.query("inventory.summary"));
1913
+ return safe8(this.client.get("/api/v1/inventory/summary"));
2385
1914
  }
2386
1915
  async isInStock(productId, locationId) {
2387
1916
  const result = await this.checkProductAvailability(productId, 1, locationId);
@@ -2396,9 +1925,6 @@ var InventoryService = class {
2396
1925
  };
2397
1926
 
2398
1927
  // src/scheduling.ts
2399
- function toVariables(input) {
2400
- return Object.fromEntries(Object.entries(input));
2401
- }
2402
1928
  function toCimplifyError9(error) {
2403
1929
  if (error instanceof CimplifyError) return error;
2404
1930
  if (error instanceof Error) {
@@ -2413,68 +1939,110 @@ async function safe9(promise) {
2413
1939
  return err(toCimplifyError9(error));
2414
1940
  }
2415
1941
  }
1942
+ function withQuery2(path, params) {
1943
+ const searchParams = new URLSearchParams();
1944
+ for (const [key, value] of Object.entries(params)) {
1945
+ if (value === void 0) continue;
1946
+ searchParams.set(key, String(value));
1947
+ }
1948
+ const query2 = searchParams.toString();
1949
+ return query2 ? `${path}?${query2}` : path;
1950
+ }
1951
+ function normalizeServiceAvailability(result) {
1952
+ if (Array.isArray(result.days) && result.days.length > 0) {
1953
+ return result;
1954
+ }
1955
+ const days = Array.isArray(result.availability) ? result.availability.map((day) => ({
1956
+ ...day,
1957
+ is_fully_booked: day.is_fully_booked ?? (typeof day.has_availability === "boolean" ? !day.has_availability : void 0)
1958
+ })) : [];
1959
+ return { ...result, days };
1960
+ }
1961
+ function firstScheduledTime(booking) {
1962
+ return booking.service_items.find((item) => typeof item.scheduled_start === "string")?.scheduled_start || booking.created_at;
1963
+ }
2416
1964
  var SchedulingService = class {
2417
1965
  constructor(client) {
2418
1966
  this.client = client;
2419
1967
  }
2420
1968
  async getServices() {
2421
- return safe9(this.client.query("scheduling.services"));
1969
+ return safe9(this.client.get("/api/v1/scheduling/services"));
2422
1970
  }
2423
1971
  /**
2424
- * Get a specific service by ID
2425
- * Note: Filters from all services client-side (no single-service endpoint)
1972
+ * Get a specific service by ID.
2426
1973
  */
2427
1974
  async getService(serviceId) {
2428
1975
  const result = await this.getServices();
2429
1976
  if (!result.ok) return result;
2430
- return ok(result.value.find((s) => s.id === serviceId) || null);
1977
+ return ok(result.value.find((service) => service.id === serviceId) || null);
2431
1978
  }
2432
1979
  async getAvailableSlots(input) {
2433
- return safe9(
2434
- this.client.query("scheduling.slots", toVariables(input))
2435
- );
1980
+ const path = withQuery2("/api/v1/scheduling/slots", {
1981
+ service_id: input.service_id,
1982
+ date: input.date,
1983
+ participant_count: input.participant_count,
1984
+ duration_minutes: input.duration_minutes
1985
+ });
1986
+ return safe9(this.client.get(path));
2436
1987
  }
2437
1988
  async checkSlotAvailability(input) {
2438
- return safe9(
2439
- this.client.query(
2440
- "scheduling.check_availability",
2441
- toVariables(input)
2442
- )
2443
- );
1989
+ const path = withQuery2("/api/v1/scheduling/slots/check", {
1990
+ service_id: input.service_id,
1991
+ slot_time: input.slot_time,
1992
+ duration_minutes: input.duration_minutes,
1993
+ participant_count: input.participant_count
1994
+ });
1995
+ return safe9(this.client.get(path));
2444
1996
  }
2445
1997
  async getServiceAvailability(params) {
1998
+ const path = withQuery2("/api/v1/scheduling/availability", {
1999
+ service_id: params.service_id,
2000
+ start_date: params.start_date,
2001
+ end_date: params.end_date,
2002
+ location_id: params.location_id,
2003
+ participant_count: params.participant_count
2004
+ });
2446
2005
  return safe9(
2447
- this.client.query(
2448
- "scheduling.availability",
2449
- toVariables(params)
2450
- )
2006
+ this.client.get(path).then((result) => normalizeServiceAvailability(result))
2451
2007
  );
2452
2008
  }
2453
2009
  async getBooking(bookingId) {
2454
- return safe9(this.client.query(`scheduling.${bookingId}`));
2010
+ const encodedId = encodeURIComponent(bookingId);
2011
+ return safe9(this.client.get(`/api/v1/scheduling/bookings/${encodedId}`));
2455
2012
  }
2456
- async getCustomerBookings() {
2457
- return safe9(this.client.query("scheduling"));
2013
+ async getCustomerBookings(customerId) {
2014
+ const path = withQuery2("/api/v1/scheduling/bookings", {
2015
+ customer_id: customerId
2016
+ });
2017
+ return safe9(this.client.get(path));
2458
2018
  }
2459
2019
  async getUpcomingBookings() {
2460
- return safe9(
2461
- this.client.query(
2462
- "scheduling[?(@.status!='completed' && @.status!='cancelled')]#sort(start_time,asc)"
2463
- )
2464
- );
2020
+ const result = await this.getCustomerBookings();
2021
+ if (!result.ok) return result;
2022
+ const upcoming = result.value.filter((booking) => {
2023
+ const status = String(booking.status).toLowerCase();
2024
+ return status !== "completed" && status !== "cancelled";
2025
+ }).sort((a, b) => firstScheduledTime(a).localeCompare(firstScheduledTime(b)));
2026
+ return ok(upcoming);
2465
2027
  }
2466
2028
  async getPastBookings(limit = 10) {
2467
- return safe9(
2468
- this.client.query(
2469
- `scheduling[?(@.status=='completed')]#sort(start_time,desc)#limit(${limit})`
2470
- )
2471
- );
2029
+ const result = await this.getCustomerBookings();
2030
+ if (!result.ok) return result;
2031
+ const past = result.value.filter((booking) => String(booking.status).toLowerCase() === "completed").sort((a, b) => firstScheduledTime(b).localeCompare(firstScheduledTime(a))).slice(0, Math.max(0, limit));
2032
+ return ok(past);
2472
2033
  }
2473
2034
  async cancelBooking(input) {
2474
- return safe9(this.client.call("scheduling.cancel_booking", input));
2035
+ const encodedId = encodeURIComponent(input.booking_id);
2036
+ return safe9(
2037
+ this.client.post(`/api/v1/scheduling/bookings/${encodedId}/cancel`, {
2038
+ reason: input.reason
2039
+ })
2040
+ );
2475
2041
  }
2476
2042
  async rescheduleBooking(input) {
2477
- return safe9(this.client.call("scheduling.reschedule_booking", input));
2043
+ return safe9(
2044
+ this.client.post("/api/v1/scheduling/bookings/reschedule", input)
2045
+ );
2478
2046
  }
2479
2047
  async getNextAvailableSlot(serviceId, fromDate) {
2480
2048
  const date = fromDate || (/* @__PURE__ */ new Date()).toISOString().split("T")[0];
@@ -2493,6 +2061,12 @@ var SchedulingService = class {
2493
2061
  if (!result.ok) return result;
2494
2062
  return ok(result.value.some((slot) => slot.is_available));
2495
2063
  }
2064
+ // Compatibility alias for callers typed against the older Booking shape.
2065
+ async getBookingLegacy(bookingId) {
2066
+ const result = await this.getBooking(bookingId);
2067
+ if (!result.ok) return result;
2068
+ return ok(result.value);
2069
+ }
2496
2070
  };
2497
2071
 
2498
2072
  // src/lite.ts
@@ -2515,47 +2089,18 @@ var LiteService = class {
2515
2089
  this.client = client;
2516
2090
  }
2517
2091
  async getBootstrap() {
2518
- return safe10(this.client.query("lite.bootstrap"));
2092
+ return safe10(this.client.get("/api/v1/lite/bootstrap"));
2519
2093
  }
2520
2094
  async getTable(tableId) {
2521
- return safe10(this.client.query(`lite.table.${tableId}`));
2522
- }
2523
- async getTableByNumber(tableNumber, locationId) {
2524
- return safe10(
2525
- this.client.query("lite.table_by_number", {
2526
- table_number: tableNumber,
2527
- location_id: locationId
2528
- })
2529
- );
2530
- }
2531
- async sendToKitchen(tableId, items) {
2532
- return safe10(
2533
- this.client.call("lite.send_to_kitchen", {
2534
- table_id: tableId,
2535
- items
2536
- })
2537
- );
2538
- }
2539
- async callWaiter(tableId, reason) {
2540
- return safe10(
2541
- this.client.call("lite.call_waiter", {
2542
- table_id: tableId,
2543
- reason
2544
- })
2545
- );
2546
- }
2547
- async requestBill(tableId) {
2548
- return safe10(
2549
- this.client.call("lite.request_bill", {
2550
- table_id: tableId
2551
- })
2552
- );
2095
+ const encodedId = encodeURIComponent(tableId);
2096
+ return safe10(this.client.get(`/api/v1/lite/tables/${encodedId}`));
2553
2097
  }
2554
2098
  async getMenu() {
2555
- return safe10(this.client.query("lite.menu"));
2099
+ return safe10(this.client.get("/api/v1/lite/menu"));
2556
2100
  }
2557
2101
  async getMenuByCategory(categoryId) {
2558
- return safe10(this.client.query(`lite.menu.category.${categoryId}`));
2102
+ const encodedId = encodeURIComponent(categoryId);
2103
+ return safe10(this.client.get(`/api/v1/lite/menu/categories/${encodedId}`));
2559
2104
  }
2560
2105
  };
2561
2106
 
@@ -2574,30 +2119,16 @@ async function safe11(promise) {
2574
2119
  return err(toCimplifyError11(error));
2575
2120
  }
2576
2121
  }
2577
- async function safeWithFallback7(primary, fallback) {
2578
- const primaryResult = await safe11(primary());
2579
- if (primaryResult.ok) return primaryResult;
2580
- if (primaryResult.error.code !== "HTTP_404" && primaryResult.error.code !== "API_ERROR") {
2581
- return primaryResult;
2582
- }
2583
- return safe11(fallback());
2584
- }
2585
2122
  var FxService = class {
2586
2123
  constructor(client) {
2587
2124
  this.client = client;
2588
2125
  }
2589
2126
  async getRate(from, to) {
2590
2127
  const path = `/api/v1/fx/rate?from=${encodeURIComponent(from)}&to=${encodeURIComponent(to)}`;
2591
- return safeWithFallback7(
2592
- () => this.client.get(path),
2593
- () => this.client.call("fx.getRate", { from, to })
2594
- );
2128
+ return safe11(this.client.get(path));
2595
2129
  }
2596
2130
  async lockQuote(request) {
2597
- return safeWithFallback7(
2598
- () => this.client.post("/api/v1/fx/quotes", request),
2599
- () => this.client.call("fx.lockQuote", request)
2600
- );
2131
+ return safe11(this.client.post("/api/v1/fx/quotes", request));
2601
2132
  }
2602
2133
  };
2603
2134
 
@@ -3206,6 +2737,74 @@ function createElements(client, businessId, options) {
3206
2737
  return new CimplifyElements(client, businessId, options);
3207
2738
  }
3208
2739
 
2740
+ // src/query/builder.ts
2741
+ function escapeQueryValue(value) {
2742
+ return value.replace(/'/g, "\\'");
2743
+ }
2744
+ var QueryBuilder = class {
2745
+ constructor(entity) {
2746
+ this.filters = [];
2747
+ this.modifiers = [];
2748
+ this.pathSegments = [];
2749
+ this.entity = entity;
2750
+ }
2751
+ path(segment) {
2752
+ this.pathSegments.push(segment);
2753
+ return this;
2754
+ }
2755
+ where(field, op, value) {
2756
+ const v = typeof value === "string" ? `'${escapeQueryValue(value)}'` : value;
2757
+ if (op === "contains" || op === "startsWith") {
2758
+ this.filters.push(`@.${field} ${op} ${v}`);
2759
+ } else {
2760
+ this.filters.push(`@.${field}${op}${v}`);
2761
+ }
2762
+ return this;
2763
+ }
2764
+ and(field, op, value) {
2765
+ return this.where(field, op, value);
2766
+ }
2767
+ sort(field, order = "asc") {
2768
+ this.modifiers.push(`sort(${field},${order})`);
2769
+ return this;
2770
+ }
2771
+ limit(n) {
2772
+ this.modifiers.push(`limit(${n})`);
2773
+ return this;
2774
+ }
2775
+ offset(n) {
2776
+ this.modifiers.push(`offset(${n})`);
2777
+ return this;
2778
+ }
2779
+ count() {
2780
+ this.modifiers.push("count");
2781
+ return this;
2782
+ }
2783
+ enriched() {
2784
+ this.modifiers.push("enriched");
2785
+ return this;
2786
+ }
2787
+ build() {
2788
+ let query2 = this.entity;
2789
+ if (this.pathSegments.length > 0) {
2790
+ query2 += "." + this.pathSegments.join(".");
2791
+ }
2792
+ if (this.filters.length > 0) {
2793
+ query2 += `[?(${this.filters.join(" && ")})]`;
2794
+ }
2795
+ for (const mod of this.modifiers) {
2796
+ query2 += `#${mod}`;
2797
+ }
2798
+ return query2;
2799
+ }
2800
+ toString() {
2801
+ return this.build();
2802
+ }
2803
+ };
2804
+ function query(entity) {
2805
+ return new QueryBuilder(entity);
2806
+ }
2807
+
3209
2808
  exports.AuthService = AuthService;
3210
2809
  exports.BusinessService = BusinessService;
3211
2810
  exports.CartOperations = CartOperations;