glitch-javascript-sdk 0.4.5 → 0.4.7

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/cjs/index.js CHANGED
@@ -15650,60 +15650,44 @@ var HTTP_METHODS = {
15650
15650
 
15651
15651
  var Requests = /** @class */ (function () {
15652
15652
  function Requests(config) {
15653
- this.config = config;
15653
+ Requests.config = config;
15654
+ Requests.axiosInstance = axios.create({
15655
+ baseURL: Requests.baseUrl,
15656
+ headers: { 'Content-Type': 'application/json' },
15657
+ });
15654
15658
  }
15655
- /**
15656
- * Sets the base url of the API.
15657
- *
15658
- * @param url The url to of the API.
15659
- */
15660
15659
  Requests.setBaseUrl = function (url) {
15661
- this.baseUrl = url;
15660
+ Requests.baseUrl = url;
15661
+ if (Requests.axiosInstance && Requests.axiosInstance.defaults) {
15662
+ Requests.axiosInstance.defaults.baseURL = url;
15663
+ }
15662
15664
  };
15663
- /**
15664
- * Sets the JSON Web token
15665
- *
15666
- * @param token
15667
- */
15668
15665
  Requests.setAuthToken = function (token) {
15669
- this.authToken = token;
15666
+ Requests.authToken = token;
15670
15667
  };
15671
- /**
15672
- * Sets the community id that will be associated with all requests
15673
- *
15674
- * @param token
15675
- */
15676
15668
  Requests.setCommunityID = function (community_id) {
15677
- this.community_id = community_id;
15669
+ Requests.community_id = community_id;
15678
15670
  };
15679
15671
  Requests.request = function (method, url, data, fileData) {
15680
15672
  var headers = {
15681
15673
  'Content-Type': 'application/json',
15682
15674
  };
15683
- if (this.authToken) {
15684
- headers['Authorization'] = "Bearer ".concat(this.authToken);
15675
+ if (Requests.authToken) {
15676
+ headers['Authorization'] = "Bearer ".concat(Requests.authToken);
15685
15677
  }
15686
15678
  if (fileData) {
15687
15679
  headers['Content-Type'] = 'multipart/form-data';
15688
15680
  }
15689
- //Remove double slashes
15690
15681
  url = url.replace(/\/\//g, '/');
15691
- var uri = "".concat(this.baseUrl).concat(url);
15692
- var axiosPromise = axios({
15682
+ var uri = "".concat(Requests.baseUrl).concat(url);
15683
+ var axiosPromise = Requests.axiosInstance({
15693
15684
  method: method,
15694
15685
  url: uri,
15695
- baseURL: this.baseUrl,
15696
15686
  data: fileData || data,
15697
15687
  headers: headers,
15698
15688
  });
15699
15689
  return axiosPromise;
15700
15690
  };
15701
- /**
15702
- * Calls a GET request to the url endpoint.
15703
- *
15704
- * @param url
15705
- * @returns
15706
- */
15707
15691
  Requests.get = function (url, params) {
15708
15692
  if (params && Object.keys(params).length > 0) {
15709
15693
  var queryString = Object.entries(params)
@@ -15717,13 +15701,11 @@ var Requests = /** @class */ (function () {
15717
15701
  .join('&');
15718
15702
  url = "".concat(url, "?").concat(queryString);
15719
15703
  }
15720
- if (this.community_id) {
15721
- // Check if the URL already contains query parameters
15704
+ if (Requests.community_id) {
15722
15705
  var separator = url.includes('?') ? '&' : '?';
15723
- // Append the community_id query parameter
15724
- url = "".concat(url).concat(separator, "community_id=").concat(this.community_id);
15706
+ url = "".concat(url).concat(separator, "community_id=").concat(Requests.community_id);
15725
15707
  }
15726
- return this.request('GET', url);
15708
+ return Requests.request('GET', url);
15727
15709
  };
15728
15710
  Requests.post = function (url, data, params) {
15729
15711
  if (params && Object.keys(params).length > 0) {
@@ -15735,11 +15717,10 @@ var Requests = /** @class */ (function () {
15735
15717
  .join('&');
15736
15718
  url = "".concat(url, "?").concat(queryString);
15737
15719
  }
15738
- if (this.community_id) {
15739
- // Add the community_id to the request body
15740
- data = __assign(__assign({}, data), { communities: [this.community_id] });
15720
+ if (Requests.community_id) {
15721
+ data = __assign(__assign({}, data), { communities: [Requests.community_id] });
15741
15722
  }
15742
- return this.request('POST', url, data);
15723
+ return Requests.request('POST', url, data);
15743
15724
  };
15744
15725
  Requests.put = function (url, data, params) {
15745
15726
  if (params && Object.keys(params).length > 0) {
@@ -15751,11 +15732,10 @@ var Requests = /** @class */ (function () {
15751
15732
  .join('&');
15752
15733
  url = "".concat(url, "?").concat(queryString);
15753
15734
  }
15754
- if (this.community_id) {
15755
- // Add the community_id to the request body
15756
- data = __assign(__assign({}, data), { community_id: this.community_id });
15735
+ if (Requests.community_id) {
15736
+ data = __assign(__assign({}, data), { community_id: Requests.community_id });
15757
15737
  }
15758
- return this.request('PUT', url, data);
15738
+ return Requests.request('PUT', url, data);
15759
15739
  };
15760
15740
  Requests.delete = function (url, params) {
15761
15741
  if (params && Object.keys(params).length > 0) {
@@ -15767,13 +15747,11 @@ var Requests = /** @class */ (function () {
15767
15747
  .join('&');
15768
15748
  url = "".concat(url, "?").concat(queryString);
15769
15749
  }
15770
- if (this.community_id) {
15771
- // Check if the URL already contains query parameters
15750
+ if (Requests.community_id) {
15772
15751
  var separator = url.includes('?') ? '&' : '?';
15773
- // Append the community_id query parameter
15774
- url = "".concat(url).concat(separator, "community_id=").concat(this.community_id);
15752
+ url = "".concat(url).concat(separator, "community_id=").concat(Requests.community_id);
15775
15753
  }
15776
- return this.request('DELETE', url);
15754
+ return Requests.request('DELETE', url);
15777
15755
  };
15778
15756
  Requests.uploadFile = function (url, filename, file, data, params) {
15779
15757
  if (params && Object.keys(params).length > 0) {
@@ -15787,14 +15765,13 @@ var Requests = /** @class */ (function () {
15787
15765
  }
15788
15766
  var formData = new FormData();
15789
15767
  formData.append(filename, file);
15790
- if (this.community_id) {
15791
- // Add the community_id to the request body
15792
- data = __assign(__assign({}, data), { communities: [this.community_id] });
15768
+ if (Requests.community_id) {
15769
+ data = __assign(__assign({}, data), { communities: [Requests.community_id] });
15793
15770
  }
15794
15771
  for (var key in data) {
15795
15772
  formData.append(key, data[key]);
15796
15773
  }
15797
- return this.request('POST', url, data, formData);
15774
+ return Requests.request('POST', url, data, formData);
15798
15775
  };
15799
15776
  Requests.uploadBlob = function (url, filename, blob, data, params) {
15800
15777
  if (params && Object.keys(params).length > 0) {
@@ -15808,23 +15785,14 @@ var Requests = /** @class */ (function () {
15808
15785
  }
15809
15786
  var formData = new FormData();
15810
15787
  formData.append(filename, blob);
15811
- if (this.community_id) {
15812
- // Add the community_id to the request body
15813
- data = __assign(__assign({}, data), { communities: [this.community_id] });
15788
+ if (Requests.community_id) {
15789
+ data = __assign(__assign({}, data), { communities: [Requests.community_id] });
15814
15790
  }
15815
15791
  for (var key in data) {
15816
15792
  formData.append(key, data[key]);
15817
15793
  }
15818
- return this.request('POST', url, data, formData);
15794
+ return Requests.request('POST', url, data, formData);
15819
15795
  };
15820
- /**
15821
- * The Route class contains the method and url, thereforce items can be
15822
- * automatically routed depending on the configuration.
15823
- *
15824
- * @param route
15825
- * @param data
15826
- * @returns
15827
- */
15828
15796
  Requests.processRoute = function (route, data, routeReplace, params) {
15829
15797
  var url = route.url;
15830
15798
  if (routeReplace) {
@@ -15833,24 +15801,21 @@ var Requests = /** @class */ (function () {
15833
15801
  }
15834
15802
  }
15835
15803
  if (route.method == HTTP_METHODS.GET) {
15836
- return this.get(url, params);
15804
+ return Requests.get(url, params);
15837
15805
  }
15838
15806
  else if (route.method == HTTP_METHODS.POST) {
15839
- return this.post(url, data, params);
15807
+ return Requests.post(url, data, params);
15840
15808
  }
15841
15809
  else if (route.method == HTTP_METHODS.PUT) {
15842
- return this.put(url, data, params);
15810
+ return Requests.put(url, data, params);
15843
15811
  }
15844
15812
  else if (route.method == HTTP_METHODS.DELETE) {
15845
- return this.delete(url, params);
15813
+ return Requests.delete(url, params);
15846
15814
  }
15847
- return this.get(url);
15815
+ return Requests.get(url);
15848
15816
  };
15849
- //The base url of the API.
15850
15817
  Requests.baseUrl = "";
15851
- //The Json Web Token to send in the header
15852
15818
  Requests.authToken = "";
15853
- //The ID of the community that will be added to request
15854
15819
  Requests.community_id = "";
15855
15820
  return Requests;
15856
15821
  }());