@usethrottle/cart 3.11.1 → 3.13.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/index.d.ts CHANGED
@@ -60,6 +60,14 @@ interface Cart {
60
60
  lineItems?: LineItem[];
61
61
  createdAt: string;
62
62
  updatedAt: string;
63
+ /**
64
+ * Resolved carts only: the installed app that prices the cart's owned
65
+ * lines. `extensionId` is null when the installation can no longer be
66
+ * found. Absent on a cart created without `lineResolver`.
67
+ */
68
+ lineResolver?: {
69
+ extensionId: string | null;
70
+ };
63
71
  }
64
72
  /**
65
73
  * The cart as the session (browser, opaque-token) surface returns it —
@@ -97,6 +105,19 @@ interface LineRecurringInput {
97
105
  intervalCount?: number;
98
106
  totalPayments?: number;
99
107
  trialDays?: number;
108
+ /**
109
+ * Custom installment schedule: 2-60 payments that differ in amount (per unit,
110
+ * minor units) and in the gap before each one. Row 0 has no gap. Replaces
111
+ * `interval`/`intervalUnit`/`intervalCount`/`totalPayments`/`trialDays`, which
112
+ * must be omitted or agree with it. The line's `unitPrice` must equal row 0.
113
+ */
114
+ schedule?: Array<{
115
+ amount: number;
116
+ intervalUnit?: SubscriptionIntervalUnit;
117
+ intervalCount?: number;
118
+ }>;
119
+ /** Read-only echo on responses: the sum of every row's per-unit amount. Ignored on input. */
120
+ scheduleUnitTotal?: number;
100
121
  }
101
122
  /** A merchant-declared subscription line's stored terms (it carries `plan`). */
