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 ADDED
@@ -0,0 +1,548 @@
1
+ # dosyadev
2
+
3
+ Official JavaScript/TypeScript SDK for [dosya.dev](https://dosya.dev) — file storage, chunked uploads, sharing, and workspace management.
4
+
5
+ [![npm version](https://img.shields.io/npm/v/dosyadev.svg)](https://www.npmjs.com/package/dosyadev)
6
+ [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)
7
+
8
+ ## Install
9
+
10
+ ```bash
11
+ npm install dosyadev
12
+ ```
13
+
14
+ ## Quick Start
15
+
16
+ ```typescript
17
+ import { DosyaClient } from "dosyadev";
18
+
19
+ const client = new DosyaClient({ apiKey: "dos_your_api_key" });
20
+
21
+ // Upload a file
22
+ const result = await client.upload.file({
23
+ workspaceId: "ws_abc123",
24
+ fileName: "video.mp4",
25
+ fileSize: 250_000_000,
26
+ body: fileBuffer,
27
+ onProgress: (p) => console.log(`${p.percent}%`),
28
+ });
29
+
30
+ // List files
31
+ const { files } = await client.files.list({
32
+ workspaceId: "ws_abc123",
33
+ sort: "newest",
34
+ });
35
+
36
+ // Download a file
37
+ const stream = await client.download.stream("file_xxx");
38
+ ```
39
+
40
+ ## Authentication
41
+
42
+ All requests require an API key. Create one from your [dosya.dev dashboard](https://dosya.dev) under **Settings > API Keys**.
43
+
44
+ ```typescript
45
+ const client = new DosyaClient({
46
+ apiKey: "dos_your_api_key",
47
+ });
48
+ ```
49
+
50
+ API keys support three scopes:
51
+
52
+ | Scope | Permissions |
53
+ |-------|-------------|
54
+ | `full` | Read, write, delete, share — everything |
55
+ | `read` | List, get, download, search |
56
+ | `upload` | Upload files only |
57
+
58
+ ## Configuration
59
+
60
+ ```typescript
61
+ const client = new DosyaClient({
62
+ apiKey: "dos_your_api_key",
63
+
64
+ // Custom base URL (default: https://dosya.dev)
65
+ baseUrl: "https://dosya.dev",
66
+
67
+ // Retry configuration
68
+ retry: {
69
+ maxRetries: 3, // default: 3
70
+ baseDelay: 500, // default: 500ms
71
+ maxDelay: 30_000, // default: 30s
72
+ },
73
+
74
+ // Rate limit callback
75
+ onRateLimit: (info) => {
76
+ console.log(`${info.remaining}/${info.limit} requests left`);
77
+ },
78
+
79
+ // Custom fetch implementation (for testing or proxies)
80
+ fetch: customFetch,
81
+ });
82
+ ```
83
+
84
+ ## Resources
85
+
86
+ ### Files
87
+
88
+ ```typescript
89
+ // List files in a workspace
90
+ const { files, folders, pagination } = await client.files.list({
91
+ workspaceId: "ws_abc",
92
+ folderId: "folder_123", // optional — root if omitted
93
+ filter: "images", // "all" | "documents" | "videos" | "images"
94
+ sort: "newest", // "newest" | "oldest" | "name_asc" | "name_desc" | "largest" | "smallest"
95
+ q: "quarterly report", // search within folder
96
+ page: 1,
97
+ perPage: 50,
98
+ });
99
+
100
+ // Get file metadata
101
+ const { file } = await client.files.get("file_xxx");
102
+
103
+ // Rename
104
+ await client.files.rename("file_xxx", "new-name.pdf");
105
+
106
+ // Move to another folder
107
+ await client.files.move("file_xxx", "folder_456");
108
+
109
+ // Copy
110
+ const { file: copy } = await client.files.copy("file_xxx", {
111
+ newName: "copy-of-report.pdf",
112
+ folderId: "folder_456",
113
+ });
114
+
115
+ // Delete (first call = soft delete, second call = permanent)
116
+ const { permanent } = await client.files.delete("file_xxx");
117
+
118
+ // Restore a soft-deleted file
119
+ await client.files.restore("file_xxx");
120
+
121
+ // Lock / unlock
122
+ await client.files.lock("file_xxx", { lockMode: "view_only" });
123
+ await client.files.unlock("file_xxx");
124
+
125
+ // Hide from non-admins
126
+ await client.files.hide("file_xxx", {
127
+ hiddenMode: "everyone",
128
+ });
129
+ ```
130
+
131
+ ### File Versions
132
+
133
+ ```typescript
134
+ // List all versions
135
+ const { versions, currentVersion } = await client.files.listVersions("file_xxx");
136
+
137
+ // Restore a previous version
138
+ await client.files.restoreVersion("file_xxx", 2);
139
+ ```
140
+
141
+ ### Upload
142
+
143
+ The SDK automatically selects the right upload strategy:
144
+ - **Files <= 10 MB**: Single PUT request
145
+ - **Files > 10 MB**: Chunked multipart upload (10 MB parts, 3 concurrent)
146
+
147
+ ```typescript
148
+ import { readFile } from "fs/promises";
149
+
150
+ const buffer = await readFile("./video.mp4");
151
+
152
+ const result = await client.upload.file({
153
+ workspaceId: "ws_abc",
154
+ fileName: "video.mp4",
155
+ fileSize: buffer.byteLength,
156
+ mimeType: "video/mp4",
157
+ region: "eu-central-1", // optional — uses workspace default
158
+ folderId: "folder_123", // optional — root if omitted
159
+ body: buffer,
160
+ onProgress: (p) => {
161
+ console.log(`${p.status}: ${p.percent}% (${p.partsCompleted}/${p.totalParts})`);
162
+ },
163
+ abortSignal: controller.signal,
164
+ });
165
+
166
+ console.log(result.file.id); // "file_xxx"
167
+ console.log(result.sessionId); // "upl_xxx"
168
+ ```
169
+
170
+ #### Upload a new version
171
+
172
+ ```typescript
173
+ await client.upload.file({
174
+ workspaceId: "ws_abc",
175
+ fileName: "video-v2.mp4",
176
+ fileSize: buffer.byteLength,
177
+ fileId: "file_xxx", // existing file ID — creates a new version
178
+ body: buffer,
179
+ });
180
+ ```
181
+
182
+ #### Resume a failed upload
183
+
184
+ ```typescript
185
+ // Upload was interrupted — resume from where it left off
186
+ const result = await client.upload.resume("upl_session_id", buffer, {
187
+ onProgress: (p) => console.log(`Resuming: ${p.percent}%`),
188
+ });
189
+ ```
190
+
191
+ #### Check upload status
192
+
193
+ ```typescript
194
+ const status = await client.upload.status("upl_session_id");
195
+ // { status: "uploading", bytesUploaded: 31457280, uploadedParts: [1, 2, 3], ... }
196
+ ```
197
+
198
+ #### Progress callback shape
199
+
200
+ ```typescript
201
+ interface UploadProgress {
202
+ bytesUploaded: number;
203
+ totalBytes: number;
204
+ percent: number; // 0–100
205
+ partsCompleted?: number; // multipart only
206
+ totalParts?: number; // multipart only
207
+ status: "initializing" | "uploading" | "completing" | "complete";
208
+ }
209
+ ```
210
+
211
+ #### Supported input types
212
+
213
+ | Type | Environment |
214
+ |------|-------------|
215
+ | `ArrayBuffer` | Node.js, Browser |
216
+ | `Uint8Array` | Node.js, Browser |
217
+ | `File` | Browser |
218
+ | `Blob` | Browser |
219
+ | `ReadableStream<Uint8Array>` | Node.js 18+, Browser |
220
+
221
+ ### Download
222
+
223
+ ```typescript
224
+ // Get a presigned download URL (valid ~1 hour)
225
+ const url = await client.download.getUrl("file_xxx");
226
+
227
+ // Download as ArrayBuffer
228
+ const buffer = await client.download.arrayBuffer("file_xxx");
229
+
230
+ // Download as a stream
231
+ const stream = await client.download.stream("file_xxx");
232
+
233
+ // Download a specific version
234
+ const url = await client.download.getUrl("file_xxx", { version: 2 });
235
+
236
+ // Get raw inline content (for previews)
237
+ const response = await client.download.raw("file_xxx");
238
+ ```
239
+
240
+ ### Folders
241
+
242
+ ```typescript
243
+ // Create a folder (supports nested paths like "a/b/c")
244
+ const { folder, createdCount } = await client.folders.create({
245
+ workspaceId: "ws_abc",
246
+ name: "Reports/2025/Q1",
247
+ parentId: "folder_parent", // optional
248
+ });
249
+
250
+ // Get folder metadata
251
+ const { folder } = await client.folders.get("folder_xxx");
252
+
253
+ // Rename
254
+ await client.folders.rename("folder_xxx", "New Name");
255
+
256
+ // Move
257
+ await client.folders.move("folder_xxx", "folder_new_parent");
258
+
259
+ // Delete (cascading — files inside are soft-deleted)
260
+ const { filesAffected, foldersRemoved } = await client.folders.delete("folder_xxx");
261
+
262
+ // Get full folder tree
263
+ const { folders } = await client.folders.tree("ws_abc");
264
+
265
+ // Lock / unlock
266
+ await client.folders.lock("folder_xxx", { lockMode: "full_lock" });
267
+ await client.folders.unlock("folder_xxx");
268
+ ```
269
+
270
+ ### Sharing
271
+
272
+ ```typescript
273
+ // Create a share link for a file
274
+ const { link } = await client.files.createShareLink("file_xxx", {
275
+ expiresInDays: 7,
276
+ password: "secret",
277
+ lockMode: "view_only",
278
+ });
279
+ console.log(link.token); // share token
280
+
281
+ // Get existing share links
282
+ const { links } = await client.files.getShareLinks("file_xxx");
283
+
284
+ // Share via email
285
+ await client.files.shareByEmail("file_xxx", {
286
+ emails: ["team@example.com"],
287
+ message: "Here's the latest report",
288
+ });
289
+
290
+ // Share multiple files as a bundle
291
+ const { link } = await client.files.createShareBundle({
292
+ fileIds: ["file_1", "file_2", "file_3"],
293
+ expiresInDays: 30,
294
+ });
295
+
296
+ // List all share links in a workspace
297
+ const { links, stats } = await client.shares.list("ws_abc");
298
+
299
+ // Revoke a share link
300
+ await client.shares.revoke("link_xxx");
301
+ ```
302
+
303
+ ### Workspaces
304
+
305
+ ```typescript
306
+ // List workspaces
307
+ const { workspaces } = await client.workspaces.list();
308
+
309
+ // Get workspace details
310
+ const { workspace, settings, isOwner } = await client.workspaces.get("ws_abc");
311
+
312
+ // Create a workspace
313
+ const { workspace } = await client.workspaces.create({
314
+ name: "My Team",
315
+ defaultRegion: "eu-central-1",
316
+ });
317
+
318
+ // Update workspace
319
+ await client.workspaces.update("ws_abc", {
320
+ name: "New Name",
321
+ iconColor: "#22c55e",
322
+ });
323
+
324
+ // Update workspace settings
325
+ await client.workspaces.updateSettings("ws_abc", {
326
+ maxFileSizeGb: 5,
327
+ blockedExtensions: ".exe,.bat",
328
+ forceSharePassword: 1,
329
+ });
330
+
331
+ // Delete workspace
332
+ await client.workspaces.delete("ws_abc");
333
+ ```
334
+
335
+ ### File Requests
336
+
337
+ Create upload links that external users can use to send you files.
338
+
339
+ ```typescript
340
+ // Create a file request
341
+ const { request } = await client.fileRequests.create({
342
+ workspaceId: "ws_abc",
343
+ title: "Please upload your documents",
344
+ message: "Upload your signed contracts here",
345
+ expiresInDays: 14,
346
+ maxFiles: 10,
347
+ maxFileSizeMb: 100,
348
+ emails: ["client@example.com"],
349
+ });
350
+ console.log(request.url); // shareable upload link
351
+
352
+ // Get request details
353
+ const { request } = await client.fileRequests.get("req_xxx");
354
+
355
+ // List received uploads
356
+ const { uploads } = await client.fileRequests.listUploads("req_xxx");
357
+
358
+ // Resend invitation emails
359
+ await client.fileRequests.resend("req_xxx");
360
+
361
+ // Delete a file request
362
+ await client.fileRequests.delete("req_xxx");
363
+ ```
364
+
365
+ ### Search
366
+
367
+ ```typescript
368
+ const results = await client.search.query({
369
+ workspaceId: "ws_abc",
370
+ q: "quarterly report",
371
+ page: 1,
372
+ perPage: 20,
373
+ });
374
+
375
+ // Results include files, folders, share links, and file requests
376
+ console.log(results.files);
377
+ console.log(results.folders);
378
+ console.log(results.shared);
379
+ console.log(results.fileRequests);
380
+ ```
381
+
382
+ ### Comments
383
+
384
+ ```typescript
385
+ // List comments on a file
386
+ const { comments } = await client.comments.list({
387
+ workspaceId: "ws_abc",
388
+ fileId: "file_xxx",
389
+ });
390
+
391
+ // Add a comment
392
+ const { comment } = await client.comments.create({
393
+ workspaceId: "ws_abc",
394
+ fileId: "file_xxx",
395
+ body: "Looks good, approved!",
396
+ });
397
+
398
+ // Reply to a comment
399
+ await client.comments.create({
400
+ workspaceId: "ws_abc",
401
+ fileId: "file_xxx",
402
+ parentId: comment.id,
403
+ body: "Thanks!",
404
+ });
405
+
406
+ // Edit a comment
407
+ await client.comments.edit("comment_xxx", "Updated text");
408
+
409
+ // Delete a comment
410
+ await client.comments.delete("comment_xxx");
411
+ ```
412
+
413
+ ### Activity Log
414
+
415
+ ```typescript
416
+ const { activities, members, pagination } = await client.activity.list({
417
+ workspaceId: "ws_abc",
418
+ category: "files", // optional filter
419
+ action: "file_uploaded", // optional filter
420
+ userId: "user_xxx", // optional filter
421
+ page: 1,
422
+ perPage: 50,
423
+ });
424
+ ```
425
+
426
+ ### User Profile & API Keys
427
+
428
+ ```typescript
429
+ // Get current user
430
+ const { user } = await client.me.profile();
431
+
432
+ // List API keys
433
+ const { keys } = await client.me.listApiKeys();
434
+
435
+ // Create a new API key
436
+ const { key } = await client.me.createApiKey({
437
+ name: "CI/CD Pipeline",
438
+ scope: "upload",
439
+ expiresInDays: 90,
440
+ });
441
+ console.log(key.plainKey); // only shown once
442
+
443
+ // Delete an API key
444
+ await client.me.deleteApiKey("key_xxx");
445
+ ```
446
+
447
+ ## Error Handling
448
+
449
+ All errors extend `DosyaError` for easy catching.
450
+
451
+ ```typescript
452
+ import { DosyaApiError, DosyaNetworkError, DosyaUploadError } from "dosyadev";
453
+
454
+ try {
455
+ await client.files.get("file_xxx");
456
+ } catch (err) {
457
+ if (err instanceof DosyaApiError) {
458
+ // Server returned { ok: false, error: "..." }
459
+ console.log(err.status); // 404
460
+ console.log(err.errorMessage); // "File not found"
461
+ }
462
+
463
+ if (err instanceof DosyaNetworkError) {
464
+ // fetch() failed (timeout, DNS, offline)
465
+ console.log(err.cause);
466
+ }
467
+
468
+ if (err instanceof DosyaUploadError) {
469
+ // Upload-specific failure
470
+ console.log(err.sessionId); // "upl_xxx"
471
+ console.log(err.partNumber); // 5 (which part failed)
472
+ }
473
+ }
474
+ ```
475
+
476
+ ## API Reference
477
+
478
+ ### All Resources & Methods
479
+
480
+ | Resource | Method | Description |
481
+ |----------|--------|-------------|
482
+ | **files** | `list(params)` | List files and folders |
483
+ | | `get(fileId)` | Get file metadata |
484
+ | | `delete(fileId)` | Soft/permanent delete |
485
+ | | `restore(fileId)` | Restore soft-deleted file |
486
+ | | `rename(fileId, name)` | Rename a file |
487
+ | | `move(fileId, folderId)` | Move to folder |
488
+ | | `copy(fileId, options?)` | Copy a file |
489
+ | | `lock(fileId, params)` | Lock a file |
490
+ | | `unlock(fileId)` | Unlock a file |
491
+ | | `hide(fileId, params?)` | Hide from non-admins |
492
+ | | `listVersions(fileId)` | Get version history |
493
+ | | `restoreVersion(fileId, version)` | Restore a version |
494
+ | | `getShareLinks(fileId)` | Get share links |
495
+ | | `createShareLink(fileId, params?)` | Create share link |
496
+ | | `shareByEmail(fileId, params)` | Share via email |
497
+ | | `createShareBundle(params)` | Share multiple files |
498
+ | **folders** | `create(params)` | Create folder(s) |
499
+ | | `get(folderId)` | Get folder metadata |
500
+ | | `rename(folderId, name)` | Rename folder |
501
+ | | `delete(folderId)` | Delete folder |
502
+ | | `move(folderId, parentId)` | Move folder |
503
+ | | `tree(workspaceId)` | Get folder tree |
504
+ | | `lock(folderId, params)` | Lock folder |
505
+ | | `unlock(folderId)` | Unlock folder |
506
+ | **upload** | `file(params)` | Upload a file (auto chunked) |
507
+ | | `resume(sessionId, body, options?)` | Resume failed upload |
508
+ | | `init(params)` | Initialize upload session |
509
+ | | `status(sessionId)` | Check upload progress |
510
+ | **download** | `getUrl(fileId, options?)` | Get presigned URL |
511
+ | | `arrayBuffer(fileId, options?)` | Download as buffer |
512
+ | | `stream(fileId, options?)` | Download as stream |
513
+ | | `raw(fileId, options?)` | Get inline content |
514
+ | **shares** | `list(workspaceId)` | List all share links |
515
+ | | `revoke(linkId)` | Revoke a share link |
516
+ | **workspaces** | `list()` | List workspaces |
517
+ | | `get(workspaceId)` | Get workspace details |
518
+ | | `create(params)` | Create workspace |
519
+ | | `update(workspaceId, params)` | Update workspace |
520
+ | | `updateSettings(workspaceId, settings)` | Update settings |
521
+ | | `delete(workspaceId)` | Delete workspace |
522
+ | **fileRequests** | `create(params)` | Create file request |
523
+ | | `get(requestId)` | Get request details |
524
+ | | `update(requestId, params)` | Update request |
525
+ | | `delete(requestId)` | Delete request |
526
+ | | `listUploads(requestId)` | List received uploads |
527
+ | | `listRecipients(requestId)` | List recipients |
528
+ | | `resend(requestId, recipientIds?)` | Resend invitations |
529
+ | **search** | `query(params)` | Search everything |
530
+ | **comments** | `list(params)` | List comments |
531
+ | | `create(params)` | Add comment |
532
+ | | `edit(commentId, body)` | Edit comment |
533
+ | | `delete(commentId)` | Delete comment |
534
+ | **activity** | `list(params)` | Activity log |
535
+ | **me** | `profile()` | Get current user |
536
+ | | `listApiKeys()` | List API keys |
537
+ | | `createApiKey(params)` | Create API key |
538
+ | | `deleteApiKey(keyId)` | Delete API key |
539
+
540
+ ## Requirements
541
+
542
+ - **Node.js** >= 18 (uses native `fetch`)
543
+ - **Browsers**: All modern browsers with `fetch` support
544
+ - **TypeScript** >= 5.0 (optional — works with plain JavaScript too)
545
+
546
+ ## License
547
+
548
+ MIT