@zappie/sdk 0.15.0 → 0.17.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.
Files changed (56) hide show
  1. package/CHANGELOG.md +20 -0
  2. package/README.md +52 -0
  3. package/dist/cart/index.js +30 -39
  4. package/dist/cart/index.js.map +1 -1
  5. package/dist/cart/item.d.ts +1 -1
  6. package/dist/cart/quote-client.js +4 -20
  7. package/dist/cart/quote-client.js.map +1 -1
  8. package/dist/cart/quote-response.js +10 -5
  9. package/dist/cart/quote-response.js.map +1 -1
  10. package/dist/cart/storage.js +2 -0
  11. package/dist/cart/storage.js.map +1 -1
  12. package/dist/checkout/embed.d.ts +2 -0
  13. package/dist/checkout/index.d.ts +2 -0
  14. package/dist/checkout/react.d.ts +2 -0
  15. package/dist/checkout-defaults/index.d.ts +3 -0
  16. package/dist/{client-C47e44tl.d.ts → client-uRLLtZJU.d.ts} +1 -1
  17. package/dist/customer/client.js +34 -12
  18. package/dist/customer/client.js.map +1 -1
  19. package/dist/customer/index.d.ts +181 -10
  20. package/dist/customer/reorder.d.ts +40 -0
  21. package/dist/customer/reorder.js +61 -0
  22. package/dist/customer/reorder.js.map +6 -0
  23. package/dist/debug.js +1 -1
  24. package/dist/index.d.ts +4 -4
  25. package/dist/index.js +1 -1
  26. package/dist/integrations/index.d.ts +1 -1
  27. package/dist/internal/perk-hold.js +35 -0
  28. package/dist/internal/perk-hold.js.map +6 -0
  29. package/dist/internal/remembered-address.js +8 -2
  30. package/dist/internal/remembered-address.js.map +1 -1
  31. package/dist/internal.d.ts +1 -1
  32. package/dist/loyalty/cart.js +56 -0
  33. package/dist/loyalty/cart.js.map +6 -0
  34. package/dist/loyalty/client.js +89 -0
  35. package/dist/loyalty/client.js.map +6 -0
  36. package/dist/loyalty/index.d.ts +526 -0
  37. package/dist/loyalty/index.js +13 -0
  38. package/dist/loyalty/index.js.map +6 -0
  39. package/dist/loyalty/react.d.ts +116 -0
  40. package/dist/loyalty/react.js +200 -0
  41. package/dist/loyalty/react.js.map +6 -0
  42. package/dist/loyalty/release.js +43 -0
  43. package/dist/loyalty/release.js.map +6 -0
  44. package/dist/loyalty/types.js +1 -0
  45. package/dist/loyalty/types.js.map +6 -0
  46. package/dist/persist/catalogue.d.ts +2 -2
  47. package/dist/react/index.d.ts +2 -1
  48. package/dist/server/index.d.ts +1 -1
  49. package/dist/testing/index.d.ts +92 -4
  50. package/dist/testing/index.js +2 -0
  51. package/dist/testing/loyalty.js +110 -0
  52. package/dist/testing/loyalty.js.map +6 -0
  53. package/dist/transport/search.d.ts +1 -1
  54. package/dist/transport/selected-menu.d.ts +2 -2
  55. package/dist/{types-ooHA0lwH.d.ts → types-K01NHM_a.d.ts} +28 -3
  56. package/package.json +14 -2
@@ -12,9 +12,6 @@ import { createCustomerRequest, readCustomerResponse } from "./request.js";
12
12
  import { watchOrder } from "./watch-order.js";
13
13
  var DEFAULT_CUSTOMER_BASE = "/_zappie/api";
14
14
  var DEFAULT_SESSION_PATH = "/_zappie/session";
