superso-js-sdk 1.0.1

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.
@@ -0,0 +1,260 @@
1
+ // ======================================
2
+ // REQUEST
3
+ // ======================================
4
+ async function request(client, endpoint, method = "GET", body, token, isFormData = false) {
5
+ const headers = {
6
+ "X-Superso-Project-Key": client.apiKey,
7
+ };
8
+ if (!isFormData) {
9
+ headers["Content-Type"] =
10
+ "application/json";
11
+ }
12
+ if (token) {
13
+ headers["Authorization"] =
14
+ `Bearer ${token}`;
15
+ }
16
+ const response = await fetch(`${client.baseUrl}/api/project/${client.projectId}${endpoint}`, {
17
+ method,
18
+ headers,
19
+ body,
20
+ });
21
+ let data = {};
22
+ try {
23
+ data =
24
+ await response.json();
25
+ }
26
+ catch (_a) {
27
+ data = {};
28
+ }
29
+ if (!response.ok) {
30
+ throw new Error((data === null || data === void 0 ? void 0 : data.message) ||
31
+ `Storage request failed (${response.status})`);
32
+ }
33
+ return data;
34
+ }
35
+ // ======================================
36
+ // UPLOAD FILE
37
+ // ======================================
38
+ export async function uploadFile(client, file, options) {
39
+ validateFile(file);
40
+ return new Promise((resolve, reject) => {
41
+ const xhr = new XMLHttpRequest();
42
+ const formData = new FormData();
43
+ formData.append("file", file);
44
+ if (options === null || options === void 0 ? void 0 : options.path) {
45
+ formData.append("path", options.path);
46
+ }
47
+ if (options === null || options === void 0 ? void 0 : options.alt_text) {
48
+ formData.append("alt_text", options.alt_text);
49
+ }
50
+ if (options === null || options === void 0 ? void 0 : options.caption) {
51
+ formData.append("caption", options.caption);
52
+ }
53
+ xhr.open("POST", `${client.baseUrl}/api/project/${client.projectId}/storage/upload`);
54
+ xhr.setRequestHeader("X-Superso-Project-Key", client.apiKey);
55
+ if (options === null || options === void 0 ? void 0 : options.token) {
56
+ xhr.setRequestHeader("Authorization", `Bearer ${options.token}`);
57
+ }
58
+ // progress
59
+ xhr.upload.onprogress =
60
+ (event) => {
61
+ if (event.lengthComputable &&
62
+ (options === null || options === void 0 ? void 0 : options.onProgress)) {
63
+ const percent = Math.round((event.loaded /
64
+ event.total) * 100);
65
+ options.onProgress(percent);
66
+ }
67
+ };
68
+ xhr.onload =
69
+ () => {
70
+ try {
71
+ const data = JSON.parse(xhr.responseText);
72
+ if (xhr.status >= 200 &&
73
+ xhr.status < 300) {
74
+ resolve(data);
75
+ }
76
+ else {
77
+ reject(new Error((data === null || data === void 0 ? void 0 : data.message) ||
78
+ "Upload failed"));
79
+ }
80
+ }
81
+ catch (error) {
82
+ reject(error);
83
+ }
84
+ };
85
+ xhr.onerror =
86
+ () => {
87
+ reject(new Error("Network error"));
88
+ };
89
+ xhr.send(formData);
90
+ });
91
+ }
92
+ // ======================================
93
+ // BATCH UPLOAD
94
+ // ======================================
95
+ export async function uploadFiles(client, files, options) {
96
+ return Promise.all(files.map((file) => uploadFile(client, file, options)));
97
+ }
98
+ // ======================================
99
+ // LIST ASSETS
100
+ // ======================================
101
+ export async function listAssets(client, token) {
102
+ return request(client, "/storage/assets", "GET", undefined, token);
103
+ }
104
+ // ======================================
105
+ // LIST FILES ALIAS
106
+ // ======================================
107
+ export async function listFiles(client, token) {
108
+ return request(client, "/storage/files", "GET", undefined, token);
109
+ }
110
+ // ======================================
111
+ // GET ASSET
112
+ // ======================================
113
+ export async function getAsset(client, assetId, token) {
114
+ return request(client, `/storage/assets/${assetId}`, "GET", undefined, token);
115
+ }
116
+ // ======================================
117
+ // DELETE ASSET
118
+ // ======================================
119
+ export async function deleteAsset(client, assetId, token) {
120
+ return request(client, `/storage/assets/${assetId}`, "DELETE", undefined, token);
121
+ }
122
+ // ======================================
123
+ // DELETE FILE ALIAS
124
+ // ======================================
125
+ export async function deleteFile(client, assetId, token) {
126
+ return request(client, `/storage/files/${assetId}`, "DELETE", undefined, token);
127
+ }
128
+ // ======================================
129
+ // REMOVE FILE ROOT ALIAS
130
+ // ======================================
131
+ export async function removeFile(client, assetId, token) {
132
+ return request(client, `/storage/${assetId}`, "DELETE", undefined, token);
133
+ }
134
+ // ======================================
135
+ // STORAGE USAGE
136
+ // ======================================
137
+ export async function getStorageUsage(client, token) {
138
+ return request(client, "/storage/usage", "GET", undefined, token);
139
+ }
140
+ // ======================================
141
+ // FILE HELPERS
142
+ // ======================================
143
+ export function getFileExtension(fileName) {
144
+ var _a;
145
+ return (_a = fileName
146
+ .split(".")
147
+ .pop()) === null || _a === void 0 ? void 0 : _a.toLowerCase();
148
+ }
149
+ export function formatBytes(bytes, decimals = 2) {
150
+ if (bytes === 0) {
151
+ return "0 Bytes";
152
+ }
153
+ const k = 1024;
154
+ const dm = decimals < 0
155
+ ? 0
156
+ : decimals;
157
+ const sizes = [
158
+ "Bytes",
159
+ "KB",
160
+ "MB",
161
+ "GB",
162
+ "TB",
163
+ ];
164
+ const i = Math.floor(Math.log(bytes) /
165
+ Math.log(k));
166
+ return (parseFloat((bytes /
167
+ Math.pow(k, i)).toFixed(dm)) +
168
+ " " +
169
+ sizes[i]);
170
+ }
171
+ // ======================================
172
+ // MIME HELPERS
173
+ // ======================================
174
+ export function isImage(mimeType) {
175
+ return mimeType.startsWith("image/");
176
+ }
177
+ export function isVideo(mimeType) {
178
+ return mimeType.startsWith("video/");
179
+ }
180
+ export function isAudio(mimeType) {
181
+ return mimeType.startsWith("audio/");
182
+ }
183
+ export function isPDF(mimeType) {
184
+ return mimeType ===
185
+ "application/pdf";
186
+ }
187
+ export function isDocument(mimeType) {
188
+ return (mimeType.includes("document") ||
189
+ mimeType.includes("word") ||
190
+ mimeType.includes("sheet") ||
191
+ mimeType.includes("excel") ||
192
+ mimeType.includes("presentation") ||
193
+ mimeType.includes("powerpoint"));
194
+ }
195
+ // ======================================
196
+ // FILE VALIDATION
197
+ // ======================================
198
+ export function validateFile(file) {
199
+ if (!file) {
200
+ throw new Error("File is required");
201
+ }
202
+ if (file.size <= 0) {
203
+ throw new Error("Invalid file");
204
+ }
205
+ return true;
206
+ }
207
+ // ======================================
208
+ // CLOUDINARY HELPERS
209
+ // ======================================
210
+ export function optimizeImage(url) {
211
+ return url.replace("/upload/", "/upload/q_auto/");
212
+ }
213
+ export function convertToWebP(url) {
214
+ return url.replace("/upload/", "/upload/f_webp/");
215
+ }
216
+ export function resizeImage(url, width, height) {
217
+ const transform = height
218
+ ? `w_${width},h_${height},c_fill`
219
+ : `w_${width}`;
220
+ return url.replace("/upload/", `/upload/${transform}/`);
221
+ }
222
+ export function thumbnail(url, size = 150) {
223
+ return url.replace("/upload/", `/upload/w_${size},h_${size},c_thumb/`);
224
+ }
225
+ export function cropImage(url, width, height) {
226
+ return url.replace("/upload/", `/upload/w_${width},h_${height},c_crop/`);
227
+ }
228
+ export function blurImage(url) {
229
+ return url.replace("/upload/", "/upload/e_blur:300/");
230
+ }
231
+ export function grayscaleImage(url) {
232
+ return url.replace("/upload/", "/upload/e_grayscale/");
233
+ }
234
+ export function autoQuality(url) {
235
+ return url.replace("/upload/", "/upload/q_auto/");
236
+ }
237
+ export function autoFormat(url) {
238
+ return url.replace("/upload/", "/upload/f_auto/");
239
+ }
240
+ // ======================================
241
+ // URL HELPERS
242
+ // ======================================
243
+ export function getSecureUrl(asset) {
244
+ return (asset.secure_url ||
245
+ asset.url);
246
+ }
247
+ export function getPublicUrl(asset) {
248
+ return asset.url;
249
+ }
250
+ // ======================================
251
+ // PRIVATE FILE READY HELPERS
252
+ // ======================================
253
+ export function buildAuthenticatedUrl(cloudName, publicId) {
254
+ return;
255
+ `https://res.cloudinary.com/${cloudName}/image/authenticated/${publicId}`;
256
+ }
257
+ export function buildPrivateUrl(cloudName, publicId) {
258
+ return;
259
+ `https://res.cloudinary.com/${cloudName}/image/private/${publicId}`;
260
+ }
package/package.json ADDED
@@ -0,0 +1,56 @@
1
+ {
2
+ "name": "superso-js-sdk",
3
+ "version": "1.0.1",
4
+ "description": "Official Superso JavaScript SDK",
5
+
6
+ "type": "module",
7
+
8
+ "main": "dist/index.js",
9
+
10
+ "types": "dist/index.d.ts",
11
+
12
+ "module": "dist/index.js",
13
+
14
+ "files": [
15
+ "dist"
16
+ ],
17
+
18
+ "scripts": {
19
+ "build": "tsc"
20
+ },
21
+
22
+ "keywords": [
23
+ "superso",
24
+ "sdk",
25
+ "backend",
26
+ "database",
27
+ "auth",
28
+ "realtime",
29
+ "storage",
30
+ "notifications",
31
+ "ai"
32
+ ],
33
+
34
+ "author": "Abdullahi",
35
+
36
+ "license": "MIT",
37
+
38
+ "repository": {
39
+ "type": "git",
40
+ "url": "https://github.com/superso/sdk"
41
+ },
42
+
43
+ "homepage": "https://superso.com",
44
+
45
+ "bugs": {
46
+ "url": "https://github.com/superso/sdk/issues"
47
+ },
48
+
49
+ "publishConfig": {
50
+ "access": "public"
51
+ },
52
+
53
+ "devDependencies": {
54
+ "typescript": "^6.0.3"
55
+ }
56
+ }