@kiva/kv-shop 1.3.2 → 1.4.1

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/dist/basket.cjs +44 -15
  2. package/dist/basket.d.ts +4 -4
  3. package/dist/basket.js +5 -8
  4. package/dist/basketCredits.cjs +197 -0
  5. package/dist/basketCredits.d.ts +18 -0
  6. package/dist/basketCredits.js +12 -0
  7. package/dist/basketItems.cjs +70 -33
  8. package/dist/basketItems.d.ts +11 -1
  9. package/dist/basketItems.js +5 -3
  10. package/dist/basketTotals.cjs +228 -0
  11. package/dist/basketTotals.d.ts +37 -0
  12. package/dist/basketTotals.js +12 -0
  13. package/dist/basketVerification.cjs +41 -0
  14. package/dist/basketVerification.d.ts +10 -0
  15. package/dist/basketVerification.js +8 -0
  16. package/dist/checkoutStatus.cjs +110 -0
  17. package/dist/checkoutStatus.d.ts +20 -0
  18. package/dist/checkoutStatus.js +10 -0
  19. package/dist/{chunk-B7RLQXI6.js → chunk-2NC7LGGO.js} +14 -12
  20. package/dist/{chunk-IYCMZ5WV.js → chunk-4ODZGLWK.js} +21 -0
  21. package/dist/chunk-AI6E33YE.js +33 -0
  22. package/dist/chunk-CBJJUUVR.js +121 -0
  23. package/dist/chunk-DZGZX4F6.js +45 -0
  24. package/dist/chunk-EJ5NGYPO.js +145 -0
  25. package/dist/chunk-FCAOCO7O.js +17 -0
  26. package/dist/chunk-JBQ2KNMP.js +50 -0
  27. package/dist/{chunk-SLMBU6L7.js → chunk-JXQPCEKG.js} +9 -2
  28. package/dist/{chunk-TIASV6B2.js → chunk-KV4SOUVF.js} +1 -1
  29. package/dist/chunk-LZ4UMRCV.js +16 -0
  30. package/dist/chunk-MRCBNXPS.js +35 -0
  31. package/dist/chunk-PC4WUPYU.js +78 -0
  32. package/dist/chunk-TPJPGUO7.js +12 -0
  33. package/dist/components/KvPaymentSelect.vue +8 -1
  34. package/dist/index.cjs +489 -56
  35. package/dist/index.d.ts +12 -5
  36. package/dist/index.js +55 -21
  37. package/dist/oneTimeCheckout.cjs +396 -9
  38. package/dist/oneTimeCheckout.d.ts +11 -10
  39. package/dist/oneTimeCheckout.js +10 -5
  40. package/dist/shopError.cjs +21 -0
  41. package/dist/shopError.d.ts +2 -0
  42. package/dist/shopError.js +1 -1
  43. package/dist/shopQueries.cjs +248 -0
  44. package/dist/shopQueries.d.ts +8 -0
  45. package/dist/shopQueries.js +13 -0
  46. package/dist/subscriptionCheckout.cjs +21 -0
  47. package/dist/subscriptionCheckout.js +2 -2
  48. package/dist/useBraintreeDropIn.cjs +29 -1
  49. package/dist/useBraintreeDropIn.d.ts +9 -9
  50. package/dist/useBraintreeDropIn.js +2 -2
  51. package/dist/validatePreCheckout.cjs +210 -0
  52. package/dist/validatePreCheckout.d.ts +22 -0
  53. package/dist/validatePreCheckout.js +13 -0
  54. package/package.json +3 -3
  55. package/dist/chunk-M4CJOCIQ.js +0 -26
  56. package/dist/chunk-NC7RAUNV.js +0 -66
