glitch-javascript-sdk 1.4.0 → 1.4.2
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 +74 -8
- package/dist/cjs/index.js.map +1 -1
- package/dist/esm/api/Media.d.ts +3 -3
- package/dist/esm/api/Scheduler.d.ts +11 -0
- package/dist/esm/api/Titles.d.ts +8 -0
- package/dist/esm/index.js +74 -8
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/util/Requests.d.ts +3 -3
- package/dist/index.d.ts +24 -5
- package/package.json +1 -1
- package/src/api/Media.ts +5 -5
- package/src/api/Scheduler.ts +14 -0
- package/src/api/Titles.ts +14 -0
- package/src/routes/SchedulerRoute.ts +2 -0
- package/src/routes/TitlesRoute.ts +2 -0
- package/src/util/Requests.ts +64 -19
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AxiosPromise } from 'axios';
|
|
1
|
+
import { AxiosPromise, AxiosProgressEvent } from 'axios';
|
|
2
2
|
import Config from '../config/Config';
|
|
3
3
|
import Route from '../routes/interface';
|
|
4
4
|
import Response from './Response';
|
|
@@ -16,8 +16,8 @@ declare class Requests {
|
|
|
16
16
|
static post<T>(url: string, data: any, params?: Record<string, any>): AxiosPromise<Response<T>>;
|
|
17
17
|
static put<T>(url: string, data: any, params?: Record<string, any>): AxiosPromise<Response<T>>;
|
|
18
18
|
static delete<T>(url: string, params?: Record<string, any>): AxiosPromise<Response<T>>;
|
|
19
|
-
static uploadFile<T>(url: string, filename: string, file: File | Blob, data?: any, params?: Record<string, any
|
|
20
|
-
static uploadBlob<T>(url: string, filename: string, blob: Blob, data?: any, params?: Record<string, any
|
|
19
|
+
static uploadFile<T>(url: string, filename: string, file: File | Blob, data?: any, params?: Record<string, any>, onUploadProgress?: (progressEvent: AxiosProgressEvent) => void): AxiosPromise<Response<T>>;
|
|
20
|
+
static uploadBlob<T>(url: string, filename: string, blob: Blob, data?: any, params?: Record<string, any>, onUploadProgress?: (progressEvent: AxiosProgressEvent) => void): AxiosPromise<Response<T>>;
|
|
21
21
|
static uploadFileInChunks<T>(file: File, uploadUrl: string, onProgress?: (totalSize: number, amountUploaded: number) => void, data?: any, chunkSize?: number): Promise<void>;
|
|
22
22
|
static processRoute<T>(route: Route, data?: object, routeReplace?: {
|
|
23
23
|
[key: string]: any;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AxiosPromise } from 'axios';
|
|
1
|
+
import { AxiosPromise, AxiosProgressEvent } from 'axios';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Config
|
|
@@ -2792,6 +2792,14 @@ declare class Titles {
|
|
|
2792
2792
|
* @returns promise
|
|
2793
2793
|
*/
|
|
2794
2794
|
static uploadBannerImageBlob<T>(title_id: string, blob: Blob, data?: object, params?: Record<string, any>): AxiosPromise<Response<T>>;
|
|
2795
|
+
/**
|
|
2796
|
+
* Add media to a title.
|
|
2797
|
+
*/
|
|
2798
|
+
static addMedia<T>(title_id: string, data?: object, params?: Record<string, any>): AxiosPromise<Response<T>>;
|
|
2799
|
+
/**
|
|
2800
|
+
* Remove media from a title.
|
|
2801
|
+
*/
|
|
2802
|
+
static removeMedia<T>(title_id: string, media_id: string, params?: Record<string, any>): AxiosPromise<Response<T>>;
|
|
2795
2803
|
}
|
|
2796
2804
|
|
|
2797
2805
|
declare class Campaigns {
|
|
@@ -3901,7 +3909,7 @@ declare class Media {
|
|
|
3901
3909
|
*
|
|
3902
3910
|
* @returns promise
|
|
3903
3911
|
*/
|
|
3904
|
-
static uploadFile<T>(file: File, data?: object, params?: Record<string, any
|
|
3912
|
+
static uploadFile<T>(file: File, data?: object, params?: Record<string, any>, onUploadProgress?: (progressEvent: AxiosProgressEvent) => void): AxiosPromise<Response<T>>;
|
|
3905
3913
|
/**
|
|
3906
3914
|
* Upload media content using a Blob.
|
|
3907
3915
|
*
|
|
@@ -3912,7 +3920,7 @@ declare class Media {
|
|
|
3912
3920
|
*
|
|
3913
3921
|
* @returns promise
|
|
3914
3922
|
*/
|
|
3915
|
-
static uploadBlob<T>(blob: Blob, data?: object, params?: Record<string, any
|
|
3923
|
+
static uploadBlob<T>(blob: Blob, data?: object, params?: Record<string, any>, onUploadProgress?: (progressEvent: AxiosProgressEvent) => void): AxiosPromise<Response<T>>;
|
|
3916
3924
|
/**
|
|
3917
3925
|
* Get media details.
|
|
3918
3926
|
*
|
|
@@ -3975,6 +3983,17 @@ declare class Scheduler {
|
|
|
3975
3983
|
* @returns promise
|
|
3976
3984
|
*/
|
|
3977
3985
|
static deleteSchedule<T>(scheduler_id: string, params?: Record<string, any>): AxiosPromise<Response<T>>;
|
|
3986
|
+
/**
|
|
3987
|
+
* Test the tone of the scheduler.
|
|
3988
|
+
*
|
|
3989
|
+
* @see https://api.glitch.fun/api/documentation#/Scheduler/updateTitlePromotionSchedule
|
|
3990
|
+
*
|
|
3991
|
+
* @param scheduler_id The ID of the promotion schedule.
|
|
3992
|
+
* @param data The data to update.
|
|
3993
|
+
*
|
|
3994
|
+
* @returns promise
|
|
3995
|
+
*/
|
|
3996
|
+
static testTone<T>(scheduler_id: string, data: object, params?: Record<string, any>): AxiosPromise<Response<T>>;
|
|
3978
3997
|
/**
|
|
3979
3998
|
* Get social media posts related to a promotion schedule.
|
|
3980
3999
|
*
|
|
@@ -4259,8 +4278,8 @@ declare class Requests {
|
|
|
4259
4278
|
static post<T>(url: string, data: any, params?: Record<string, any>): AxiosPromise<Response<T>>;
|
|
4260
4279
|
static put<T>(url: string, data: any, params?: Record<string, any>): AxiosPromise<Response<T>>;
|
|
4261
4280
|
static delete<T>(url: string, params?: Record<string, any>): AxiosPromise<Response<T>>;
|
|
4262
|
-
static uploadFile<T>(url: string, filename: string, file: File | Blob, data?: any, params?: Record<string, any
|
|
4263
|
-
static uploadBlob<T>(url: string, filename: string, blob: Blob, data?: any, params?: Record<string, any
|
|
4281
|
+
static uploadFile<T>(url: string, filename: string, file: File | Blob, data?: any, params?: Record<string, any>, onUploadProgress?: (progressEvent: AxiosProgressEvent) => void): AxiosPromise<Response<T>>;
|
|
4282
|
+
static uploadBlob<T>(url: string, filename: string, blob: Blob, data?: any, params?: Record<string, any>, onUploadProgress?: (progressEvent: AxiosProgressEvent) => void): AxiosPromise<Response<T>>;
|
|
4264
4283
|
static uploadFileInChunks<T>(file: File, uploadUrl: string, onProgress?: (totalSize: number, amountUploaded: number) => void, data?: any, chunkSize?: number): Promise<void>;
|
|
4265
4284
|
static processRoute<T>(route: Route, data?: object, routeReplace?: {
|
|
4266
4285
|
[key: string]: any;
|
package/package.json
CHANGED
package/src/api/Media.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import MediaRoute from "../routes/MediaRoute";
|
|
2
2
|
import Requests from "../util/Requests";
|
|
3
3
|
import Response from "../util/Response";
|
|
4
|
-
import { AxiosPromise } from "axios";
|
|
4
|
+
import { AxiosProgressEvent, AxiosPromise } from "axios";
|
|
5
5
|
|
|
6
6
|
class Media {
|
|
7
7
|
/**
|
|
@@ -14,8 +14,8 @@ class Media {
|
|
|
14
14
|
*
|
|
15
15
|
* @returns promise
|
|
16
16
|
*/
|
|
17
|
-
public static uploadFile<T>(file: File, data?: object, params?: Record<string, any
|
|
18
|
-
return Requests.uploadFile(MediaRoute.routes.upload.url, 'media', file, data, params);
|
|
17
|
+
public static uploadFile<T>(file: File, data?: object, params?: Record<string, any>, onUploadProgress?: (progressEvent: AxiosProgressEvent) => void): AxiosPromise<Response<T>> {
|
|
18
|
+
return Requests.uploadFile(MediaRoute.routes.upload.url, 'media', file, data, params, onUploadProgress);
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
/**
|
|
@@ -28,8 +28,8 @@ class Media {
|
|
|
28
28
|
*
|
|
29
29
|
* @returns promise
|
|
30
30
|
*/
|
|
31
|
-
public static uploadBlob<T>(blob: Blob, data?: object, params?: Record<string, any
|
|
32
|
-
return Requests.uploadBlob(MediaRoute.routes.upload.url, 'media', blob, data, params);
|
|
31
|
+
public static uploadBlob<T>(blob: Blob, data?: object, params?: Record<string, any>, onUploadProgress?: (progressEvent: AxiosProgressEvent) => void): AxiosPromise<Response<T>> {
|
|
32
|
+
return Requests.uploadBlob(MediaRoute.routes.upload.url, 'media', blob, data, params, onUploadProgress);
|
|
33
33
|
}
|
|
34
34
|
|
|
35
35
|
/**
|
package/src/api/Scheduler.ts
CHANGED
|
@@ -68,6 +68,20 @@ class Scheduler {
|
|
|
68
68
|
return Requests.processRoute(SchedulerRoute.routes.deleteSchedule, {}, { scheduler_id }, params);
|
|
69
69
|
}
|
|
70
70
|
|
|
71
|
+
/**
|
|
72
|
+
* Test the tone of the scheduler.
|
|
73
|
+
*
|
|
74
|
+
* @see https://api.glitch.fun/api/documentation#/Scheduler/updateTitlePromotionSchedule
|
|
75
|
+
*
|
|
76
|
+
* @param scheduler_id The ID of the promotion schedule.
|
|
77
|
+
* @param data The data to update.
|
|
78
|
+
*
|
|
79
|
+
* @returns promise
|
|
80
|
+
*/
|
|
81
|
+
public static testTone<T>(scheduler_id: string, data: object, params?: Record<string, any>): AxiosPromise<Response<T>> {
|
|
82
|
+
return Requests.processRoute(SchedulerRoute.routes.testTone, data, { scheduler_id }, params);
|
|
83
|
+
}
|
|
84
|
+
|
|
71
85
|
/**
|
|
72
86
|
* Get social media posts related to a promotion schedule.
|
|
73
87
|
*
|
package/src/api/Titles.ts
CHANGED
|
@@ -196,6 +196,20 @@ class Titles {
|
|
|
196
196
|
return Requests.uploadBlob(url, 'image', blob, data);
|
|
197
197
|
}
|
|
198
198
|
|
|
199
|
+
/**
|
|
200
|
+
* Add media to a title.
|
|
201
|
+
*/
|
|
202
|
+
public static addMedia<T>(title_id: string, data?: object, params?: Record<string, any>): AxiosPromise<Response<T>> {
|
|
203
|
+
return Requests.processRoute(TitlesRoute.routes.addMedia, data, { title_id: title_id }, params);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Remove media from a title.
|
|
208
|
+
*/
|
|
209
|
+
public static removeMedia<T>(title_id: string, media_id: string, params?: Record<string, any>): AxiosPromise<Response<T>> {
|
|
210
|
+
return Requests.processRoute(TitlesRoute.routes.removeMedia, {}, { title_id: title_id, media_id: media_id }, params);
|
|
211
|
+
}
|
|
212
|
+
|
|
199
213
|
}
|
|
200
214
|
|
|
201
215
|
export default Titles;
|
|
@@ -18,6 +18,8 @@ class SchedulerRoute {
|
|
|
18
18
|
updateUpdate: { url: '/schedulers/{scheduler_id}/updates/{update_id}', method: HTTP_METHODS.PUT },
|
|
19
19
|
deleteUpdate: { url: '/schedulers/{scheduler_id}/updates/{update_id}', method: HTTP_METHODS.DELETE },
|
|
20
20
|
|
|
21
|
+
testTone: { url: '/schedulers/{scheduler_id}/tone', method: HTTP_METHODS.POST },
|
|
22
|
+
|
|
21
23
|
// Clear OAuth Routes
|
|
22
24
|
clearTwitterAuth: { url: '/schedulers/{scheduler_id}/clearTwitterAuth', method: HTTP_METHODS.DELETE },
|
|
23
25
|
clearFacebookAuth: { url: '/schedulers/{scheduler_id}/clearFacebookAuth', method: HTTP_METHODS.DELETE },
|
|
@@ -15,6 +15,8 @@ class TitlesRoute {
|
|
|
15
15
|
uploadBannerImage : {url : '/titles/{title_id}/uploadBannerImage', method: HTTP_METHODS.POST},
|
|
16
16
|
addAdministrator : { url: '/titles/{title_id}/addAdministrator', method: HTTP_METHODS.POST },
|
|
17
17
|
removeAdministrator : { url: '/titles/{title_id}/removeAdministrator/{user_id}', method: HTTP_METHODS.DELETE },
|
|
18
|
+
addMedia: { url: '/titles/{title_id}/addMedia', method: HTTP_METHODS.POST },
|
|
19
|
+
removeMedia: { url: '/titles/{title_id}/removeMedia/{media_id}', method: HTTP_METHODS.DELETE },
|
|
18
20
|
};
|
|
19
21
|
|
|
20
22
|
}
|
package/src/util/Requests.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import axios, { AxiosPromise } from 'axios';
|
|
1
|
+
import axios, { AxiosPromise, AxiosRequestConfig, AxiosProgressEvent } from 'axios';
|
|
2
2
|
import Config from '../config/Config';
|
|
3
3
|
import HTTP_METHODS from '../constants/HttpMethods';
|
|
4
4
|
import Route from '../routes/interface';
|
|
@@ -140,8 +140,10 @@ class Requests {
|
|
|
140
140
|
filename: string,
|
|
141
141
|
file: File | Blob,
|
|
142
142
|
data?: any,
|
|
143
|
-
params?: Record<string, any
|
|
143
|
+
params?: Record<string, any>,
|
|
144
|
+
onUploadProgress?: (progressEvent: AxiosProgressEvent) => void
|
|
144
145
|
): AxiosPromise<Response<T>> {
|
|
146
|
+
// Process URL and params
|
|
145
147
|
if (params && Object.keys(params).length > 0) {
|
|
146
148
|
const queryString = Object.entries(params)
|
|
147
149
|
.map(([key, value]) => `${key}=${encodeURIComponent(value)}`)
|
|
@@ -149,8 +151,8 @@ class Requests {
|
|
|
149
151
|
url = `${url}?${queryString}`;
|
|
150
152
|
}
|
|
151
153
|
|
|
154
|
+
// Prepare FormData
|
|
152
155
|
const formData = new FormData();
|
|
153
|
-
|
|
154
156
|
formData.append(filename, file);
|
|
155
157
|
|
|
156
158
|
if (Requests.community_id) {
|
|
@@ -164,7 +166,27 @@ class Requests {
|
|
|
164
166
|
formData.append(key, data[key]);
|
|
165
167
|
}
|
|
166
168
|
|
|
167
|
-
|
|
169
|
+
// Prepare headers
|
|
170
|
+
let headers: { [key: string]: string } = {
|
|
171
|
+
'Content-Type': 'multipart/form-data',
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
if (Requests.authToken) {
|
|
175
|
+
headers['Authorization'] = `Bearer ${Requests.authToken}`;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// Format URL
|
|
179
|
+
url = url.replace(/\/\//g, '/');
|
|
180
|
+
const uri = `${Requests.baseUrl}${url}`.replace(/\/\//g, '/');
|
|
181
|
+
|
|
182
|
+
// Make the request
|
|
183
|
+
return axios({
|
|
184
|
+
method: 'POST',
|
|
185
|
+
url: uri,
|
|
186
|
+
data: formData,
|
|
187
|
+
headers,
|
|
188
|
+
onUploadProgress,
|
|
189
|
+
});
|
|
168
190
|
}
|
|
169
191
|
|
|
170
192
|
public static uploadBlob<T>(
|
|
@@ -172,8 +194,10 @@ class Requests {
|
|
|
172
194
|
filename: string,
|
|
173
195
|
blob: Blob,
|
|
174
196
|
data?: any,
|
|
175
|
-
params?: Record<string, any
|
|
197
|
+
params?: Record<string, any>,
|
|
198
|
+
onUploadProgress?: (progressEvent: AxiosProgressEvent) => void
|
|
176
199
|
): AxiosPromise<Response<T>> {
|
|
200
|
+
// Process URL and params
|
|
177
201
|
if (params && Object.keys(params).length > 0) {
|
|
178
202
|
const queryString = Object.entries(params)
|
|
179
203
|
.map(([key, value]) => `${key}=${encodeURIComponent(value)}`)
|
|
@@ -181,8 +205,8 @@ class Requests {
|
|
|
181
205
|
url = `${url}?${queryString}`;
|
|
182
206
|
}
|
|
183
207
|
|
|
208
|
+
// Prepare FormData
|
|
184
209
|
const formData = new FormData();
|
|
185
|
-
|
|
186
210
|
formData.append(filename, blob);
|
|
187
211
|
|
|
188
212
|
if (Requests.community_id) {
|
|
@@ -196,9 +220,30 @@ class Requests {
|
|
|
196
220
|
formData.append(key, data[key]);
|
|
197
221
|
}
|
|
198
222
|
|
|
199
|
-
|
|
223
|
+
// Prepare headers
|
|
224
|
+
let headers: { [key: string]: string } = {
|
|
225
|
+
'Content-Type': 'multipart/form-data',
|
|
226
|
+
};
|
|
227
|
+
|
|
228
|
+
if (Requests.authToken) {
|
|
229
|
+
headers['Authorization'] = `Bearer ${Requests.authToken}`;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
// Format URL
|
|
233
|
+
url = url.replace(/\/\//g, '/');
|
|
234
|
+
const uri = `${Requests.baseUrl}${url}`.replace(/\/\//g, '/');
|
|
235
|
+
|
|
236
|
+
// Make the request
|
|
237
|
+
return axios({
|
|
238
|
+
method: 'POST',
|
|
239
|
+
url: uri,
|
|
240
|
+
data: formData,
|
|
241
|
+
headers,
|
|
242
|
+
onUploadProgress,
|
|
243
|
+
});
|
|
200
244
|
}
|
|
201
245
|
|
|
246
|
+
|
|
202
247
|
// Method adapted for browser environments
|
|
203
248
|
|
|
204
249
|
public static async uploadFileInChunks<T>(
|
|
@@ -206,27 +251,27 @@ class Requests {
|
|
|
206
251
|
uploadUrl: string,
|
|
207
252
|
onProgress?: (totalSize: number, amountUploaded: number) => void,
|
|
208
253
|
data?: any,
|
|
209
|
-
chunkSize
|
|
254
|
+
chunkSize?: number, // Default chunk size of 1MB
|
|
210
255
|
): Promise<void> {
|
|
211
256
|
|
|
212
|
-
if(!chunkSize) {
|
|
257
|
+
if (!chunkSize) {
|
|
213
258
|
chunkSize = 1024 * 1024
|
|
214
259
|
}
|
|
215
260
|
const fileSize = file.size;
|
|
216
261
|
const totalChunks = Math.ceil(fileSize / chunkSize);
|
|
217
262
|
let currentChunkIndex = 0;
|
|
218
263
|
let totalUploaded = 0; // Keep track of the total uploaded bytes
|
|
219
|
-
|
|
264
|
+
|
|
220
265
|
// Generate a unique identifier for this upload session using the Web Cryptography API
|
|
221
266
|
const array = new Uint32Array(4);
|
|
222
267
|
window.crypto.getRandomValues(array);
|
|
223
268
|
const identifier = Array.from(array, dec => ('0' + dec.toString(16)).substr(-2)).join('');
|
|
224
|
-
|
|
269
|
+
|
|
225
270
|
while (currentChunkIndex <= totalChunks) {
|
|
226
271
|
const start = currentChunkIndex * chunkSize;
|
|
227
272
|
const end = Math.min(start + chunkSize, fileSize);
|
|
228
273
|
const chunk = file.slice(start, end);
|
|
229
|
-
|
|
274
|
+
|
|
230
275
|
const formData = new FormData();
|
|
231
276
|
formData.append('video', chunk, file.name);
|
|
232
277
|
formData.append('chunkIndex', currentChunkIndex.toString());
|
|
@@ -239,30 +284,30 @@ class Requests {
|
|
|
239
284
|
formData.append(key, data[key]);
|
|
240
285
|
}
|
|
241
286
|
}
|
|
242
|
-
|
|
287
|
+
|
|
243
288
|
// Construct the full URL if necessary or use a method to determine the base URL
|
|
244
289
|
const fullUploadUrl = `${Requests.baseUrl}${uploadUrl}`;
|
|
245
|
-
|
|
290
|
+
|
|
246
291
|
// Make sure the authorization token is included if required
|
|
247
292
|
const headers: { [key: string]: string } = {};
|
|
248
293
|
if (Requests.authToken) {
|
|
249
294
|
headers['Authorization'] = `Bearer ${Requests.authToken}`;
|
|
250
295
|
}
|
|
251
|
-
|
|
296
|
+
|
|
252
297
|
// Perform the upload
|
|
253
|
-
await axios.post(fullUploadUrl, formData, {
|
|
298
|
+
await axios.post(fullUploadUrl, formData, {
|
|
254
299
|
headers,
|
|
255
300
|
onUploadProgress: (progressEvent) => {
|
|
256
301
|
const currentChunkProgress = progressEvent.loaded; // Bytes uploaded of the current chunk
|
|
257
302
|
// Calculate the total uploaded size including previous chunks and the current chunk's progress
|
|
258
303
|
const totalProgress = totalUploaded + currentChunkProgress;
|
|
259
304
|
|
|
260
|
-
if(onProgress){
|
|
305
|
+
if (onProgress) {
|
|
261
306
|
onProgress(fileSize, end);
|
|
262
307
|
}
|
|
263
|
-
}
|
|
308
|
+
}
|
|
264
309
|
});
|
|
265
|
-
|
|
310
|
+
|
|
266
311
|
currentChunkIndex++;
|
|
267
312
|
}
|
|
268
313
|
}
|