vault-sdk-prod 1.0.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,333 @@
1
+ # Vault SDK
2
+
3
+ A lightweight Node.js SDK for the Vault service. Upload, organize, and manage files and folders, handle storage plans, and connect via WebSocket for real-time events.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ npm install vault-sdk-dev
9
+ ```
10
+
11
+ ## Quick Start
12
+
13
+ ```javascript
14
+ import Vault from "vault-sdk-dev";
15
+
16
+ const vault = new Vault({
17
+ VAULT_ACCESS_KEY: "your-access-key",
18
+ VAULT_SECRET_KEY: "your-secret-key",
19
+ VAULT_CLIENT_API_KEY: "your-client-api-key",
20
+ VAULT_BASE_URL: "https://api.your-service.com",
21
+ VAULT_WS_URL: "wss://api.your-service.com/ws", // optional, for WebSocket
22
+ });
23
+ ```
24
+
25
+ All configuration parameters except `VAULT_WS_URL` are required. The SDK will throw a clear error listing any missing ones.
26
+
27
+ ## API Reference
28
+
29
+ ### File Upload
30
+
31
+ #### `uploadFile(file, vaultId, parentId?)`
32
+
33
+ Upload a single file to the vault. This method is a convenience wrapper for the full secure upload flow used by the SDK:
34
+
35
+ 1. Calculate SHA-256 hash of the content (content dedupe + integrity checks).
36
+ 2. Call `getPresignedUrl(...)` to generate a temporary S3 upload URL.
37
+ 3. Upload the file data directly to the presigned URL.
38
+ 4. Call `registerUpload(...)` to register the file metadata in Vault.
39
+
40
+ ```javascript
41
+ import fs from "fs";
42
+ import crypto from "crypto";
43
+
44
+ const fileBuffer = fs.readFileSync("./photo.jpg");
45
+ const fileHash = crypto.createHash("sha256").update(fileBuffer).digest("hex");
46
+
47
+ // Step 1 & 2: Request a presigned URL from the Vault API
48
+ const presigned = await vault.getPresignedUrl({
49
+ vaultId: "your-vault-id",
50
+ fileName: "photo.jpg",
51
+ fileType: "image/jpeg",
52
+ fileSize: fileBuffer.length,
53
+ contentHash: fileHash,
54
+ folderId: undefined, // optional
55
+ });
56
+
57
+ // Step 3: Upload payload directly to S3 (or storage provider)
58
+ await axios.put(presigned.url, fileBuffer, {
59
+ headers: {
60
+ "Content-Type": presigned.contentType || "application/octet-stream",
61
+ },
62
+ });
63
+
64
+ // Step 4: Register completed upload in Vault
65
+ const result = await vault.registerUpload({
66
+ vaultId: "your-vault-id",
67
+ fileName: "photo.jpg",
68
+ filebaseKey: presigned.key,
69
+ fileSize: fileBuffer.length,
70
+ contentHash: fileHash,
71
+ folderId: undefined,
72
+ });
73
+
74
+ console.log("Registered file:", result);
75
+
76
+ // Convenience wrapper (same result)
77
+ const fullUploadResult = await vault.uploadFile(
78
+ { buffer: fileBuffer, name: "photo.jpg", type: "image/jpeg" },
79
+ "your-vault-id"
80
+ );
81
+
82
+ // Upload into a specific folder
83
+ const result2 = await vault.uploadFile(
84
+ { buffer: fileBuffer, name: "report.pdf", type: "application/pdf" },
85
+ "your-vault-id",
86
+ "parent-folder-id"
87
+ );
88
+ ```
89
+
90
+ #### `uploadFiles(files, vaultId, parentId?)`
91
+
92
+ Upload multiple files in parallel. Each file is handled independently — one failure won't block the others.
93
+
94
+ ```javascript
95
+ const results = await vault.uploadFiles(
96
+ [
97
+ { buffer: buf1, name: "file1.pdf", type: "application/pdf" },
98
+ { buffer: buf2, name: "file2.jpg", type: "image/jpeg" },
99
+ ],
100
+ "your-vault-id"
101
+ );
102
+
103
+ // Each result has a status:
104
+ // { status: "success", fileName: "file1.pdf", ... }
105
+ // { status: "failed", fileName: "file2.jpg", error: "...", code: "..." }
106
+ ```
107
+
108
+ ### File Retrieval
109
+
110
+ #### `getFiles(vaultId, query?)`
111
+
112
+ Search for files by name.
113
+
114
+ ```javascript
115
+ const files = await vault.getFiles('vault-id', 'search-query');
116
+ ```
117
+
118
+ #### `getAllFiles(vaultId)`
119
+
120
+ Get all files in the vault.
121
+
122
+ ```javascript
123
+ const allFiles = await vault.getAllFiles("your-vault-id");
124
+ ```
125
+
126
+ ### File Management
127
+
128
+ #### `deleteFile(vaultId, fileId)`
129
+
130
+ Delete a file.
131
+
132
+ ```javascript
133
+ await vault.deleteFile("your-vault-id", "file-id");
134
+ ```
135
+
136
+ #### `renameItem(vaultId, itemId, newName)`
137
+
138
+ Rename a file or folder.
139
+
140
+ ```javascript
141
+ await vault.renameItem("your-vault-id", "item-id", "New Name.pdf");
142
+ ```
143
+
144
+ ### Starred Files
145
+
146
+ #### `addToStarred(vaultId, fileId, isStarred)`
147
+
148
+ Star or unstar a file.
149
+
150
+ ```javascript
151
+ await vault.addToStarred("your-vault-id", "file-id", true);
152
+ ```
153
+
154
+ #### `getStarredFiles(vaultId)`
155
+
156
+ Get all starred files.
157
+
158
+ ```javascript
159
+ const starred = await vault.getStarredFiles("your-vault-id");
160
+ ```
161
+
162
+ ### Folder Operations
163
+
164
+ #### `createFolder(vaultId, folderName, parentId?)`
165
+
166
+ Create a new folder. Omit `parentId` to create in root.
167
+
168
+ ```javascript
169
+ await vault.createFolder("your-vault-id", "Documents");
170
+ await vault.createFolder("your-vault-id", "Invoices", "parent-folder-id");
171
+ ```
172
+
173
+ #### `deleteFolder(vaultId, folderId)`
174
+
175
+ Delete a folder.
176
+
177
+ ```javascript
178
+ await vault.deleteFolder("your-vault-id", "folder-id");
179
+ ```
180
+
181
+ ### Storage & Plans
182
+
183
+ #### `getStorageDetails(vaultId)`
184
+
185
+ Check your vault's storage usage.
186
+
187
+ ```javascript
188
+ const storage = await vault.getStorageDetails("your-vault-id");
189
+ ```
190
+
191
+ #### `getAllPlans(vaultId)`
192
+
193
+ Get available storage plans.
194
+
195
+ ```javascript
196
+ const plans = await vault.getAllPlans("your-vault-id");
197
+ ```
198
+
199
+ #### `buyPlan(vaultId, priceId)`
200
+
201
+ Purchase a storage plan.
202
+
203
+ ```javascript
204
+ const purchase = await vault.buyPlan("your-vault-id", "price-id");
205
+ ```
206
+
207
+ #### `cancelSubscription(vaultId)`
208
+
209
+ Cancel the active subscription at period end.
210
+
211
+ ```javascript
212
+ const result = await vault.cancelSubscription("your-vault-id");
213
+ ```
214
+
215
+ #### `createUpcomingPlan(vaultId, priceId)`
216
+
217
+ Schedule an upcoming plan (starts after current active plan ends).
218
+
219
+ ```javascript
220
+ const result = await vault.createUpcomingPlan("your-vault-id", "price-id");
221
+ ```
222
+
223
+ #### `cancelUpcomingPlan(vaultId)`
224
+
225
+ Cancel auto-renewal for a pending upcoming plan.
226
+
227
+ ```javascript
228
+ const result = await vault.cancelUpcomingPlan("your-vault-id");
229
+ ```
230
+
231
+ #### `getSubscriptions(vaultId)`
232
+
233
+ Get active subscriptions.
234
+
235
+ ```javascript
236
+ const subs = await vault.getSubscriptions("your-vault-id");
237
+ ```
238
+
239
+ ### Platform Operations
240
+
241
+ #### `createPlatformUser(email, platformId?)`
242
+
243
+ Create a new SDK user link. `platformId` is optional.
244
+
245
+ ```javascript
246
+ const user = await vault.createPlatformUser("user@example.com", "platform-id");
247
+ const sdkUser = await vault.createPlatformUser("user@example.com");
248
+ ```
249
+
250
+ #### `importVault(vaultId, platformId?)`
251
+
252
+ Import an existing vault. When `platformId` is omitted, SDK access is enabled and the client is linked directly to the user.
253
+
254
+ ```javascript
255
+ const result = await vault.importVault("vault-id", "platform-id");
256
+ const resultWithoutPlatform = await vault.importVault("vault-id");
257
+ ```
258
+
259
+ ### Media
260
+
261
+ #### `getMedia(vaultId)`
262
+
263
+ Fetch media associated with a vault.
264
+
265
+ ```javascript
266
+ const media = await vault.getMedia("your-vault-id");
267
+ ```
268
+
269
+ ### WebSocket
270
+
271
+ #### `connectToWebsocket()`
272
+
273
+ Establish a real-time WebSocket connection. Requires `VAULT_WS_URL` in the constructor.
274
+
275
+ ```javascript
276
+ await vault.connectToWebsocket();
277
+
278
+ vault.on("message", (data) => {
279
+ console.log("Received:", data);
280
+ });
281
+
282
+ vault.on("stream_error", (error) => {
283
+ console.error("WebSocket error:", error);
284
+ });
285
+ ```
286
+
287
+ ## Error Handling
288
+
289
+ The SDK provides specific, actionable error messages. All errors include a `code` for programmatic handling.
290
+
291
+ ```javascript
292
+ import Vault, { VaultError, ValidationError } from "vault-sdk-dev";
293
+
294
+ try {
295
+ await vault.uploadFile(file, vaultId);
296
+ } catch (error) {
297
+ if (error instanceof ValidationError) {
298
+ // Parameter validation failed
299
+ console.error(error.message); // "[Vault SDK] 'uploadFile': Parameter 'vaultId' must be a valid string..."
300
+ console.error(error.code); // "INVALID_PARAMETER"
301
+ console.error(error.param); // "vaultId"
302
+ } else if (error instanceof VaultError) {
303
+ // API or network error
304
+ console.error(error.message); // "[Vault SDK] 'uploadFile': Authentication failed..."
305
+ console.error(error.code); // "UNAUTHORIZED"
306
+ console.error(error.status); // 401
307
+ }
308
+ }
309
+ ```
310
+
311
+ ### Error Codes
312
+
313
+ | Code | Description |
314
+ |------|-------------|
315
+ | `MISSING_CONFIG` | Required configuration parameter not provided |
316
+ | `INVALID_PARAMETER` | Method parameter failed validation |
317
+ | `BAD_REQUEST` | Server rejected the request (400) |
318
+ | `UNAUTHORIZED` | Authentication failed — check your keys (401) |
319
+ | `FORBIDDEN` | API key lacks permission for this operation (403) |
320
+ | `NOT_FOUND` | Requested resource doesn't exist (404) |
321
+ | `CONFLICT` | Resource already exists (409) |
322
+ | `FILE_TOO_LARGE` | File exceeds max upload size (413) |
323
+ | `RATE_LIMITED` | Too many requests — slow down (429) |
324
+ | `SERVER_ERROR` | Server-side error (500) |
325
+ | `NETWORK_ERROR` | No response — check network/URL |
326
+ | `WEBSOCKET_ERROR` | WebSocket connection failed |
327
+ | `STORAGE_UPLOAD_FAILED` | File failed to upload to storage |
328
+ | `PRESIGN_FAILED` | Could not get upload URL |
329
+ | `REGISTER_FAILED` | File uploaded but registration failed |
330
+
331
+ ## License
332
+
333
+ vDoIT Technologies Ltd 2025
package/index.js ADDED
@@ -0,0 +1,3 @@
1
+ import Vault from "./src/Vault.js";
2
+ export { VaultError, ValidationError } from "./src/utils/validationError.js";
3
+ export default Vault;
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "vault-sdk-prod",
3
+ "version": "1.0.0",
4
+ "description": "Vault SDK — File storage client for uploading, managing, and organizing files in your vault",
5
+ "main": "index.js",
6
+ "type": "module",
7
+ "scripts": {
8
+ "test": "echo \"Error: no test specified\" && exit 1"
9
+ },
10
+ "author": "vDoIT Technologies Ltd, Sector 66, Gurugram, Haryana 122011, India",
11
+ "contributors": [
12
+ "Shreyash Gupta <shreyashgupta125@gmail.com>"
13
+ ],
14
+ "license": "ISC",
15
+ "dependencies": {
16
+ "axios": "^1.7.5",
17
+ "ws": "^8.18.0"
18
+ },
19
+ "peerDependencies": {
20
+ "axios": "^1.6.0"
21
+ },
22
+ "keywords": [
23
+ "vault",
24
+ "storage",
25
+ "file-upload",
26
+ "s3",
27
+ "sdk"
28
+ ]
29
+ }