@kiva/kv-shop 1.2.21 → 1.2.23

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/basket.cjs CHANGED
@@ -22,9 +22,9 @@ __export(basket_exports, {
22
22
  createBasket: () => createBasket,
23
23
  getBasketID: () => getBasketID,
24
24
  getCookieValue: () => getCookieValue,
25
- handleInvalidBasketForDonation: () => handleInvalidBasketForDonation,
26
25
  hasBasketExpired: () => hasBasketExpired,
27
- setBasketID: () => setBasketID
26
+ setBasketID: () => setBasketID,
27
+ setCookieValue: () => setCookieValue
28
28
  });
29
29
  module.exports = __toCommonJS(basket_exports);
30
30
  var import_core = require("@apollo/client/core");
@@ -42,9 +42,8 @@ var ShopError = class extends Error {
42
42
  }
43
43
  };
44
44
  function parseShopError(error) {
45
- const errorCode = error?.code ?? error?.name ?? "";
45
+ const errorCode = error?.code ?? error?.extensions?.code ?? error?.name ?? "";
46
46
  const errorMessage = typeof error === "string" ? error : error?.message ?? "";
47
- const ctxErrorMsg = error?.ctxErrorMsg ?? null;
48
47
  if (errorCode === "invalidMethodParameter" && errorMessage.includes("paymentMethod.create")) {
49
48
  return new ShopError({
50
49
  code: "paymentMethod.create.invalidMethodParameter",
@@ -55,7 +54,7 @@ function parseShopError(error) {
55
54
  return new ShopError({
56
55
  code: errorCode,
57
56
  original: error
58
- }, ctxErrorMsg ?? "Something went wrong. Please, refresh the page and try again.");
57
+ }, "There was a problem with your basket. Please, refresh the page and try again.");
59
58
  }
60
59
  return new ShopError({
61
60
  code: "shop.unknown",
@@ -69,42 +68,39 @@ var getCookieValue = (name) => {
69
68
  return decodeURIComponent(document.cookie.match(`(^|;)\\s*${name}\\s*=\\s*([^;]+)`)?.pop() || "");
70
69
  }
71
70
  };
71
+ var setCookieValue = (name, value, options = "") => {
72
+ document.cookie = `${name}=${encodeURIComponent(value)};${options}`;
73
+ };
72
74
  function getBasketID() {
73
75
  return getCookieValue("kvbskt");
74
76
  }
75
- function setBasketID() {
77
+ function setBasketID(basketId) {
78
+ setCookieValue("kvbskt", basketId, "path=/;secure;");
76
79
  }
77
80
  async function createBasket(apollo) {
78
81
  try {
79
- apollo.mutate({
82
+ return apollo.mutate({
80
83
  mutation: import_core.gql`mutation createNewBasketForUser { shop { id createBasket } }`
81
84
  }).then(({ data }) => {
82
85
  const newBasketId = data.shop?.createBasket ?? null;
83
86
  if (newBasketId) {
84
- document.cookie = `kvbskt=${newBasketId}; path=/; secure=true;`;
87
+ setBasketID(newBasketId);
85
88
  }
86
89
  });
87
90
  } catch (error) {
88
91
  throw parseShopError(error);
89
92
  }
90
93
  }
91
- function hasBasketExpired(errorCode) {
94
+ function hasBasketExpired(error) {
95
+ const errorCode = error?.code ?? error?.extensions?.code ?? error?.name ?? "";
92
96
  return ["shop.invalidBasketId", "shop.basketRequired"].includes(errorCode);
93
97
  }
94
- async function handleInvalidBasketForDonation({ donationAmount, navigateToCheckout = false, apollo }) {
95
- if (typeof window !== "undefined" && typeof document !== "undefined") {
96
- document.cookie = `kvbskt=; expires=${(/* @__PURE__ */ new Date(0)).toUTCString()}; path=/;`;
97
- await createBasket(apollo);
98
- document.cookie = `kvatbamt=${JSON.stringify({ donationAmount, navigateToCheckout })}; path=/;`;
99
- window.location.reload();
100
- }
101
- }
102
98
  // Annotate the CommonJS export names for ESM import in node:
103
99
  0 && (module.exports = {
104
100
  createBasket,
105
101
  getBasketID,
106
102
  getCookieValue,
107
- handleInvalidBasketForDonation,
108
103
  hasBasketExpired,
109
- setBasketID
104
+ setBasketID,
105
+ setCookieValue
110
106
  });
package/dist/basket.d.ts CHANGED
@@ -1,12 +1,8 @@
1
1
  declare const getCookieValue: (name: string) => string;
2
+ declare const setCookieValue: (name: string, value: string, options?: string) => void;
2
3
  declare function getBasketID(): string;
3
- declare function setBasketID(): void;
4
- declare function createBasket(apollo: any): Promise<void>;
5
- declare function hasBasketExpired(errorCode: any): boolean;
6
- declare function handleInvalidBasketForDonation({ donationAmount, navigateToCheckout, apollo }: {
7
- donationAmount: any;
8
- navigateToCheckout?: boolean;
9
- apollo: any;
10
- }): Promise<void>;
4
+ declare function setBasketID(basketId: any): void;
5
+ declare function createBasket(apollo: any): Promise<any>;
6
+ declare function hasBasketExpired(error: any): boolean;
11
7
 
12
- export { createBasket, getBasketID, getCookieValue, handleInvalidBasketForDonation, hasBasketExpired, setBasketID };
8
+ export { createBasket, getBasketID, getCookieValue, hasBasketExpired, setBasketID, setCookieValue };
package/dist/basket.js CHANGED
@@ -2,16 +2,16 @@ import {
2
2
  createBasket,
3
3
  getBasketID,
4
4
  getCookieValue,
5
- handleInvalidBasketForDonation,
6
5
  hasBasketExpired,
7
- setBasketID
8
- } from "./chunk-V7MS7POQ.js";
9
- import "./chunk-BHFCSJBE.js";
6
+ setBasketID,
7
+ setCookieValue
8
+ } from "./chunk-RZZTEPBR.js";
9
+ import "./chunk-WXX4N673.js";
10
10
  export {
11
11
  createBasket,
12
12
  getBasketID,
13
13
  getCookieValue,
14
- handleInvalidBasketForDonation,
15
14
  hasBasketExpired,
16
- setBasketID
15
+ setBasketID,
16
+ setCookieValue
17
17
  };
@@ -51,9 +51,8 @@ var ShopError = class extends Error {
51
51
  }
52
52
  };
53
53
  function parseShopError(error) {
54
- const errorCode = error?.code ?? error?.name ?? "";
54
+ const errorCode = error?.code ?? error?.extensions?.code ?? error?.name ?? "";
55
55
  const errorMessage = typeof error === "string" ? error : error?.message ?? "";
56
- const ctxErrorMsg = error?.ctxErrorMsg ?? null;
57
56
  if (errorCode === "invalidMethodParameter" && errorMessage.includes("paymentMethod.create")) {
58
57
  return new ShopError({
59
58
  code: "paymentMethod.create.invalidMethodParameter",
@@ -64,7 +63,7 @@ function parseShopError(error) {
64
63
  return new ShopError({
65
64
  code: errorCode,
66
65
  original: error
67
- }, ctxErrorMsg ?? "Something went wrong. Please, refresh the page and try again.");
66
+ }, "There was a problem with your basket. Please, refresh the page and try again.");
68
67
  }
69
68
  return new ShopError({
70
69
  code: "shop.unknown",
@@ -78,90 +77,82 @@ var getCookieValue = (name) => {
78
77
  return decodeURIComponent(document.cookie.match(`(^|;)\\s*${name}\\s*=\\s*([^;]+)`)?.pop() || "");
79
78
  }
80
79
  };
80
+ var setCookieValue = (name, value, options = "") => {
81
+ document.cookie = `${name}=${encodeURIComponent(value)};${options}`;
82
+ };
81
83
  function getBasketID() {
82
84
  return getCookieValue("kvbskt");
83
85
  }
86
+ function setBasketID(basketId) {
87
+ setCookieValue("kvbskt", basketId, "path=/;secure;");
88
+ }
84
89
  async function createBasket(apollo) {
85
90
  try {
86
- apollo.mutate({
91
+ return apollo.mutate({
87
92
  mutation: import_core.gql`mutation createNewBasketForUser { shop { id createBasket } }`
88
93
  }).then(({ data }) => {
89
94
  const newBasketId = data.shop?.createBasket ?? null;
90
95
  if (newBasketId) {
91
- document.cookie = `kvbskt=${newBasketId}; path=/; secure=true;`;
96
+ setBasketID(newBasketId);
92
97
  }
93
98
  });
94
99
  } catch (error) {
95
100
  throw parseShopError(error);
96
101
  }
97
102
  }
98
- function hasBasketExpired(errorCode) {
103
+ function hasBasketExpired(error) {
104
+ const errorCode = error?.code ?? error?.extensions?.code ?? error?.name ?? "";
99
105
  return ["shop.invalidBasketId", "shop.basketRequired"].includes(errorCode);
100
106
  }
101
- async function handleInvalidBasketForDonation({ donationAmount, navigateToCheckout = false, apollo }) {
102
- if (typeof window !== "undefined" && typeof document !== "undefined") {
103
- document.cookie = `kvbskt=; expires=${(/* @__PURE__ */ new Date(0)).toUTCString()}; path=/;`;
104
- await createBasket(apollo);
105
- document.cookie = `kvatbamt=${JSON.stringify({ donationAmount, navigateToCheckout })}; path=/;`;
106
- window.location.reload();
107
- }
108
- }
109
107
 
110
108
  // src/basketItems.ts
111
- async function setTipDonation({ amount, apollo }) {
112
- let data;
113
- let error;
114
- let hasFailedAddToBasket = false;
115
- const donationAmount = (0, import_numeral.default)(amount).format("0.00");
109
+ async function callShopMutation(apollo, mutation, variables, maxretries = 2) {
116
110
  try {
117
111
  const result = await apollo.mutate({
118
- mutation: import_core2.gql`mutation setTipDonation($price: Money!, $basketId: String) {
119
- shop (basketId: $basketId) {
120
- id
121
- updateDonation (donation: {
122
- price: $price,
123
- isTip: true
124
- })
125
- {
126
- id
127
- price
128
- isTip
129
- }
130
- }
131
- }`,
112
+ mutation,
132
113
  variables: {
133
- price: donationAmount,
114
+ ...variables,
134
115
  basketId: getBasketID()
135
116
  }
136
117
  });
137
118
  if (result?.errors?.length) {
138
- error = result?.errors?.[0];
139
- (result?.errors ?? []).forEach((err) => {
140
- if (hasBasketExpired(err?.extensions?.code)) {
141
- hasFailedAddToBasket = true;
142
- error = {
143
- ...err,
144
- code: err?.extensions?.code,
145
- ctxErrorMsg: "Something went wrong with your donation, refreshing the page to try again"
146
- };
119
+ const basketErrors = result?.errors.filter((err) => hasBasketExpired(err));
120
+ if (basketErrors.length) {
121
+ if (maxretries > 0) {
122
+ await createBasket(apollo);
123
+ return callShopMutation(apollo, mutation, variables, maxretries - 1);
147
124
  }
148
- });
149
- if (hasFailedAddToBasket) {
150
- await handleInvalidBasketForDonation({
151
- donationAmount,
152
- navigateToCheckout: true,
153
- apollo
154
- });
125
+ throw basketErrors[0];
126
+ }
127
+ const otherErrors = result?.errors?.filter((err) => !hasBasketExpired(err));
128
+ if (otherErrors.length) {
129
+ throw otherErrors[0];
155
130
  }
156
- } else {
157
- data = result?.data;
158
131
  }
132
+ return result?.data;
159
133
  } catch (e) {
160
- error = e;
161
- }
162
- if (error) {
163
- throw parseShopError(error);
134
+ if (e instanceof ShopError) {
135
+ throw e;
136
+ }
137
+ throw parseShopError(e);
164
138
  }
139
+ }
140
+ async function setTipDonation({ amount, apollo }) {
141
+ const donationAmount = (0, import_numeral.default)(amount).format("0.00");
142
+ const data = await callShopMutation(apollo, import_core2.gql`mutation setTipDonation($price: Money!, $basketId: String) {
143
+ shop (basketId: $basketId) {
144
+ id
145
+ updateDonation (donation: {
146
+ price: $price,
147
+ isTip: true
148
+ })
149
+ {
150
+ id
151
+ price
152
+ isTip
153
+ }
154
+ }
155
+ }`, { price: donationAmount });
165
156
  return data?.shop?.updateDonation;
166
157
  }
167
158
  // Annotate the CommonJS export names for ESM import in node:
@@ -1,8 +1,8 @@
1
- import { ApolloClient, NormalizedCacheObject } from '@apollo/client/core';
1
+ import { ApolloClient } from '@apollo/client/core';
2
2
 
3
3
  interface SetTipDonationOptions {
4
4
  amount: string | number;
5
- apollo: ApolloClient<NormalizedCacheObject>;
5
+ apollo: ApolloClient<any>;
6
6
  }
7
7
  declare function setTipDonation({ amount, apollo }: SetTipDonationOptions): Promise<any>;
8
8
 
@@ -1,8 +1,8 @@
1
1
  import {
2
2
  setTipDonation
3
- } from "./chunk-7KE2LSWP.js";
4
- import "./chunk-V7MS7POQ.js";
5
- import "./chunk-BHFCSJBE.js";
3
+ } from "./chunk-6TIZWV2J.js";
4
+ import "./chunk-RZZTEPBR.js";
5
+ import "./chunk-WXX4N673.js";
6
6
  export {
7
7
  setTipDonation
8
8
  };
@@ -0,0 +1,66 @@
1
+ import {
2
+ createBasket,
3
+ getBasketID,
4
+ hasBasketExpired
5
+ } from "./chunk-RZZTEPBR.js";
6
+ import {
7
+ ShopError,
8
+ parseShopError
9
+ } from "./chunk-WXX4N673.js";
10
+
11
+ // src/basketItems.ts
12
+ import { gql } from "@apollo/client/core";
13
+ import numeral from "numeral";
14
+ async function callShopMutation(apollo, mutation, variables, maxretries = 2) {
15
+ try {
16
+ const result = await apollo.mutate({
17
+ mutation,
18
+ variables: {
19
+ ...variables,
20
+ basketId: getBasketID()
21
+ }
22
+ });
23
+ if (result?.errors?.length) {
24
+ const basketErrors = result?.errors.filter((err) => hasBasketExpired(err));
25
+ if (basketErrors.length) {
26
+ if (maxretries > 0) {
27
+ await createBasket(apollo);
28
+ return callShopMutation(apollo, mutation, variables, maxretries - 1);
29
+ }
30
+ throw basketErrors[0];
31
+ }
32
+ const otherErrors = result?.errors?.filter((err) => !hasBasketExpired(err));
33
+ if (otherErrors.length) {
34
+ throw otherErrors[0];
35
+ }
36
+ }
37
+ return result?.data;
38
+ } catch (e) {
39
+ if (e instanceof ShopError) {
40
+ throw e;
41
+ }
42
+ throw parseShopError(e);
43
+ }
44
+ }
45
+ async function setTipDonation({ amount, apollo }) {
46
+ const donationAmount = numeral(amount).format("0.00");
47
+ const data = await callShopMutation(apollo, gql`mutation setTipDonation($price: Money!, $basketId: String) {
48
+ shop (basketId: $basketId) {
49
+ id
50
+ updateDonation (donation: {
51
+ price: $price,
52
+ isTip: true
53
+ })
54
+ {
55
+ id
56
+ price
57
+ isTip
58
+ }
59
+ }
60
+ }`, { price: donationAmount });
61
+ return data?.shop?.updateDonation;
62
+ }
63
+
64
+ export {
65
+ setTipDonation
66
+ };
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  ShopError,
3
3
  parseShopError
4
- } from "./chunk-BHFCSJBE.js";
4
+ } from "./chunk-WXX4N673.js";
5
5
 
6
6
  // src/subscriptionCheckout.ts
7
7
  import { gql } from "@apollo/client/core";
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  parseShopError
3
- } from "./chunk-BHFCSJBE.js";
3
+ } from "./chunk-WXX4N673.js";
4
4
 
5
5
  // src/basket.ts
6
6
  import { gql } from "@apollo/client/core";
@@ -9,42 +9,39 @@ var getCookieValue = (name) => {
9
9
  return decodeURIComponent(document.cookie.match(`(^|;)\\s*${name}\\s*=\\s*([^;]+)`)?.pop() || "");
10
10
  }
11
11
  };
12
+ var setCookieValue = (name, value, options = "") => {
13
+ document.cookie = `${name}=${encodeURIComponent(value)};${options}`;
14
+ };
12
15
  function getBasketID() {
13
16
  return getCookieValue("kvbskt");
14
17
  }
15
- function setBasketID() {
18
+ function setBasketID(basketId) {
19
+ setCookieValue("kvbskt", basketId, "path=/;secure;");
16
20
  }
17
21
  async function createBasket(apollo) {
18
22
  try {
19
- apollo.mutate({
23
+ return apollo.mutate({
20
24
  mutation: gql`mutation createNewBasketForUser { shop { id createBasket } }`
21
25
  }).then(({ data }) => {
22
26
  const newBasketId = data.shop?.createBasket ?? null;
23
27
  if (newBasketId) {
24
- document.cookie = `kvbskt=${newBasketId}; path=/; secure=true;`;
28
+ setBasketID(newBasketId);
25
29
  }
26
30
  });
27
31
  } catch (error) {
28
32
  throw parseShopError(error);
29
33
  }
30
34
  }
31
- function hasBasketExpired(errorCode) {
35
+ function hasBasketExpired(error) {
36
+ const errorCode = error?.code ?? error?.extensions?.code ?? error?.name ?? "";
32
37
  return ["shop.invalidBasketId", "shop.basketRequired"].includes(errorCode);
33
38
  }
34
- async function handleInvalidBasketForDonation({ donationAmount, navigateToCheckout = false, apollo }) {
35
- if (typeof window !== "undefined" && typeof document !== "undefined") {
36
- document.cookie = `kvbskt=; expires=${(/* @__PURE__ */ new Date(0)).toUTCString()}; path=/;`;
37
- await createBasket(apollo);
38
- document.cookie = `kvatbamt=${JSON.stringify({ donationAmount, navigateToCheckout })}; path=/;`;
39
- window.location.reload();
40
- }
41
- }
42
39
 
43
40
  export {
44
41
  getCookieValue,
42
+ setCookieValue,
45
43
  getBasketID,
46
44
  setBasketID,
47
45
  createBasket,
48
- hasBasketExpired,
49
- handleInvalidBasketForDonation
46
+ hasBasketExpired
50
47
  };
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  ShopError,
3
3
  parseShopError
4
- } from "./chunk-BHFCSJBE.js";
4
+ } from "./chunk-WXX4N673.js";
5
5
 
6
6
  // src/useBraintreeDropIn.ts
7
7
  import { gql } from "@apollo/client/core";
@@ -11,9 +11,8 @@ var ShopError = class extends Error {
11
11
  }
12
12
  };
13
13
  function parseShopError(error) {
14
- const errorCode = error?.code ?? error?.name ?? "";
14
+ const errorCode = error?.code ?? error?.extensions?.code ?? error?.name ?? "";
15
15
  const errorMessage = typeof error === "string" ? error : error?.message ?? "";
16
- const ctxErrorMsg = error?.ctxErrorMsg ?? null;
17
16
  if (errorCode === "invalidMethodParameter" && errorMessage.includes("paymentMethod.create")) {
18
17
  return new ShopError({
19
18
  code: "paymentMethod.create.invalidMethodParameter",
@@ -24,7 +23,7 @@ function parseShopError(error) {
24
23
  return new ShopError({
25
24
  code: errorCode,
26
25
  original: error
27
- }, ctxErrorMsg ?? "Something went wrong. Please, refresh the page and try again.");
26
+ }, "There was a problem with your basket. Please, refresh the page and try again.");
28
27
  }
29
28
  return new ShopError({
30
29
  code: "shop.unknown",
package/dist/index.cjs CHANGED
@@ -38,10 +38,10 @@ __export(src_exports, {
38
38
  getBasketID: () => getBasketID,
39
39
  getClientToken: () => getClientToken,
40
40
  getCookieValue: () => getCookieValue,
41
- handleInvalidBasketForDonation: () => handleInvalidBasketForDonation,
42
41
  hasBasketExpired: () => hasBasketExpired,
43
42
  parseShopError: () => parseShopError,
44
43
  setBasketID: () => setBasketID,
44
+ setCookieValue: () => setCookieValue,
45
45
  setTipDonation: () => setTipDonation,
46
46
  useBraintreeDropIn: () => useBraintreeDropIn,
47
47
  waitOnTransaction: () => waitOnTransaction
@@ -64,9 +64,8 @@ var ShopError = class extends Error {
64
64
  }
65
65
  };
66
66
  function parseShopError(error) {
67
- const errorCode = error?.code ?? error?.name ?? "";
67
+ const errorCode = error?.code ?? error?.extensions?.code ?? error?.name ?? "";
68
68
  const errorMessage = typeof error === "string" ? error : error?.message ?? "";
69
- const ctxErrorMsg = error?.ctxErrorMsg ?? null;
70
69
  if (errorCode === "invalidMethodParameter" && errorMessage.includes("paymentMethod.create")) {
71
70
  return new ShopError({
72
71
  code: "paymentMethod.create.invalidMethodParameter",
@@ -77,7 +76,7 @@ function parseShopError(error) {
77
76
  return new ShopError({
78
77
  code: errorCode,
79
78
  original: error
80
- }, ctxErrorMsg ?? "Something went wrong. Please, refresh the page and try again.");
79
+ }, "There was a problem with your basket. Please, refresh the page and try again.");
81
80
  }
82
81
  return new ShopError({
83
82
  code: "shop.unknown",
@@ -91,94 +90,84 @@ var getCookieValue = (name) => {
91
90
  return decodeURIComponent(document.cookie.match(`(^|;)\\s*${name}\\s*=\\s*([^;]+)`)?.pop() || "");
92
91
  }
93
92
  };
93
+ var setCookieValue = (name, value, options = "") => {
94
+ document.cookie = `${name}=${encodeURIComponent(value)};${options}`;
95
+ };
94
96
  function getBasketID() {
95
97
  return getCookieValue("kvbskt");
96
98
  }
97
- function setBasketID() {
99
+ function setBasketID(basketId) {
100
+ setCookieValue("kvbskt", basketId, "path=/;secure;");
98
101
  }
99
102
  async function createBasket(apollo) {
100
103
  try {
101
- apollo.mutate({
104
+ return apollo.mutate({
102
105
  mutation: import_core.gql`mutation createNewBasketForUser { shop { id createBasket } }`
103
106
  }).then(({ data }) => {
104
107
  const newBasketId = data.shop?.createBasket ?? null;
105
108
  if (newBasketId) {
106
- document.cookie = `kvbskt=${newBasketId}; path=/; secure=true;`;
109
+ setBasketID(newBasketId);
107
110
  }
108
111
  });
109
112
  } catch (error) {
110
113
  throw parseShopError(error);
111
114
  }
112
115
  }
113
- function hasBasketExpired(errorCode) {
116
+ function hasBasketExpired(error) {
117
+ const errorCode = error?.code ?? error?.extensions?.code ?? error?.name ?? "";
114
118
  return ["shop.invalidBasketId", "shop.basketRequired"].includes(errorCode);
115
119
  }
116
- async function handleInvalidBasketForDonation({ donationAmount, navigateToCheckout = false, apollo }) {
117
- if (typeof window !== "undefined" && typeof document !== "undefined") {
118
- document.cookie = `kvbskt=; expires=${(/* @__PURE__ */ new Date(0)).toUTCString()}; path=/;`;
119
- await createBasket(apollo);
120
- document.cookie = `kvatbamt=${JSON.stringify({ donationAmount, navigateToCheckout })}; path=/;`;
121
- window.location.reload();
122
- }
123
- }
124
120
 
125
121
  // src/basketItems.ts
126
122
  var import_core2 = require("@apollo/client/core");
127
123
  var import_numeral = __toESM(require("numeral"), 1);
128
- async function setTipDonation({ amount, apollo }) {
129
- let data;
130
- let error;
131
- let hasFailedAddToBasket = false;
132
- const donationAmount = (0, import_numeral.default)(amount).format("0.00");
124
+ async function callShopMutation(apollo, mutation, variables, maxretries = 2) {
133
125
  try {
134
126
  const result = await apollo.mutate({
135
- mutation: import_core2.gql`mutation setTipDonation($price: Money!, $basketId: String) {
136
- shop (basketId: $basketId) {
137
- id
138
- updateDonation (donation: {
139
- price: $price,
140
- isTip: true
141
- })
142
- {
143
- id
144
- price
145
- isTip
146
- }
147
- }
148
- }`,
127
+ mutation,
149
128
  variables: {
150
- price: donationAmount,
129
+ ...variables,
151
130
  basketId: getBasketID()
152
131
  }
153
132
  });
154
133
  if (result?.errors?.length) {
155
- error = result?.errors?.[0];
156
- (result?.errors ?? []).forEach((err) => {
157
- if (hasBasketExpired(err?.extensions?.code)) {
158
- hasFailedAddToBasket = true;
159
- error = {
160
- ...err,
161
- code: err?.extensions?.code,
162
- ctxErrorMsg: "Something went wrong with your donation, refreshing the page to try again"
163
- };
134
+ const basketErrors = result?.errors.filter((err) => hasBasketExpired(err));
135
+ if (basketErrors.length) {
136
+ if (maxretries > 0) {
137
+ await createBasket(apollo);
138
+ return callShopMutation(apollo, mutation, variables, maxretries - 1);
164
139
  }
165
- });
166
- if (hasFailedAddToBasket) {
167
- await handleInvalidBasketForDonation({
168
- donationAmount,
169
- navigateToCheckout: true,
170
- apollo
171
- });
140
+ throw basketErrors[0];
141
+ }
142
+ const otherErrors = result?.errors?.filter((err) => !hasBasketExpired(err));
143
+ if (otherErrors.length) {
144
+ throw otherErrors[0];
172
145
  }
173
- } else {
174
- data = result?.data;
175
146
  }
147
+ return result?.data;
176
148
  } catch (e) {
177
- error = e;
178
- }
179
- if (error) {
180
- throw parseShopError(error);
149
+ if (e instanceof ShopError) {
150
+ throw e;
151
+ }
152
+ throw parseShopError(e);
181
153
  }
154
+ }
155
+ async function setTipDonation({ amount, apollo }) {
156
+ const donationAmount = (0, import_numeral.default)(amount).format("0.00");
157
+ const data = await callShopMutation(apollo, import_core2.gql`mutation setTipDonation($price: Money!, $basketId: String) {
158
+ shop (basketId: $basketId) {
159
+ id
160
+ updateDonation (donation: {
161
+ price: $price,
162
+ isTip: true
163
+ })
164
+ {
165
+ id
166
+ price
167
+ isTip
168
+ }
169
+ }
170
+ }`, { price: donationAmount });
182
171
  return data?.shop?.updateDonation;
183
172
  }
184
173
 
@@ -461,10 +450,10 @@ function useBraintreeDropIn() {
461
450
  getBasketID,
462
451
  getClientToken,
463
452
  getCookieValue,
464
- handleInvalidBasketForDonation,
465
453
  hasBasketExpired,
466
454
  parseShopError,
467
455
  setBasketID,
456
+ setCookieValue,
468
457
  setTipDonation,
469
458
  useBraintreeDropIn,
470
459
  waitOnTransaction
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export { createBasket, getBasketID, getCookieValue, handleInvalidBasketForDonation, hasBasketExpired, setBasketID } from './basket.js';
1
+ export { createBasket, getBasketID, getCookieValue, hasBasketExpired, setBasketID, setCookieValue } from './basket.js';
2
2
  export { SetTipDonationOptions, setTipDonation } from './basketItems.js';
3
3
  export { OneTimeCheckoutOptions, WaitOnTransactionOptions, executeOneTimeCheckout, waitOnTransaction } from './oneTimeCheckout.js';
4
4
  export { ShopError, ShopErrorOptions, parseShopError } from './shopError.js';
package/dist/index.js CHANGED
@@ -1,14 +1,14 @@
1
1
  import {
2
2
  setTipDonation
3
- } from "./chunk-7KE2LSWP.js";
3
+ } from "./chunk-6TIZWV2J.js";
4
4
  import {
5
5
  createBasket,
6
6
  getBasketID,
7
7
  getCookieValue,
8
- handleInvalidBasketForDonation,
9
8
  hasBasketExpired,
10
- setBasketID
11
- } from "./chunk-V7MS7POQ.js";
9
+ setBasketID,
10
+ setCookieValue
11
+ } from "./chunk-RZZTEPBR.js";
12
12
  import {
13
13
  executeOneTimeCheckout,
14
14
  waitOnTransaction
@@ -16,16 +16,16 @@ import {
16
16
  import {
17
17
  checkSubscriptionStatus,
18
18
  executeNewSubscriptionCheckout
19
- } from "./chunk-23ED7LBZ.js";
19
+ } from "./chunk-O63WXJQ5.js";
20
20
  import {
21
21
  defaultPaymentTypes,
22
22
  getClientToken,
23
23
  useBraintreeDropIn
24
- } from "./chunk-VMLGWF7L.js";
24
+ } from "./chunk-VHRGXVDB.js";
25
25
  import {
26
26
  ShopError,
27
27
  parseShopError
28
- } from "./chunk-BHFCSJBE.js";
28
+ } from "./chunk-WXX4N673.js";
29
29
  export {
30
30
  ShopError,
31
31
  checkSubscriptionStatus,
@@ -36,10 +36,10 @@ export {
36
36
  getBasketID,
37
37
  getClientToken,
38
38
  getCookieValue,
39
- handleInvalidBasketForDonation,
40
39
  hasBasketExpired,
41
40
  parseShopError,
42
41
  setBasketID,
42
+ setCookieValue,
43
43
  setTipDonation,
44
44
  useBraintreeDropIn,
45
45
  waitOnTransaction
@@ -35,9 +35,8 @@ var ShopError = class extends Error {
35
35
  }
36
36
  };
37
37
  function parseShopError(error) {
38
- const errorCode = error?.code ?? error?.name ?? "";
38
+ const errorCode = error?.code ?? error?.extensions?.code ?? error?.name ?? "";
39
39
  const errorMessage = typeof error === "string" ? error : error?.message ?? "";
40
- const ctxErrorMsg = error?.ctxErrorMsg ?? null;
41
40
  if (errorCode === "invalidMethodParameter" && errorMessage.includes("paymentMethod.create")) {
42
41
  return new ShopError({
43
42
  code: "paymentMethod.create.invalidMethodParameter",
@@ -48,7 +47,7 @@ function parseShopError(error) {
48
47
  return new ShopError({
49
48
  code: errorCode,
50
49
  original: error
51
- }, ctxErrorMsg ?? "Something went wrong. Please, refresh the page and try again.");
50
+ }, "There was a problem with your basket. Please, refresh the page and try again.");
52
51
  }
53
52
  return new ShopError({
54
53
  code: "shop.unknown",
package/dist/shopError.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  ShopError,
3
3
  parseShopError
4
- } from "./chunk-BHFCSJBE.js";
4
+ } from "./chunk-WXX4N673.js";
5
5
  export {
6
6
  ShopError,
7
7
  parseShopError
@@ -38,9 +38,8 @@ var ShopError = class extends Error {
38
38
  }
39
39
  };
40
40
  function parseShopError(error) {
41
- const errorCode = error?.code ?? error?.name ?? "";
41
+ const errorCode = error?.code ?? error?.extensions?.code ?? error?.name ?? "";
42
42
  const errorMessage = typeof error === "string" ? error : error?.message ?? "";
43
- const ctxErrorMsg = error?.ctxErrorMsg ?? null;
44
43
  if (errorCode === "invalidMethodParameter" && errorMessage.includes("paymentMethod.create")) {
45
44
  return new ShopError({
46
45
  code: "paymentMethod.create.invalidMethodParameter",
@@ -51,7 +50,7 @@ function parseShopError(error) {
51
50
  return new ShopError({
52
51
  code: errorCode,
53
52
  original: error
54
- }, ctxErrorMsg ?? "Something went wrong. Please, refresh the page and try again.");
53
+ }, "There was a problem with your basket. Please, refresh the page and try again.");
55
54
  }
56
55
  return new ShopError({
57
56
  code: "shop.unknown",
@@ -1,8 +1,8 @@
1
1
  import {
2
2
  checkSubscriptionStatus,
3
3
  executeNewSubscriptionCheckout
4
- } from "./chunk-23ED7LBZ.js";
5
- import "./chunk-BHFCSJBE.js";
4
+ } from "./chunk-O63WXJQ5.js";
5
+ import "./chunk-WXX4N673.js";
6
6
  export {
7
7
  checkSubscriptionStatus,
8
8
  executeNewSubscriptionCheckout
@@ -51,9 +51,8 @@ var ShopError = class extends Error {
51
51
  }
52
52
  };
53
53
  function parseShopError(error) {
54
- const errorCode = error?.code ?? error?.name ?? "";
54
+ const errorCode = error?.code ?? error?.extensions?.code ?? error?.name ?? "";
55
55
  const errorMessage = typeof error === "string" ? error : error?.message ?? "";
56
- const ctxErrorMsg = error?.ctxErrorMsg ?? null;
57
56
  if (errorCode === "invalidMethodParameter" && errorMessage.includes("paymentMethod.create")) {
58
57
  return new ShopError({
59
58
  code: "paymentMethod.create.invalidMethodParameter",
@@ -64,7 +63,7 @@ function parseShopError(error) {
64
63
  return new ShopError({
65
64
  code: errorCode,
66
65
  original: error
67
- }, ctxErrorMsg ?? "Something went wrong. Please, refresh the page and try again.");
66
+ }, "There was a problem with your basket. Please, refresh the page and try again.");
68
67
  }
69
68
  return new ShopError({
70
69
  code: "shop.unknown",
@@ -2,8 +2,8 @@ import {
2
2
  defaultPaymentTypes,
3
3
  getClientToken,
4
4
  useBraintreeDropIn
5
- } from "./chunk-VMLGWF7L.js";
6
- import "./chunk-BHFCSJBE.js";
5
+ } from "./chunk-VHRGXVDB.js";
6
+ import "./chunk-WXX4N673.js";
7
7
  export {
8
8
  useBraintreeDropIn as default,
9
9
  defaultPaymentTypes,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kiva/kv-shop",
3
- "version": "1.2.21",
3
+ "version": "1.2.23",
4
4
  "type": "module",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -38,7 +38,7 @@
38
38
  },
39
39
  "dependencies": {
40
40
  "@apollo/client": "^3.7.14",
41
- "@kiva/kv-components": "^3.40.0",
41
+ "@kiva/kv-components": "^3.40.1",
42
42
  "@types/braintree-web-drop-in": "^1.34.2",
43
43
  "braintree-web-drop-in": "^1.37.0",
44
44
  "numeral": "^2.0.6",
@@ -53,5 +53,5 @@
53
53
  "optional": true
54
54
  }
55
55
  },
56
- "gitHead": "8b34af3a17a7eea46565344414767c930f948bdd"
56
+ "gitHead": "4ce025324410f92a58a6e235886145d501290f79"
57
57
  }
@@ -1,72 +0,0 @@
1
- import {
2
- getBasketID,
3
- handleInvalidBasketForDonation,
4
- hasBasketExpired
5
- } from "./chunk-V7MS7POQ.js";
6
- import {
7
- parseShopError
8
- } from "./chunk-BHFCSJBE.js";
9
-
10
- // src/basketItems.ts
11
- import { gql } from "@apollo/client/core";
12
- import numeral from "numeral";
13
- async function setTipDonation({ amount, apollo }) {
14
- let data;
15
- let error;
16
- let hasFailedAddToBasket = false;
17
- const donationAmount = numeral(amount).format("0.00");
18
- try {
19
- const result = await apollo.mutate({
20
- mutation: gql`mutation setTipDonation($price: Money!, $basketId: String) {
21
- shop (basketId: $basketId) {
22
- id
23
- updateDonation (donation: {
24
- price: $price,
25
- isTip: true
26
- })
27
- {
28
- id
29
- price
30
- isTip
31
- }
32
- }
33
- }`,
34
- variables: {
35
- price: donationAmount,
36
- basketId: getBasketID()
37
- }
38
- });
39
- if (result?.errors?.length) {
40
- error = result?.errors?.[0];
41
- (result?.errors ?? []).forEach((err) => {
42
- if (hasBasketExpired(err?.extensions?.code)) {
43
- hasFailedAddToBasket = true;
44
- error = {
45
- ...err,
46
- code: err?.extensions?.code,
47
- ctxErrorMsg: "Something went wrong with your donation, refreshing the page to try again"
48
- };
49
- }
50
- });
51
- if (hasFailedAddToBasket) {
52
- await handleInvalidBasketForDonation({
53
- donationAmount,
54
- navigateToCheckout: true,
55
- apollo
56
- });
57
- }
58
- } else {
59
- data = result?.data;
60
- }
61
- } catch (e) {
62
- error = e;
63
- }
64
- if (error) {
65
- throw parseShopError(error);
66
- }
67
- return data?.shop?.updateDonation;
68
- }
69
-
70
- export {
71
- setTipDonation
72
- };