102
123
  interface StoredLineRecurring {
@@ -111,6 +132,14 @@ interface StoredLineRecurring {
111
132
  amount: number | null;
112
133
  /** Display mirror of `totalPayments`. */
113
134
  count: number | null;
135
+ /** Present only on a custom installment schedule line. */
136
+ schedule?: Array<{
137
+ amount: number;
138
+ intervalUnit: string | null;
139
+ intervalCount: number | null;
140
+ }>;
141
+ /** Present only on a schedule line: the sum of every row's per-unit amount. */
142
+ scheduleUnitTotal?: number;
114
143
  }
115
144
  /** The plan line Throttle adds for a checkout session `recurring` intent. */
116
145
  interface LegacyLineRecurring {
@@ -144,6 +173,55 @@ interface LineItem {
144
173
  imageUrl: string | null;
145
174
  position: number;
146
175
  metadata: Record<string, unknown>;
176
+ /**
177
+ * Resolved carts only: the app's last answer for this line, or null on a
178
+ * line the app does not own. Absent on an unresolved cart.
179
+ */
180
+ resolution?: LineResolution | null;
181
+ }
182
+ /**
183
+ * An owned line's state on a resolved cart.
184
+ *
185
+ * - `resolved`: the app priced the line on its last answer.
186
+ * - `refused`: the app refused the line on its last answer; `code` says why
187
+ * (`OUT_OF_STOCK`, `QUANTITY_UNAVAILABLE`, `BELOW_MIN`, `ABOVE_MAX`,
188
+ * `NOT_FOUND`, `NOT_PURCHASABLE`, `NO_PRICE_IN_CURRENCY`,
189
+ * `PRICE_SOURCE_MISMATCH`).
190
+ * - `stale`: the last re-resolve failed; the line keeps its earlier price and
191
+ * `code` keeps any earlier refusal. Checkout re-resolves it.
192
+ *
193
+ * `maxQuantity` / `minQuantity` are the limits from the app's last answer
194
+ * (a refusal's own `maxQuantity` wins), or null.
195
+ */
196
+ interface LineResolution {
197
+ status: 'resolved' | 'refused' | 'stale';
198
+ code: string | null;
199
+ maxQuantity: number | null;
200
+ minQuantity: number | null;
201
+ }
202
+ /**
203
+ * A line of a resolved cart: the app sets its name, price and image, so the
204
+ * browser sends only which item and how many.
205
+ */
206
+ interface AddResolvedLineItemInput {
207
+ /** The app's id for the item (for Product Catalog, the variant SKU). */
208
+ referenceId: string;
209
+ /** 1 to 10,000. Defaults to 1. */
210
+ quantity?: number;
211
+ /**
212
+ * Buyer metadata. The reserved keys (`lineResolver`, `requiresShipping`)
213
+ * and the keys that change a shipping or tax quote are set by the app and
214
+ * answer `400 price_locked`.
215
+ */
216
+ metadata?: Record<string, unknown>;
217
+ }
218
+ /**
219
+ * Pick a shipping method on a resolved cart (or in an environment where
220
+ * browser carts may only hold resolved lines): Throttle quotes the amount,
221
+ * so only the method id is sent.
222
+ */
223
+ interface SelectShippingMethodInput {
224
+ methodId: string;
147
225
  }
148
226
  interface SelectedShipping {
149
227
  methodId: string;
@@ -198,6 +276,24 @@ interface CreateCartInput {
198
276
  currency?: string;
199
277
  netN?: number | null;
200
278
  metadata?: Record<string, unknown>;
279
+ /**
280
+ * Make this a resolved cart: the installed app with this extension id
281
+ * prices its lines (add them with `items.addResolvedItem()`). Requires
282
+ * `currency`, which must be the environment's currency.
283
+ */
284
+ lineResolver?: {
285
+ extensionId: string;
286
+ };
287
+ /** Buyer locale passed to the app that prices the lines (BCP 47, e.g. `en-GB`). */
288
+ locale?: string;
289
+ /**
290
+ * Resolved lines to add at create (requires `lineResolver`): 1 to 50, each
291
+ * `referenceId` at most once, priced in one call to the app, all or nothing.
292
+ */
293
+ lineItems?: Array<{
294
+ referenceId: string;
295
+ quantity?: number;
296
+ }>;
201
297
  }
202
298
  interface UpdateCartInput {
203
299
  customerId?: string;
@@ -457,6 +553,12 @@ declare class CartClient {
457
553
  };
458
554
  get items(): {
459
555
  add: (cartId: string, input: AddLineItemInput) => Promise<LineItem>;
556
+ /**
557
+ * Add a line to a resolved cart (one created with `lineResolver`). Only
558
+ * the item, quantity and metadata are sent; the app sets the name, price
559
+ * and image. Refusals throw the typed resolver errors.
560
+ */
561
+ addResolvedItem: (cartId: string, input: AddResolvedLineItemInput) => Promise<LineItem>;
460
562
  update: (cartId: string, itemId: string, input: UpdateLineItemInput) => Promise<LineItem>;
461
563
  remove: (cartId: string, itemId: string) => Promise<void>;
462
564
  };
@@ -546,6 +648,16 @@ interface CreateCartSessionInput {
546
648
  currency?: string;
547
649
  netN?: number;
548
650
  metadata?: Record<string, unknown>;
651
+ /**
652
+ * Make this a resolved cart: the installed app with this extension id
653
+ * prices its lines (add them with `addResolvedItem()`). Requires
654
+ * `currency`, which must be the environment's currency.
655
+ */
656
+ lineResolver?: {
657
+ extensionId: string;
658
+ };
659
+ /** Buyer locale passed to the app that prices the lines (BCP 47, e.g. `en-US`). */
660
+ locale?: string;
549
661
  }
550
662
  interface CartBranding {
551
663
  logoUrl: string | null;
@@ -639,9 +751,32 @@ declare class CartSessionClient {
639
751
  */
640
752
  summary(): Promise<CartSessionSummary>;
641
753
  addItem(input: AddLineItemInput): Promise<CartSessionCart>;
754
+ /**
755
+ * Add a line to a resolved cart (one created with `lineResolver`). Only the
756
+ * item, quantity and metadata are sent; the app sets the name, price and
757
+ * image. A refusal throws `QuantityUnavailableError`,
758
+ * `LineNotPurchasableError` or `LineResolverUnavailableError`.
759
+ */
760
+ addResolvedItem(input: AddResolvedLineItemInput): Promise<CartSessionCart>;
761
+ /**
762
+ * Ask the app that prices a resolved cart to re-check every line it owns.
763
+ * `changed` is true when a price, name or refusal changed. An unresolved
764
+ * cart comes back unchanged. Throws `LineResolverUnavailableError` (nothing
765
+ * changed) when the app does not answer.
766
+ */
767
+ refresh(): Promise<CartSessionCart & {
768
+ changed: boolean;
769
+ }>;
642
770
  updateItem(itemId: string, input: UpdateLineItemInput): Promise<CartSessionCart>;
643
771
  removeItem(itemId: string): Promise<CartSessionCart>;
644
772
  selectShipping(input: SelectShippingInput): Promise<CartSessionCart>;
773
+ /**
774
+ * Pick a shipping method on a resolved cart. Throttle quotes the amount, so
775
+ * only `methodId` is sent. A method Throttle does not quote for the cart
776
+ * (it needs a shipping address first) throws `ShippingMethodUnavailableError`;
777
+ * a failed quote throws `ShippingQuoteUnavailableError`.
778
+ */
779
+ selectShippingMethod(input: SelectShippingMethodInput): Promise<CartSessionCart>;
645
780
  clearShipping(): Promise<CartSessionCart>;
646
781
  applyDiscount(code: string): Promise<CartSessionCart>;
647
782
  removeDiscount(): Promise<CartSessionCart>;
@@ -738,6 +873,174 @@ declare class CartTimeoutError extends ThrottleError {
738
873
  path?: string;
739
874
  });
740
875
  }
876
+ /** Constructor arguments shared by the resolved-cart error classes. */
877
+ interface CartErrorArgs {
878
+ message: string;
879
+ statusCode?: number;
880
+ details?: Record<string, unknown>;
881
+ responseHeaders?: ThrottleErrorArgs['responseHeaders'];
882
+ }
883
+ /** `details` of `400 price_locked`: the request fields the app sets. */
884
+ type PriceLockedDetails = {
885
+ fields: string[];
886
+ };
887
+ /**
888
+ * `400 price_locked`: the request sent a field the pricing app owns (a
889
+ * price, name, image, or a reserved or quote-affecting metadata key). On a
890
+ * resolved cart, add lines with `addResolvedItem()`.
891
+ */
892
+ declare class PriceLockedError extends ThrottleApiError {
893
+ readonly details?: PriceLockedDetails;
894
+ /** The refused fields, e.g. `['unitPrice', 'metadata.lineResolver']`. */
895
+ readonly fields: string[];
896
+ constructor(args: CartErrorArgs);
897
+ }
898
+ /** `details` of `409 quantity_unavailable`. */
899
+ type QuantityUnavailableDetails = {
900
+ /** `OUT_OF_STOCK`, `QUANTITY_UNAVAILABLE`, `BELOW_MIN`, `ABOVE_MAX` or `MAX_LINE_ITEM_QUANTITY`. */
901
+ reason: string;
902
+ maxQuantity?: number;
903
+ referenceId?: string;
904
+ itemId?: string;
905
+ };
906
+ /**
907
+ * `409 quantity_unavailable`: the app refused the quantity. Nothing in the
908
+ * cart changed. `maxQuantity` is how many can be ordered, when known.
909
+ */
910
+ declare class QuantityUnavailableError extends ThrottleApiError {
911
+ readonly details?: QuantityUnavailableDetails;
912
+ readonly reason: string | null;
913
+ readonly maxQuantity: number | null;
914
+ constructor(args: CartErrorArgs);
915
+ }
916
+ /** A line refused at checkout hand-off (`line_not_purchasable` `details.lines`). */
917
+ type RefusedCartLine = {
918
+ itemId: string;
919
+ referenceId: string;
920
+ code: string;
921
+ maxQuantity?: number;
922
+ };
923
+ /**
924
+ * `details` of `422 line_not_purchasable`: `{ reason, referenceId, itemId? }`
925
+ * on an add or quantity change, `{ lines }` at the checkout hand-off.
926
+ */
927
+ type LineNotPurchasableDetails = {
928
+ /** `NOT_FOUND`, `NOT_PURCHASABLE`, `NO_PRICE_IN_CURRENCY` or `PRICE_SOURCE_MISMATCH`. */
929
+ reason?: string;
930
+ referenceId?: string;
931
+ itemId?: string;
932
+ lines?: RefusedCartLine[];
933
+ };
934
+ /**
935
+ * `422 line_not_purchasable`: the app refused the item (`reason`), or, at
936
+ * checkout, some lines cannot be bought (`lines`; remove them or change their
937
+ * quantity, then check out again).
938
+ */
939
+ declare class LineNotPurchasableError extends ThrottleApiError {
940
+ readonly details?: LineNotPurchasableDetails;
941
+ readonly reason: string | null;
942
+ readonly lines: RefusedCartLine[] | null;
943
+ constructor(args: CartErrorArgs);
944
+ }
945
+ /** `details` of `503 line_resolver_unavailable`. */
946
+ type LineResolverUnavailableDetails = {
947
+ reason?: string;
948
+ };
949
+ /**
950
+ * `503 line_resolver_unavailable`: the app that prices the cart did not
951
+ * answer (or answered badly). Nothing in the cart changed; retry later.
952
+ */
953
+ declare class LineResolverUnavailableError extends ThrottleApiError {
954
+ readonly details?: LineResolverUnavailableDetails;
955
+ readonly reason: string | null;
956
+ constructor(args: CartErrorArgs);
957
+ }
958
+ /** `details` of `422 line_resolver_not_available`. */
959
+ type LineResolverNotAvailableDetails = {
960
+ reason?: string;
961
+ };
962
+ /**
963
+ * `422 line_resolver_not_available`: the app named in `lineResolver` is not
964
+ * installed (or cannot price carts) in this environment, so no cart was created.
965
+ */
966
+ declare class LineResolverNotAvailableError extends ThrottleApiError {
967
+ readonly details?: LineResolverNotAvailableDetails;
968
+ readonly reason: string | null;
969
+ constructor(args: CartErrorArgs);
970
+ }
971
+ /** `details` of `422 currency_not_enabled`. */
972
+ type CurrencyNotEnabledDetails = {
973
+ currency: string;
974
+ enabledCurrencies: string[];
975
+ };
976
+ /** `422 currency_not_enabled`: a resolved cart must use the environment's currency. */
977
+ declare class CurrencyNotEnabledError extends ThrottleApiError {
978
+ readonly details?: CurrencyNotEnabledDetails;
979
+ constructor(args: CartErrorArgs);
980
+ }
981
+ /**
982
+ * `422 resolved_lines_required`: the store only accepts cart lines priced by
983
+ * an installed app; create the cart session with `lineResolver`.
984
+ */
985
+ declare class ResolvedLinesRequiredError extends ThrottleApiError {
986
+ constructor(args: CartErrorArgs);
987
+ }
988
+ /** `details` of `422 line_limit_reached`. */
989
+ type LineLimitReachedDetails = {
990
+ maxLines: number;
991
+ };
992
+ /** `422 line_limit_reached`: a cart holds at most `maxLines` lines priced by one app. */
993
+ declare class LineLimitReachedError extends ThrottleApiError {
994
+ readonly details?: LineLimitReachedDetails;
995
+ constructor(args: CartErrorArgs);
996
+ }
997
+ /**
998
+ * The stored shipping selection after the checkout hand-off re-quoted it.
999
+ * `amount` is the new quote, or null with a `reason`: `method_unavailable`
1000
+ * (no longer offered, so cleared) or `quote_unavailable` (could not be
1001
+ * quoted now; kept for the next hand-off).
1002
+ */
1003
+ type CartShippingChange = {
1004
+ methodId: string;
1005
+ previousAmount: number;
1006
+ amount: number | null;
1007
+ reason?: 'method_unavailable' | 'quote_unavailable';
1008
+ };
1009
+ /** `details` of `409 cart_prices_changed`. */
1010
+ type CartPricesChangedDetails = {
1011
+ cart: CartSessionCart;
1012
+ shipping?: CartShippingChange;
1013
+ };
1014
+ /**
1015
+ * `409 cart_prices_changed` at checkout: prices (or shipping) changed since
1016
+ * the buyer last saw the cart. The new prices are already applied; show
1017
+ * `cart`, then check out again. `cart` is null when the response carried none.
1018
+ */
1019
+ declare class CartPricesChangedError extends ThrottleApiError {
1020
+ readonly details?: CartPricesChangedDetails;
1021
+ readonly cart: CartSessionCart | null;
1022
+ readonly shipping: CartShippingChange | null;
1023
+ constructor(args: CartErrorArgs);
1024
+ }
1025
+ /**
1026
+ * `422 shipping_method_unavailable`: on a resolved cart shipping is priced by
1027
+ * Throttle; the method is not one Throttle quotes for this cart.
1028
+ */
1029
+ declare class ShippingMethodUnavailableError extends ThrottleApiError {
1030
+ constructor(args: CartErrorArgs);
1031
+ }
1032
+ /** `503 shipping_quote_unavailable`: shipping cannot be priced right now. Nothing changed. */
1033
+ declare class ShippingQuoteUnavailableError extends ThrottleApiError {
1034
+ constructor(args: CartErrorArgs);
1035
+ }
1036
+ /** `422 amount_too_large`: the line or cart amount is larger than a cart can hold. */
1037
+ declare class AmountTooLargeError extends ThrottleApiError {
1038
+ constructor(args: CartErrorArgs);
1039
+ }
1040
+ /** `409 cart_changed`: the cart changed while the item was being added; nothing was added. Retry. */
1041
+ declare class CartChangedError extends ThrottleApiError {
1042
+ constructor(args: CartErrorArgs);
1043
+ }
741
1044
  /**
742
1045
  * Map a raw API error envelope to the most specific Cart SDK error class.
743
1046
  * Falls back to {@link ThrottleApiError} for everything unrecognised.
@@ -750,4 +1053,4 @@ declare function cartErrorFromResponse(args: {
750
1053
  responseHeaders?: ThrottleErrorArgs['responseHeaders'];
751
1054
  }): ThrottleApiError;
752
1055
 
753
- export { type AddLineItemInput, type AppliedDiscount, type Cart, type CartAddress, type CartBranding, CartClient, type CartEvent, CartNotFoundError, CartNotOpenError, type CartSession, type CartSessionCart, CartSessionClient, type CartSessionClientOptions, type CartSessionSummary, CartTimeoutError, type CheckoutHandoff, type CheckoutHandoffInput, type CheckoutInput, type CreateCartInput, type CreateCartSessionInput, type ExternalShippingTaxSnapshotInput, type LegacyLineRecurring, type LineItem, type LineRecurringInput, type Order, type SelectShippingInput, type SelectedShipping, type ShippingTaxAddress, type ShippingTaxCalculationResponse, type ShippingTaxCartCalculateInput, type ShippingTaxMethod, type ShippingTaxQuoteInput, type ShippingTaxQuoteItem, type StoredLineRecurring, StorefrontQuoteClient, type SubscriptionInterval, type SubscriptionIntervalUnit, type TaxLine, type TaxLineInput, ThrottleApiError, type UpdateCartInput, type UpdateCartSessionInput, type UpdateLineItemInput, cartErrorFromResponse };
1056
+ export { type AddLineItemInput, type AddResolvedLineItemInput, AmountTooLargeError, type AppliedDiscount, type Cart, type CartAddress, type CartBranding, CartChangedError, CartClient, type CartErrorArgs, type CartEvent, CartNotFoundError, CartNotOpenError, type CartPricesChangedDetails, CartPricesChangedError, type CartSession, type CartSessionCart, CartSessionClient, type CartSessionClientOptions, type CartSessionSummary, type CartShippingChange, CartTimeoutError, type CheckoutHandoff, type CheckoutHandoffInput, type CheckoutInput, type CreateCartInput, type CreateCartSessionInput, type CurrencyNotEnabledDetails, CurrencyNotEnabledError, type ExternalShippingTaxSnapshotInput, type LegacyLineRecurring, type LineItem, type LineLimitReachedDetails, LineLimitReachedError, type LineNotPurchasableDetails, LineNotPurchasableError, type LineRecurringInput, type LineResolution, type LineResolverNotAvailableDetails, LineResolverNotAvailableError, type LineResolverUnavailableDetails, LineResolverUnavailableError, type Order, type PriceLockedDetails, PriceLockedError, type QuantityUnavailableDetails, QuantityUnavailableError, type RefusedCartLine, ResolvedLinesRequiredError, type SelectShippingInput, type SelectShippingMethodInput, type SelectedShipping, ShippingMethodUnavailableError, ShippingQuoteUnavailableError, type ShippingTaxAddress, type ShippingTaxCalculationResponse, type ShippingTaxCartCalculateInput, type ShippingTaxMethod, type ShippingTaxQuoteInput, type ShippingTaxQuoteItem, type StoredLineRecurring, StorefrontQuoteClient, type SubscriptionInterval, type SubscriptionIntervalUnit, type TaxLine, type TaxLineInput, ThrottleApiError, type UpdateCartInput, type UpdateCartSessionInput, type UpdateLineItemInput, cartErrorFromResponse };
package/dist/index.js CHANGED
@@ -42,6 +42,130 @@ var CartTimeoutError = class extends ThrottleError {
42
42
  this.name = "CartTimeoutError";
43
43
  }
44
44
  };
45
+ function coded(code, statusCode, args) {
46
+ return {
47
+ code,
48
+ statusCode: args.statusCode ?? statusCode,
49
+ message: args.message,
50
+ details: args.details,
51
+ responseHeaders: args.responseHeaders
52
+ };
53
+ }
54
+ var str = (v) => typeof v === "string" ? v : null;
55
+ var num = (v) => typeof v === "number" ? v : null;
56
+ var PriceLockedError = class extends ThrottleApiError {
57
+ /** The refused fields, e.g. `['unitPrice', 'metadata.lineResolver']`. */
58
+ fields;
59
+ constructor(args) {
60
+ super(coded("price_locked", 400, args));
61
+ this.name = "PriceLockedError";
62
+ this.fields = Array.isArray(args.details?.fields) ? args.details.fields : [];
63
+ }
64
+ };
65
+ var QuantityUnavailableError = class extends ThrottleApiError {
66
+ reason;
67
+ maxQuantity;
68
+ constructor(args) {
69
+ super(coded("quantity_unavailable", 409, args));
70
+ this.name = "QuantityUnavailableError";
71
+ this.reason = str(args.details?.reason);
72
+ this.maxQuantity = num(args.details?.maxQuantity);
73
+ }
74
+ };
75
+ var LineNotPurchasableError = class extends ThrottleApiError {
76
+ reason;
77
+ lines;
78
+ constructor(args) {
79
+ super(coded("line_not_purchasable", 422, args));
80
+ this.name = "LineNotPurchasableError";
81
+ this.reason = str(args.details?.reason);
82
+ this.lines = Array.isArray(args.details?.lines) ? args.details.lines : null;
83
+ }
84
+ };
85
+ var LineResolverUnavailableError = class extends ThrottleApiError {
86
+ reason;
87
+ constructor(args) {
88
+ super(coded("line_resolver_unavailable", 503, args));
89
+ this.name = "LineResolverUnavailableError";
90
+ this.reason = str(args.details?.reason);
91
+ }
92
+ };
93
+ var LineResolverNotAvailableError = class extends ThrottleApiError {
94
+ reason;
95
+ constructor(args) {
96
+ super(coded("line_resolver_not_available", 422, args));
97
+ this.name = "LineResolverNotAvailableError";
98
+ this.reason = str(args.details?.reason);
99
+ }
100
+ };
101
+ var CurrencyNotEnabledError = class extends ThrottleApiError {
102
+ constructor(args) {
103
+ super(coded("currency_not_enabled", 422, args));
104
+ this.name = "CurrencyNotEnabledError";
105
+ }
106
+ };
107
+ var ResolvedLinesRequiredError = class extends ThrottleApiError {
108
+ constructor(args) {
109
+ super(coded("resolved_lines_required", 422, args));
110
+ this.name = "ResolvedLinesRequiredError";
111
+ }
112
+ };
113
+ var LineLimitReachedError = class extends ThrottleApiError {
114
+ constructor(args) {
115
+ super(coded("line_limit_reached", 422, args));
116
+ this.name = "LineLimitReachedError";
117
+ }
118
+ };
119
+ var CartPricesChangedError = class extends ThrottleApiError {
120
+ cart;
121
+ shipping;
122
+ constructor(args) {
123
+ super(coded("cart_prices_changed", 409, args));
124
+ this.name = "CartPricesChangedError";
125
+ const d = args.details;
126
+ this.cart = d?.cart && typeof d.cart === "object" ? d.cart : null;
127
+ this.shipping = d?.shipping && typeof d.shipping === "object" ? d.shipping : null;
128
+ }
129
+ };
130
+ var ShippingMethodUnavailableError = class extends ThrottleApiError {
131
+ constructor(args) {
132
+ super(coded("shipping_method_unavailable", 422, args));
133
+ this.name = "ShippingMethodUnavailableError";
134
+ }
135
+ };
136
+ var ShippingQuoteUnavailableError = class extends ThrottleApiError {
137
+ constructor(args) {
138
+ super(coded("shipping_quote_unavailable", 503, args));
139
+ this.name = "ShippingQuoteUnavailableError";
140
+ }
141
+ };
142
+ var AmountTooLargeError = class extends ThrottleApiError {
143
+ constructor(args) {
144
+ super(coded("amount_too_large", 422, args));
145
+ this.name = "AmountTooLargeError";
146
+ }
147
+ };
148
+ var CartChangedError = class extends ThrottleApiError {
149
+ constructor(args) {
150
+ super(coded("cart_changed", 409, args));
151
+ this.name = "CartChangedError";
152
+ }
153
+ };
154
+ var RESOLVED_CART_ERRORS = {
155
+ price_locked: PriceLockedError,
156
+ quantity_unavailable: QuantityUnavailableError,
157
+ line_not_purchasable: LineNotPurchasableError,
158
+ line_resolver_unavailable: LineResolverUnavailableError,
159
+ line_resolver_not_available: LineResolverNotAvailableError,
160
+ currency_not_enabled: CurrencyNotEnabledError,
161
+ resolved_lines_required: ResolvedLinesRequiredError,
162
+ line_limit_reached: LineLimitReachedError,
163
+ cart_prices_changed: CartPricesChangedError,
164
+ shipping_method_unavailable: ShippingMethodUnavailableError,
165
+ shipping_quote_unavailable: ShippingQuoteUnavailableError,
166
+ amount_too_large: AmountTooLargeError,
167
+ cart_changed: CartChangedError
168
+ };
45
169
  function cartErrorFromResponse(args) {
46
170
  if (args.statusCode === 409 && args.code === "cart_not_open") {
47
171
  return new CartNotOpenError({
@@ -59,6 +183,15 @@ function cartErrorFromResponse(args) {
59
183
  responseHeaders: args.responseHeaders
60
184
  });
61
185
  }
186
+ const ResolvedCartError = Object.prototype.hasOwnProperty.call(RESOLVED_CART_ERRORS, args.code) ? RESOLVED_CART_ERRORS[args.code] : void 0;
187
+ if (ResolvedCartError) {
188
+ return new ResolvedCartError({
189
+ message: args.message,
190
+ statusCode: args.statusCode,
191
+ details: args.details,
192
+ responseHeaders: args.responseHeaders
193
+ });
194
+ }
62
195
  return new ThrottleApiError(args);
63
196
  }
64
197
 
@@ -116,6 +249,17 @@ var CartClient = class {
116
249
  get items() {
117
250
  return {
118
251
  add: (cartId, input) => this.request("POST", `/api/v1/carts/${segment(cartId)}/items`, input),
252
+ /**
253
+ * Add a line to a resolved cart (one created with `lineResolver`). Only
254
+ * the item, quantity and metadata are sent; the app sets the name, price
255
+ * and image. Refusals throw the typed resolver errors.
256
+ */
257
+ addResolvedItem: (cartId, input) => {
258
+ const body = { referenceId: input.referenceId };
259
+ if (input.quantity !== void 0) body.quantity = input.quantity;
260
+ if (input.metadata !== void 0) body.metadata = input.metadata;
261
+ return this.request("POST", `/api/v1/carts/${segment(cartId)}/items`, body);
262
+ },
119
263
  update: (cartId, itemId, input) => this.request("PATCH", `/api/v1/carts/${segment(cartId)}/items/${segment(itemId)}`, input),
120
264
  remove: (cartId, itemId) => this.request("DELETE", `/api/v1/carts/${segment(cartId)}/items/${segment(itemId)}`)
121
265
  };
@@ -288,6 +432,32 @@ var CartSessionClient = class _CartSessionClient {
288
432
  async addItem(input) {
289
433
  return (await this.request("POST", `${this.basePath()}/items`, input)).cart;
290
434
  }
435
+ /**
436
+ * Add a line to a resolved cart (one created with `lineResolver`). Only the
437
+ * item, quantity and metadata are sent; the app sets the name, price and
438
+ * image. A refusal throws `QuantityUnavailableError`,
439
+ * `LineNotPurchasableError` or `LineResolverUnavailableError`.
440
+ */
441
+ async addResolvedItem(input) {
442
+ const body = { referenceId: input.referenceId };
443
+ if (input.quantity !== void 0) body.quantity = input.quantity;
444
+ if (input.metadata !== void 0) body.metadata = input.metadata;
445
+ return (await this.request("POST", `${this.basePath()}/items`, body)).cart;
446
+ }
447
+ /**
448
+ * Ask the app that prices a resolved cart to re-check every line it owns.
449
+ * `changed` is true when a price, name or refusal changed. An unresolved
450
+ * cart comes back unchanged. Throws `LineResolverUnavailableError` (nothing
451
+ * changed) when the app does not answer.
452
+ */
453
+ async refresh() {
454
+ const data = await this.request(
455
+ "POST",
456
+ `${this.basePath()}/refresh`,
457
+ {}
458
+ );
459
+ return { ...data.cart, changed: data.changed };
460
+ }
291
461
  async updateItem(itemId, input) {
292
462
  return (await this.request(
293
463
  "PATCH",
@@ -301,6 +471,17 @@ var CartSessionClient = class _CartSessionClient {
301
471
  async selectShipping(input) {
302
472
  return (await this.request("POST", `${this.basePath()}/shipping`, input)).cart;
303
473
  }
474
+ /**
475
+ * Pick a shipping method on a resolved cart. Throttle quotes the amount, so
476
+ * only `methodId` is sent. A method Throttle does not quote for the cart
477
+ * (it needs a shipping address first) throws `ShippingMethodUnavailableError`;
478
+ * a failed quote throws `ShippingQuoteUnavailableError`.
479
+ */
480
+ async selectShippingMethod(input) {
481
+ return (await this.request("POST", `${this.basePath()}/shipping`, {
482
+ methodId: input.methodId
483
+ })).cart;
484
+ }
304
485
  async clearShipping() {
305
486
  return (await this.request("DELETE", `${this.basePath()}/shipping`)).cart;
306
487
  }
@@ -388,11 +569,24 @@ var CartSessionClient = class _CartSessionClient {
388
569
  }
389
570
  };
390
571
  export {
572
+ AmountTooLargeError,
573
+ CartChangedError,
391
574
  CartClient,
392
575
  CartNotFoundError,
393
576
  CartNotOpenError,
577
+ CartPricesChangedError,
394
578
  CartSessionClient,
395
579
  CartTimeoutError,
580
+ CurrencyNotEnabledError,
581
+ LineLimitReachedError,
582
+ LineNotPurchasableError,
583
+ LineResolverNotAvailableError,
584
+ LineResolverUnavailableError,
585
+ PriceLockedError,
586
+ QuantityUnavailableError,
587
+ ResolvedLinesRequiredError,
588
+ ShippingMethodUnavailableError,
589
+ ShippingQuoteUnavailableError,
396
590
  StorefrontQuoteClient,
397
591
  ThrottleApiError,
398
592
  ThrottleError2 as ThrottleError,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@usethrottle/cart",
3
- "version": "3.11.1",
3
+ "version": "3.13.0",
4
4
  "description": "Typed REST client for the Throttle Cart API.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",