glitch-javascript-sdk 0.4.4 → 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.
@@ -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.4",
3
+ "version": "0.4.6",
4
4
  "description": "Javascript SDK for Glitch",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",
package/src/api/Auth.ts CHANGED
@@ -80,7 +80,7 @@ class Auth {
80
80
  * @returns promise
81
81
  */
82
82
  public static resetPassword<T>(data : object) : AxiosPromise<Response<T>> {
83
- return Requests.processRoute(AuthRoutes.routes.forgot_password, data);
83
+ return Requests.processRoute(AuthRoutes.routes.reset_password, data);
84
84
  }
85
85
 
86
86
  }
@@ -1,86 +1,64 @@
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
+ Requests.axiosInstance.defaults.baseURL = url;
32
25
  }
33
26
 
34
- /**
35
- * Sets the JSON Web token
36
- *
37
- * @param token
38
- */
39
- public static setAuthToken(token : string) {
40
- this.authToken = token;
27
+ public static setAuthToken(token: string) {
28
+ Requests.authToken = token;
41
29
  }
42
30
 
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;
31
+ public static setCommunityID(community_id: string | undefined) {
32
+ Requests.community_id = community_id;
50
33
  }
51
34
 
52
-
53
-
54
35
  private static request<T>(
55
36
  method: 'GET' | 'POST' | 'PUT' | 'DELETE',
56
37
  url: string,
57
38
  data?: any,
58
39
  fileData?: any
59
40
  ): AxiosPromise<Response<T>> {
60
-
61
- let headers : { [key: string]: string } = {
41
+ let headers: { [key: string]: string } = {
62
42
  'Content-Type': 'application/json',
63
43
  };
64
44
 
65
- if(this.authToken) {
66
- headers['Authorization'] = `Bearer ${this.authToken}`;
67
- }
45
+ if (Requests.authToken) {
46
+ headers['Authorization'] = `Bearer ${Requests.authToken}`;
47
+ }
68
48
 
69
49
  if (fileData) {
70
50
  headers['Content-Type'] = 'multipart/form-data';
71
51
  }
72
52
 
73
- //Remove double slashes
74
53
  url = url.replace(/\/\//g, '/');
75
54
 
76
- const uri = `${this.baseUrl}${url}`;
55
+ const uri = `${Requests.baseUrl}${url}`;
77
56
 
78
57
  const validUri = uri.replace(/\/\//g, '/');
79
58
 
80
- const axiosPromise = axios({
59
+ const axiosPromise = Requests.axiosInstance({
81
60
  method,
82
61
  url: uri,
83
- baseURL: this.baseUrl,
84
62
  data: fileData || data,
85
63
  headers,
86
64
  });
@@ -88,16 +66,8 @@ class Requests {
88
66
  return axiosPromise;
89
67
  }
90
68
 
91
- /**
92
- * Calls a GET request to the url endpoint.
93
- *
94
- * @param url
95
- * @returns
96
- */
97
69
  public static get<T>(url: string, params?: Record<string, any>): AxiosPromise<Response<T>> {
98
-
99
70
  if (params && Object.keys(params).length > 0) {
100
-
101
71
  const queryString = Object.entries(params)
102
72
  .map(([key, value]) => {
103
73
  if (Array.isArray(value)) {
@@ -108,19 +78,16 @@ class Requests {
108
78
  .join('&');
109
79
  url = `${url}?${queryString}`;
110
80
  }
111
-
112
- if (this.community_id) {
113
- // Check if the URL already contains query parameters
81
+
82
+ if (Requests.community_id) {
114
83
  const separator = url.includes('?') ? '&' : '?';
115
- // Append the community_id query parameter
116
- url = `${url}${separator}community_id=${this.community_id}`;
84
+ url = `${url}${separator}community_id=${Requests.community_id}`;
117
85
  }
118
86
 
119
- return this.request<T>('GET', url);
87
+ return Requests.request<T>('GET', url);
120
88
  }
121
89
 
122
90
  public static post<T>(url: string, data: any, params?: Record<string, any>): AxiosPromise<Response<T>> {
123
-
124
91
  if (params && Object.keys(params).length > 0) {
125
92
  const queryString = Object.entries(params)
126
93
  .map(([key, value]) => `${key}=${encodeURIComponent(value)}`)
@@ -128,19 +95,17 @@ class Requests {
128
95
  url = `${url}?${queryString}`;
129
96
  }
130
97
 
131
- if (this.community_id) {
132
- // Add the community_id to the request body
98
+ if (Requests.community_id) {
133
99
  data = {
134
100
  ...data,
135
- communities: [ this.community_id ]
101
+ communities: [Requests.community_id],
136
102
  };
137
103
  }
138
104
 
139
- return this.request<T>('POST', url, data);
105
+ return Requests.request<T>('POST', url, data);
140
106
  }
141
107
 
142
108
  public static put<T>(url: string, data: any, params?: Record<string, any>): AxiosPromise<Response<T>> {
143
-
144
109
  if (params && Object.keys(params).length > 0) {
145
110
  const queryString = Object.entries(params)
146
111
  .map(([key, value]) => `${key}=${encodeURIComponent(value)}`)
@@ -148,19 +113,17 @@ class Requests {
148
113
  url = `${url}?${queryString}`;
149
114
  }
150
115
 
151
- if (this.community_id) {
152
- // Add the community_id to the request body
116
+ if (Requests.community_id) {
153
117
  data = {
154
118
  ...data,
155
- community_id: this.community_id
119
+ community_id: Requests.community_id,
156
120
  };
157
121
  }
158
122
 
159
- return this.request<T>('PUT', url, data);
123
+ return Requests.request<T>('PUT', url, data);
160
124
  }
161
125
 
162
126
  public static delete<T>(url: string, params?: Record<string, any>): AxiosPromise<Response<T>> {
163
-
164
127
  if (params && Object.keys(params).length > 0) {
165
128
  const queryString = Object.entries(params)
166
129
  .map(([key, value]) => `${key}=${encodeURIComponent(value)}`)
@@ -168,40 +131,36 @@ class Requests {
168
131
  url = `${url}?${queryString}`;
169
132
  }
170
133
 
171
- if (this.community_id) {
172
- // Check if the URL already contains query parameters
134
+ if (Requests.community_id) {
173
135
  const separator = url.includes('?') ? '&' : '?';
174
- // Append the community_id query parameter
175
- url = `${url}${separator}community_id=${this.community_id}`;
136
+ url = `${url}${separator}community_id=${Requests.community_id}`;
176
137
  }
177
138
 
178
- return this.request<T>('DELETE', url);
139
+ return Requests.request<T>('DELETE', url);
179
140
  }
180
141
 
181
142
  public static uploadFile<T>(
182
143
  url: string,
183
- filename : string,
144
+ filename: string,
184
145
  file: File,
185
146
  data?: any,
186
147
  params?: Record<string, any>
187
148
  ): AxiosPromise<Response<T>> {
188
-
189
149
  if (params && Object.keys(params).length > 0) {
190
150
  const queryString = Object.entries(params)
191
151
  .map(([key, value]) => `${key}=${encodeURIComponent(value)}`)
192
152
  .join('&');
193
153
  url = `${url}?${queryString}`;
194
154
  }
195
-
155
+
196
156
  const formData = new FormData();
197
157
 
198
158
  formData.append(filename, file);
199
159
 
200
- if (this.community_id) {
201
- // Add the community_id to the request body
160
+ if (Requests.community_id) {
202
161
  data = {
203
162
  ...data,
204
- communities: [ this.community_id ]
163
+ communities: [Requests.community_id],
205
164
  };
206
165
  }
207
166
 
@@ -209,17 +168,16 @@ class Requests {
209
168
  formData.append(key, data[key]);
210
169
  }
211
170
 
212
- return this.request<T>('POST', url, data, formData);
171
+ return Requests.request<T>('POST', url, data, formData);
213
172
  }
214
173
 
215
174
  public static uploadBlob<T>(
216
175
  url: string,
217
- filename : string,
176
+ filename: string,
218
177
  blob: Blob,
219
178
  data?: any,
220
179
  params?: Record<string, any>
221
180
  ): AxiosPromise<Response<T>> {
222
-
223
181
  if (params && Object.keys(params).length > 0) {
224
182
  const queryString = Object.entries(params)
225
183
  .map(([key, value]) => `${key}=${encodeURIComponent(value)}`)
@@ -231,61 +189,41 @@ class Requests {
231
189
 
232
190
  formData.append(filename, blob);
233
191
 
234
- if (this.community_id) {
235
- // Add the community_id to the request body
192
+ if (Requests.community_id) {
236
193
  data = {
237
194
  ...data,
238
- communities: [ this.community_id ]
195
+ communities: [Requests.community_id],
239
196
  };
240
197
  }
241
198
 
242
-
243
199
  for (let key in data) {
244
200
  formData.append(key, data[key]);
245
201
  }
246
202
 
247
- return this.request<T>('POST', url, data, formData);
203
+ return Requests.request<T>('POST', url, data, formData);
248
204
  }
249
205
 
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
-
206
+ public static processRoute<T>(route: Route, data?: object, routeReplace?: { [key: string]: any }, params?: Record<string, any>): AxiosPromise<Response<T>> {
260
207
  let url = route.url;
261
208
 
262
- if(routeReplace) {
263
-
209
+ if (routeReplace) {
264
210
  for (let key in routeReplace) {
265
211
  url = url.replace("{" + key + "}", routeReplace[key]);
266
212
  }
267
-
268
213
  }
269
214
 
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);
215
+ if (route.method == HTTP_METHODS.GET) {
216
+ return Requests.get(url, params);
217
+ } else if (route.method == HTTP_METHODS.POST) {
218
+ return Requests.post(url, data, params);
219
+ } else if (route.method == HTTP_METHODS.PUT) {
220
+ return Requests.put(url, data, params);
221
+ } else if (route.method == HTTP_METHODS.DELETE) {
222
+ return Requests.delete(url, params);
278
223
  }
279
224
 
280
- return this.get(url);
281
-
225
+ return Requests.get(url);
282
226
  }
283
-
284
227
  }
285
228
 
286
229
  export default Requests;
287
-
288
-
289
-
290
-
291
-