glitch-javascript-sdk 0.2.5 → 0.2.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.
@@ -15,6 +15,9 @@ class Requests {
15
15
  //The Json Web Token to send in the header
16
16
  private static authToken = "";
17
17
 
18
+ //The ID of the community that will be added to request
19
+ private static community_id? = "";
20
+
18
21
  constructor(config: Config) {
19
22
  this.config = config;
20
23
  }
@@ -37,6 +40,16 @@ class Requests {
37
40
  this.authToken = token;
38
41
  }
39
42
 
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;
50
+ }
51
+
52
+
40
53
 
41
54
  private static request<T>(
42
55
  method: 'GET' | 'POST' | 'PUT' | 'DELETE',
@@ -81,19 +94,81 @@ class Requests {
81
94
  * @param url
82
95
  * @returns
83
96
  */
84
- public static get<T>(url: string): AxiosPromise<Response<T>> {
97
+ public static get<T>(url: string, params?: Record<string, any>): AxiosPromise<Response<T>> {
98
+
99
+ if (params && Object.keys(params).length > 0) {
100
+ const queryString = Object.entries(params)
101
+ .map(([key, value]) => `${key}=${encodeURIComponent(value)}`)
102
+ .join('&');
103
+ url = `${url}?${queryString}`;
104
+ }
105
+
106
+ if (this.community_id) {
107
+ // Check if the URL already contains query parameters
108
+ const separator = url.includes('?') ? '&' : '?';
109
+ // Append the community_id query parameter
110
+ url = `${url}${separator}community_id=${this.community_id}`;
111
+ }
112
+
85
113
  return this.request<T>('GET', url);
86
114
  }
87
115
 
88
- public static post<T>(url: string, data: any): AxiosPromise<Response<T>> {
116
+ public static post<T>(url: string, data: any, params?: Record<string, any>): AxiosPromise<Response<T>> {
117
+
118
+ if (params && Object.keys(params).length > 0) {
119
+ const queryString = Object.entries(params)
120
+ .map(([key, value]) => `${key}=${encodeURIComponent(value)}`)
121
+ .join('&');
122
+ url = `${url}?${queryString}`;
123
+ }
124
+
125
+ if (this.community_id) {
126
+ // Add the community_id to the request body
127
+ data = {
128
+ ...data,
129
+ communities: [ this.community_id ]
130
+ };
131
+ }
132
+
89
133
  return this.request<T>('POST', url, data);
90
134
  }
91
135
 
92
- public static put<T>(url: string, data: any): AxiosPromise<Response<T>> {
136
+ public static put<T>(url: string, data: any, params?: Record<string, any>): AxiosPromise<Response<T>> {
137
+
138
+ if (params && Object.keys(params).length > 0) {
139
+ const queryString = Object.entries(params)
140
+ .map(([key, value]) => `${key}=${encodeURIComponent(value)}`)
141
+ .join('&');
142
+ url = `${url}?${queryString}`;
143
+ }
144
+
145
+ if (this.community_id) {
146
+ // Add the community_id to the request body
147
+ data = {
148
+ ...data,
149
+ community_id: this.community_id
150
+ };
151
+ }
152
+
93
153
  return this.request<T>('PUT', url, data);
94
154
  }
95
155
 
96
- public static delete<T>(url: string): AxiosPromise<Response<T>> {
156
+ public static delete<T>(url: string, params?: Record<string, any>): AxiosPromise<Response<T>> {
157
+
158
+ if (params && Object.keys(params).length > 0) {
159
+ const queryString = Object.entries(params)
160
+ .map(([key, value]) => `${key}=${encodeURIComponent(value)}`)
161
+ .join('&');
162
+ url = `${url}?${queryString}`;
163
+ }
164
+
165
+ if (this.community_id) {
166
+ // Check if the URL already contains query parameters
167
+ const separator = url.includes('?') ? '&' : '?';
168
+ // Append the community_id query parameter
169
+ url = `${url}${separator}community_id=${this.community_id}`;
170
+ }
171
+
97
172
  return this.request<T>('DELETE', url);
98
173
  }
99
174
 
@@ -101,8 +176,16 @@ class Requests {
101
176
  url: string,
102
177
  filename : string,
103
178
  file: File,
104
- data?: any
179
+ data?: any,
180
+ params?: Record<string, any>
105
181
  ): AxiosPromise<Response<T>> {
182
+
183
+ if (params && Object.keys(params).length > 0) {
184
+ const queryString = Object.entries(params)
185
+ .map(([key, value]) => `${key}=${encodeURIComponent(value)}`)
186
+ .join('&');
187
+ url = `${url}?${queryString}`;
188
+ }
106
189
 
107
190
  const formData = new FormData();
108
191
 
@@ -119,9 +202,17 @@ class Requests {
119
202
  url: string,
120
203
  filename : string,
121
204
  blob: Blob,
122
- data?: any
205
+ data?: any,
206
+ params?: Record<string, any>
123
207
  ): AxiosPromise<Response<T>> {
124
208
 
209
+ if (params && Object.keys(params).length > 0) {
210
+ const queryString = Object.entries(params)
211
+ .map(([key, value]) => `${key}=${encodeURIComponent(value)}`)
212
+ .join('&');
213
+ url = `${url}?${queryString}`;
214
+ }
215
+
125
216
  const formData = new FormData();
126
217
 
127
218
  formData.append(filename, blob);
@@ -141,7 +232,7 @@ class Requests {
141
232
  * @param data
142
233
  * @returns
143
234
  */
144
- public static processRoute<T>(route : Route, data? : object, routeReplace? : {[key: string]: any}) : AxiosPromise<Response<T>> {
235
+ public static processRoute<T>(route : Route, data? : object, routeReplace? : {[key: string]: any}, params?: Record<string, any>) : AxiosPromise<Response<T>> {
145
236
 
146
237
  let url = route.url;
147
238
 
@@ -154,13 +245,13 @@ class Requests {
154
245
  }
155
246
 
156
247
  if(route.method == HTTP_METHODS.GET) {
157
- return this.get(url);
248
+ return this.get(url, params);
158
249
  } else if(route.method == HTTP_METHODS.POST) {
159
- return this.post(url, data);
250
+ return this.post(url, data, params);
160
251
  } else if(route.method == HTTP_METHODS.PUT) {
161
- return this.put(url, data);
252
+ return this.put(url, data, params);
162
253
  } else if(route.method == HTTP_METHODS.DELETE) {
163
- return this.delete(url);
254
+ return this.delete(url, params);
164
255
  }
165
256
 
166
257
  return this.get(url);