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