@kiva/kv-shop 1.1.11 → 1.2.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.
package/dist/basket.cjs CHANGED
@@ -19,11 +19,51 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
19
19
  // src/basket.ts
20
20
  var basket_exports = {};
21
21
  __export(basket_exports, {
22
+ createBasket: () => createBasket,
22
23
  getBasketID: () => getBasketID,
23
24
  getCookieValue: () => getCookieValue,
25
+ handleInvalidBasketForDonation: () => handleInvalidBasketForDonation,
26
+ hasBasketExpired: () => hasBasketExpired,
24
27
  setBasketID: () => setBasketID
25
28
  });
26
29
  module.exports = __toCommonJS(basket_exports);
30
+ var import_core = require("@apollo/client/core");
31
+
32
+ // src/shopError.ts
33
+ var ShopError = class extends Error {
34
+ constructor({ code, original }, ...params) {
35
+ super(...params);
36
+ if (Error.captureStackTrace) {
37
+ Error.captureStackTrace(this, ShopError);
38
+ }
39
+ this.name = "ShopError";
40
+ this.code = code;
41
+ this.original = original;
42
+ }
43
+ };
44
+ function parseShopError(error) {
45
+ const errorCode = error?.code ?? error?.name ?? "";
46
+ const errorMessage = typeof error === "string" ? error : error?.message ?? "";
47
+ const ctxErrorMsg = error?.ctxErrorMsg ?? null;
48
+ if (errorCode === "invalidMethodParameter" && errorMessage.includes("paymentMethod.create")) {
49
+ return new ShopError({
50
+ code: "paymentMethod.create.invalidMethodParameter",
51
+ original: error
52
+ }, "There was a problem validating your payment information. Please double-check the details and try again.");
53
+ }
54
+ if (errorCode === "shop.invalidBasketId" || errorCode === "shop.basketRequired") {
55
+ return new ShopError({
56
+ code: errorCode,
57
+ original: error
58
+ }, ctxErrorMsg ?? "Something went wrong. Please, refresh the page and try again.");
59
+ }
60
+ return new ShopError({
61
+ code: "shop.unknown",
62
+ original: error
63
+ }, "An unknown error occurred.");
64
+ }
65
+
66
+ // src/basket.ts
27
67
  var getCookieValue = (name) => {
28
68
  if (typeof document !== void 0) {
29
69
  return decodeURIComponent(document.cookie.match(`(^|;)\\s*${name}\\s*=\\s*([^;]+)`)?.pop() || "");
@@ -32,11 +72,39 @@ var getCookieValue = (name) => {
32
72
  function getBasketID() {
33
73
  return getCookieValue("kvbskt");
34
74
  }
35
- function setBasketID(basketId) {
75
+ function setBasketID() {
76
+ }
77
+ async function createBasket(apollo) {
78
+ try {
79
+ apollo.mutate({
80
+ mutation: import_core.gql`mutation createNewBasketForUser { shop { id createBasket } }`
81
+ }).then(({ data }) => {
82
+ const newBasketId = data.shop?.createBasket ?? null;
83
+ if (newBasketId) {
84
+ document.cookie = `kvbskt=${newBasketId}; path=/; secure=true;`;
85
+ }
86
+ });
87
+ } catch (error) {
88
+ throw parseShopError(error);
89
+ }
90
+ }
91
+ function hasBasketExpired(errorCode) {
92
+ return ["shop.invalidBasketId", "shop.basketRequired"].includes(errorCode);
93
+ }
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
+ }
36
101
  }
37
102
  // Annotate the CommonJS export names for ESM import in node:
38
103
  0 && (module.exports = {
104
+ createBasket,
39
105
  getBasketID,
40
106
  getCookieValue,
107
+ handleInvalidBasketForDonation,
108
+ hasBasketExpired,
41
109
  setBasketID
42
110
  });
package/dist/basket.d.ts CHANGED
@@ -1,5 +1,12 @@
1
1
  declare const getCookieValue: (name: string) => string;
2
2
  declare function getBasketID(): string;
3
- declare function setBasketID(basketId: string): void;
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
11
 
5
- export { getBasketID, getCookieValue, setBasketID };
12
+ export { createBasket, getBasketID, getCookieValue, handleInvalidBasketForDonation, hasBasketExpired, setBasketID };
package/dist/basket.js CHANGED
@@ -1,10 +1,17 @@
1
1
  import {
2
+ createBasket,
2
3
  getBasketID,
3
4
  getCookieValue,
5
+ handleInvalidBasketForDonation,
6
+ hasBasketExpired,
4
7
  setBasketID
5
- } from "./chunk-F6QCMBHV.js";
8
+ } from "./chunk-V7MS7POQ.js";
9
+ import "./chunk-BHFCSJBE.js";
6
10
  export {
11
+ createBasket,
7
12
  getBasketID,
8
13
  getCookieValue,
14
+ handleInvalidBasketForDonation,
15
+ hasBasketExpired,
9
16
  setBasketID
10
17
  };
@@ -32,18 +32,11 @@ __export(basketItems_exports, {
32
32
  setTipDonation: () => setTipDonation
33
33
  });
34
34
  module.exports = __toCommonJS(basketItems_exports);
35
- var import_core = require("@apollo/client/core");
35
+ var import_core2 = require("@apollo/client/core");
36
36
  var import_numeral = __toESM(require("numeral"), 1);
37
37
 
38
38
  // src/basket.ts
39
- var getCookieValue = (name) => {
40
- if (typeof document !== void 0) {
41
- return decodeURIComponent(document.cookie.match(`(^|;)\\s*${name}\\s*=\\s*([^;]+)`)?.pop() || "");
42
- }
43
- };
44
- function getBasketID() {
45
- return getCookieValue("kvbskt");
46
- }
39
+ var import_core = require("@apollo/client/core");
47
40
 
48
41
  // src/shopError.ts
49
42
  var ShopError = class extends Error {
@@ -60,25 +53,69 @@ var ShopError = class extends Error {
60
53
  function parseShopError(error) {
61
54
  const errorCode = error?.code ?? error?.name ?? "";
62
55
  const errorMessage = typeof error === "string" ? error : error?.message ?? "";
56
+ const ctxErrorMsg = error?.ctxErrorMsg ?? null;
63
57
  if (errorCode === "invalidMethodParameter" && errorMessage.includes("paymentMethod.create")) {
64
58
  return new ShopError({
65
59
  code: "paymentMethod.create.invalidMethodParameter",
66
60
  original: error
67
61
  }, "There was a problem validating your payment information. Please double-check the details and try again.");
68
62
  }
63
+ if (errorCode === "shop.invalidBasketId" || errorCode === "shop.basketRequired") {
64
+ return new ShopError({
65
+ code: errorCode,
66
+ original: error
67
+ }, ctxErrorMsg ?? "Something went wrong. Please, refresh the page and try again.");
68
+ }
69
69
  return new ShopError({
70
70
  code: "shop.unknown",
71
71
  original: error
72
72
  }, "An unknown error occurred.");
73
73
  }
74
74
 
75
+ // src/basket.ts
76
+ var getCookieValue = (name) => {
77
+ if (typeof document !== void 0) {
78
+ return decodeURIComponent(document.cookie.match(`(^|;)\\s*${name}\\s*=\\s*([^;]+)`)?.pop() || "");
79
+ }
80
+ };
81
+ function getBasketID() {
82
+ return getCookieValue("kvbskt");
83
+ }
84
+ async function createBasket(apollo) {
85
+ try {
86
+ apollo.mutate({
87
+ mutation: import_core.gql`mutation createNewBasketForUser { shop { id createBasket } }`
88
+ }).then(({ data }) => {
89
+ const newBasketId = data.shop?.createBasket ?? null;
90
+ if (newBasketId) {
91
+ document.cookie = `kvbskt=${newBasketId}; path=/; secure=true;`;
92
+ }
93
+ });
94
+ } catch (error) {
95
+ throw parseShopError(error);
96
+ }
97
+ }
98
+ function hasBasketExpired(errorCode) {
99
+ return ["shop.invalidBasketId", "shop.basketRequired"].includes(errorCode);
100
+ }
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
+
75
110
  // src/basketItems.ts
76
111
  async function setTipDonation({ amount, apollo }) {
77
112
  let data;
78
113
  let error;
114
+ let hasFailedAddToBasket = false;
115
+ const donationAmount = (0, import_numeral.default)(amount).format("0.00");
79
116
  try {
80
117
  const result = await apollo.mutate({
81
- mutation: import_core.gql`mutation setTipDonation($price: Money!, $basketId: String) {
118
+ mutation: import_core2.gql`mutation setTipDonation($price: Money!, $basketId: String) {
82
119
  shop (basketId: $basketId) {
83
120
  id
84
121
  updateDonation (donation: {
@@ -93,12 +130,29 @@ async function setTipDonation({ amount, apollo }) {
93
130
  }
94
131
  }`,
95
132
  variables: {
96
- price: (0, import_numeral.default)(amount).format("0.00"),
133
+ price: donationAmount,
97
134
  basketId: getBasketID()
98
135
  }
99
136
  });
100
- if (result?.error || result?.errors?.length) {
101
- error = result?.error ?? result?.errors?.[0];
137
+ 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
+ };
147
+ }
148
+ });
149
+ if (hasFailedAddToBasket) {
150
+ await handleInvalidBasketForDonation({
151
+ donationAmount,
152
+ navigateToCheckout: true,
153
+ apollo
154
+ });
155
+ }
102
156
  } else {
103
157
  data = result?.data;
104
158
  }
@@ -1,6 +1,8 @@
1
+ import { ApolloClient, NormalizedCacheObject } from '@apollo/client/core';
2
+
1
3
  interface SetTipDonationOptions {
2
4
  amount: string | number;
3
- apollo: any;
5
+ apollo: ApolloClient<NormalizedCacheObject>;
4
6
  }
5
7
  declare function setTipDonation({ amount, apollo }: SetTipDonationOptions): Promise<any>;
6
8
 
@@ -1,8 +1,8 @@
1
1
  import {
2
2
  setTipDonation
3
- } from "./chunk-AEAZBR36.js";
4
- import "./chunk-F6QCMBHV.js";
5
- import "./chunk-H35VQXDR.js";
3
+ } from "./chunk-7KE2LSWP.js";
4
+ import "./chunk-V7MS7POQ.js";
5
+ import "./chunk-BHFCSJBE.js";
6
6
  export {
7
7
  setTipDonation
8
8
  };
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  ShopError,
3
3
  parseShopError
4
- } from "./chunk-H35VQXDR.js";
4
+ } from "./chunk-BHFCSJBE.js";
5
5
 
6
6
  // src/subscriptionCheckout.ts
7
7
  import { gql } from "@apollo/client/core";
@@ -0,0 +1,72 @@
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
+ };
@@ -13,12 +13,19 @@ var ShopError = class extends Error {
13
13
  function parseShopError(error) {
14
14
  const errorCode = error?.code ?? error?.name ?? "";
15
15
  const errorMessage = typeof error === "string" ? error : error?.message ?? "";
16
+ const ctxErrorMsg = error?.ctxErrorMsg ?? null;
16
17
  if (errorCode === "invalidMethodParameter" && errorMessage.includes("paymentMethod.create")) {
17
18
  return new ShopError({
18
19
  code: "paymentMethod.create.invalidMethodParameter",
19
20
  original: error
20
21
  }, "There was a problem validating your payment information. Please double-check the details and try again.");
21
22
  }
23
+ if (errorCode === "shop.invalidBasketId" || errorCode === "shop.basketRequired") {
24
+ return new ShopError({
25
+ code: errorCode,
26
+ original: error
27
+ }, ctxErrorMsg ?? "Something went wrong. Please, refresh the page and try again.");
28
+ }
22
29
  return new ShopError({
23
30
  code: "shop.unknown",
24
31
  original: error
@@ -0,0 +1,50 @@
1
+ import {
2
+ parseShopError
3
+ } from "./chunk-BHFCSJBE.js";
4
+
5
+ // src/basket.ts
6
+ 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
+ function getBasketID() {
13
+ return getCookieValue("kvbskt");
14
+ }
15
+ function setBasketID() {
16
+ }
17
+ async function createBasket(apollo) {
18
+ try {
19
+ apollo.mutate({
20
+ mutation: gql`mutation createNewBasketForUser { shop { id createBasket } }`
21
+ }).then(({ data }) => {
22
+ const newBasketId = data.shop?.createBasket ?? null;
23
+ if (newBasketId) {
24
+ document.cookie = `kvbskt=${newBasketId}; path=/; secure=true;`;
25
+ }
26
+ });
27
+ } catch (error) {
28
+ throw parseShopError(error);
29
+ }
30
+ }
31
+ function hasBasketExpired(errorCode) {
32
+ return ["shop.invalidBasketId", "shop.basketRequired"].includes(errorCode);
33
+ }
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
+
43
+ export {
44
+ getCookieValue,
45
+ getBasketID,
46
+ setBasketID,
47
+ createBasket,
48
+ hasBasketExpired,
49
+ handleInvalidBasketForDonation
50
+ };
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  ShopError,
3
3
  parseShopError
4
- } from "./chunk-H35VQXDR.js";
4
+ } from "./chunk-BHFCSJBE.js";
5
5
 
6
6
  // src/useBraintreeDropIn.ts
7
7
  import { gql } from "@apollo/client/core";
package/dist/index.cjs CHANGED
@@ -31,12 +31,15 @@ var src_exports = {};
31
31
  __export(src_exports, {
32
32
  ShopError: () => ShopError,
33
33
  checkSubscriptionStatus: () => checkSubscriptionStatus,
34
+ createBasket: () => createBasket,
34
35
  defaultPaymentTypes: () => defaultPaymentTypes,
35
36
  executeNewSubscriptionCheckout: () => executeNewSubscriptionCheckout,
36
37
  executeOneTimeCheckout: () => executeOneTimeCheckout,
37
38
  getBasketID: () => getBasketID,
38
39
  getClientToken: () => getClientToken,
39
40
  getCookieValue: () => getCookieValue,
41
+ handleInvalidBasketForDonation: () => handleInvalidBasketForDonation,
42
+ hasBasketExpired: () => hasBasketExpired,
40
43
  parseShopError: () => parseShopError,
41
44
  setBasketID: () => setBasketID,
42
45
  setTipDonation: () => setTipDonation,
@@ -46,20 +49,7 @@ __export(src_exports, {
46
49
  module.exports = __toCommonJS(src_exports);
47
50
 
48
51
  // src/basket.ts
49
- var getCookieValue = (name) => {
50
- if (typeof document !== void 0) {
51
- return decodeURIComponent(document.cookie.match(`(^|;)\\s*${name}\\s*=\\s*([^;]+)`)?.pop() || "");
52
- }
53
- };
54
- function getBasketID() {
55
- return getCookieValue("kvbskt");
56
- }
57
- function setBasketID(basketId) {
58
- }
59
-
60
- // src/basketItems.ts
61
52
  var import_core = require("@apollo/client/core");
62
- var import_numeral = __toESM(require("numeral"), 1);
63
53
 
64
54
  // src/shopError.ts
65
55
  var ShopError = class extends Error {
@@ -76,25 +66,73 @@ var ShopError = class extends Error {
76
66
  function parseShopError(error) {
77
67
  const errorCode = error?.code ?? error?.name ?? "";
78
68
  const errorMessage = typeof error === "string" ? error : error?.message ?? "";
69
+ const ctxErrorMsg = error?.ctxErrorMsg ?? null;
79
70
  if (errorCode === "invalidMethodParameter" && errorMessage.includes("paymentMethod.create")) {
80
71
  return new ShopError({
81
72
  code: "paymentMethod.create.invalidMethodParameter",
82
73
  original: error
83
74
  }, "There was a problem validating your payment information. Please double-check the details and try again.");
84
75
  }
76
+ if (errorCode === "shop.invalidBasketId" || errorCode === "shop.basketRequired") {
77
+ return new ShopError({
78
+ code: errorCode,
79
+ original: error
80
+ }, ctxErrorMsg ?? "Something went wrong. Please, refresh the page and try again.");
81
+ }
85
82
  return new ShopError({
86
83
  code: "shop.unknown",
87
84
  original: error
88
85
  }, "An unknown error occurred.");
89
86
  }
90
87
 
88
+ // src/basket.ts
89
+ var getCookieValue = (name) => {
90
+ if (typeof document !== void 0) {
91
+ return decodeURIComponent(document.cookie.match(`(^|;)\\s*${name}\\s*=\\s*([^;]+)`)?.pop() || "");
92
+ }
93
+ };
94
+ function getBasketID() {
95
+ return getCookieValue("kvbskt");
96
+ }
97
+ function setBasketID() {
98
+ }
99
+ async function createBasket(apollo) {
100
+ try {
101
+ apollo.mutate({
102
+ mutation: import_core.gql`mutation createNewBasketForUser { shop { id createBasket } }`
103
+ }).then(({ data }) => {
104
+ const newBasketId = data.shop?.createBasket ?? null;
105
+ if (newBasketId) {
106
+ document.cookie = `kvbskt=${newBasketId}; path=/; secure=true;`;
107
+ }
108
+ });
109
+ } catch (error) {
110
+ throw parseShopError(error);
111
+ }
112
+ }
113
+ function hasBasketExpired(errorCode) {
114
+ return ["shop.invalidBasketId", "shop.basketRequired"].includes(errorCode);
115
+ }
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
+
91
125
  // src/basketItems.ts
126
+ var import_core2 = require("@apollo/client/core");
127
+ var import_numeral = __toESM(require("numeral"), 1);
92
128
  async function setTipDonation({ amount, apollo }) {
93
129
  let data;
94
130
  let error;
131
+ let hasFailedAddToBasket = false;
132
+ const donationAmount = (0, import_numeral.default)(amount).format("0.00");
95
133
  try {
96
134
  const result = await apollo.mutate({
97
- mutation: import_core.gql`mutation setTipDonation($price: Money!, $basketId: String) {
135
+ mutation: import_core2.gql`mutation setTipDonation($price: Money!, $basketId: String) {
98
136
  shop (basketId: $basketId) {
99
137
  id
100
138
  updateDonation (donation: {
@@ -109,12 +147,29 @@ async function setTipDonation({ amount, apollo }) {
109
147
  }
110
148
  }`,
111
149
  variables: {
112
- price: (0, import_numeral.default)(amount).format("0.00"),
150
+ price: donationAmount,
113
151
  basketId: getBasketID()
114
152
  }
115
153
  });
116
- if (result?.error || result?.errors?.length) {
117
- error = result?.error ?? result?.errors?.[0];
154
+ 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
+ };
164
+ }
165
+ });
166
+ if (hasFailedAddToBasket) {
167
+ await handleInvalidBasketForDonation({
168
+ donationAmount,
169
+ navigateToCheckout: true,
170
+ apollo
171
+ });
172
+ }
118
173
  } else {
119
174
  data = result?.data;
120
175
  }
@@ -128,12 +183,12 @@ async function setTipDonation({ amount, apollo }) {
128
183
  }
129
184
 
130
185
  // src/oneTimeCheckout.ts
131
- var import_core2 = require("@apollo/client/core");
186
+ var import_core3 = require("@apollo/client/core");
132
187
  async function executeOneTimeCheckout({ apollo }) {
133
188
  }
134
189
  async function waitOnTransaction({ apollo, transactionId }) {
135
190
  const result = await apollo.query({
136
- query: import_core2.gql`
191
+ query: import_core3.gql`
137
192
  query checkoutStatus($transactionId: String!, $visitorId: string) {
138
193
  checkoutStatus(transactionId: $transactionId, visitorId: $visitorId) {
139
194
  errorCode
@@ -150,10 +205,10 @@ async function waitOnTransaction({ apollo, transactionId }) {
150
205
  }
151
206
 
152
207
  // src/subscriptionCheckout.ts
153
- var import_core3 = require("@apollo/client/core");
208
+ var import_core4 = require("@apollo/client/core");
154
209
  async function checkSubscriptionStatus(apollo) {
155
210
  const { data: subsData } = await apollo.query({
156
- query: import_core3.gql`query subscriptionStatus{
211
+ query: import_core4.gql`query subscriptionStatus{
157
212
  my {
158
213
  id
159
214
  subscriptions {
@@ -203,7 +258,7 @@ async function executeNewSubscriptionCheckout({
203
258
  donateAmount,
204
259
  dayOfMonth
205
260
  },
206
- mutation: import_core3.gql`mutation createAutoDepositSubscription(
261
+ mutation: import_core4.gql`mutation createAutoDepositSubscription(
207
262
  $nonce: String!,
208
263
  $deviceData: String,
209
264
  $amount: Money!,
@@ -247,13 +302,13 @@ async function executeNewSubscriptionCheckout({
247
302
  }
248
303
 
249
304
  // src/useBraintreeDropIn.ts
250
- var import_core4 = require("@apollo/client/core");
305
+ var import_core5 = require("@apollo/client/core");
251
306
  var import_numeral2 = __toESM(require("numeral"), 1);
252
307
  var import_vue_demi = require("vue-demi");
253
308
  var defaultPaymentTypes = ["paypal", "card", "applePay", "googlePay"];
254
309
  async function getClientToken(apollo) {
255
310
  const { data, error, errors } = await apollo.query({
256
- query: import_core4.gql`query getClientToken {
311
+ query: import_core5.gql`query getClientToken {
257
312
  shop {
258
313
  id
259
314
  getClientToken(useCustomerId: true)
@@ -399,12 +454,15 @@ function useBraintreeDropIn() {
399
454
  0 && (module.exports = {
400
455
  ShopError,
401
456
  checkSubscriptionStatus,
457
+ createBasket,
402
458
  defaultPaymentTypes,
403
459
  executeNewSubscriptionCheckout,
404
460
  executeOneTimeCheckout,
405
461
  getBasketID,
406
462
  getClientToken,
407
463
  getCookieValue,
464
+ handleInvalidBasketForDonation,
465
+ hasBasketExpired,
408
466
  parseShopError,
409
467
  setBasketID,
410
468
  setTipDonation,
package/dist/index.d.ts CHANGED
@@ -1,8 +1,9 @@
1
- export { getBasketID, getCookieValue, setBasketID } from './basket.js';
1
+ export { createBasket, getBasketID, getCookieValue, handleInvalidBasketForDonation, hasBasketExpired, setBasketID } 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';
5
5
  export { SubscriptionCheckoutOptions, checkSubscriptionStatus, executeNewSubscriptionCheckout } from './subscriptionCheckout.js';
6
6
  export { DropInInitOptions, PayPalFlowType, PaymentType, defaultPaymentTypes, getClientToken, default as useBraintreeDropIn } from './useBraintreeDropIn.js';
7
+ import '@apollo/client/core';
7
8
  import 'braintree-web-drop-in';
8
9
  import '@vue/composition-api';
package/dist/index.js CHANGED
@@ -1,11 +1,14 @@
1
1
  import {
2
2
  setTipDonation
3
- } from "./chunk-AEAZBR36.js";
3
+ } from "./chunk-7KE2LSWP.js";
4
4
  import {
5
+ createBasket,
5
6
  getBasketID,
6
7
  getCookieValue,
8
+ handleInvalidBasketForDonation,
9
+ hasBasketExpired,
7
10
  setBasketID
8
- } from "./chunk-F6QCMBHV.js";
11
+ } from "./chunk-V7MS7POQ.js";
9
12
  import {
10
13
  executeOneTimeCheckout,
11
14
  waitOnTransaction
@@ -13,25 +16,28 @@ import {
13
16
  import {
14
17
  checkSubscriptionStatus,
15
18
  executeNewSubscriptionCheckout
16
- } from "./chunk-6XPXXVBS.js";
19
+ } from "./chunk-23ED7LBZ.js";
17
20
  import {
18
21
  defaultPaymentTypes,
19
22
  getClientToken,
20
23
  useBraintreeDropIn
21
- } from "./chunk-HWDTIIXN.js";
24
+ } from "./chunk-VMLGWF7L.js";
22
25
  import {
23
26
  ShopError,
24
27
  parseShopError
25
- } from "./chunk-H35VQXDR.js";
28
+ } from "./chunk-BHFCSJBE.js";
26
29
  export {
27
30
  ShopError,
28
31
  checkSubscriptionStatus,
32
+ createBasket,
29
33
  defaultPaymentTypes,
30
34
  executeNewSubscriptionCheckout,
31
35
  executeOneTimeCheckout,
32
36
  getBasketID,
33
37
  getClientToken,
34
38
  getCookieValue,
39
+ handleInvalidBasketForDonation,
40
+ hasBasketExpired,
35
41
  parseShopError,
36
42
  setBasketID,
37
43
  setTipDonation,
@@ -37,12 +37,19 @@ var ShopError = class extends Error {
37
37
  function parseShopError(error) {
38
38
  const errorCode = error?.code ?? error?.name ?? "";
39
39
  const errorMessage = typeof error === "string" ? error : error?.message ?? "";
40
+ const ctxErrorMsg = error?.ctxErrorMsg ?? null;
40
41
  if (errorCode === "invalidMethodParameter" && errorMessage.includes("paymentMethod.create")) {
41
42
  return new ShopError({
42
43
  code: "paymentMethod.create.invalidMethodParameter",
43
44
  original: error
44
45
  }, "There was a problem validating your payment information. Please double-check the details and try again.");
45
46
  }
47
+ if (errorCode === "shop.invalidBasketId" || errorCode === "shop.basketRequired") {
48
+ return new ShopError({
49
+ code: errorCode,
50
+ original: error
51
+ }, ctxErrorMsg ?? "Something went wrong. Please, refresh the page and try again.");
52
+ }
46
53
  return new ShopError({
47
54
  code: "shop.unknown",
48
55
  original: error
package/dist/shopError.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  ShopError,
3
3
  parseShopError
4
- } from "./chunk-H35VQXDR.js";
4
+ } from "./chunk-BHFCSJBE.js";
5
5
  export {
6
6
  ShopError,
7
7
  parseShopError
@@ -40,12 +40,19 @@ var ShopError = class extends Error {
40
40
  function parseShopError(error) {
41
41
  const errorCode = error?.code ?? error?.name ?? "";
42
42
  const errorMessage = typeof error === "string" ? error : error?.message ?? "";
43
+ const ctxErrorMsg = error?.ctxErrorMsg ?? null;
43
44
  if (errorCode === "invalidMethodParameter" && errorMessage.includes("paymentMethod.create")) {
44
45
  return new ShopError({
45
46
  code: "paymentMethod.create.invalidMethodParameter",
46
47
  original: error
47
48
  }, "There was a problem validating your payment information. Please double-check the details and try again.");
48
49
  }
50
+ if (errorCode === "shop.invalidBasketId" || errorCode === "shop.basketRequired") {
51
+ return new ShopError({
52
+ code: errorCode,
53
+ original: error
54
+ }, ctxErrorMsg ?? "Something went wrong. Please, refresh the page and try again.");
55
+ }
49
56
  return new ShopError({
50
57
  code: "shop.unknown",
51
58
  original: error
@@ -1,8 +1,8 @@
1
1
  import {
2
2
  checkSubscriptionStatus,
3
3
  executeNewSubscriptionCheckout
4
- } from "./chunk-6XPXXVBS.js";
5
- import "./chunk-H35VQXDR.js";
4
+ } from "./chunk-23ED7LBZ.js";
5
+ import "./chunk-BHFCSJBE.js";
6
6
  export {
7
7
  checkSubscriptionStatus,
8
8
  executeNewSubscriptionCheckout
@@ -53,12 +53,19 @@ var ShopError = class extends Error {
53
53
  function parseShopError(error) {
54
54
  const errorCode = error?.code ?? error?.name ?? "";
55
55
  const errorMessage = typeof error === "string" ? error : error?.message ?? "";
56
+ const ctxErrorMsg = error?.ctxErrorMsg ?? null;
56
57
  if (errorCode === "invalidMethodParameter" && errorMessage.includes("paymentMethod.create")) {
57
58
  return new ShopError({
58
59
  code: "paymentMethod.create.invalidMethodParameter",
59
60
  original: error
60
61
  }, "There was a problem validating your payment information. Please double-check the details and try again.");
61
62
  }
63
+ if (errorCode === "shop.invalidBasketId" || errorCode === "shop.basketRequired") {
64
+ return new ShopError({
65
+ code: errorCode,
66
+ original: error
67
+ }, ctxErrorMsg ?? "Something went wrong. Please, refresh the page and try again.");
68
+ }
62
69
  return new ShopError({
63
70
  code: "shop.unknown",
64
71
  original: error
@@ -2,8 +2,8 @@ import {
2
2
  defaultPaymentTypes,
3
3
  getClientToken,
4
4
  useBraintreeDropIn
5
- } from "./chunk-HWDTIIXN.js";
6
- import "./chunk-H35VQXDR.js";
5
+ } from "./chunk-VMLGWF7L.js";
6
+ import "./chunk-BHFCSJBE.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.1.11",
3
+ "version": "1.2.1",
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.23.1",
41
+ "@kiva/kv-components": "^3.24.0",
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": "732e903edf4eda79775caf8a429c1f2afd09f0ea"
56
+ "gitHead": "37410d19aa27c86c01a93e6423439b4da500673b"
57
57
  }
@@ -1,51 +0,0 @@
1
- import {
2
- getBasketID
3
- } from "./chunk-F6QCMBHV.js";
4
- import {
5
- parseShopError
6
- } from "./chunk-H35VQXDR.js";
7
-
8
- // src/basketItems.ts
9
- import { gql } from "@apollo/client/core";
10
- import numeral from "numeral";
11
- async function setTipDonation({ amount, apollo }) {
12
- let data;
13
- let error;
14
- try {
15
- const result = await apollo.mutate({
16
- mutation: gql`mutation setTipDonation($price: Money!, $basketId: String) {
17
- shop (basketId: $basketId) {
18
- id
19
- updateDonation (donation: {
20
- price: $price,
21
- isTip: true
22
- })
23
- {
24
- id
25
- price
26
- isTip
27
- }
28
- }
29
- }`,
30
- variables: {
31
- price: numeral(amount).format("0.00"),
32
- basketId: getBasketID()
33
- }
34
- });
35
- if (result?.error || result?.errors?.length) {
36
- error = result?.error ?? result?.errors?.[0];
37
- } else {
38
- data = result?.data;
39
- }
40
- } catch (e) {
41
- error = e;
42
- }
43
- if (error) {
44
- throw parseShopError(error);
45
- }
46
- return data?.shop?.updateDonation;
47
- }
48
-
49
- export {
50
- setTipDonation
51
- };
@@ -1,17 +0,0 @@
1
- // src/basket.ts
2
- var getCookieValue = (name) => {
3
- if (typeof document !== void 0) {
4
- return decodeURIComponent(document.cookie.match(`(^|;)\\s*${name}\\s*=\\s*([^;]+)`)?.pop() || "");
5
- }
6
- };
7
- function getBasketID() {
8
- return getCookieValue("kvbskt");
9
- }
10
- function setBasketID(basketId) {
11
- }
12
-
13
- export {
14
- getCookieValue,
15
- getBasketID,
16
- setBasketID
17
- };