glitch-javascript-sdk 0.7.8 → 0.7.9
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 +66 -0
- package/dist/cjs/index.js.map +1 -1
- package/dist/esm/api/Social.d.ts +1 -0
- package/dist/esm/index.js +66 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/util/Requests.d.ts +1 -0
- package/dist/index.d.ts +2 -0
- package/package.json +2 -1
- package/src/api/Social.ts +9 -0
- package/src/routes/SocialRoute.ts +1 -0
- package/src/util/Requests.ts +72 -0
|
@@ -18,6 +18,7 @@ declare class Requests {
|
|
|
18
18
|
static delete<T>(url: string, params?: Record<string, any>): AxiosPromise<Response<T>>;
|
|
19
19
|
static uploadFile<T>(url: string, filename: string, file: File | Blob, data?: any, params?: Record<string, any>): AxiosPromise<Response<T>>;
|
|
20
20
|
static uploadBlob<T>(url: string, filename: string, blob: Blob, data?: any, params?: Record<string, any>): AxiosPromise<Response<T>>;
|
|
21
|
+
static uploadFileInChunks<T>(file: File, uploadUrl: string, onProgress?: (totalSize: number, amountUploaded: number) => void, data?: any, chunkSize?: number): Promise<void>;
|
|
21
22
|
static processRoute<T>(route: Route, data?: object, routeReplace?: {
|
|
22
23
|
[key: string]: any;
|
|
23
24
|
}, params?: Record<string, any>): AxiosPromise<Response<T>>;
|
package/dist/index.d.ts
CHANGED
|
@@ -1823,6 +1823,7 @@ declare class Social {
|
|
|
1823
1823
|
static postVideoToTikTokBlob<T>(blob: Blob, data?: object, params?: Record<string, any>): AxiosPromise<Response<T>>;
|
|
1824
1824
|
static postVideoToFacebookGroupFile<T>(file: File, data?: object, params?: Record<string, any>): AxiosPromise<Response<T>>;
|
|
1825
1825
|
static postVideoToFacebookGroupBlob<T>(blob: Blob, data?: object, params?: Record<string, any>): AxiosPromise<Response<T>>;
|
|
1826
|
+
static postVideoToTwitter<T>(file: File, data?: object, onProgress?: (totalSize: number, amountUploaded: number) => void, params?: Record<string, any>): Promise<void>;
|
|
1826
1827
|
}
|
|
1827
1828
|
|
|
1828
1829
|
declare class Templates {
|
|
@@ -2565,6 +2566,7 @@ declare class Requests {
|
|
|
2565
2566
|
static delete<T>(url: string, params?: Record<string, any>): AxiosPromise<Response<T>>;
|
|
2566
2567
|
static uploadFile<T>(url: string, filename: string, file: File | Blob, data?: any, params?: Record<string, any>): AxiosPromise<Response<T>>;
|
|
2567
2568
|
static uploadBlob<T>(url: string, filename: string, blob: Blob, data?: any, params?: Record<string, any>): AxiosPromise<Response<T>>;
|
|
2569
|
+
static uploadFileInChunks<T>(file: File, uploadUrl: string, onProgress?: (totalSize: number, amountUploaded: number) => void, data?: any, chunkSize?: number): Promise<void>;
|
|
2568
2570
|
static processRoute<T>(route: Route, data?: object, routeReplace?: {
|
|
2569
2571
|
[key: string]: any;
|
|
2570
2572
|
}, 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.7.
|
|
3
|
+
"version": "0.7.9",
|
|
4
4
|
"description": "Javascript SDK for Glitch",
|
|
5
5
|
"main": "dist/cjs/index.js",
|
|
6
6
|
"module": "dist/esm/index.js",
|
|
@@ -37,6 +37,7 @@
|
|
|
37
37
|
"dependencies": {
|
|
38
38
|
"axios": "^1.6.2",
|
|
39
39
|
"buffer": "^6.0.3",
|
|
40
|
+
"crypto-js": "^4.2.0",
|
|
40
41
|
"fast-text-encoding": "^1.0.6",
|
|
41
42
|
"fetch-blob": "^4.0.0",
|
|
42
43
|
"isomorphic-form-data": "^2.0.0",
|
package/src/api/Social.ts
CHANGED
|
@@ -41,7 +41,16 @@ class Social {
|
|
|
41
41
|
return Requests.uploadBlob(url, 'video', blob, data);
|
|
42
42
|
}
|
|
43
43
|
|
|
44
|
+
public static postVideoToTwitter<T>(file: File, data? : object, onProgress ?: (totalSize: number, amountUploaded: number) => void, params?: Record<string, any>) : Promise<void> {
|
|
44
45
|
|
|
46
|
+
let url = SocialRoute.routes.postVideoToTwitter.url;
|
|
47
|
+
|
|
48
|
+
return Requests.uploadFileInChunks(file, url, onProgress, data);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
|
|
45
54
|
|
|
46
55
|
}
|
|
47
56
|
|
|
@@ -6,6 +6,7 @@ class SocialRoute {
|
|
|
6
6
|
public static routes: { [key: string]: Route } = {
|
|
7
7
|
postVideoToTikTok: { url: '/social/postVideoToTikTok', method: HTTP_METHODS.POST },
|
|
8
8
|
postVideoToFacebookGroup: { url: '/social/postVideoToFacebookGroup', method: HTTP_METHODS.POST },
|
|
9
|
+
postVideoToTwitter: { url: '/social/postVideoToTwitter', method: HTTP_METHODS.POST },
|
|
9
10
|
};
|
|
10
11
|
|
|
11
12
|
}
|
package/src/util/Requests.ts
CHANGED
|
@@ -3,6 +3,8 @@ 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 * as crypto from 'crypto';
|
|
7
|
+
|
|
6
8
|
|
|
7
9
|
class Requests {
|
|
8
10
|
private static config: Config;
|
|
@@ -197,6 +199,76 @@ class Requests {
|
|
|
197
199
|
return Requests.request<T>('POST', url, data, formData);
|
|
198
200
|
}
|
|
199
201
|
|
|
202
|
+
// Method adapted for browser environments
|
|
203
|
+
|
|
204
|
+
public static async uploadFileInChunks<T>(
|
|
205
|
+
file: File,
|
|
206
|
+
uploadUrl: string,
|
|
207
|
+
onProgress?: (totalSize: number, amountUploaded: number) => void,
|
|
208
|
+
data?: any,
|
|
209
|
+
chunkSize ?: number, // Default chunk size of 1MB
|
|
210
|
+
): Promise<void> {
|
|
211
|
+
|
|
212
|
+
if(!chunkSize) {
|
|
213
|
+
chunkSize = 1024 * 1024
|
|
214
|
+
}
|
|
215
|
+
const fileSize = file.size;
|
|
216
|
+
const totalChunks = Math.ceil(fileSize / chunkSize);
|
|
217
|
+
let currentChunkIndex = 0;
|
|
218
|
+
let totalUploaded = 0; // Keep track of the total uploaded bytes
|
|
219
|
+
|
|
220
|
+
// Generate a unique identifier for this upload session using the Web Cryptography API
|
|
221
|
+
const array = new Uint32Array(4);
|
|
222
|
+
window.crypto.getRandomValues(array);
|
|
223
|
+
const identifier = Array.from(array, dec => ('0' + dec.toString(16)).substr(-2)).join('');
|
|
224
|
+
|
|
225
|
+
while (currentChunkIndex < totalChunks) {
|
|
226
|
+
const start = currentChunkIndex * chunkSize;
|
|
227
|
+
const end = Math.min(start + chunkSize, fileSize);
|
|
228
|
+
const chunk = file.slice(start, end);
|
|
229
|
+
|
|
230
|
+
const formData = new FormData();
|
|
231
|
+
formData.append('video', chunk, file.name);
|
|
232
|
+
formData.append('chunkIndex', currentChunkIndex.toString());
|
|
233
|
+
formData.append('totalChunks', totalChunks.toString());
|
|
234
|
+
formData.append('identifier', identifier);
|
|
235
|
+
|
|
236
|
+
// If there's additional data, append each key-value pair to the formData
|
|
237
|
+
if (data) {
|
|
238
|
+
for (const key in data) {
|
|
239
|
+
formData.append(key, data[key]);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
// Construct the full URL if necessary or use a method to determine the base URL
|
|
244
|
+
const fullUploadUrl = `${Requests.baseUrl}${uploadUrl}`;
|
|
245
|
+
|
|
246
|
+
// Make sure the authorization token is included if required
|
|
247
|
+
const headers: { [key: string]: string } = {};
|
|
248
|
+
if (Requests.authToken) {
|
|
249
|
+
headers['Authorization'] = `Bearer ${Requests.authToken}`;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// Perform the upload
|
|
253
|
+
await axios.post(fullUploadUrl, formData, {
|
|
254
|
+
headers,
|
|
255
|
+
onUploadProgress: (progressEvent) => {
|
|
256
|
+
const currentChunkProgress = progressEvent.loaded; // Bytes uploaded of the current chunk
|
|
257
|
+
// Calculate the total uploaded size including previous chunks and the current chunk's progress
|
|
258
|
+
const totalProgress = totalUploaded + currentChunkProgress;
|
|
259
|
+
|
|
260
|
+
if(onProgress){
|
|
261
|
+
onProgress(fileSize, totalProgress);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
currentChunkIndex++;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
|
|
271
|
+
|
|
200
272
|
public static processRoute<T>(route: Route, data?: object, routeReplace?: { [key: string]: any }, params?: Record<string, any>): AxiosPromise<Response<T>> {
|
|
201
273
|
let url = route.url;
|
|
202
274
|
|