15
- function same(left, right) {
16
- return left?.id === right?.id && left?.given_name === right?.given_name && left?.phone_masked === right?.phone_masked;
17
- }
18
15
  function createCustomer(options = {}) {
19
16
  const base = (options.customerBase ?? DEFAULT_CUSTOMER_BASE).replace(
20
17
  /\/+$/,
@@ -37,7 +34,7 @@ function createCustomer(options = {}) {
37
34
  }
38
35
  };
39
36
  const update = (next, nextState) => {
40
- if (state === nextState && same(customer, next)) {
37
+ if (JSON.stringify([state, customer]) === JSON.stringify([nextState, next])) {
41
38
  return;
42
39
  }
43
40
  state = nextState;
@@ -116,10 +113,13 @@ function createCustomer(options = {}) {
116
113
  },
117
114
  /** Sign out through Zappie hosting, forgetting the delivery address remembered on this device and the one in each saved bag, whose items stay. A call while pending does nothing. */
118
115
  signOut() {
119
- void leaving.start(() => {
116
+ void leaving.start(async () => {
120
117
  globalThis.__zappieShopper = void 0;
121
118
  forgetRememberedAddress();
122
- forgetBagAddresses();
119
+ const perks = forgetBagAddresses();
120
+ if (perks.length) {
121
+ await globalThis.__zappieReleasePerks?.(perks);
122
+ }
123
123
  return leaveTo("/_zappie/logout", true);
124
124
  });
125
125
  },
@@ -137,17 +137,39 @@ function createCustomer(options = {}) {
137
137
  method: "DELETE"
138
138
  }),
139
139
  orders(options2 = {}) {
140
- const query = new URLSearchParams();
141
- for (const key of ["limit", "cursor"]) {
142
- if (options2[key] !== void 0) {
143
- query.set(key, String(options2[key]));
144
- }
145
- }
140
+ const query = new URLSearchParams(JSON.parse(JSON.stringify(options2)));
146
141
  return request(`/me/orders${query.size ? `?${query}` : ""}`);
147
142
  },
148
143
  order: readOrder,
144
+ /**
145
+ * Raise a payment for what is left on the order, its `amount_due`, and return the payment page
146
+ * to send the customer to. An order paid in full answers `nothing_to_pay`, and one that ended
147
+ * answers `order_closed`. Each call makes its own Idempotency-Key unless you pass one.
148
+ */
149
+ payBalance: (id, pay = {}) => request(
150
+ `/me/orders/${encodeURIComponent(id)}/payments`,
151
+ {
152
+ method: "POST",
153
+ headers: {
154
+ "Content-Type": "application/json",
155
+ "Idempotency-Key": pay.idempotencyKey ?? crypto.randomUUID()
156
+ },
157
+ // JSON leaves out use_wallet_balance when it is undefined.
158
+ body: JSON.stringify({ use_wallet_balance: pay.useWalletBalance })
159
+ }
160
+ ),
161
+ /**
162
+ * Rebuild the order as bag lines at the same branch, priced at today's menu. It creates
163
+ * nothing. Lines the branch no longer sells as ordered come back in `dropped`. Put the lines in
164
+ * the bag with `addReorderToCart` from `@zappie/sdk/customer/reorder`.
165
+ */
166
+ reorder: (id) => request(`/me/orders/${encodeURIComponent(id)}/reorder`, {
167
+ method: "POST"
168
+ }),
149
169
  /** The order's receipt as a PDF File, named as the API names it. Needs the orders:read scope. */
150
170
  receipt: (id) => orderReceipt(base, id, fetcher),
171
+ /** The receipt's same-origin address, for a download link. */
172
+ receiptUrl: (id) => `${base}/me/orders/${encodeURIComponent(id)}/receipt`,
151
173
  watchOrder(id, watchOptions) {
152
174
  return watchOrder(base, id, readOrder, watchOptions);
153
175
  }
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../src/customer/client.ts"],
4
- "mappings": ";AAEA,SAAS,+BAA+B;AACxC,SAAS,wBAAwB;AAGjC,SAAS,oBAAoB;AAC7B;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,aAAa,SAAS,oBAAoB;AACnD,SAAS,uBAAuB,4BAA4B;AAa5D,SAAS,kBAAkB;AAE3B,IAAM,wBAAwB;AAC9B,IAAM,uBAAuB;AAE7B,SAAS,KACP,MACA,OACS;AACT,SACE,MAAM,OAAO,OAAO,MACpB,MAAM,eAAe,OAAO,cAC5B,MAAM,iBAAiB,OAAO;AAElC;AASO,SAAS,eAAe,UAAiC,CAAC,GAAG;AAClE,QAAM,QAAQ,QAAQ,gBAAgB,uBAAuB;AAAA,IAC3D;AAAA,IACA;AAAA,EACF;AACA,QAAM,cAAc,QAAQ,eAAe;AAC3C,QAAM,UAAU,QAAQ,SAAS,WAAW;AAC5C,MAAI,WAAW,QAAQ,mBAAmB;AAC1C,MAAI,QACF,QAAQ,oBAAoB,SACxB,YACA,WACE,kBACA;AAGR,QAAM,SAAS,MAAM;AACnB,QAAI,UAAU,WAAW;AACvB,oBAAc,UAAU,MAAM,IAAI;AAAA,IACpC;AAAA,EACF;AACA,SAAO;AACP,QAAM,YAAY,oBAAI,IAAsB;AAC5C,QAAM,SAAS,MAAM;AACnB,eAAW,YAAY,WAAW;AAChC,eAAS,OAAO,QAAQ;AAAA,IAC1B;AAAA,EACF;AACA,QAAM,SAAS,CAAC,MAA8B,cAAyB;AACrE,QAAI,UAAU,aAAa,KAAK,UAAU,IAAI,GAAG;AAC/C;AAAA,IACF;AACA,YAAQ;AACR,eAAW;AACX,WAAO;AACP,eAAW,eAAe,gBAAgB,EAAE,OAAO,SAAS,CAAC;AAC7D,WAAO;AAAA,EACT;AAEA,QAAM,UAAU,wBAAwB;AACxC,UA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AA;AAAA;AAAA;AAAA;AAAA,IAMF,SAAS,CAAC,OACR,QAAiB,cAAc,mBAAmB,EAAE,CAAC,YAAY;AAAA,MAC/D,QAAQ;AAAA,IACV,CAAC;AAAA;AAAA,IAEH,SAAS,CAAC,OAAe,aAAa,MAAM,IAAI,OAAO;AAAA;AAAA,IAEvD,YAAY,CAAC,OACX,GAAG,IAAI,cAAc,mBAAmB,EAAE,CAAC;AAAA,IAC7C,WAAW,IAAY,cAAiC;AACtD,aAAO,WAAW,MAAM,IAAI,WAAW,YAAY;AAAA,IACrD;AAAA,EACF;AACF;",
5
5
  "names": ["options"]
6
6
  }
@@ -1,4 +1,4 @@
1
- import { ZappieError } from '@zappie/sdk';
1
+ import { WireIssue, ZappieError } from '@zappie/sdk';
2
2
 
3
3
  /**
4
4
  * Whether the customer session is unchecked, anonymous or authenticated.
@@ -43,17 +43,67 @@ type Money = {
43
43
  currency: string;
44
44
  };
45
45
  /**
46
- * A selected option recorded on an order line, including its name, quantity and price.
46
+ * A selected option recorded on an order line, including its group, name, quantity and price.
47
47
  */
48
48
  type OrderChoice = {
49
49
  group_id: string;
50
+ /** The option group's name, such as "Spread". */
51
+ group_name: string;
50
52
  choice_id: string;
51
53
  name: string;
52
54
  price: Money;
53
55
  quantity: number;
54
56
  };
55
57
  /**
56
- * An ordered item with its recorded prices, selected options and optional variant and notes.
58
+ * What the customer typed for one text group on an order line.
59
+ */
60
+ type OrderText = {
61
+ group_id: string;
62
+ group_name: string;
63
+ value: string;
64
+ };
65
+ /**
66
+ * The kind of a charge on an order.
67
+ */
68
+ type OrderChargeCode = "service_charge" | "delivery" | "packaging" | "vat" | "small_order" | "platform_fee" | "other";
69
+ /**
70
+ * A line's share of a charge on the order. An included share is already in the line's price.
71
+ */
72
+ type OrderLineCharge = {
73
+ code: OrderChargeCode;
74
+ label: string;
75
+ amount: Money;
76
+ included: boolean;
77
+ };
78
+ /**
79
+ * A charge on the order. An included charge is already in the menu prices and adds nothing to the
80
+ * total. A waived charge shows at zero.
81
+ */
82
+ type OrderCharge = OrderLineCharge & {
83
+ waived: boolean;
84
+ };
85
+ /**
86
+ * A discount on the order. The amount is positive, like the order's discount.
87
+ */
88
+ type OrderDiscount = {
89
+ label: string;
90
+ /** The code the customer entered, when the discount came with one. */
91
+ promo_code?: string;
92
+ amount: Money;
93
+ };
94
+ /**
95
+ * A successful or refunded payment toward the order. It never carries a card number or a payment
96
+ * reference.
97
+ */
98
+ type OrderPayment = {
99
+ method: string;
100
+ amount: Money;
101
+ status: "successful" | "refunded";
102
+ paid_at: string;
103
+ };
104
+ /**
105
+ * An ordered item with its recorded prices, selected options, typed text and its share of each
106
+ * charge. `voided` is true only on a line in `voided_lines`.
57
107
  */
58
108
  type OrderLine = {
59
109
  item_id: string;
@@ -63,14 +113,48 @@ type OrderLine = {
63
113
  unit_price: Money;
64
114
  total: Money;
65
115
  notes?: string;
116
+ voided: boolean;
66
117
  choices: OrderChoice[];
118
+ texts: OrderText[];
119
+ charges: OrderLineCharge[];
67
120
  };
68
121
  /**
69
- * A customer order with fulfilment, payment totals, lines and a status timeline.
122
+ * Where an order stands.
123
+ */
124
+ type OrderStatus = "pending" | "accepted" | "ready" | "out_for_delivery" | "arrived" | "delivery_failed" | "completed" | "cancelled" | "rejected";
125
+ /**
126
+ * A detailed step. `ready_for_pickup`, `ready_for_rider_pickup`, `searching_for_rider`,
127
+ * `rider_assigned` and `waiting_for_pickup` read as the `ready` status, `picked_up` and
128
+ * `on_the_way` as `out_for_delivery`. The rest match the status of the same name.
129
+ */
130
+ type OrderStepCode = "pending" | "accepted" | "ready_for_pickup" | "ready_for_rider_pickup" | "searching_for_rider" | "rider_assigned" | "waiting_for_pickup" | "picked_up" | "on_the_way" | "arrived" | "completed" | "cancelled" | "rejected" | "delivery_failed";
131
+ /**
132
+ * A step the order took, with its name and one sentence on what it means, ready to show.
133
+ */
134
+ type OrderTimelineStep = {
135
+ /** The public status the step reads as. Several steps can share one. */
136
+ status: string;
137
+ step: OrderStepCode;
138
+ title: string;
139
+ description: string;
140
+ at: string;
141
+ };
142
+ /**
143
+ * The step the business moves the order to next.
144
+ */
145
+ type OrderNextStep = {
146
+ step: OrderStepCode;
147
+ title: string;
148
+ description: string;
149
+ };
150
+ /**
151
+ * A customer order with its number, fulfilment, charges, discounts, payments, lines and timeline.
70
152
  */
71
153
  type CustomerOrder = {
72
154
  id: string;
73
- status: "pending" | "accepted" | "ready" | "out_for_delivery" | "arrived" | "completed" | "cancelled" | "rejected";
155
+ /** The order number the business and the customer know the order by, such as "#1042". Show it instead of the id. */
156
+ reference: string;
157
+ status: OrderStatus;
74
158
  fulfilment: "delivery" | "pickup";
75
159
  branch_id?: string;
76
160
  currency: string;
@@ -80,16 +164,29 @@ type CustomerOrder = {
80
164
  total: Money;
81
165
  paid: boolean;
82
166
  amount_paid: Money;
167
+ /** What the customer can still pay with `payBalance`. Zero once the order is paid or has ended. */
168
+ amount_due: Money;
83
169
  lines: OrderLine[];
170
+ /** Lines the business took off the order after it was placed. No total counts them. */
171
+ voided_lines: OrderLine[];
172
+ /** Each charge in the order it was added, the delivery fee among them. */
173
+ charges: OrderCharge[];
174
+ discounts: OrderDiscount[];
175
+ /** Successful and refunded payments, oldest first. */
176
+ payments: OrderPayment[];
84
177
  placed_at: string;
85
178
  updated_at: string;
86
179
  scheduled_at?: string;
87
- timeline: {
88
- status: string;
89
- at: string;
90
- }[];
180
+ /** When the business expects the order to be ready. It stays after that time passes. */
181
+ ready_at?: string;
182
+ /** Every step the order has taken, oldest first. */
183
+ timeline: OrderTimelineStep[];
184
+ /** The step that comes next. Null once the order has ended and during the rider's steps. */
185
+ next_step: OrderNextStep | null;
91
186
  eta_minutes?: number;
92
187
  contact_phone?: string;
188
+ /** The customer's note for whoever brings the order. */
189
+ delivery_instructions?: string;
93
190
  dropoff_address?: {
94
191
  line1?: string;
95
192
  lat?: number;
@@ -100,6 +197,66 @@ type CustomerOrder = {
100
197
  method?: string;
101
198
  };
102
199
  };
200
+ /**
201
+ * The payment `payBalance` raised. Send the customer to `url` to finish it.
202
+ */
203
+ type OrderBalancePayment = {
204
+ payment_request_id: string;
205
+ amount: Money;
206
+ url: string;
207
+ };
208
+ /**
209
+ * Options for paying what is left on an order.
210
+ */
211
+ type PayBalanceOptions = {
212
+ /** Take what the customer's wallet holds first. Needs the wallet scope. */
213
+ useWalletBalance?: boolean;
214
+ /** Retry a failed attempt with the same key. Each call makes its own by default. */
215
+ idempotencyKey?: string;
216
+ };
217
+ /**
218
+ * An order line rebuilt for the bag at today's prices, as POST /quote takes it.
219
+ */
220
+ type ReorderLine = {
221
+ line_id: string;
222
+ item_id: string;
223
+ menu_id?: string;
224
+ variant_id?: string;
225
+ quantity: number;
226
+ options?: {
227
+ group_id: string;
228
+ choice_id: string;
229
+ quantity?: number;
230
+ }[];
231
+ inputs?: Record<string, string>;
232
+ note?: string;
233
+ name: string;
234
+ unit_price: Money;
235
+ options_total: Money;
236
+ line_total: Money;
237
+ };
238
+ /**
239
+ * An order line the branch no longer sells as it was ordered, with the issues that say why.
240
+ */
241
+ type ReorderDroppedLine = {
242
+ line_id: string;
243
+ /** The name on the original order. */
244
+ name: string;
245
+ quantity: number;
246
+ /** Each issue as the quote words it. */
247
+ issues: WireIssue[];
248
+ };
249
+ /**
250
+ * An order rebuilt as bag lines at the same branch, for the same fulfilment, priced at today's menu.
251
+ */
252
+ type Reorder = {
253
+ order_id: string;
254
+ branch_id: string;
255
+ fulfilment: "delivery" | "pickup";
256
+ menu_version: string;
257
+ lines: ReorderLine[];
258
+ dropped: ReorderDroppedLine[];
259
+ };
103
260
  /**
104
261
  * A page of customer orders and the cursor for the next page, or null at the end.
105
262
  */
@@ -159,8 +316,22 @@ declare function createCustomer(options?: CustomerClientOptions): {
159
316
  limit?: number;
160
317
  }): Promise<OrdersPage>;
161
318
  order: (id: string, signal?: AbortSignal) => Promise<CustomerOrder>;
319
+ /**
320
+ * Raise a payment for what is left on the order, its `amount_due`, and return the payment page
321
+ * to send the customer to. An order paid in full answers `nothing_to_pay`, and one that ended
322
+ * answers `order_closed`. Each call makes its own Idempotency-Key unless you pass one.
323
+ */
324
+ payBalance: (id: string, pay?: PayBalanceOptions) => Promise<OrderBalancePayment>;
325
+ /**
326
+ * Rebuild the order as bag lines at the same branch, priced at today's menu. It creates
327
+ * nothing. Lines the branch no longer sells as ordered come back in `dropped`. Put the lines in
328
+ * the bag with `addReorderToCart` from `@zappie/sdk/customer/reorder`.
329
+ */
330
+ reorder: (id: string) => Promise<Reorder>;
162
331
  /** The order's receipt as a PDF File, named as the API names it. Needs the orders:read scope. */
163
332
  receipt: (id: string) => Promise<File>;
333
+ /** The receipt's same-origin address, for a download link. */
334
+ receiptUrl: (id: string) => string;
164
335
  watchOrder(id: string, watchOptions: WatchOrderOptions): () => void;
165
336
  };
166
337
  /**
@@ -179,4 +350,4 @@ declare class CustomerError extends ZappieError {
179
350
  }
180
351
 
181
352
  export { CustomerError, createCustomer };
182
- export type { AuthState, CreateAddress, CustomerClient, CustomerClientOptions, CustomerListener, CustomerOrder, CustomerProfile, Money, OrderChoice, OrderLine, OrdersPage, SavedAddress, SessionCustomer, WatchOrderOptions };
353
+ export type { AuthState, CreateAddress, CustomerClient, CustomerClientOptions, CustomerListener, CustomerOrder, CustomerProfile, Money, OrderBalancePayment, OrderCharge, OrderChargeCode, OrderChoice, OrderDiscount, OrderLine, OrderLineCharge, OrderNextStep, OrderPayment, OrderStatus, OrderStepCode, OrderText, OrderTimelineStep, OrdersPage, PayBalanceOptions, Reorder, ReorderDroppedLine, ReorderLine, SavedAddress, SessionCustomer, WatchOrderOptions };
@@ -0,0 +1,40 @@
1
+ import { CartLine, Zappie } from '@zappie/sdk';
2
+ import { Reorder } from '@zappie/sdk/customer';
3
+
4
+ /**
5
+ * An order line that didn't make it into the bag, with the issues that say why.
6
+ */
7
+ type DroppedReorderLine = {
8
+ lineId: string;
9
+ /** The name on the original order. */
10
+ name: string;
11
+ quantity: number;
12
+ issues: {
13
+ code: string;
14
+ message: string;
15
+ }[];
16
+ };
17
+ /**
18
+ * What `addReorderToCart` put in the bag and what it left out.
19
+ */
20
+ type ReorderResult = {
21
+ added: CartLine[];
22
+ /** The lines the API dropped, then the lines the bag refused. Show them so the customer knows. */
23
+ dropped: DroppedReorderLine[];
24
+ };
25
+ /**
26
+ * Put a reorder's lines in the bag, next to what it already holds. A bag at another branch moves to
27
+ * the order's branch first, which keeps the lines that branch sells. A pickup order sets pickup
28
+ * when the bag has no fulfilment yet. A delivery order leaves the address to checkout.
29
+ *
30
+ * @param zappie - The storefront client, whose cart and menu take the lines.
31
+ * @param reorder - What `customer.reorder(orderId)` returned.
32
+ * @param options - A signal that stops it between lines.
33
+ * @returns The lines added, and every line left out with the issues that say why.
34
+ */
35
+ declare function addReorderToCart(zappie: Pick<Zappie, "cart" | "menu">, reorder: Reorder, options?: {
36
+ signal?: AbortSignal;
37
+ }): Promise<ReorderResult>;
38
+
39
+ export { addReorderToCart };
40
+ export type { DroppedReorderLine, ReorderResult };
@@ -0,0 +1,61 @@
1
+ // src/customer/reorder.ts
2
+ async function addReorderToCart(zappie, reorder, options = {}) {
3
+ const { cart } = zappie;
4
+ const branch = reorder.branch_id;
5
+ await cart.hydrate();
6
+ if (cart.getSnapshot().branchId !== branch) {
7
+ await cart.moveTo(branch);
8
+ }
9
+ const added = [];
10
+ const dropped = reorder.dropped.map((line) => ({
11
+ lineId: line.line_id,
12
+ name: line.name,
13
+ quantity: line.quantity,
14
+ issues: line.issues.map(({ code, message }) => ({ code, message }))
15
+ }));
16
+ for (const line of reorder.lines) {
17
+ options.signal?.throwIfAborted();
18
+ try {
19
+ const item = await zappie.menu.item(line.item_id, {
20
+ branch,
21
+ ...options.signal ? { signal: options.signal } : {}
22
+ });
23
+ added.push(
24
+ cart.add(item, {
25
+ quantity: line.quantity,
26
+ choices: (line.options ?? []).map((option) => ({
27
+ groupId: option.group_id,
28
+ choiceId: option.choice_id,
29
+ quantity: option.quantity ?? 1
30
+ })),
31
+ ...line.menu_id ? { menuId: line.menu_id } : {},
32
+ ...line.variant_id ? { variantId: line.variant_id } : {},
33
+ ...line.inputs ? { inputs: line.inputs } : {},
34
+ ...line.note ? { note: line.note } : {}
35
+ })
36
+ );
37
+ } catch (error) {
38
+ options.signal?.throwIfAborted();
39
+ const reasons = error.issues;
40
+ dropped.push({
41
+ lineId: line.line_id,
42
+ name: line.name,
43
+ quantity: line.quantity,
44
+ issues: reasons?.length ? reasons.map(({ code, message }) => ({ code, message })) : [
45
+ {
46
+ code: error.code ?? "item_unavailable",
47
+ message: `${line.name} isn't available right now.`
48
+ }
49
+ ]
50
+ });
51
+ }
52
+ }
53
+ if (reorder.fulfilment === "pickup" && !cart.getSnapshot().fulfilment) {
54
+ cart.setFulfilment({ type: "pickup", branchId: branch });
55
+ }
56
+ return { added, dropped };
57
+ }
58
+ export {
59
+ addReorderToCart
60
+ };
61
+ //# sourceMappingURL=reorder.js.map
@@ -0,0 +1,6 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/customer/reorder.ts"],
4
+ "mappings": ";AAoCA,eAAsB,iBACpB,QACA,SACA,UAAoC,CAAC,GACb;AACxB,QAAM,EAAE,KAAK,IAAI;AACjB,QAAM,SAAS,QAAQ;AACvB,QAAM,KAAK,QAAQ;AACnB,MAAI,KAAK,YAAY,EAAE,aAAa,QAAQ;AAC1C,UAAM,KAAK,OAAO,MAAM;AAAA,EAC1B;AACA,QAAM,QAAoB,CAAC;AAC3B,QAAM,UAAgC,QAAQ,QAAQ,IAAI,CAAC,UAAU;AAAA,IACnE,QAAQ,KAAK;AAAA,IACb,MAAM,KAAK;AAAA,IACX,UAAU,KAAK;AAAA,IACf,QAAQ,KAAK,OAAO,IAAI,CAAC,EAAE,MAAM,QAAQ,OAAO,EAAE,MAAM,QAAQ,EAAE;AAAA,EACpE,EAAE;AACF,aAAW,QAAQ,QAAQ,OAAO;AAChC,YAAQ,QAAQ,eAAe;AAC/B,QAAI;AACF,YAAM,OAAO,MAAM,OAAO,KAAK,KAAK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CHANGED
@@ -1,5 +1,5 @@
1
1
  // src/debug.ts
2
- var VERSION = true ? "0.15.0" : "0.0.0-development";
2
+ var VERSION = true ? "0.17.0" : "0.0.0-development";
3
3
  function installDebug(client) {
4
4
  if (typeof window === "undefined" || client.mode !== "test") {
5
5
  return;
package/dist/index.d.ts CHANGED
@@ -1,8 +1,8 @@
1
- import { a as ZappieError, T as Transport, R as RequestCtx, g as MenuFulfilment, h as MenuResponse, k as SearchRequest, l as SearchPage, m as MenuVersion } from './client-C47e44tl.js';
1
+ import { a as ZappieError, T as Transport, R as RequestCtx, g as MenuFulfilment, h as MenuResponse, k as SearchRequest, l as SearchPage, m as MenuVersion } from './client-uRLLtZJU.js';
2
2
  import { SelectedMenuRequest, SelectedMenuResponse } from './transport/selected-menu.js';
3
- export { A as API_VERSION, E as ErrorDetails, e as MenuReadOptions, M as MenuResource, n as ReadOptions, S as StorageLike, c as StoreResource, Z as Zappie, b as ZappieClientOptions, b as ZappieConfig, f as ZappieErrorCode, d as ZappieEvents, o as createZappie, p as isZappieError } from './client-C47e44tl.js';
4
- import { g as CartIssue, a as Quote, b as CartLineInput, C as CartLine, Q as QuoteRequest, h as WireQuote } from './types-ooHA0lwH.js';
5
- export { i as CartFix, c as CartFulfilment, e as CartIssueCode, d as CartModule, j as CartSnapshot, f as CartState, k as CartSyncAdapter, l as CartSyncDocument, D as DeliveryAddress, m as WireFix, W as WireFulfilment, n as WireIssue, o as WireLine, p as WireMoney } from './types-ooHA0lwH.js';
3
+ export { A as API_VERSION, E as ErrorDetails, e as MenuReadOptions, M as MenuResource, n as ReadOptions, S as StorageLike, c as StoreResource, Z as Zappie, b as ZappieClientOptions, b as ZappieConfig, f as ZappieErrorCode, d as ZappieEvents, o as createZappie, p as isZappieError } from './client-uRLLtZJU.js';
4
+ import { g as CartIssue, a as Quote, b as CartLineInput, C as CartLine, Q as QuoteRequest, i as WireQuote } from './types-K01NHM_a.js';
5
+ export { A as AppliedPerk, j as CartFix, c as CartFulfilment, e as CartIssueCode, d as CartModule, k as CartSnapshot, f as CartState, l as CartSyncAdapter, m as CartSyncDocument, D as DeliveryAddress, n as WireFix, W as WireFulfilment, o as WireIssue, p as WireLine, h as WireMoney } from './types-K01NHM_a.js';
6
6
  import { I as Item, O as OptionGroup, M as Money, a as Menu, B as Branch, S as Store, C as CategoryPage } from './models-asoc6fS3.js';
7
7
  export { b as BranchCategory, c as Category, d as Choice, e as Currency, F as Formatters, f as Fulfilment, g as MenuSchedule, h as MenuSection, i as MoneyFormatOptions, j as OpeningTime, k as OptionCondition, P as PaymentMethodKind, l as SpecialHours, V as Variant, m as format, n as money } from './models-asoc6fS3.js';
8
8
 
package/dist/index.js CHANGED
@@ -11,7 +11,7 @@ import { ZappieError, isZappieError } from "./errors.js";
11
11
  export * from "./models.js";
12
12
  import { format, money } from "./money.js";
13
13
  import { RestTransport } from "./transport/rest.js";
14
- var VERSION = true ? "0.15.0" : "0.0.0-development";
14
+ var VERSION = true ? "0.17.0" : "0.0.0-development";
15
15
  export {
16
16
  API_VERSION,
17
17
  RestTransport,
@@ -1,6 +1,6 @@
1
1
  import { RequestCtx } from '../index.js';
2
2
  import '../models-asoc6fS3.js';
3
- import '../types-ooHA0lwH.js';
3
+ import '../types-K01NHM_a.js';
4
4
 
5
5
  /** Public integration IDs and browser keys. Your app decides when to load scripts. */
6
6
  type Integrations = {
@@ -0,0 +1,35 @@
1
+ // src/internal/perk-hold.ts
2
+ var PREFIX = "zappie:perk-hold:";
3
+ function holdPerks(ids, session) {
4
+ try {
5
+ for (const id of ids ?? []) {
6
+ localStorage[PREFIX + id] = `${session.id} ${session.expires_at}`;
7
+ }
8
+ } catch {
9
+ }
10
+ }
11
+ function heldBy(id) {
12
+ try {
13
+ const [session, until] = (localStorage.getItem(PREFIX + id) ?? "").split(
14
+ " "
15
+ );
16
+ if (session && Date.parse(until) > Date.now()) {
17
+ return session;
18
+ }
19
+ localStorage.removeItem(PREFIX + id);
20
+ } catch {
21
+ }
22
+ return void 0;
23
+ }
24
+ function unhold(id) {
25
+ try {
26
+ localStorage.removeItem(PREFIX + id);
27
+ } catch {
28
+ }
29
+ }
30
+ export {
31
+ heldBy,
32
+ holdPerks,
33
+ unhold
34
+ };
35
+ //# sourceMappingURL=perk-hold.js.map
@@ -0,0 +1,6 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/internal/perk-hold.ts"],
4
+ "mappings": ";AAIA,IAAM,SAAS;AAOR,SAAS,UACd,KACA,SACA;AACA,MAAI;AACF,eAAW,MAAM,OAAO,CAAC,GAAG;AAC1B,mBAAa,SAAS,EAAE,IAAI,GAAG,QAAQ,EAAE,IAAI,QAAQ,UAAU;AAAA,IACjE;AAAA,EACF,QAAQ;AAAA,EAER;AACF;AAGO,SAAS,OAAO,IAAgC;AACrD,MAAI;AACF,UAAM,CAAC,SAAS,KAAK,KAAK,aAAa,QAAQ,SAAS,EAAE,KAAK,IAAI;AAAA,MACjE;AAAA,IACF;AACA,QAAI,WAAW,KAAK,MAAM,KAAM,IAAI,KAAK,IAAI,GAAG;AAC9C,aAAO;AAAA,IACT;AACA,iBAAa,WAAW,SAAS,EAAE;AAAA,EACrC,QAAQ;AAAA,EAER;AACA,SAAO;AACT;AAGO,SAAS,OAAO,IAAY;AACjC,MAAI;AACF,iBAAa,WAAW,SAAS,EAAE;AAAA,EACrC,QAAQ;AAAA,EAER;AACF;",
5
+ "names": []
6
+ }
@@ -36,12 +36,17 @@ function settleShopper(id, storage) {
36
36
  }
37
37
  }
38
38
  function forgetBagAddresses() {
39
+ const perks = [];
39
40
  try {
40
41
  for (const key of Object.keys(localStorage)) {
41
42
  try {
42
43
  const bag = key.startsWith("zappie:cart:v1:") ? JSON.parse(localStorage[key]) : void 0;
43
- if (bag?.fulfilment?.type == "delivery") {
44
- bag.fulfilment = null;
44
+ if (bag) {
45
+ perks.push(...bag.perks ?? []);
46
+ bag.perks = [];
47
+ if (bag.fulfilment?.type == "delivery") {
48
+ bag.fulfilment = null;
49
+ }
45
50
  localStorage[key] = JSON.stringify(bag);
46
51
  }
47
52
  } catch {
@@ -49,6 +54,7 @@ function forgetBagAddresses() {
49
54
  }
50
55
  } catch {
51
56
  }
57
+ return perks;
52
58
  }
53
59
  export {
54
60
  ADDRESS_KEY,
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../src/internal/remembered-address.ts"],
4
- "mappings": ";AAiBO,IAAM,cAAc;AAcpB,SAAS,QACd,QAC2B;AAC3B,QAAM,KAAK,QAAQ;AACnB,SAAO,OAAO,OAAO,YAAY,KAC7B,KACA,QAAQ,UAAU,OAChB,OACA;AACR;AAGO,SAAS,eACd,SACuB;AACvB,MAAI;AACF,WAAO,YAAY,SACf,UACA,OAAO,WAAW,cAChB,OACA,OAAO;AAAA,EACf,QAAQ;AACN,WAAO;AAAA,EACT;AACF;AAGO,SAAS,WACd,SAC2B;AAC3B,MAAI;AACF,UAAM,SAAS,KAAK,MAAM,SAAS,QAAQ,WAAW,KAAK,GAAG;AAC9D,WAAO,UAAU,OAAO,WAAW,WAC9B,SACD;AAAA,EACN,QAAQ;AACN,WAAO;AAAA,EACT;AACF;AAGO,SAAS,wBAAwB,SAAiC;AACvE,MAAI;AACF,mBAAe,OAAO,GAAG,WAAW,WAAW;AAAA,EACjD,QAAQ;AAAA,EAER;AACF;AAGO,SAAS,eAA0C;AACxD,SAAO,WAAW,iBAAiB;AACrC;AAMO,SAAS,cACd,IACA,SACA;AACA,aAAW,kBAAkB,EAAE,GAAG;AAClC,QAAM,QAAQ,QAAQ,WAAW,eAAe,OAAO,CAAC,CAAC;AACzD,MAAI,SAAS,UAAU,IAAI;AACzB,4BAAwB,OAAO;AAAA,EACjC;AACF;AAMO,SAAS,qBAAqB;AACnC,MAAI;AACF,eAAW,OAAO,OAAO,KAAK,YAAY,GAAG;AAC3C,UAAI;AACF,cAAM,MAAM,IAAI,WAAW,iBAAiB,IACvC,KAAK,MAAM,aAAa,GAAG,CAAE,IAG9B;AACJ,YAAI,K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AI,SAAS,CAAC,CAAE;AAC/B,cAAI,QAAQ,CAAC;AACb,cAAI,IAAI,YAAY,QAAQ,YAAY;AACtC,gBAAI,aAAa;AAAA,UACnB;AACA,uBAAa,GAAG,IAAI,KAAK,UAAU,GAAG;AAAA,QACxC;AAAA,MACF,QAAQ;AAAA,MAER;AAAA,IACF;AAAA,EACF,QAAQ;AAAA,EAER;AACA,SAAO;AACT;",
5
5
  "names": []
6
6
  }
@@ -1,5 +1,5 @@
1
1
  import { CartFulfilment, WireFulfilment } from './index.js';
2
- export { v as validateConfig } from './client-C47e44tl.js';
2
+ export { v as validateConfig } from './client-uRLLtZJU.js';
3
3
  import './models-asoc6fS3.js';
4
4
 
5
5
  /**
@@ -0,0 +1,56 @@
1
+ // src/loyalty/cart.ts
2
+ import { ZappieError } from "../internal/customer-http.js";
3
+ import { held, releasePerks } from "./release.js";
4
+ async function applyPerk(cart, loyalty, perk, options = {}) {
5
+ if (cart.fulfilment?.type === "dine_in") {
6
+ throw new ZappieError(
7
+ "invalid_request",
8
+ "Perks can't be used on an order at the table"
9
+ );
10
+ }
11
+ const id = typeof perk === "string" ? perk : perk.id;
12
+ const redemption = await loyalty.redeem(id, options);
13
+ const applied = {
14
+ redemptionId: redemption.id,
15
+ perkId: redemption.perk?.id ?? id,
16
+ name: redemption.perk?.name ?? (typeof perk === "string" ? "" : perk.name),
17
+ pointsSpent: redemption.points_spent ?? 0,
18
+ expiresAt: redemption.expires_at ?? new Date(Date.now() + 864e5).toISOString(),
19
+ ...typeof perk === "string" ? {} : { reward: perk.reward.type }
20
+ };
21
+ try {
22
+ cart.setPerks([
23
+ ...cart.getSnapshot().perks.filter((p) => p.redemptionId !== applied.redemptionId),
24
+ applied
25
+ ]);
26
+ } catch (error) {
27
+ await releasePerks([applied], loyalty.base);
28
+ throw error;
29
+ }
30
+ return applied;
31
+ }
32
+ async function removePerk(cart, loyalty, redemptionId) {
33
+ if (await held(redemptionId, loyalty.base)) {
34
+ throw new ZappieError(
35
+ "invalid_request",
36
+ "This perk is in a payment that is still open. Finish or cancel that payment first."
37
+ );
38
+ }
39
+ try {
40
+ await loyalty.cancel(redemptionId);
41
+ } catch (error) {
42
+ if (!["redemption_used", "not_found"].includes(
43
+ error.apiCode ?? ""
44
+ )) {
45
+ throw error;
46
+ }
47
+ }
48
+ cart.setPerks(
49
+ cart.getSnapshot().perks.filter((p) => p.redemptionId !== redemptionId)
50
+ );
51
+ }
52
+ export {
53
+ applyPerk,
54
+ removePerk
55
+ };
56
+ //# sourceMappingURL=cart.js.map