@@ -0,0 +1,228 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // src/basketTotals.ts
20
+ var basketTotals_exports = {};
21
+ __export(basketTotals_exports, {
22
+ basketTotalsQuery: () => basketTotalsQuery,
23
+ watchBasketTotals: () => watchBasketTotals
24
+ });
25
+ module.exports = __toCommonJS(basketTotals_exports);
26
+ var import_core2 = require("@apollo/client/core");
27
+
28
+ // src/basket.ts
29
+ var import_core = require("@apollo/client/core");
30
+
31
+ // src/util/cookie.ts
32
+ var getCookieValue = (name) => {
33
+ if (typeof document !== void 0) {
34
+ return decodeURIComponent(document.cookie.match(`(^|;)\\s*${name}\\s*=\\s*([^;]+)`)?.pop() || "");
35
+ }
36
+ };
37
+ var setCookieValue = (name, value, options = "") => {
38
+ if (typeof document !== void 0) {
39
+ document.cookie = `${name}=${encodeURIComponent(value)};${options}`;
40
+ }
41
+ };
42
+
43
+ // src/shopError.ts
44
+ var ShopError = class extends Error {
45
+ constructor({ code, original }, ...params) {
46
+ super(...params);
47
+ if (Error.captureStackTrace) {
48
+ Error.captureStackTrace(this, ShopError);
49
+ }
50
+ this.name = "ShopError";
51
+ this.code = code;
52
+ this.original = original;
53
+ }
54
+ aggregateErrors(errors) {
55
+ this.errors = errors;
56
+ }
57
+ };
58
+ function parseShopError(error) {
59
+ if (error instanceof ShopError) {
60
+ return error;
61
+ }
62
+ const errorCode = error?.code ?? error?.extensions?.code ?? error?.name ?? "";
63
+ const errorMessage = typeof error === "string" ? error : error?.message ?? "";
64
+ if (errorCode === "invalidMethodParameter" && errorMessage.includes("paymentMethod.create")) {
65
+ return new ShopError({
66
+ code: "paymentMethod.create.invalidMethodParameter",
67
+ original: error
68
+ }, "There was a problem validating your payment information. Please double-check the details and try again.");
69
+ }
70
+ if (errorMessage.includes("Invalid request: ")) {
71
+ const finalError = errorMessage.split("Invalid request: ")[1].split("., ").map((e) => e.matchAll(/[A-Z_]+: (.*)/g))[0];
72
+ const finalCode = finalError[1];
73
+ const finalMessage = finalError[2];
74
+ return new ShopError({
75
+ code: `paymentMethod.${finalCode}`,
76
+ original: error
77
+ }, finalMessage);
78
+ }
79
+ if (errorCode === "insufficientFunds" || errorMessage.includes("There is not enough credit")) {
80
+ return new ShopError({
81
+ code: "shop.insufficientFunds",
82
+ original: error
83
+ }, "There is not enough money to complete the checkout. Please double-check the details and try again.");
84
+ }
85
+ if (errorCode === "shop.invalidBasketId" || errorCode === "shop.basketRequired" || errorCode === "shop.alreadyCheckedOut") {
86
+ return new ShopError({
87
+ code: errorCode,
88
+ original: error
89
+ }, "There was a problem with your basket. Please, refresh the page and try again.");
90
+ }
91
+ if (errorCode === "donationAmountTooLarge") {
92
+ return new ShopError({
93
+ code: errorCode,
94
+ original: error
95
+ }, errorMessage);
96
+ }
97
+ return new ShopError({
98
+ code: "shop.unknown",
99
+ original: error
100
+ }, "An unknown error occurred.");
101
+ }
102
+
103
+ // src/basket.ts
104
+ function getBasketID() {
105
+ return getCookieValue("kvbskt");
106
+ }
107
+ function setBasketID(basketId) {
108
+ setCookieValue("kvbskt", basketId, "path=/;secure;");
109
+ }
110
+ async function createBasketHelper(apollo) {
111
+ try {
112
+ return apollo.mutate({
113
+ mutation: import_core.gql`mutation createNewBasketForUser { shop { id createBasket } }`
114
+ }).then(({ data }) => {
115
+ const newBasketId = data.shop?.createBasket ?? null;
116
+ if (newBasketId) {
117
+ setBasketID(newBasketId);
118
+ }
119
+ });
120
+ } catch (error) {
121
+ throw parseShopError(error);
122
+ }
123
+ }
124
+ var activeBasketCreationQuery = null;
125
+ async function createBasket(apollo) {
126
+ if (activeBasketCreationQuery) {
127
+ return activeBasketCreationQuery;
128
+ }
129
+ activeBasketCreationQuery = createBasketHelper(apollo);
130
+ return activeBasketCreationQuery;
131
+ }
132
+ function hasBasketExpired(error) {
133
+ const errorCode = error?.code ?? error?.extensions?.code ?? error?.name ?? "";
134
+ return ["shop.invalidBasketId", "shop.basketRequired", "shop.alreadyCheckedOut"].includes(errorCode);
135
+ }
136
+
137
+ // src/shopQueries.ts
138
+ var watchQueries = /* @__PURE__ */ new Set();
139
+ function watchShopQuery(apollo, options, maxretries = 2) {
140
+ let retries = 0;
141
+ watchQueries.add(options.query);
142
+ const observable = apollo.watchQuery({
143
+ ...options,
144
+ variables: {
145
+ ...options.variables,
146
+ basketId: getBasketID()
147
+ }
148
+ });
149
+ const oldSubscribe = observable.subscribe.bind(observable);
150
+ observable.subscribe = (...args) => {
151
+ let nextFn;
152
+ let errorFn;
153
+ let completeFn;
154
+ if (typeof args[0] === "function") {
155
+ [nextFn, errorFn, completeFn] = args;
156
+ } else {
157
+ nextFn = args[0]?.next;
158
+ errorFn = args[0]?.error;
159
+ completeFn = args[0]?.complete;
160
+ }
161
+ const newErrorFn = (err) => {
162
+ const basketErrors = err?.graphQLErrors?.filter((e) => hasBasketExpired(e));
163
+ if (basketErrors.length) {
164
+ if (retries < maxretries) {
165
+ createBasket(apollo).then(() => {
166
+ retries += 1;
167
+ observable.refetch({
168
+ ...observable.variables,
169
+ basketId: getBasketID()
170
+ });
171
+ });
172
+ } else {
173
+ errorFn(parseShopError(basketErrors[0]));
174
+ }
175
+ } else {
176
+ if (err?.graphQLErrors?.length) {
177
+ errorFn(parseShopError(err.graphQLErrors[0]));
178
+ }
179
+ if (err?.networkError) {
180
+ errorFn(parseShopError(err.networkError));
181
+ }
182
+ }
183
+ };
184
+ return oldSubscribe(nextFn, newErrorFn, completeFn);
185
+ };
186
+ return observable;
187
+ }
188
+
189
+ // src/basketTotals.ts
190
+ var basketTotalsQuery = import_core2.gql`query basketTotals($basketId: String) {
191
+ shop (basketId: $basketId) {
192
+ id
193
+ basket {
194
+ id
195
+ totals {
196
+ bonusAppliedTotal
197
+ bonusAvailableTotal
198
+ creditAmountNeeded
199
+ creditAppliedTotal
200
+ creditAvailableTotal
201
+ donationTotal
202
+ itemTotal
203
+ freeTrialAppliedTotal
204
+ freeTrialAvailableTotal
205
+ loanReservationTotal
206
+ kivaCardTotal
207
+ kivaCreditAppliedTotal
208
+ kivaCreditAvailableTotal
209
+ kivaCreditRemaining
210
+ kivaCreditToReapply
211
+ redemptionCodeAppliedTotal
212
+ redemptionCodeAvailableTotal
213
+ universalCodeAppliedTotal
214
+ universalCodeAvailableTotal
215
+ }
216
+ }
217
+ }
218
+ }`;
219
+ function watchBasketTotals(apollo) {
220
+ return watchShopQuery(apollo, {
221
+ query: basketTotalsQuery
222
+ });
223
+ }
224
+ // Annotate the CommonJS export names for ESM import in node:
225
+ 0 && (module.exports = {
226
+ basketTotalsQuery,
227
+ watchBasketTotals
228
+ });
@@ -0,0 +1,37 @@
1
+ import * as _apollo_client_core from '@apollo/client/core';
2
+ import { ApolloClient } from '@apollo/client/core';
3
+ import * as graphql_language_ast from 'graphql/language/ast';
4
+
5
+ declare const basketTotalsQuery: graphql_language_ast.DocumentNode;
6
+ interface BasketTotalsData {
7
+ shop: {
8
+ id: string;
9
+ basket: {
10
+ id: string;
11
+ totals: {
12
+ bonusAppliedTotal: string;
13
+ bonusAvailableTotal: string;
14
+ creditAmountNeeded: string;
15
+ creditAppliedTotal: string;
16
+ creditAvailableTotal: string;
17
+ donationTotal: string;
18
+ itemTotal: string;
19
+ freeTrialAppliedTotal: string;
20
+ freeTrialAvailableTotal: string;
21
+ loanReservationTotal: string;
22
+ kivaCardTotal: string;
23
+ kivaCreditAppliedTotal: string;
24
+ kivaCreditAvailableTotal: string;
25
+ kivaCreditRemaining: string;
26
+ kivaCreditToReapply: string;
27
+ redemptionCodeAppliedTotal: string;
28
+ redemptionCodeAvailableTotal: string;
29
+ universalCodeAppliedTotal: string;
30
+ universalCodeAvailableTotal: string;
31
+ } | null;
32
+ } | null;
33
+ } | null;
34
+ }
35
+ declare function watchBasketTotals(apollo: ApolloClient<any>): _apollo_client_core.ObservableQuery<BasketTotalsData, any>;
36
+
37
+ export { BasketTotalsData, basketTotalsQuery, watchBasketTotals };
@@ -0,0 +1,12 @@
1
+ import {
2
+ basketTotalsQuery,
3
+ watchBasketTotals
4
+ } from "./chunk-DZGZX4F6.js";
5
+ import "./chunk-CBJJUUVR.js";
6
+ import "./chunk-2NC7LGGO.js";
7
+ import "./chunk-4ODZGLWK.js";
8
+ import "./chunk-LZ4UMRCV.js";
9
+ export {
10
+ basketTotalsQuery,
11
+ watchBasketTotals
12
+ };
@@ -0,0 +1,41 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // src/basketVerification.ts
20
+ var basketVerification_exports = {};
21
+ __export(basketVerification_exports, {
22
+ VerificationState: () => VerificationState,
23
+ isBasketVerified: () => isBasketVerified
24
+ });
25
+ module.exports = __toCommonJS(basketVerification_exports);
26
+ var VerificationState = /* @__PURE__ */ ((VerificationState2) => {
27
+ VerificationState2["VERIFIED"] = "verified";
28
+ VerificationState2["PENDING"] = "pending";
29
+ VerificationState2["REQUIRED"] = "required";
30
+ VerificationState2["MAY_BE_NEEDED"] = "may_be_needed";
31
+ VerificationState2["NOT_NEEDED"] = "not_needed";
32
+ return VerificationState2;
33
+ })(VerificationState || {});
34
+ function isBasketVerified(state) {
35
+ return state === "verified" /* VERIFIED */ || state === "not_needed" /* NOT_NEEDED */;
36
+ }
37
+ // Annotate the CommonJS export names for ESM import in node:
38
+ 0 && (module.exports = {
39
+ VerificationState,
40
+ isBasketVerified
41
+ });
@@ -0,0 +1,10 @@
1
+ declare enum VerificationState {
2
+ VERIFIED = "verified",
3
+ PENDING = "pending",
4
+ REQUIRED = "required",
5
+ MAY_BE_NEEDED = "may_be_needed",
6
+ NOT_NEEDED = "not_needed"
7
+ }
8
+ declare function isBasketVerified(state: VerificationState): boolean;
9
+
10
+ export { VerificationState, isBasketVerified };
@@ -0,0 +1,8 @@
1
+ import {
2
+ VerificationState,
3
+ isBasketVerified
4
+ } from "./chunk-FCAOCO7O.js";
5
+ export {
6
+ VerificationState,
7
+ isBasketVerified
8
+ };
@@ -0,0 +1,110 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // src/checkoutStatus.ts
20
+ var checkoutStatus_exports = {};
21
+ __export(checkoutStatus_exports, {
22
+ getCheckoutStatus: () => getCheckoutStatus,
23
+ pollForFinishedCheckout: () => pollForFinishedCheckout
24
+ });
25
+ module.exports = __toCommonJS(checkoutStatus_exports);
26
+ var import_core = require("@apollo/client/core");
27
+
28
+ // src/util/poll.ts
29
+ function wait(ms) {
30
+ return new Promise((resolve) => setTimeout(resolve, ms));
31
+ }
32
+ async function poll(fn, fnCondition, interval, timeout) {
33
+ const endTime = Date.now() + timeout;
34
+ let result = await fn();
35
+ while (!fnCondition(result)) {
36
+ if (Date.now() > endTime) {
37
+ throw new Error("Polling timed out");
38
+ }
39
+ await wait(interval);
40
+ result = await fn();
41
+ }
42
+ return result;
43
+ }
44
+
45
+ // src/util/cookie.ts
46
+ var getCookieValue = (name) => {
47
+ if (typeof document !== void 0) {
48
+ return decodeURIComponent(document.cookie.match(`(^|;)\\s*${name}\\s*=\\s*([^;]+)`)?.pop() || "");
49
+ }
50
+ };
51
+
52
+ // src/util/visitorId.ts
53
+ function getVisitorID() {
54
+ return getCookieValue("uiv");
55
+ }
56
+
57
+ // src/checkoutStatus.ts
58
+ async function getCheckoutStatus({ apollo, transactionSagaId }) {
59
+ return apollo.query({
60
+ query: import_core.gql`
61
+ query checkoutStatus($transactionId: String!, $visitorId: String) {
62
+ checkoutStatus(transactionId: $transactionId, visitorId: $visitorId) {
63
+ basketId
64
+ errorCode
65
+ errorMessage
66
+ receipt {
67
+ checkoutId
68
+ }
69
+ requestedAt
70
+ status
71
+ transactionId
72
+ updatedAt
73
+ }
74
+ }
75
+ `,
76
+ variables: {
77
+ transactionId: transactionSagaId,
78
+ visitorId: getVisitorID()
79
+ }
80
+ });
81
+ }
82
+ async function pollForFinishedCheckout({
83
+ apollo,
84
+ transactionSagaId,
85
+ interval = 1e3,
86
+ timeout = 6e4
87
+ }) {
88
+ return poll(
89
+ // function to poll
90
+ () => getCheckoutStatus({
91
+ apollo,
92
+ transactionSagaId
93
+ }),
94
+ // function to check for completed status
95
+ (result) => {
96
+ const { status, errorCode, errorMessage } = result?.data?.checkoutStatus;
97
+ if (status === "COMPLETED" || errorCode || errorMessage) {
98
+ return true;
99
+ }
100
+ return false;
101
+ },
102
+ interval,
103
+ timeout
104
+ );
105
+ }
106
+ // Annotate the CommonJS export names for ESM import in node:
107
+ 0 && (module.exports = {
108
+ getCheckoutStatus,
109
+ pollForFinishedCheckout
110
+ });
@@ -0,0 +1,20 @@
1
+ import { ApolloClient } from '@apollo/client/core';
2
+
3
+ interface GetCheckoutStatusOptions {
4
+ apollo: ApolloClient<any>;
5
+ transactionSagaId: string;
6
+ }
7
+ interface CheckoutStatusQueryResult {
8
+ data: {
9
+ checkoutStatus: any;
10
+ } | null;
11
+ errors?: any;
12
+ }
13
+ declare function getCheckoutStatus({ apollo, transactionSagaId }: GetCheckoutStatusOptions): Promise<CheckoutStatusQueryResult>;
14
+ interface PollForCheckoutStatusOptions extends GetCheckoutStatusOptions {
15
+ interval?: number;
16
+ timeout?: number;
17
+ }
18
+ declare function pollForFinishedCheckout({ apollo, transactionSagaId, interval, timeout, }: PollForCheckoutStatusOptions): Promise<CheckoutStatusQueryResult>;
19
+
20
+ export { CheckoutStatusQueryResult, GetCheckoutStatusOptions, PollForCheckoutStatusOptions, getCheckoutStatus, pollForFinishedCheckout };
@@ -0,0 +1,10 @@
1
+ import {
2
+ getCheckoutStatus,
3
+ pollForFinishedCheckout
4
+ } from "./chunk-PC4WUPYU.js";
5
+ import "./chunk-TPJPGUO7.js";
6
+ import "./chunk-LZ4UMRCV.js";
7
+ export {
8
+ getCheckoutStatus,
9
+ pollForFinishedCheckout
10
+ };
@@ -1,24 +1,20 @@
1
1
  import {
2
2
  parseShopError
3
- } from "./chunk-IYCMZ5WV.js";
3
+ } from "./chunk-4ODZGLWK.js";
4
+ import {
5
+ getCookieValue,
6
+ setCookieValue
7
+ } from "./chunk-LZ4UMRCV.js";
4
8
 
