dosyadev 0.1.0
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/README.md +548 -0
- package/dist/index.cjs +992 -0
- package/dist/index.d.cts +711 -0
- package/dist/index.d.ts +711 -0
- package/dist/index.js +986 -0
- package/package.json +63 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,992 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/errors.ts
|
|
4
|
+
var DosyaError = class extends Error {
|
|
5
|
+
constructor(message) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.name = "DosyaError";
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
var DosyaApiError = class extends DosyaError {
|
|
11
|
+
status;
|
|
12
|
+
errorMessage;
|
|
13
|
+
raw;
|
|
14
|
+
constructor(status, errorMessage, raw) {
|
|
15
|
+
super(`[${status}] ${errorMessage}`);
|
|
16
|
+
this.name = "DosyaApiError";
|
|
17
|
+
this.status = status;
|
|
18
|
+
this.errorMessage = errorMessage;
|
|
19
|
+
this.raw = raw;
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
var DosyaNetworkError = class extends DosyaError {
|
|
23
|
+
cause;
|
|
24
|
+
constructor(message, options) {
|
|
25
|
+
super(message);
|
|
26
|
+
this.name = "DosyaNetworkError";
|
|
27
|
+
this.cause = options?.cause;
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
var DosyaUploadError = class extends DosyaError {
|
|
31
|
+
sessionId;
|
|
32
|
+
partNumber;
|
|
33
|
+
constructor(message, sessionId, partNumber) {
|
|
34
|
+
super(message);
|
|
35
|
+
this.name = "DosyaUploadError";
|
|
36
|
+
this.sessionId = sessionId;
|
|
37
|
+
this.partNumber = partNumber;
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
// src/http.ts
|
|
42
|
+
function camelToSnake(str) {
|
|
43
|
+
return str.replace(/[A-Z]/g, (m) => `_${m.toLowerCase()}`);
|
|
44
|
+
}
|
|
45
|
+
function snakeToCamel(str) {
|
|
46
|
+
return str.replace(/_([a-z])/g, (_, c) => c.toUpperCase());
|
|
47
|
+
}
|
|
48
|
+
function convertKeys(obj, fn) {
|
|
49
|
+
if (Array.isArray(obj)) return obj.map((v) => convertKeys(v, fn));
|
|
50
|
+
if (obj !== null && typeof obj === "object" && !(obj instanceof Date)) {
|
|
51
|
+
const out = {};
|
|
52
|
+
for (const [k, v] of Object.entries(obj)) {
|
|
53
|
+
out[fn(k)] = convertKeys(v, fn);
|
|
54
|
+
}
|
|
55
|
+
return out;
|
|
56
|
+
}
|
|
57
|
+
return obj;
|
|
58
|
+
}
|
|
59
|
+
var HttpClient = class {
|
|
60
|
+
baseUrl;
|
|
61
|
+
apiKey;
|
|
62
|
+
fetchFn;
|
|
63
|
+
maxRetries;
|
|
64
|
+
baseDelay;
|
|
65
|
+
maxDelay;
|
|
66
|
+
onRateLimit;
|
|
67
|
+
constructor(options) {
|
|
68
|
+
this.baseUrl = (options.baseUrl ?? "https://dosya.dev").replace(/\/+$/, "");
|
|
69
|
+
this.apiKey = options.apiKey;
|
|
70
|
+
this.fetchFn = options.fetch ?? globalThis.fetch.bind(globalThis);
|
|
71
|
+
this.maxRetries = options.retry?.maxRetries ?? 3;
|
|
72
|
+
this.baseDelay = options.retry?.baseDelay ?? 500;
|
|
73
|
+
this.maxDelay = options.retry?.maxDelay ?? 3e4;
|
|
74
|
+
this.onRateLimit = options.onRateLimit;
|
|
75
|
+
}
|
|
76
|
+
async request(opts) {
|
|
77
|
+
const res = await this.doFetch(opts);
|
|
78
|
+
if (opts.rawResponse) {
|
|
79
|
+
return res;
|
|
80
|
+
}
|
|
81
|
+
const text = await res.text();
|
|
82
|
+
let json;
|
|
83
|
+
try {
|
|
84
|
+
json = JSON.parse(text);
|
|
85
|
+
} catch {
|
|
86
|
+
throw new DosyaApiError(res.status, `Invalid JSON response: ${text.slice(0, 200)}`);
|
|
87
|
+
}
|
|
88
|
+
if (!json.ok) {
|
|
89
|
+
throw new DosyaApiError(
|
|
90
|
+
res.status,
|
|
91
|
+
json.error ?? "Unknown error",
|
|
92
|
+
json
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
const { ok: _, ...payload } = json;
|
|
96
|
+
return convertKeys(payload, snakeToCamel);
|
|
97
|
+
}
|
|
98
|
+
async requestRaw(opts) {
|
|
99
|
+
return this.doFetch(opts);
|
|
100
|
+
}
|
|
101
|
+
async doFetch(opts) {
|
|
102
|
+
const url = this.buildUrl(opts.path, opts.query);
|
|
103
|
+
const headers = {
|
|
104
|
+
Authorization: `Bearer ${this.apiKey}`,
|
|
105
|
+
...opts.headers
|
|
106
|
+
};
|
|
107
|
+
let fetchBody;
|
|
108
|
+
if (opts.rawBody !== void 0) {
|
|
109
|
+
fetchBody = opts.rawBody;
|
|
110
|
+
} else if (opts.body !== void 0) {
|
|
111
|
+
headers["Content-Type"] = "application/json";
|
|
112
|
+
fetchBody = JSON.stringify(convertKeys(opts.body, camelToSnake));
|
|
113
|
+
}
|
|
114
|
+
let lastError;
|
|
115
|
+
for (let attempt = 0; attempt <= this.maxRetries; attempt++) {
|
|
116
|
+
try {
|
|
117
|
+
const res = await this.fetchFn(url, {
|
|
118
|
+
method: opts.method,
|
|
119
|
+
headers,
|
|
120
|
+
body: fetchBody,
|
|
121
|
+
signal: opts.signal,
|
|
122
|
+
redirect: opts.rawResponse ? "manual" : "follow"
|
|
123
|
+
});
|
|
124
|
+
this.extractRateLimit(res);
|
|
125
|
+
if (res.status === 429) {
|
|
126
|
+
const retryAfter = res.headers.get("Retry-After");
|
|
127
|
+
const delay = retryAfter ? parseInt(retryAfter, 10) * 1e3 : this.getDelay(attempt);
|
|
128
|
+
if (attempt < this.maxRetries) {
|
|
129
|
+
await this.sleep(delay);
|
|
130
|
+
continue;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
if (res.status >= 500 && attempt < this.maxRetries) {
|
|
134
|
+
await this.sleep(this.getDelay(attempt));
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
return res;
|
|
138
|
+
} catch (err) {
|
|
139
|
+
lastError = err;
|
|
140
|
+
if (err instanceof DOMException && err.name === "AbortError") throw err;
|
|
141
|
+
if (attempt < this.maxRetries) {
|
|
142
|
+
await this.sleep(this.getDelay(attempt));
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
throw new DosyaNetworkError(
|
|
148
|
+
`Request to ${opts.method} ${opts.path} failed after ${this.maxRetries + 1} attempts`,
|
|
149
|
+
{ cause: lastError }
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
buildUrl(path, query) {
|
|
153
|
+
const url = new URL(path, this.baseUrl);
|
|
154
|
+
if (query) {
|
|
155
|
+
for (const [key, value] of Object.entries(query)) {
|
|
156
|
+
if (value != null && value !== "") {
|
|
157
|
+
url.searchParams.set(camelToSnake(key), String(value));
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
return url.toString();
|
|
162
|
+
}
|
|
163
|
+
extractRateLimit(res) {
|
|
164
|
+
if (!this.onRateLimit) return;
|
|
165
|
+
const limit = res.headers.get("X-RateLimit-Limit");
|
|
166
|
+
const remaining = res.headers.get("X-RateLimit-Remaining");
|
|
167
|
+
const reset = res.headers.get("X-RateLimit-Reset");
|
|
168
|
+
if (limit && remaining && reset) {
|
|
169
|
+
this.onRateLimit({
|
|
170
|
+
limit: parseInt(limit, 10),
|
|
171
|
+
remaining: parseInt(remaining, 10),
|
|
172
|
+
resetAt: parseInt(reset, 10)
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
getDelay(attempt) {
|
|
177
|
+
const delay = this.baseDelay * 2 ** attempt;
|
|
178
|
+
const jitter = delay * 0.2 * Math.random();
|
|
179
|
+
return Math.min(delay + jitter, this.maxDelay);
|
|
180
|
+
}
|
|
181
|
+
sleep(ms) {
|
|
182
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
// src/resources/files.ts
|
|
187
|
+
var FilesResource = class {
|
|
188
|
+
constructor(http) {
|
|
189
|
+
this.http = http;
|
|
190
|
+
}
|
|
191
|
+
http;
|
|
192
|
+
async list(params) {
|
|
193
|
+
return this.http.request({
|
|
194
|
+
method: "GET",
|
|
195
|
+
path: "/api/files",
|
|
196
|
+
query: {
|
|
197
|
+
workspace_id: params.workspaceId,
|
|
198
|
+
folder_id: params.folderId,
|
|
199
|
+
filter: params.filter,
|
|
200
|
+
sort: params.sort,
|
|
201
|
+
q: params.q,
|
|
202
|
+
deleted: params.deleted,
|
|
203
|
+
hidden: params.hidden,
|
|
204
|
+
page: params.page,
|
|
205
|
+
per_page: params.perPage
|
|
206
|
+
}
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
async get(fileId) {
|
|
210
|
+
return this.http.request({
|
|
211
|
+
method: "GET",
|
|
212
|
+
path: `/api/files/${fileId}`
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
async delete(fileId) {
|
|
216
|
+
return this.http.request({
|
|
217
|
+
method: "DELETE",
|
|
218
|
+
path: `/api/files/${fileId}`
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
async restore(fileId) {
|
|
222
|
+
await this.http.request({
|
|
223
|
+
method: "PUT",
|
|
224
|
+
path: `/api/files/${fileId}`
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
async rename(fileId, name) {
|
|
228
|
+
return this.http.request({
|
|
229
|
+
method: "PUT",
|
|
230
|
+
path: `/api/files/${fileId}/rename`,
|
|
231
|
+
body: { name }
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
async move(fileId, folderId) {
|
|
235
|
+
await this.http.request({
|
|
236
|
+
method: "PUT",
|
|
237
|
+
path: `/api/files/${fileId}/move`,
|
|
238
|
+
body: { folderId }
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
async copy(fileId, options) {
|
|
242
|
+
return this.http.request({
|
|
243
|
+
method: "POST",
|
|
244
|
+
path: `/api/files/${fileId}/copy`,
|
|
245
|
+
body: {
|
|
246
|
+
newName: options?.newName,
|
|
247
|
+
folderId: options?.folderId
|
|
248
|
+
}
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
async lock(fileId, params) {
|
|
252
|
+
await this.http.request({
|
|
253
|
+
method: "POST",
|
|
254
|
+
path: `/api/files/${fileId}/lock`,
|
|
255
|
+
body: { lockMode: params.lockMode, password: params.password }
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
async unlock(fileId) {
|
|
259
|
+
await this.http.request({
|
|
260
|
+
method: "POST",
|
|
261
|
+
path: `/api/files/${fileId}/unlock`
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
async hide(fileId, params) {
|
|
265
|
+
await this.http.request({
|
|
266
|
+
method: "POST",
|
|
267
|
+
path: `/api/files/${fileId}/hide`,
|
|
268
|
+
body: {
|
|
269
|
+
hiddenMode: params?.hiddenMode,
|
|
270
|
+
targetIds: params?.targetIds
|
|
271
|
+
}
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
async listVersions(fileId) {
|
|
275
|
+
return this.http.request({
|
|
276
|
+
method: "GET",
|
|
277
|
+
path: `/api/files/${fileId}/versions`
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
async restoreVersion(fileId, versionNumber) {
|
|
281
|
+
return this.http.request({
|
|
282
|
+
method: "POST",
|
|
283
|
+
path: `/api/files/${fileId}/versions/restore`,
|
|
284
|
+
body: { versionNumber }
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
async getShareLinks(fileId) {
|
|
288
|
+
return this.http.request({
|
|
289
|
+
method: "GET",
|
|
290
|
+
path: `/api/files/${fileId}/share`
|
|
291
|
+
});
|
|
292
|
+
}
|
|
293
|
+
async createShareLink(fileId, params) {
|
|
294
|
+
return this.http.request({
|
|
295
|
+
method: "POST",
|
|
296
|
+
path: `/api/files/${fileId}/share`,
|
|
297
|
+
body: {
|
|
298
|
+
expiresInDays: params?.expiresInDays,
|
|
299
|
+
password: params?.password,
|
|
300
|
+
lockMode: params?.lockMode
|
|
301
|
+
}
|
|
302
|
+
});
|
|
303
|
+
}
|
|
304
|
+
async shareByEmail(fileId, params) {
|
|
305
|
+
await this.http.request({
|
|
306
|
+
method: "POST",
|
|
307
|
+
path: `/api/files/${fileId}/share-email`,
|
|
308
|
+
body: { emails: params.emails, message: params.message }
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
async createShareBundle(params) {
|
|
312
|
+
return this.http.request({
|
|
313
|
+
method: "POST",
|
|
314
|
+
path: "/api/files/share-bundle",
|
|
315
|
+
body: {
|
|
316
|
+
fileIds: params.fileIds,
|
|
317
|
+
expiresInDays: params.expiresInDays,
|
|
318
|
+
password: params.password
|
|
319
|
+
}
|
|
320
|
+
});
|
|
321
|
+
}
|
|
322
|
+
};
|
|
323
|
+
|
|
324
|
+
// src/resources/folders.ts
|
|
325
|
+
var FoldersResource = class {
|
|
326
|
+
constructor(http) {
|
|
327
|
+
this.http = http;
|
|
328
|
+
}
|
|
329
|
+
http;
|
|
330
|
+
async create(params) {
|
|
331
|
+
return this.http.request({
|
|
332
|
+
method: "POST",
|
|
333
|
+
path: "/api/folders",
|
|
334
|
+
body: {
|
|
335
|
+
workspaceId: params.workspaceId,
|
|
336
|
+
parentId: params.parentId,
|
|
337
|
+
name: params.name
|
|
338
|
+
}
|
|
339
|
+
});
|
|
340
|
+
}
|
|
341
|
+
async get(folderId) {
|
|
342
|
+
return this.http.request({
|
|
343
|
+
method: "GET",
|
|
344
|
+
path: `/api/folders/${folderId}`
|
|
345
|
+
});
|
|
346
|
+
}
|
|
347
|
+
async rename(folderId, name) {
|
|
348
|
+
return this.http.request({
|
|
349
|
+
method: "PUT",
|
|
350
|
+
path: `/api/folders/${folderId}`,
|
|
351
|
+
body: { name }
|
|
352
|
+
});
|
|
353
|
+
}
|
|
354
|
+
async delete(folderId) {
|
|
355
|
+
return this.http.request({
|
|
356
|
+
method: "DELETE",
|
|
357
|
+
path: `/api/folders/${folderId}`
|
|
358
|
+
});
|
|
359
|
+
}
|
|
360
|
+
async move(folderId, parentId) {
|
|
361
|
+
await this.http.request({
|
|
362
|
+
method: "PUT",
|
|
363
|
+
path: `/api/folders/${folderId}/move`,
|
|
364
|
+
body: { parentId }
|
|
365
|
+
});
|
|
366
|
+
}
|
|
367
|
+
async tree(workspaceId) {
|
|
368
|
+
return this.http.request({
|
|
369
|
+
method: "GET",
|
|
370
|
+
path: "/api/folders/tree",
|
|
371
|
+
query: { workspace_id: workspaceId }
|
|
372
|
+
});
|
|
373
|
+
}
|
|
374
|
+
async lock(folderId, params) {
|
|
375
|
+
await this.http.request({
|
|
376
|
+
method: "POST",
|
|
377
|
+
path: `/api/folders/${folderId}/lock`,
|
|
378
|
+
body: { lockMode: params.lockMode, password: params.password }
|
|
379
|
+
});
|
|
380
|
+
}
|
|
381
|
+
async unlock(folderId) {
|
|
382
|
+
await this.http.request({
|
|
383
|
+
method: "POST",
|
|
384
|
+
path: `/api/folders/${folderId}/unlock`
|
|
385
|
+
});
|
|
386
|
+
}
|
|
387
|
+
};
|
|
388
|
+
|
|
389
|
+
// src/resources/upload.ts
|
|
390
|
+
var CONCURRENCY = 3;
|
|
391
|
+
var UploadResource = class {
|
|
392
|
+
constructor(http) {
|
|
393
|
+
this.http = http;
|
|
394
|
+
}
|
|
395
|
+
http;
|
|
396
|
+
async file(params) {
|
|
397
|
+
params.onProgress?.({
|
|
398
|
+
bytesUploaded: 0,
|
|
399
|
+
totalBytes: params.fileSize,
|
|
400
|
+
percent: 0,
|
|
401
|
+
status: "initializing"
|
|
402
|
+
});
|
|
403
|
+
const session = await this.init({
|
|
404
|
+
workspaceId: params.workspaceId,
|
|
405
|
+
fileName: params.fileName,
|
|
406
|
+
fileSize: params.fileSize,
|
|
407
|
+
mimeType: params.mimeType,
|
|
408
|
+
region: params.region,
|
|
409
|
+
folderId: params.folderId,
|
|
410
|
+
fileId: params.fileId
|
|
411
|
+
});
|
|
412
|
+
if (session.resumable) {
|
|
413
|
+
return this.uploadMultipart(
|
|
414
|
+
session.sessionId,
|
|
415
|
+
session.resumable,
|
|
416
|
+
params.body,
|
|
417
|
+
params.fileSize,
|
|
418
|
+
session.mimeType,
|
|
419
|
+
params.onProgress,
|
|
420
|
+
params.abortSignal
|
|
421
|
+
);
|
|
422
|
+
}
|
|
423
|
+
return this.uploadSingle(
|
|
424
|
+
session.sessionId,
|
|
425
|
+
session.uploadUrl,
|
|
426
|
+
params.body,
|
|
427
|
+
session.mimeType,
|
|
428
|
+
params.fileSize,
|
|
429
|
+
params.onProgress,
|
|
430
|
+
params.abortSignal
|
|
431
|
+
);
|
|
432
|
+
}
|
|
433
|
+
async resume(sessionId, body, options) {
|
|
434
|
+
const st = await this.status(sessionId);
|
|
435
|
+
if (st.status === "complete") {
|
|
436
|
+
throw new DosyaUploadError("Upload session is already complete", sessionId);
|
|
437
|
+
}
|
|
438
|
+
if (!st.hasMultipart || !st.partSize || !st.totalParts) {
|
|
439
|
+
throw new DosyaUploadError(
|
|
440
|
+
"Cannot resume a non-multipart upload session",
|
|
441
|
+
sessionId
|
|
442
|
+
);
|
|
443
|
+
}
|
|
444
|
+
return this.uploadMultipart(
|
|
445
|
+
sessionId,
|
|
446
|
+
{
|
|
447
|
+
partSize: st.partSize,
|
|
448
|
+
totalParts: st.totalParts,
|
|
449
|
+
partUploadUrl: `/api/upload/${sessionId}/part`,
|
|
450
|
+
completeUrl: `/api/upload/${sessionId}/complete`,
|
|
451
|
+
statusUrl: `/api/upload/${sessionId}/status`
|
|
452
|
+
},
|
|
453
|
+
body,
|
|
454
|
+
st.sizeBytes,
|
|
455
|
+
"application/octet-stream",
|
|
456
|
+
options?.onProgress,
|
|
457
|
+
options?.abortSignal,
|
|
458
|
+
st.uploadedParts
|
|
459
|
+
);
|
|
460
|
+
}
|
|
461
|
+
async init(params) {
|
|
462
|
+
return this.http.request({
|
|
463
|
+
method: "POST",
|
|
464
|
+
path: "/api/upload/init",
|
|
465
|
+
body: {
|
|
466
|
+
workspaceId: params.workspaceId,
|
|
467
|
+
fileName: params.fileName,
|
|
468
|
+
fileSize: params.fileSize,
|
|
469
|
+
mimeType: params.mimeType,
|
|
470
|
+
region: params.region,
|
|
471
|
+
folderId: params.folderId,
|
|
472
|
+
fileId: params.fileId
|
|
473
|
+
}
|
|
474
|
+
});
|
|
475
|
+
}
|
|
476
|
+
async status(sessionId) {
|
|
477
|
+
return this.http.request({
|
|
478
|
+
method: "GET",
|
|
479
|
+
path: `/api/upload/${sessionId}/status`
|
|
480
|
+
});
|
|
481
|
+
}
|
|
482
|
+
// ── Private ──
|
|
483
|
+
async uploadSingle(sessionId, uploadUrl, body, mimeType, totalBytes, onProgress, signal) {
|
|
484
|
+
onProgress?.({
|
|
485
|
+
bytesUploaded: 0,
|
|
486
|
+
totalBytes,
|
|
487
|
+
percent: 0,
|
|
488
|
+
status: "uploading"
|
|
489
|
+
});
|
|
490
|
+
const buf = await this.sourceToBuffer(body);
|
|
491
|
+
const result = await this.http.request({
|
|
492
|
+
method: "PUT",
|
|
493
|
+
path: uploadUrl,
|
|
494
|
+
rawBody: buf,
|
|
495
|
+
headers: { "Content-Type": mimeType },
|
|
496
|
+
signal
|
|
497
|
+
});
|
|
498
|
+
onProgress?.({
|
|
499
|
+
bytesUploaded: totalBytes,
|
|
500
|
+
totalBytes,
|
|
501
|
+
percent: 100,
|
|
502
|
+
status: "complete"
|
|
503
|
+
});
|
|
504
|
+
return { file: result.file, sessionId };
|
|
505
|
+
}
|
|
506
|
+
async uploadMultipart(sessionId, resumable, body, totalBytes, mimeType, onProgress, signal, alreadyUploaded = []) {
|
|
507
|
+
const { partSize, totalParts, partUploadUrl, completeUrl } = resumable;
|
|
508
|
+
const uploadedSet = new Set(alreadyUploaded);
|
|
509
|
+
let bytesUploaded = alreadyUploaded.length * partSize;
|
|
510
|
+
if (bytesUploaded > totalBytes) bytesUploaded = totalBytes;
|
|
511
|
+
onProgress?.({
|
|
512
|
+
bytesUploaded,
|
|
513
|
+
totalBytes,
|
|
514
|
+
percent: Math.round(bytesUploaded / totalBytes * 100),
|
|
515
|
+
partsCompleted: uploadedSet.size,
|
|
516
|
+
totalParts,
|
|
517
|
+
status: "uploading"
|
|
518
|
+
});
|
|
519
|
+
const allBytes = await this.sourceToBuffer(body);
|
|
520
|
+
const parts = [];
|
|
521
|
+
for (let i = 1; i <= totalParts; i++) {
|
|
522
|
+
if (uploadedSet.has(i)) continue;
|
|
523
|
+
const start = (i - 1) * partSize;
|
|
524
|
+
const end = Math.min(start + partSize, allBytes.byteLength);
|
|
525
|
+
parts.push({
|
|
526
|
+
partNumber: i,
|
|
527
|
+
chunk: new Uint8Array(allBytes.slice(start, end))
|
|
528
|
+
});
|
|
529
|
+
}
|
|
530
|
+
let partIdx = 0;
|
|
531
|
+
const uploadPart = async () => {
|
|
532
|
+
while (partIdx < parts.length) {
|
|
533
|
+
if (signal?.aborted) throw new DOMException("Upload aborted", "AbortError");
|
|
534
|
+
const idx = partIdx++;
|
|
535
|
+
const part = parts[idx];
|
|
536
|
+
let lastErr;
|
|
537
|
+
for (let attempt = 0; attempt < 3; attempt++) {
|
|
538
|
+
try {
|
|
539
|
+
await this.http.request({
|
|
540
|
+
method: "PUT",
|
|
541
|
+
path: `${partUploadUrl}/${part.partNumber}`,
|
|
542
|
+
rawBody: part.chunk,
|
|
543
|
+
headers: { "Content-Type": mimeType },
|
|
544
|
+
signal
|
|
545
|
+
});
|
|
546
|
+
break;
|
|
547
|
+
} catch (err) {
|
|
548
|
+
lastErr = err;
|
|
549
|
+
if (err instanceof DOMException && err.name === "AbortError") throw err;
|
|
550
|
+
if (attempt === 2) {
|
|
551
|
+
throw new DosyaUploadError(
|
|
552
|
+
`Failed to upload part ${part.partNumber} after 3 attempts: ${lastErr}`,
|
|
553
|
+
sessionId,
|
|
554
|
+
part.partNumber
|
|
555
|
+
);
|
|
556
|
+
}
|
|
557
|
+
await new Promise((r) => setTimeout(r, 500 * 2 ** attempt));
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
uploadedSet.add(part.partNumber);
|
|
561
|
+
bytesUploaded = Math.min(uploadedSet.size * partSize, totalBytes);
|
|
562
|
+
onProgress?.({
|
|
563
|
+
bytesUploaded,
|
|
564
|
+
totalBytes,
|
|
565
|
+
percent: Math.round(bytesUploaded / totalBytes * 100),
|
|
566
|
+
partsCompleted: uploadedSet.size,
|
|
567
|
+
totalParts,
|
|
568
|
+
status: "uploading"
|
|
569
|
+
});
|
|
570
|
+
}
|
|
571
|
+
};
|
|
572
|
+
const workers = Array.from(
|
|
573
|
+
{ length: Math.min(CONCURRENCY, parts.length) },
|
|
574
|
+
() => uploadPart()
|
|
575
|
+
);
|
|
576
|
+
await Promise.all(workers);
|
|
577
|
+
onProgress?.({
|
|
578
|
+
bytesUploaded: totalBytes,
|
|
579
|
+
totalBytes,
|
|
580
|
+
percent: 99,
|
|
581
|
+
partsCompleted: totalParts,
|
|
582
|
+
totalParts,
|
|
583
|
+
status: "completing"
|
|
584
|
+
});
|
|
585
|
+
const result = await this.http.request({
|
|
586
|
+
method: "POST",
|
|
587
|
+
path: completeUrl,
|
|
588
|
+
signal
|
|
589
|
+
});
|
|
590
|
+
onProgress?.({
|
|
591
|
+
bytesUploaded: totalBytes,
|
|
592
|
+
totalBytes,
|
|
593
|
+
percent: 100,
|
|
594
|
+
partsCompleted: totalParts,
|
|
595
|
+
totalParts,
|
|
596
|
+
status: "complete"
|
|
597
|
+
});
|
|
598
|
+
return { file: result.file, sessionId };
|
|
599
|
+
}
|
|
600
|
+
async sourceToBuffer(source) {
|
|
601
|
+
if (source instanceof ArrayBuffer) return source;
|
|
602
|
+
if (source instanceof Uint8Array) {
|
|
603
|
+
const copy = new ArrayBuffer(source.byteLength);
|
|
604
|
+
new Uint8Array(copy).set(source);
|
|
605
|
+
return copy;
|
|
606
|
+
}
|
|
607
|
+
if (typeof Blob !== "undefined" && source instanceof Blob) {
|
|
608
|
+
return source.arrayBuffer();
|
|
609
|
+
}
|
|
610
|
+
if (source instanceof ReadableStream) {
|
|
611
|
+
return this.streamToBuffer(source);
|
|
612
|
+
}
|
|
613
|
+
if ("arrayBuffer" in source && typeof source.arrayBuffer === "function") {
|
|
614
|
+
return source.arrayBuffer();
|
|
615
|
+
}
|
|
616
|
+
throw new Error(`Unsupported upload source type: ${typeof source}`);
|
|
617
|
+
}
|
|
618
|
+
async streamToBuffer(stream) {
|
|
619
|
+
const reader = stream.getReader();
|
|
620
|
+
const chunks = [];
|
|
621
|
+
let totalLength = 0;
|
|
622
|
+
while (true) {
|
|
623
|
+
const { done, value } = await reader.read();
|
|
624
|
+
if (done) break;
|
|
625
|
+
chunks.push(value);
|
|
626
|
+
totalLength += value.byteLength;
|
|
627
|
+
}
|
|
628
|
+
const result = new Uint8Array(totalLength);
|
|
629
|
+
let offset = 0;
|
|
630
|
+
for (const chunk of chunks) {
|
|
631
|
+
result.set(chunk, offset);
|
|
632
|
+
offset += chunk.byteLength;
|
|
633
|
+
}
|
|
634
|
+
return result.buffer;
|
|
635
|
+
}
|
|
636
|
+
};
|
|
637
|
+
|
|
638
|
+
// src/resources/download.ts
|
|
639
|
+
var DownloadResource = class {
|
|
640
|
+
constructor(http) {
|
|
641
|
+
this.http = http;
|
|
642
|
+
}
|
|
643
|
+
http;
|
|
644
|
+
async getUrl(fileId, options) {
|
|
645
|
+
const res = await this.http.requestRaw({
|
|
646
|
+
method: "GET",
|
|
647
|
+
path: `/api/files/${fileId}/download`,
|
|
648
|
+
query: {
|
|
649
|
+
version: options?.version,
|
|
650
|
+
ut: options?.unlockToken
|
|
651
|
+
},
|
|
652
|
+
rawResponse: true
|
|
653
|
+
});
|
|
654
|
+
const location = res.headers.get("Location");
|
|
655
|
+
if (location) return location;
|
|
656
|
+
if (res.url) return res.url;
|
|
657
|
+
throw new Error("Could not extract download URL from response");
|
|
658
|
+
}
|
|
659
|
+
async arrayBuffer(fileId, options) {
|
|
660
|
+
const url = await this.getUrl(fileId, options);
|
|
661
|
+
const res = await fetch(url);
|
|
662
|
+
if (!res.ok) {
|
|
663
|
+
throw new Error(`Download failed with status ${res.status}`);
|
|
664
|
+
}
|
|
665
|
+
return res.arrayBuffer();
|
|
666
|
+
}
|
|
667
|
+
async stream(fileId, options) {
|
|
668
|
+
const url = await this.getUrl(fileId, options);
|
|
669
|
+
const res = await fetch(url);
|
|
670
|
+
if (!res.ok) {
|
|
671
|
+
throw new Error(`Download failed with status ${res.status}`);
|
|
672
|
+
}
|
|
673
|
+
if (!res.body) {
|
|
674
|
+
throw new Error("Response body is null \u2014 streaming not supported");
|
|
675
|
+
}
|
|
676
|
+
return res.body;
|
|
677
|
+
}
|
|
678
|
+
async raw(fileId, options) {
|
|
679
|
+
return this.http.requestRaw({
|
|
680
|
+
method: "GET",
|
|
681
|
+
path: `/api/files/${fileId}/raw`,
|
|
682
|
+
query: {
|
|
683
|
+
version: options?.version,
|
|
684
|
+
ut: options?.unlockToken
|
|
685
|
+
}
|
|
686
|
+
});
|
|
687
|
+
}
|
|
688
|
+
};
|
|
689
|
+
|
|
690
|
+
// src/resources/shares.ts
|
|
691
|
+
var SharesResource = class {
|
|
692
|
+
constructor(http) {
|
|
693
|
+
this.http = http;
|
|
694
|
+
}
|
|
695
|
+
http;
|
|
696
|
+
async list(workspaceId) {
|
|
697
|
+
return this.http.request({
|
|
698
|
+
method: "GET",
|
|
699
|
+
path: "/api/shares",
|
|
700
|
+
query: { workspace_id: workspaceId }
|
|
701
|
+
});
|
|
702
|
+
}
|
|
703
|
+
async revoke(linkId) {
|
|
704
|
+
await this.http.request({
|
|
705
|
+
method: "POST",
|
|
706
|
+
path: `/api/shares/${linkId}/revoke`
|
|
707
|
+
});
|
|
708
|
+
}
|
|
709
|
+
};
|
|
710
|
+
|
|
711
|
+
// src/resources/workspaces.ts
|
|
712
|
+
var WorkspacesResource = class {
|
|
713
|
+
constructor(http) {
|
|
714
|
+
this.http = http;
|
|
715
|
+
}
|
|
716
|
+
http;
|
|
717
|
+
async list() {
|
|
718
|
+
return this.http.request({
|
|
719
|
+
method: "GET",
|
|
720
|
+
path: "/api/workspaces"
|
|
721
|
+
});
|
|
722
|
+
}
|
|
723
|
+
async get(workspaceId) {
|
|
724
|
+
return this.http.request({
|
|
725
|
+
method: "GET",
|
|
726
|
+
path: `/api/workspaces/${workspaceId}`
|
|
727
|
+
});
|
|
728
|
+
}
|
|
729
|
+
async create(params) {
|
|
730
|
+
return this.http.request({
|
|
731
|
+
method: "POST",
|
|
732
|
+
path: "/api/workspaces",
|
|
733
|
+
body: {
|
|
734
|
+
name: params.name,
|
|
735
|
+
iconInitials: params.iconInitials,
|
|
736
|
+
iconColor: params.iconColor,
|
|
737
|
+
defaultRegion: params.defaultRegion
|
|
738
|
+
}
|
|
739
|
+
});
|
|
740
|
+
}
|
|
741
|
+
async update(workspaceId, params) {
|
|
742
|
+
await this.http.request({
|
|
743
|
+
method: "PUT",
|
|
744
|
+
path: `/api/workspaces/${workspaceId}`,
|
|
745
|
+
body: {
|
|
746
|
+
name: params.name,
|
|
747
|
+
iconInitials: params.iconInitials,
|
|
748
|
+
iconColor: params.iconColor,
|
|
749
|
+
defaultRegion: params.defaultRegion
|
|
750
|
+
}
|
|
751
|
+
});
|
|
752
|
+
}
|
|
753
|
+
async updateSettings(workspaceId, settings) {
|
|
754
|
+
await this.http.request({
|
|
755
|
+
method: "PUT",
|
|
756
|
+
path: `/api/workspaces/${workspaceId}/settings`,
|
|
757
|
+
body: settings
|
|
758
|
+
});
|
|
759
|
+
}
|
|
760
|
+
async delete(workspaceId) {
|
|
761
|
+
await this.http.request({
|
|
762
|
+
method: "DELETE",
|
|
763
|
+
path: `/api/workspaces/${workspaceId}`
|
|
764
|
+
});
|
|
765
|
+
}
|
|
766
|
+
};
|
|
767
|
+
|
|
768
|
+
// src/resources/file-requests.ts
|
|
769
|
+
var FileRequestsResource = class {
|
|
770
|
+
constructor(http) {
|
|
771
|
+
this.http = http;
|
|
772
|
+
}
|
|
773
|
+
http;
|
|
774
|
+
async create(params) {
|
|
775
|
+
return this.http.request({
|
|
776
|
+
method: "POST",
|
|
777
|
+
path: "/api/file-requests/create",
|
|
778
|
+
body: {
|
|
779
|
+
workspaceId: params.workspaceId,
|
|
780
|
+
folderId: params.folderId,
|
|
781
|
+
title: params.title,
|
|
782
|
+
message: params.message,
|
|
783
|
+
password: params.password,
|
|
784
|
+
expiresInDays: params.expiresInDays,
|
|
785
|
+
allowedExtensions: params.allowedExtensions,
|
|
786
|
+
maxFileSizeMb: params.maxFileSizeMb,
|
|
787
|
+
maxFiles: params.maxFiles,
|
|
788
|
+
emails: params.emails
|
|
789
|
+
}
|
|
790
|
+
});
|
|
791
|
+
}
|
|
792
|
+
async get(requestId) {
|
|
793
|
+
return this.http.request({
|
|
794
|
+
method: "GET",
|
|
795
|
+
path: `/api/file-requests/${requestId}`
|
|
796
|
+
});
|
|
797
|
+
}
|
|
798
|
+
async update(requestId, params) {
|
|
799
|
+
await this.http.request({
|
|
800
|
+
method: "PUT",
|
|
801
|
+
path: `/api/file-requests/${requestId}`,
|
|
802
|
+
body: { title: params.title, message: params.message }
|
|
803
|
+
});
|
|
804
|
+
}
|
|
805
|
+
async delete(requestId) {
|
|
806
|
+
await this.http.request({
|
|
807
|
+
method: "DELETE",
|
|
808
|
+
path: `/api/file-requests/${requestId}`
|
|
809
|
+
});
|
|
810
|
+
}
|
|
811
|
+
async listUploads(requestId) {
|
|
812
|
+
return this.http.request({
|
|
813
|
+
method: "GET",
|
|
814
|
+
path: `/api/file-requests/${requestId}/uploads`
|
|
815
|
+
});
|
|
816
|
+
}
|
|
817
|
+
async listRecipients(requestId) {
|
|
818
|
+
return this.http.request({
|
|
819
|
+
method: "GET",
|
|
820
|
+
path: `/api/file-requests/${requestId}/recipients`
|
|
821
|
+
});
|
|
822
|
+
}
|
|
823
|
+
async resend(requestId, recipientIds) {
|
|
824
|
+
await this.http.request({
|
|
825
|
+
method: "POST",
|
|
826
|
+
path: `/api/file-requests/${requestId}/resend`,
|
|
827
|
+
body: { recipientIds }
|
|
828
|
+
});
|
|
829
|
+
}
|
|
830
|
+
};
|
|
831
|
+
|
|
832
|
+
// src/resources/search.ts
|
|
833
|
+
var SearchResource = class {
|
|
834
|
+
constructor(http) {
|
|
835
|
+
this.http = http;
|
|
836
|
+
}
|
|
837
|
+
http;
|
|
838
|
+
async query(params) {
|
|
839
|
+
return this.http.request({
|
|
840
|
+
method: "GET",
|
|
841
|
+
path: "/api/search",
|
|
842
|
+
query: {
|
|
843
|
+
workspace_id: params.workspaceId,
|
|
844
|
+
q: params.q,
|
|
845
|
+
page: params.page,
|
|
846
|
+
per_page: params.perPage
|
|
847
|
+
}
|
|
848
|
+
});
|
|
849
|
+
}
|
|
850
|
+
};
|
|
851
|
+
|
|
852
|
+
// src/resources/comments.ts
|
|
853
|
+
var CommentsResource = class {
|
|
854
|
+
constructor(http) {
|
|
855
|
+
this.http = http;
|
|
856
|
+
}
|
|
857
|
+
http;
|
|
858
|
+
async list(params) {
|
|
859
|
+
return this.http.request({
|
|
860
|
+
method: "GET",
|
|
861
|
+
path: "/api/comments",
|
|
862
|
+
query: {
|
|
863
|
+
workspace_id: params.workspaceId,
|
|
864
|
+
file_id: params.fileId,
|
|
865
|
+
folder_id: params.folderId
|
|
866
|
+
}
|
|
867
|
+
});
|
|
868
|
+
}
|
|
869
|
+
async create(params) {
|
|
870
|
+
return this.http.request({
|
|
871
|
+
method: "POST",
|
|
872
|
+
path: "/api/comments",
|
|
873
|
+
body: {
|
|
874
|
+
workspaceId: params.workspaceId,
|
|
875
|
+
fileId: params.fileId,
|
|
876
|
+
folderId: params.folderId,
|
|
877
|
+
parentId: params.parentId,
|
|
878
|
+
body: params.body
|
|
879
|
+
}
|
|
880
|
+
});
|
|
881
|
+
}
|
|
882
|
+
async edit(commentId, body) {
|
|
883
|
+
return this.http.request({
|
|
884
|
+
method: "PUT",
|
|
885
|
+
path: `/api/comments/${commentId}`,
|
|
886
|
+
body: { body }
|
|
887
|
+
});
|
|
888
|
+
}
|
|
889
|
+
async delete(commentId) {
|
|
890
|
+
await this.http.request({
|
|
891
|
+
method: "DELETE",
|
|
892
|
+
path: `/api/comments/${commentId}`
|
|
893
|
+
});
|
|
894
|
+
}
|
|
895
|
+
};
|
|
896
|
+
|
|
897
|
+
// src/resources/me.ts
|
|
898
|
+
var MeResource = class {
|
|
899
|
+
constructor(http) {
|
|
900
|
+
this.http = http;
|
|
901
|
+
}
|
|
902
|
+
http;
|
|
903
|
+
async profile() {
|
|
904
|
+
return this.http.request({
|
|
905
|
+
method: "GET",
|
|
906
|
+
path: "/api/me"
|
|
907
|
+
});
|
|
908
|
+
}
|
|
909
|
+
async listApiKeys() {
|
|
910
|
+
return this.http.request({
|
|
911
|
+
method: "GET",
|
|
912
|
+
path: "/api/me/api-keys"
|
|
913
|
+
});
|
|
914
|
+
}
|
|
915
|
+
async createApiKey(params) {
|
|
916
|
+
return this.http.request({
|
|
917
|
+
method: "POST",
|
|
918
|
+
path: "/api/me/api-keys",
|
|
919
|
+
body: {
|
|
920
|
+
name: params.name,
|
|
921
|
+
scope: params.scope,
|
|
922
|
+
expiresInDays: params.expiresInDays
|
|
923
|
+
}
|
|
924
|
+
});
|
|
925
|
+
}
|
|
926
|
+
async deleteApiKey(keyId) {
|
|
927
|
+
await this.http.request({
|
|
928
|
+
method: "DELETE",
|
|
929
|
+
path: `/api/me/api-keys/${keyId}`
|
|
930
|
+
});
|
|
931
|
+
}
|
|
932
|
+
};
|
|
933
|
+
|
|
934
|
+
// src/resources/activity.ts
|
|
935
|
+
var ActivityResource = class {
|
|
936
|
+
constructor(http) {
|
|
937
|
+
this.http = http;
|
|
938
|
+
}
|
|
939
|
+
http;
|
|
940
|
+
async list(params) {
|
|
941
|
+
return this.http.request({
|
|
942
|
+
method: "GET",
|
|
943
|
+
path: "/api/activity",
|
|
944
|
+
query: {
|
|
945
|
+
workspace_id: params.workspaceId,
|
|
946
|
+
page: params.page,
|
|
947
|
+
per_page: params.perPage,
|
|
948
|
+
category: params.category,
|
|
949
|
+
action: params.action,
|
|
950
|
+
user_id: params.userId
|
|
951
|
+
}
|
|
952
|
+
});
|
|
953
|
+
}
|
|
954
|
+
};
|
|
955
|
+
|
|
956
|
+
// src/client.ts
|
|
957
|
+
var DosyaClient = class {
|
|
958
|
+
files;
|
|
959
|
+
folders;
|
|
960
|
+
upload;
|
|
961
|
+
download;
|
|
962
|
+
shares;
|
|
963
|
+
workspaces;
|
|
964
|
+
fileRequests;
|
|
965
|
+
search;
|
|
966
|
+
comments;
|
|
967
|
+
me;
|
|
968
|
+
activity;
|
|
969
|
+
constructor(options) {
|
|
970
|
+
if (!options.apiKey?.startsWith("dos_")) {
|
|
971
|
+
throw new Error("API key must start with 'dos_'");
|
|
972
|
+
}
|
|
973
|
+
const http = new HttpClient(options);
|
|
974
|
+
this.files = new FilesResource(http);
|
|
975
|
+
this.folders = new FoldersResource(http);
|
|
976
|
+
this.upload = new UploadResource(http);
|
|
977
|
+
this.download = new DownloadResource(http);
|
|
978
|
+
this.shares = new SharesResource(http);
|
|
979
|
+
this.workspaces = new WorkspacesResource(http);
|
|
980
|
+
this.fileRequests = new FileRequestsResource(http);
|
|
981
|
+
this.search = new SearchResource(http);
|
|
982
|
+
this.comments = new CommentsResource(http);
|
|
983
|
+
this.me = new MeResource(http);
|
|
984
|
+
this.activity = new ActivityResource(http);
|
|
985
|
+
}
|
|
986
|
+
};
|
|
987
|
+
|
|
988
|
+
exports.DosyaApiError = DosyaApiError;
|
|
989
|
+
exports.DosyaClient = DosyaClient;
|
|
990
|
+
exports.DosyaError = DosyaError;
|
|
991
|
+
exports.DosyaNetworkError = DosyaNetworkError;
|
|
992
|
+
exports.DosyaUploadError = DosyaUploadError;
|