order3000 0.16.0 → 0.18.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/client.mjs CHANGED
@@ -32,6 +32,24 @@ class TokenManager {
32
32
  }
33
33
  return this.defaultVenueId;
34
34
  }
35
+ /**
36
+ * Async counterpart to {@link getVenueId}. Fetches the token if it
37
+ * hasn't been loaded yet (which is what populates `defaultVenueId` via
38
+ * `setDefaultVenueId`), then returns the venue id.
39
+ *
40
+ * Prefer this over `getVenueId()` from request paths — a cold SDK
41
+ * instance will otherwise synchronously throw "No venue ID available"
42
+ * before the token has had a chance to load.
43
+ */
44
+ async getVenueIdAsync() {
45
+ if (this.userVenueId) {
46
+ return this.userVenueId;
47
+ }
48
+ if (!this.defaultVenueId) {
49
+ await this.getToken();
50
+ }
51
+ return this.getVenueId();
52
+ }
35
53
  async refreshToken() {
36
54
  if (this.refreshPromise) return this.refreshPromise;
37
55
  this.refreshPromise = fetch(this.tokenEndpoint, {
@@ -83,11 +101,11 @@ class BaseResource {
83
101
  this.baseUrl = baseUrl;
84
102
  this.resourcePath = resourcePath;
85
103
  }
86
- async request(method, path, data) {
104
+ async request(method, path, data, cacheOptions) {
87
105
  const token = await this.tokenManager.getToken();
88
106
  const venueId = this.tokenManager.getVenueId();
89
107
  const url = `${this.baseUrl}/venues/${venueId}${this.resourcePath}${path}`;
90
- const response = await fetch(url, {
108
+ const init = {
91
109
  method,
92
110
  headers: {
93
111
  Authorization: `Bearer ${token}`,
@@ -95,14 +113,75 @@ class BaseResource {
95
113
  },
96
114
  body: data ? JSON.stringify(data) : void 0,
97
115
  signal: AbortSignal.timeout(36e4)
98
- });
116
+ };
117
+ if (cacheOptions?.next !== void 0) {
118
+ init.next = cacheOptions.next;
119
+ } else {
120
+ init.cache = cacheOptions?.cache ?? "no-store";
121
+ }
122
+ const response = await fetch(url, init);
99
123
  if (!response.ok) {
100
- throw new Error(`HTTP ${response.status}: ${response.statusText}`);
124
+ let body = "";
125
+ try {
126
+ body = await response.text();
127
+ } catch {
128
+ }
129
+ throw new Error(
130
+ `HTTP ${response.status}: ${response.statusText}${body ? ` \u2014 ${body}` : ""}`
131
+ );
101
132
  }
102
133
  return response.json();
103
134
  }
104
135
  }
105
136
 
137
+ class AreasResource extends BaseResource {
138
+ constructor(tokenManager, baseUrl) {
139
+ super(tokenManager, baseUrl, "/areas");
140
+ }
141
+ /**
142
+ * List bookable + public areas for the venue.
143
+ */
144
+ async list(options) {
145
+ const queryString = this.buildExpandQuery(options?.expand);
146
+ return this.request("GET", queryString);
147
+ }
148
+ /**
149
+ * Fetch a single area by id or accessor.
150
+ */
151
+ async getByAccessor(accessor, options) {
152
+ const queryString = this.buildExpandQuery(options?.expand);
153
+ return this.request("GET", `/${accessor}${queryString}`);
154
+ }
155
+ /**
156
+ * List occupied date/time windows for an area between `from` and `to`.
157
+ */
158
+ async getAvailability(accessor, options) {
159
+ const params = new URLSearchParams();
160
+ if (options?.from) params.set("from", options.from);
161
+ if (options?.to) params.set("to", options.to);
162
+ const qs = params.toString();
163
+ const query = qs ? `?${qs}` : "";
164
+ return this.request(
165
+ "GET",
166
+ `/${accessor}/availability${query}`
167
+ );
168
+ }
169
+ /**
170
+ * Submit a public booking request for an area.
171
+ */
172
+ async submitBookingRequest(accessor, payload) {
173
+ return this.request(
174
+ "POST",
175
+ `/${accessor}/booking-requests`,
176
+ payload
177
+ );
178
+ }
179
+ buildExpandQuery(expand) {
180
+ if (!expand?.length) return "";
181
+ return `?expand=${expand.join(",")}`;
182
+ }
183
+ }
184
+
106
185
  class BasketsResource extends BaseResource {
107
186
  constructor(tokenManager, baseUrl) {
108
187
  super(tokenManager, baseUrl, "/baskets");
@@ -144,6 +223,51 @@ class BasketsResource extends BaseResource {
144
223
  `/items/${basketItemId}?${params.toString()}`
145
224
  );
146
225
  }
226
+ /**
227
+ * Canonical totals for a basket. Clients read every displayed monetary
228
+ * figure (effective total, deposit credit, cash refund) from this
229
+ * endpoint — they never multiply deposit counts by amounts locally.
230
+ *
231
+ * Pass `declaredReturns` as references to depositware templates plus a
232
+ * count. order3000 refuses deposit money amounts on input.
233
+ */
234
+ async getTotals(basketId, body = {}) {
235
+ return this.request(
236
+ "POST",
237
+ `/${basketId}/totals`,
238
+ { declaredReturns: body.declaredReturns ?? [] }
239
+ );
240
+ }
241
+ /**
242
+ * Mark every active basket for the session as `abandoned`.
243
+ *
244
+ * Used by thamarai's "Neu starten" flow: after a non-team order
245
+ * has been placed (its basket converted), the user might want to
246
+ * start a fresh order. A subsequent `add()` call would otherwise
247
+ * reuse any leftover active basket on the same session — which
248
+ * can happen if a prior compose flow left an active row behind.
249
+ * Calling `abandon()` first guarantees the next `add()` inserts a
250
+ * brand-new active basket with exactly one item.
251
+ */
252
+ async abandon(sessionId) {
253
+ return this.request("POST", "/abandon", {
254
+ sessionId
255
+ });
256
+ }
257
+ /**
258
+ * Persist per-template declared-return counts onto the basket.
259
+ *
260
+ * Used by team-order participants so the Lead's breakdown popover
261
+ * sees every member's intent in real time (the standard
262
+ * `useTeamOrderRealtime` postgres-changes subscription on
263
+ * `baskets` then propagates the change to the team).
264
+ *
265
+ * Server validates `sessionId === basket.session_id` — only the
266
+ * basket's owner can write to its declared returns.
267
+ */
268
+ async setDeclaredReturns(basketId, body) {
269
+ return this.request("POST", `/${basketId}/declared-returns`, body);
270
+ }
147
271
  }
148
272
 
149
273
  class EventsResource extends BaseResource {
@@ -203,7 +327,12 @@ class EventsResource extends BaseResource {
203
327
  */
204
328
  async getByAccessor(accessor, options) {
205
329
  const queryString = this.buildQueryString(options);
206
- return this.request("GET", `/${accessor}${queryString}`);
330
+ return this.request(
331
+ "GET",
332
+ `/${accessor}${queryString}`,
333
+ void 0,
334
+ { next: { revalidate: 2700 } }
335
+ );
207
336
  }
208
337
  buildQueryString(options) {
209
338
  if (!options?.expand?.length) return "";
@@ -264,6 +393,18 @@ class OrdersResource extends BaseResource {
264
393
  constructor(tokenManager, baseUrl) {
265
394
  super(tokenManager, baseUrl, "/orders");
266
395
  }
396
+ /**
397
+ * Most recent non-team order for the guest session, with captured
398
+ * basket items + totals + declared returns inlined. `null` when the
399
+ * session has no qualifying order yet (fresh session, or only team
400
+ * orders which go through `teamOrders` instead).
401
+ */
402
+ async getLatest(sessionId) {
403
+ return this.request(
404
+ "GET",
405
+ `/latest?sessionId=${encodeURIComponent(sessionId)}`
406
+ );
407
+ }
267
408
  /**
268
409
  * Place a new order.
269
410
  *
@@ -288,6 +429,14 @@ class OrdersResource extends BaseResource {
288
429
  async confirm(orderId, request) {
289
430
  return this.request("POST", `/${orderId}/confirm`, request);
290
431
  }
432
+ /**
433
+ * Record an additional cash handover amount for a team-order participant
434
+ * who has added items after their initial cash payment. No Stripe
435
+ * interaction — this updates the handover total the Lead will collect.
436
+ */
437
+ async addCashDelta(request) {
438
+ return this.request("POST", "/cash-delta", request);
439
+ }
291
440
  }
292
441
 
293
442
  class PaymentIntentsResource extends BaseResource {
@@ -303,6 +452,24 @@ class PaymentIntentsResource extends BaseResource {
303
452
  async create(request) {
304
453
  return this.request("POST", "", request);
305
454
  }
455
+ /**
456
+ * Create a delta PaymentIntent for a team-order participant who added
457
+ * items after paying. Reuses the saved card from the initial auth so the
458
+ * client only needs one tap (unless Stripe triggers 3DS).
459
+ */
460
+ async createDelta(request) {
461
+ return this.request("POST", "/delta", request);
462
+ }
463
+ /**
464
+ * Release part of a team-order participant's existing Stripe authorisation
465
+ * when they remove items from their basket. Walks payments newest-first,
466
+ * cancelling fully-consumed PIs and `update({ amount })`-ing the last
467
+ * partially-trimmed one. No client-side confirmation — lowering a
468
+ * `requires_capture` PI's amount is a single server round-trip.
469
+ */
470
+ async reduceDelta(request) {
471
+ return this.request("POST", "/delta-reduce", request);
472
+ }
306
473
  }
307
474
 
308
475
  class ReservationsResource extends BaseResource {
@@ -391,6 +558,221 @@ class SubscriptionsResource extends BaseResource {
391
558
  }
392
559
  }
393
560
 
561
+ const paymentMethodEnum = {
562
+ enumValues: ["card", "cash", "voucher"]
563
+ };
564
+ const fulfillmentTypeEnum = {
565
+ enumValues: ["pickup", "delivery"]
566
+ };
567
+ const teamOrderStatusEnum = {
568
+ enumValues: [
569
+ "draft",
570
+ "collecting",
571
+ "locked",
572
+ "placed",
573
+ "ready",
574
+ "completed",
575
+ "cancelled"
576
+ ]
577
+ };
578
+ const participantStatusEnum = {
579
+ enumValues: [
580
+ "browsing",
581
+ "authorized",
582
+ "cash_pending",
583
+ "placed",
584
+ "completed",
585
+ "cancelled"
586
+ ]
587
+ };
588
+ class TeamOrdersResource {
589
+ constructor(tokenManager, baseUrl) {
590
+ this.tokenManager = tokenManager;
591
+ this.baseUrl = baseUrl;
592
+ }
593
+ async request(method, url, data) {
594
+ const token = await this.tokenManager.getToken();
595
+ const response = await fetch(url, {
596
+ method,
597
+ headers: {
598
+ Authorization: `Bearer ${token}`,
599
+ "Content-Type": "application/json"
600
+ },
601
+ body: data ? JSON.stringify(data) : void 0,
602
+ // Bypass Next.js fetch cache — these are live reads/writes, never
603
+ // safe to memoise at the SDK layer.
604
+ cache: "no-store",
605
+ signal: AbortSignal.timeout(36e4)
606
+ });
607
+ if (!response.ok) {
608
+ let body = "";
609
+ try {
610
+ body = await response.text();
611
+ } catch {
612
+ }
613
+ throw new Error(
614
+ `HTTP ${response.status}: ${response.statusText}${body ? ` \u2014 ${body}` : ""}`
615
+ );
616
+ }
617
+ return response.json();
618
+ }
619
+ /**
620
+ * Lead creates a team order from their active basket.
621
+ * Returns the unguessable `publicId` the lead shares with coworkers.
622
+ */
623
+ async create(request) {
624
+ const venueId = await this.tokenManager.getVenueIdAsync();
625
+ return this.request(
626
+ "POST",
627
+ `${this.baseUrl}/venues/${venueId}/team-orders`,
628
+ request
629
+ );
630
+ }
631
+ /**
632
+ * Public read by publicId. Pass the caller's sessionId so the response
633
+ * can set `isLead` correctly for UI gating.
634
+ */
635
+ async get(publicId, sessionId) {
636
+ const query = sessionId ? `?sessionId=${encodeURIComponent(sessionId)}` : "";
637
+ return this.request(
638
+ "GET",
639
+ `${this.baseUrl}/team-orders/${encodeURIComponent(publicId)}${query}`
640
+ );
641
+ }
642
+ /**
643
+ * Participant joins a team order. Idempotent: safe to call on every page
644
+ * mount of the shared URL.
645
+ */
646
+ async join(publicId, request) {
647
+ return this.request(
648
+ "POST",
649
+ `${this.baseUrl}/team-orders/${encodeURIComponent(publicId)}/join`,
650
+ request
651
+ );
652
+ }
653
+ /**
654
+ * Lead submits the team order. Drops basket-only participants, captures
655
+ * every authorised Stripe PaymentIntent atomically, flips child orders to
656
+ * `pending`. The "Submit anyways" UX is a client-side confirmation dialog
657
+ * that calls the same endpoint — no server variant exists.
658
+ */
659
+ async submit(publicId, request) {
660
+ return this.request(
661
+ "POST",
662
+ `${this.baseUrl}/team-orders/${encodeURIComponent(publicId)}/submit`,
663
+ request
664
+ );
665
+ }
666
+ /**
667
+ * Lead cancels pre-placement. Voids every authorised Stripe
668
+ * PaymentIntent (no refunds needed — nothing was captured).
669
+ */
670
+ async cancel(publicId, request) {
671
+ return this.request(
672
+ "POST",
673
+ `${this.baseUrl}/team-orders/${encodeURIComponent(publicId)}/cancel`,
674
+ request
675
+ );
676
+ }
677
+ /**
678
+ * Lead pushes the deadline. A `locked` team flips back to `collecting`
679
+ * when the new deadline is in the future.
680
+ */
681
+ async extendDeadline(publicId, request) {
682
+ return this.request(
683
+ "POST",
684
+ `${this.baseUrl}/team-orders/${encodeURIComponent(publicId)}/extend-deadline`,
685
+ request
686
+ );
687
+ }
688
+ /**
689
+ * Lead commits (or changes) the shared pickup/delivery time. Required
690
+ * before `submit` — which backfills each child order's `fulfillmentAt`
691
+ * from this value at capture time.
692
+ */
693
+ async setFulfillmentAt(publicId, request) {
694
+ return this.request(
695
+ "POST",
696
+ `${this.baseUrl}/team-orders/${encodeURIComponent(publicId)}/set-fulfillment-at`,
697
+ request
698
+ );
699
+ }
700
+ /**
701
+ * Lead sets the free-form "where we'll eat" text on the team order.
702
+ * Broadcast to participants via the standard realtime refetch path.
703
+ */
704
+ async setDiningLocation(publicId, request) {
705
+ return this.request(
706
+ "POST",
707
+ `${this.baseUrl}/team-orders/${encodeURIComponent(publicId)}/set-dining-location`,
708
+ request
709
+ );
710
+ }
711
+ /**
712
+ * Lead sets when the team will start eating. Client sends HH:MM; server
713
+ * computes the delta against `fulfillmentAt` and stores it as
714
+ * `dining_offset_minutes` so the intent survives any later pickup change.
715
+ */
716
+ async setDiningTime(publicId, request) {
717
+ return this.request(
718
+ "POST",
719
+ `${this.baseUrl}/team-orders/${encodeURIComponent(publicId)}/set-dining-time`,
720
+ request
721
+ );
722
+ }
723
+ /**
724
+ * Toggle a participant's "settled" tick on the team Lead's
725
+ * post-placement settle-up checklist. Writes to
726
+ * `orders.leadSettledAt` — sets it to `NOW()` when `settled` is
727
+ * `true`, clears it back to `null` when `false`.
728
+ *
729
+ * Authorized only for the team Lead's session — server rejects
730
+ * non-Lead callers and `orderId`s that don't belong to this team.
731
+ */
732
+ async setOrderLeadSettled(publicId, request) {
733
+ return this.request(
734
+ "POST",
735
+ `${this.baseUrl}/team-orders/${encodeURIComponent(publicId)}/lead-settled`,
736
+ request
737
+ );
738
+ }
739
+ /**
740
+ * Any participant proposes an auto-extend when their local clock sees
741
+ * that `fulfillmentAt` has passed while the team order is still
742
+ * collecting. Idempotent on the server side: concurrent callers race
743
+ * but only one write lands.
744
+ */
745
+ async autoExtendFulfillmentIfExpired(publicId, request) {
746
+ return this.request(
747
+ "POST",
748
+ `${this.baseUrl}/team-orders/${encodeURIComponent(publicId)}/auto-extend-fulfillment-if-expired`,
749
+ request
750
+ );
751
+ }
752
+ /**
753
+ * Lead flips pickup ↔ delivery. Pass `deliveryAddress` when switching
754
+ * TO delivery if no address is already set on the team order.
755
+ */
756
+ async switchFulfillment(publicId, request) {
757
+ return this.request(
758
+ "POST",
759
+ `${this.baseUrl}/team-orders/${encodeURIComponent(publicId)}/switch-fulfillment`,
760
+ request
761
+ );
762
+ }
763
+ /**
764
+ * Lead replaces the delivery address (inserts a new row in the shared
765
+ * addresses table with type='PHYSICAL' and links it to the team order).
766
+ */
767
+ async updateDeliveryAddress(publicId, request) {
768
+ return this.request(
769
+ "POST",
770
+ `${this.baseUrl}/team-orders/${encodeURIComponent(publicId)}/update-delivery-address`,
771
+ request
772
+ );
773
+ }
774
+ }
775
+
394
776
  var __defProp$2 = Object.defineProperty;
395
777
  var __defNormalProp$2 = (obj, key, value) => key in obj ? __defProp$2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
396
778
  var __publicField$2 = (obj, key, value) => __defNormalProp$2(obj, typeof key !== "symbol" ? key + "" : key, value);
@@ -715,6 +1097,7 @@ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { en
715
1097
  var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
716
1098
  class Order3000Client {
717
1099
  constructor(config) {
1100
+ __publicField(this, "areas");
718
1101
  __publicField(this, "baskets");
719
1102
  __publicField(this, "events");
720
1103
  __publicField(this, "menus");
@@ -722,6 +1105,7 @@ class Order3000Client {
722
1105
  __publicField(this, "paymentIntents");
723
1106
  __publicField(this, "reservations");
724
1107
  __publicField(this, "subscriptions");
1108
+ __publicField(this, "teamOrders");
725
1109
  __publicField(this, "realtime");
726
1110
  __publicField(this, "baseUrl");
727
1111
  __publicField(this, "tokenManager");
@@ -733,6 +1117,7 @@ class Order3000Client {
733
1117
  tokenUrl,
734
1118
  config.venueId
735
1119
  );
1120
+ this.areas = new AreasResource(this.tokenManager, this.baseUrl);
736
1121
  this.baskets = new BasketsResource(this.tokenManager, this.baseUrl);
737
1122
  this.events = new EventsResource(this.tokenManager, this.baseUrl);
738
1123
  this.menus = new MenusResource(this.tokenManager, this.baseUrl);
@@ -749,8 +1134,9 @@ class Order3000Client {
749
1134
  this.tokenManager,
750
1135
  this.baseUrl
751
1136
  );
1137
+ this.teamOrders = new TeamOrdersResource(this.tokenManager, this.baseUrl);
752
1138
  this.realtime = new RealtimeManager(this.tokenManager, this.baseUrl);
753
1139
  }
754
1140
  }
755
1141
 
756
- export { Order3000Client, getScheduleInfo };
1142
+ export { Order3000Client, fulfillmentTypeEnum, getScheduleInfo, participantStatusEnum, paymentMethodEnum, teamOrderStatusEnum };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "order3000",
3
- "version": "0.16.0",
3
+ "version": "0.18.0",
4
4
  "description": "order3000 node.js sdk",
5
5
  "main": "./dist/client.cjs",
6
6
  "module": "./dist/client.mjs",
@@ -32,9 +32,9 @@
32
32
  "prettier": "^3.7.4",
33
33
  "tsx": "^4.19.2",
34
34
  "typescript": "^5.9.3",
35
+ "@kolaveri/eslint-config": "1.5.0",
35
36
  "@kolaveri/tsconfig": "1.0.1",
36
- "@kolaveri/prettier-config": "1.0.6",
37
- "@kolaveri/eslint-config": "1.4.2"
37
+ "@kolaveri/prettier-config": "1.0.6"
38
38
  },
39
39
  "files": [
40
40
  "dist"