5
9
  // src/basket.ts
6
10
  import { gql } from "@apollo/client/core";
7
- var getCookieValue = (name) => {
8
- if (typeof document !== void 0) {
9
- return decodeURIComponent(document.cookie.match(`(^|;)\\s*${name}\\s*=\\s*([^;]+)`)?.pop() || "");
10
- }
11
- };
12
- var setCookieValue = (name, value, options = "") => {
13
- document.cookie = `${name}=${encodeURIComponent(value)};${options}`;
14
- };
15
11
  function getBasketID() {
16
12
  return getCookieValue("kvbskt");
17
13
  }
18
14
  function setBasketID(basketId) {
19
15
  setCookieValue("kvbskt", basketId, "path=/;secure;");
20
16
  }
21
- async function createBasket(apollo) {
17
+ async function createBasketHelper(apollo) {
22
18
  try {
23
19
  return apollo.mutate({
24
20
  mutation: gql`mutation createNewBasketForUser { shop { id createBasket } }`
@@ -32,14 +28,20 @@ async function createBasket(apollo) {
32
28
  throw parseShopError(error);
33
29
  }
34
30
  }
31
+ var activeBasketCreationQuery = null;
32
+ async function createBasket(apollo) {
33
+ if (activeBasketCreationQuery) {
34
+ return activeBasketCreationQuery;
35
+ }
36
+ activeBasketCreationQuery = createBasketHelper(apollo);
37
+ return activeBasketCreationQuery;
38
+ }
35
39
  function hasBasketExpired(error) {
36
40
  const errorCode = error?.code ?? error?.extensions?.code ?? error?.name ?? "";
37
41
  return ["shop.invalidBasketId", "shop.basketRequired", "shop.alreadyCheckedOut"].includes(errorCode);
38
42
  }
39
43
 
40
44
  export {
41
- getCookieValue,
42
- setCookieValue,
43
45
  getBasketID,
44
46
  setBasketID,
45
47
  createBasket,
@@ -9,8 +9,14 @@ var ShopError = class extends Error {
9
9
  this.code = code;
10
10
  this.original = original;
11
11
  }
12
+ aggregateErrors(errors) {
13
+ this.errors = errors;
14
+ }
12
15
  };
13
16
  function parseShopError(error) {
17
+ if (error instanceof ShopError) {
18
+ return error;
19
+ }
14
20
  const errorCode = error?.code ?? error?.extensions?.code ?? error?.name ?? "";
15
21
  const errorMessage = typeof error === "string" ? error : error?.message ?? "";
16
22
  if (errorCode === "invalidMethodParameter" && errorMessage.includes("paymentMethod.create")) {
@@ -19,6 +25,21 @@ function parseShopError(error) {
19
25
  original: error
20
26
  }, "There was a problem validating your payment information. Please double-check the details and try again.");
21
27
  }
28
+ if (errorMessage.includes("Invalid request: ")) {
29
+ const finalError = errorMessage.split("Invalid request: ")[1].split("., ").map((e) => e.matchAll(/[A-Z_]+: (.*)/g))[0];
30
+ const finalCode = finalError[1];
31
+ const finalMessage = finalError[2];
32
+ return new ShopError({
33
+ code: `paymentMethod.${finalCode}`,
34
+ original: error
35
+ }, finalMessage);
36
+ }
37
+ if (errorCode === "insufficientFunds" || errorMessage.includes("There is not enough credit")) {
38
+ return new ShopError({
39
+ code: "shop.insufficientFunds",
40
+ original: error
41
+ }, "There is not enough money to complete the checkout. Please double-check the details and try again.");
42
+ }
22
43
  if (errorCode === "shop.invalidBasketId" || errorCode === "shop.basketRequired" || errorCode === "shop.alreadyCheckedOut") {
23
44
  return new ShopError({
24
45
  code: errorCode,
@@ -0,0 +1,33 @@
1
+ import {
2
+ callShopMutation
3
+ } from "./chunk-CBJJUUVR.js";
4
+
5
+ // src/basketItems.ts
6
+ import { gql } from "@apollo/client/core";
7
+ import numeral from "numeral";
8
+ async function setTipDonation({ amount, apollo }) {
9
+ const donationAmount = numeral(amount).format("0.00");
10
+ const data = await callShopMutation(apollo, {
11
+ awaitRefetchQueries: true,
12
+ mutation: gql`mutation setTipDonation($price: Money!, $basketId: String) {
13
+ shop (basketId: $basketId) {
14
+ id
15
+ updateDonation (donation: {
16
+ price: $price,
17
+ isTip: true
18
+ })
19
+ {
20
+ id
21
+ price
22
+ isTip
23
+ }
24
+ }
25
+ }`,
26
+ variables: { price: donationAmount }
27
+ });
28
+ return data?.shop?.updateDonation;
29
+ }
30
+
31
+ export {
32
+ setTipDonation
33
+ };