swell-js 4.0.2 → 4.0.3

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.
@@ -6950,58 +6950,48 @@
6950
6950
  } : void 0
6951
6951
  };
6952
6952
  }
6953
- async function vaultRequest(method, url, data, opt = void 0) {
6954
- const vaultUrl = options$1.vaultUrl;
6955
- const timeout = options$1.timeout;
6956
- const requestId = vaultRequestId();
6957
- const callback = `swell_vault_response_${requestId}`;
6958
- data = {
6953
+ async function vaultRequest(method, url, data) {
6954
+ const { vaultUrl, timeout, key } = options$1;
6955
+ const requestData = {
6959
6956
  $jsonp: {
6960
6957
  method,
6961
- callback
6958
+ callback: "none"
6962
6959
  },
6963
6960
  $data: data,
6964
- $key: options$1.key
6961
+ $key: key
6965
6962
  };
6966
- return new Promise((resolve, reject) => {
6967
- const script = document.createElement("script");
6968
- script.type = "text/javascript";
6969
- script.src = `${trimEnd(vaultUrl)}/${trimStart(url)}?${serializeData(
6970
- data
6971
- )}`;
6972
- const errorTimeout = setTimeout(() => {
6973
- window[callback]({
6974
- $error: `Request timed out after ${timeout / 1e3} seconds`,
6975
- $status: 500
6976
- });
6977
- }, timeout);
6978
- window[callback] = (result) => {
6979
- clearTimeout(errorTimeout);
6980
- if (result && result.$error) {
6981
- const err = new Error(result.$error);
6982
- err.code = "request_error";
6983
- err.status = result.$status;
6984
- reject(err);
6985
- } else if (!result || result.$status >= 300) {
6986
- const err = new Error(
6987
- "A connection error occurred while making the request"
6988
- );
6989
- err.code = "connection_error";
6990
- err.status = result.$status;
6991
- reject(err);
6992
- } else {
6993
- resolve(result.$data);
6994
- }
6995
- delete window[callback];
6996
- script.parentNode.removeChild(script);
6997
- };
6998
- document.getElementsByTagName("head")[0].appendChild(script);
6963
+ const requestUrl = `${trimEnd(vaultUrl)}/${trimStart(url)}?${serializeData(
6964
+ requestData
6965
+ )}`;
6966
+ const abortController = new AbortController();
6967
+ const id = setTimeout(() => abortController.abort(), timeout);
6968
+ const result = await fetch(requestUrl, {
6969
+ signal: abortController.signal
6970
+ }).then((response) => response.json()).catch((error) => {
6971
+ if (error.name === "AbortError") {
6972
+ const timeoutError = new Error(
6973
+ `Request timed out after ${timeout / 1e3} seconds`
6974
+ );
6975
+ timeoutError.status = 500;
6976
+ throw timeoutError;
6977
+ }
6978
+ throw new Error(error.message);
6999
6979
  });
7000
- }
7001
- function vaultRequestId() {
7002
- window.__swell_vault_request_id = window.__swell_vault_request_id || 0;
7003
- window.__swell_vault_request_id++;
7004
- return window.__swell_vault_request_id;
6980
+ clearTimeout(id);
6981
+ if (result == null ? void 0 : result.$error) {
6982
+ const requestError = new Error(result.$error);
6983
+ requestError.code = "request_error";
6984
+ requestError.status = result.$status;
6985
+ throw requestError;
6986
+ } else if (!result || result.$status >= 300) {
6987
+ const connectionError = new Error(
6988
+ "A connection error occurred while making the request"
6989
+ );
6990
+ connectionError.code = "connection_error";
6991
+ connectionError.status = result == null ? void 0 : result.$status;
6992
+ throw connectionError;
6993
+ }
6994
+ return result.$data;
7005
6995
  }
7006
6996
  function serializeData(data) {
7007
6997
  const s = [];
@@ -10305,9 +10295,7 @@
10305
10295
  }
10306
10296
  async _onClick(googlePayment, paymentDataRequest) {
10307
10297
  try {
10308
- const paymentData = await this.googleClient.loadPaymentData(
10309
- paymentDataRequest
10310
- );
10298
+ const paymentData = await this.googleClient.loadPaymentData(paymentDataRequest);
10311
10299
  if (paymentData) {
10312
10300
  await this._submitPayment(googlePayment, paymentData);
10313
10301
  }
@@ -11842,7 +11830,7 @@
11842
11830
  previewContent: null
11843
11831
  };
11844
11832
  const api = {
11845
- version: "4.0.2",
11833
+ version: "4.0.3",
11846
11834
  options,
11847
11835
  request,
11848
11836
  init(store, key, opt = {}) {