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.
@@ -1,52 +1,24 @@
1
+ import { AxiosPromise } from 'axios';
1
2
  import Config from '../config/Config';
2
3
  import Route from '../routes/interface';
3
4
  import Response from './Response';
4
- import { AxiosPromise } from 'axios';
5
5
  declare class Requests {
6
- config: Config;
6
+ private static axiosInstance;
7
+ private static config;
7
8
  private static baseUrl;
8
9
  private static authToken;
9
10
  private static community_id?;
10
11
  constructor(config: Config);
11
- /**
12
- * Sets the base url of the API.
13
- *
14
- * @param url The url to of the API.
15
- */
16
12
  static setBaseUrl(url: string): void;
17
- /**
18
- * Sets the JSON Web token
19
- *
20
- * @param token
21
- */
22
13
  static setAuthToken(token: string): void;
23
- /**
24
- * Sets the community id that will be associated with all requests
25
- *
26
- * @param token
27
- */
28
14
  static setCommunityID(community_id: string | undefined): void;
29
15
  private static request;
30
- /**
31
- * Calls a GET request to the url endpoint.
32
- *
33
- * @param url
34
- * @returns
35
- */
36
16
  static get<T>(url: string, params?: Record<string, any>): AxiosPromise<Response<T>>;
37
17
  static post<T>(url: string, data: any, params?: Record<string, any>): AxiosPromise<Response<T>>;
38
18
  static put<T>(url: string, data: any, params?: Record<string, any>): AxiosPromise<Response<T>>;
39
19
  static delete<T>(url: string, params?: Record<string, any>): AxiosPromise<Response<T>>;
40
20
  static uploadFile<T>(url: string, filename: string, file: File, data?: any, params?: Record<string, any>): AxiosPromise<Response<T>>;
41
21
  static uploadBlob<T>(url: string, filename: string, blob: Blob, data?: any, params?: Record<string, any>): AxiosPromise<Response<T>>;
42
- /**
43
- * The Route class contains the method and url, thereforce items can be
44
- * automatically routed depending on the configuration.
45
- *
46
- * @param route
47
- * @param data
48
- * @returns
49
- */
50
22
  static processRoute<T>(route: Route, data?: object, routeReplace?: {
51
23
  [key: string]: any;
52
24
  }, params?: Record<string, any>): AxiosPromise<Response<T>>;
package/dist/index.d.ts CHANGED
@@ -1674,50 +1674,22 @@ interface Route {
1674
1674
  }
1675
1675
 
1676
1676
  declare class Requests {
1677
- config: Config;
1677
+ private static axiosInstance;
1678
+ private static config;
1678
1679
  private static baseUrl;
1679
1680
  private static authToken;
1680
1681
  private static community_id?;
1681
1682
  constructor(config: Config);
1682
- /**
1683
- * Sets the base url of the API.
1684
- *
1685
- * @param url The url to of the API.
1686
- */
1687
1683
  static setBaseUrl(url: string): void;
1688
- /**
1689
- * Sets the JSON Web token
1690
- *
1691
- * @param token
1692
- */
1693
1684
  static setAuthToken(token: string): void;
1694
- /**
1695
- * Sets the community id that will be associated with all requests
1696
- *
1697
- * @param token
1698
- */
1699
1685
  static setCommunityID(community_id: string | undefined): void;
1700
1686
  private static request;
1701
- /**
1702
- * Calls a GET request to the url endpoint.
1703
- *
1704
- * @param url
1705
- * @returns
1706
- */
1707
1687
  static get<T>(url: string, params?: Record<string, any>): AxiosPromise<Response<T>>;
1708
1688
  static post<T>(url: string, data: any, params?: Record<string, any>): AxiosPromise<Response<T>>;
1709
1689
  static put<T>(url: string, data: any, params?: Record<string, any>): AxiosPromise<Response<T>>;
1710
1690
  static delete<T>(url: string, params?: Record<string, any>): AxiosPromise<Response<T>>;
1711
1691
  static uploadFile<T>(url: string, filename: string, file: File, data?: any, params?: Record<string, any>): AxiosPromise<Response<T>>;
1712
1692
  static uploadBlob<T>(url: string, filename: string, blob: Blob, data?: any, params?: Record<string, any>): AxiosPromise<Response<T>>;
1713
- /**
1714
- * The Route class contains the method and url, thereforce items can be
1715
- * automatically routed depending on the configuration.
1716
- *
1717
- * @param route
1718
- * @param data
1719
- * @returns
1720
- */
1721
1693
  static processRoute<T>(route: Route, data?: object, routeReplace?: {
1722
1694
  [key: string]: any;
1723
1695
  }, params?: Record<string, any>): AxiosPromise<Response<T>>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "glitch-javascript-sdk",
3
- "version": "0.4.5",
3
+ "version": "0.4.7",
4
4
  "description": "Javascript SDK for Glitch",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",
@@ -1,86 +1,67 @@
1
- import axios from 'axios';
1
+ import axios, { AxiosInstance, AxiosPromise } from 'axios';
2
2
  import Config from '../config/Config';
3
3
  import HTTP_METHODS from '../constants/HttpMethods';
4
4
  import Route from '../routes/interface';
5
5
  import Response from './Response';
6
- import { AxiosPromise } from 'axios';
7
6
 
8
7
  class Requests {
9
-
10
- config: Config;
11
-
12
- //The base url of the API.
8
+ private static axiosInstance: AxiosInstance;
9
+ private static config: Config;
13
10
  private static baseUrl = "";
14
-
15
- //The Json Web Token to send in the header
16
11
  private static authToken = "";
17
-
18
- //The ID of the community that will be added to request
19
- private static community_id? = "";
12
+ private static community_id?: string = "";
20
13
 
21
14
  constructor(config: Config) {
22
- this.config = config;
15
+ Requests.config = config;
16
+ Requests.axiosInstance = axios.create({
17
+ baseURL: Requests.baseUrl,
18
+ headers: { 'Content-Type': 'application/json' },
19
+ });
23
20
  }
24
21
 
25
- /**
26
- * Sets the base url of the API.
27
- *
28
- * @param url The url to of the API.
29
- */
30
- public static setBaseUrl(url : string) {
31
- this.baseUrl = url;
22
+ public static setBaseUrl(url: string) {
23
+ Requests.baseUrl = url;
24
+
25
+ if(Requests.axiosInstance && Requests.axiosInstance.defaults) {
26
+ Requests.axiosInstance.defaults.baseURL = url;
27
+ }
32
28
  }
33
29
 
34
- /**
35
- * Sets the JSON Web token
36
- *
37
- * @param token
38
- */
39
- public static setAuthToken(token : string) {
40
- this.authToken = token;
30
+ public static setAuthToken(token: string) {
31
+ Requests.authToken = token;
41
32
  }
42
33
 
43
- /**
44
- * Sets the community id that will be associated with all requests
45
- *
46
- * @param token
47
- */
48
- public static setCommunityID(community_id : string | undefined) {
49
- this.community_id = community_id;
34
+ public static setCommunityID(community_id: string | undefined) {
35
+ Requests.community_id = community_id;
50
36
  }
51
37
 
52
-
53
-
54
38
  private static request<T>(
55
39
  method: 'GET' | 'POST' | 'PUT' | 'DELETE',
56
40
  url: string,
57
41
  data?: any,
58
42
  fileData?: any
59
43
  ): AxiosPromise<Response<T>> {
60
-
61
- let headers : { [key: string]: string } = {
44
+ let headers: { [key: string]: string } = {
62
45
  'Content-Type': 'application/json',
63
46
  };
64
47
 
65
- if(this.authToken) {
66
- headers['Authorization'] = `Bearer ${this.authToken}`;
67
- }
48
+ if (Requests.authToken) {
49
+ headers['Authorization'] = `Bearer ${Requests.authToken}`;
50
+ }
68
51
 
69
52
  if (fileData) {
70
53
  headers['Content-Type'] = 'multipart/form-data';
71
54
  }
72
55
 
73
- //Remove double slashes
74
56
  url = url.replace(/\/\//g, '/');
75
57
 
76
- const uri = `${this.baseUrl}${url}`;
58
+ const uri = `${Requests.baseUrl}${url}`;
77
59
 
78
60
  const validUri = uri.replace(/\/\//g, '/');
79
61
 
80
- const axiosPromise = axios({
62
+ const axiosPromise = Requests.axiosInstance({
81
63
  method,
82
64
  url: uri,
83
- baseURL: this.baseUrl,
84
65
  data: fileData || data,
85
66
  headers,
86
67
  });
@@ -88,16 +69,8 @@ class Requests {
88
69
  return axiosPromise;
89
70
  }
90
71
 
91
- /**
92
- * Calls a GET request to the url endpoint.
93
- *
94
- * @param url
95
- * @returns
96
- */
97
72
  public static get<T>(url: string, params?: Record<string, any>): AxiosPromise<Response<T>> {
98
-
99
73
  if (params && Object.keys(params).length > 0) {
100
-
101
74
  const queryString = Object.entries(params)
102
75
  .map(([key, value]) => {
103
76
  if (Array.isArray(value)) {
@@ -108,19 +81,16 @@ class Requests {
108
81
  .join('&');
109
82
  url = `${url}?${queryString}`;
110
83
  }
111
-
112
- if (this.community_id) {
113
- // Check if the URL already contains query parameters
84
+
85
+ if (Requests.community_id) {
114
86
  const separator = url.includes('?') ? '&' : '?';
115
- // Append the community_id query parameter
116
- url = `${url}${separator}community_id=${this.community_id}`;
87
+ url = `${url}${separator}community_id=${Requests.community_id}`;
117
88
  }
118
89
 
119
- return this.request<T>('GET', url);
90
+ return Requests.request<T>('GET', url);
120
91
  }
121
92
 
122
93
  public static post<T>(url: string, data: any, params?: Record<string, any>): AxiosPromise<Response<T>> {
123
-
124
94
  if (params && Object.keys(params).length > 0) {
125
95
  const queryString = Object.entries(params)
126
96
  .map(([key, value]) => `${key}=${encodeURIComponent(value)}`)
@@ -128,19 +98,17 @@ class Requests {
128
98
  url = `${url}?${queryString}`;
129
99
  }
130
100
 
131
- if (this.community_id) {
132
- // Add the community_id to the request body
101
+ if (Requests.community_id) {
133
102
  data = {
134
103
  ...data,
135
- communities: [ this.community_id ]
104
+ communities: [Requests.community_id],
136
105
  };
137
106
  }
138
107
 
139
- return this.request<T>('POST', url, data);
108
+ return Requests.request<T>('POST', url, data);
140
109
  }
141
110
 
142
111
  public static put<T>(url: string, data: any, params?: Record<string, any>): AxiosPromise<Response<T>> {
143
-
144
112
  if (params && Object.keys(params).length > 0) {
145
113
  const queryString = Object.entries(params)
146
114
  .map(([key, value]) => `${key}=${encodeURIComponent(value)}`)
@@ -148,19 +116,17 @@ class Requests {
148
116
  url = `${url}?${queryString}`;
149
117
  }
150
118
 
151
- if (this.community_id) {
152
- // Add the community_id to the request body
119
+ if (Requests.community_id) {
153
120
  data = {
154
121
  ...data,
155
- community_id: this.community_id
122
+ community_id: Requests.community_id,
156
123
  };
157
124
  }
158
125
 
159
- return this.request<T>('PUT', url, data);
126
+ return Requests.request<T>('PUT', url, data);
160
127
  }
161
128
 
162
129
  public static delete<T>(url: string, params?: Record<string, any>): AxiosPromise<Response<T>> {
163
-
164
130
  if (params && Object.keys(params).length > 0) {
165
131
  const queryString = Object.entries(params)
166
132
  .map(([key, value]) => `${key}=${encodeURIComponent(value)}`)
@@ -168,40 +134,36 @@ class Requests {
168
134
  url = `${url}?${queryString}`;
169
135
  }
170
136
 
171
- if (this.community_id) {
172
- // Check if the URL already contains query parameters
137
+ if (Requests.community_id) {
173
138
  const separator = url.includes('?') ? '&' : '?';
174
- // Append the community_id query parameter
175
- url = `${url}${separator}community_id=${this.community_id}`;
139
+ url = `${url}${separator}community_id=${Requests.community_id}`;
176
140
  }
177
141
 
178
- return this.request<T>('DELETE', url);
142
+ return Requests.request<T>('DELETE', url);
179
143
  }
180
144
 
181
145
  public static uploadFile<T>(
182
146
  url: string,
183
- filename : string,
147
+ filename: string,
184
148
  file: File,
185
149
  data?: any,
186
150
  params?: Record<string, any>
187
151
  ): AxiosPromise<Response<T>> {
188
-
189
152
  if (params && Object.keys(params).length > 0) {
190
153
  const queryString = Object.entries(params)
191
154
  .map(([key, value]) => `${key}=${encodeURIComponent(value)}`)
192
155
  .join('&');
193
156
  url = `${url}?${queryString}`;
194
157
  }
195
-
158
+
196
159
  const formData = new FormData();
197
160
 
198
161
  formData.append(filename, file);
199
162
 
200
- if (this.community_id) {
201
- // Add the community_id to the request body
163
+ if (Requests.community_id) {
202
164
  data = {
203
165
  ...data,
204
- communities: [ this.community_id ]
166
+ communities: [Requests.community_id],
205
167
  };
206
168
  }
207
169
 
@@ -209,17 +171,16 @@ class Requests {
209
171
  formData.append(key, data[key]);
210
172
  }
211
173
 
212
- return this.request<T>('POST', url, data, formData);
174
+ return Requests.request<T>('POST', url, data, formData);
213
175
  }
214
176
 
215
177
  public static uploadBlob<T>(
216
178
  url: string,
217
- filename : string,
179
+ filename: string,
218
180
  blob: Blob,
219
181
  data?: any,
220
182
  params?: Record<string, any>
221
183
  ): AxiosPromise<Response<T>> {
222
-
223
184
  if (params && Object.keys(params).length > 0) {
224
185
  const queryString = Object.entries(params)
225
186
  .map(([key, value]) => `${key}=${encodeURIComponent(value)}`)
@@ -231,61 +192,41 @@ class Requests {
231
192
 
232
193
  formData.append(filename, blob);
233
194
 
234
- if (this.community_id) {
235
- // Add the community_id to the request body
195
+ if (Requests.community_id) {
236
196
  data = {
237
197
  ...data,
238
- communities: [ this.community_id ]
198
+ communities: [Requests.community_id],
239
199
  };
240
200
  }
241
201
 
242
-
243
202
  for (let key in data) {
244
203
  formData.append(key, data[key]);
245
204
  }
246
205
 
247
- return this.request<T>('POST', url, data, formData);
206
+ return Requests.request<T>('POST', url, data, formData);
248
207
  }
249
208
 
250
- /**
251
- * The Route class contains the method and url, thereforce items can be
252
- * automatically routed depending on the configuration.
253
- *
254
- * @param route
255
- * @param data
256
- * @returns
257
- */
258
- public static processRoute<T>(route : Route, data? : object, routeReplace? : {[key: string]: any}, params?: Record<string, any>) : AxiosPromise<Response<T>> {
259
-
209
+ public static processRoute<T>(route: Route, data?: object, routeReplace?: { [key: string]: any }, params?: Record<string, any>): AxiosPromise<Response<T>> {
260
210
  let url = route.url;
261
211
 
262
- if(routeReplace) {
263
-
212
+ if (routeReplace) {
264
213
  for (let key in routeReplace) {
265
214
  url = url.replace("{" + key + "}", routeReplace[key]);
266
215
  }
267
-
268
216
  }
269
217
 
270
- if(route.method == HTTP_METHODS.GET) {
271
- return this.get(url, params);
272
- } else if(route.method == HTTP_METHODS.POST) {
273
- return this.post(url, data, params);
274
- } else if(route.method == HTTP_METHODS.PUT) {
275
- return this.put(url, data, params);
276
- } else if(route.method == HTTP_METHODS.DELETE) {
277
- return this.delete(url, params);
218
+ if (route.method == HTTP_METHODS.GET) {
219
+ return Requests.get(url, params);
220
+ } else if (route.method == HTTP_METHODS.POST) {
221
+ return Requests.post(url, data, params);
222
+ } else if (route.method == HTTP_METHODS.PUT) {
223
+ return Requests.put(url, data, params);
224
+ } else if (route.method == HTTP_METHODS.DELETE) {
225
+ return Requests.delete(url, params);
278
226
  }
279
227
 
280
- return this.get(url);
281
-
228
+ return Requests.get(url);
282
229
  }
283
-
284
230
  }
285
231
 
286
232
  export default Requests;
287
-
288
-
289
-
290
-
291
-