glitch-javascript-sdk 0.4.5 → 0.4.6

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