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 +333 -0
- package/index.js +3 -0
- package/package.json +29 -0
- package/src/Vault.js +1052 -0
- package/src/utils/validationError.js +109 -0
package/src/Vault.js
ADDED
|
@@ -0,0 +1,1052 @@
|
|
|
1
|
+
import crypto from "crypto";
|
|
2
|
+
import axios from "axios";
|
|
3
|
+
import WebSocket from "ws";
|
|
4
|
+
import EventEmitter from "events";
|
|
5
|
+
import { validator, VaultError, HTTP_ERROR_MAP } from "./utils/validationError.js";
|
|
6
|
+
|
|
7
|
+
class Vault extends EventEmitter {
|
|
8
|
+
/**
|
|
9
|
+
* Create a new Vault SDK instance.
|
|
10
|
+
*
|
|
11
|
+
* @param {Object} config
|
|
12
|
+
* @param {string} config.VAULT_ACCESS_KEY - Your API access key
|
|
13
|
+
* @param {string} config.VAULT_SECRET_KEY - Your API secret key (used for HMAC signing)
|
|
14
|
+
* @param {string} config.VAULT_CLIENT_API_KEY - Your client-specific API key
|
|
15
|
+
* @param {string} config.VAULT_BASE_URL - Base URL of the Vault API (e.g. "https://api.example.com")
|
|
16
|
+
* @param {string} [config.VAULT_WS_URL] - WebSocket URL for real-time events (e.g. "wss://api.example.com/ws")
|
|
17
|
+
*
|
|
18
|
+
* @throws {VaultError} If any required configuration parameter is missing
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* const vault = new Vault({
|
|
22
|
+
* VAULT_ACCESS_KEY: "your-access-key",
|
|
23
|
+
* VAULT_SECRET_KEY: "your-secret-key",
|
|
24
|
+
* VAULT_CLIENT_API_KEY: "your-client-api-key",
|
|
25
|
+
* VAULT_BASE_URL: "https://api.example.com",
|
|
26
|
+
* VAULT_WS_URL: "wss://api.example.com/ws",
|
|
27
|
+
* });
|
|
28
|
+
*/
|
|
29
|
+
constructor({
|
|
30
|
+
VAULT_ACCESS_KEY,
|
|
31
|
+
VAULT_SECRET_KEY,
|
|
32
|
+
VAULT_CLIENT_API_KEY,
|
|
33
|
+
VAULT_BASE_URL,
|
|
34
|
+
VAULT_WS_URL,
|
|
35
|
+
} = {}) {
|
|
36
|
+
super();
|
|
37
|
+
|
|
38
|
+
const missing = [];
|
|
39
|
+
if (!VAULT_ACCESS_KEY) missing.push("VAULT_ACCESS_KEY");
|
|
40
|
+
if (!VAULT_SECRET_KEY) missing.push("VAULT_SECRET_KEY");
|
|
41
|
+
if (!VAULT_CLIENT_API_KEY) missing.push("VAULT_CLIENT_API_KEY");
|
|
42
|
+
if (!VAULT_BASE_URL) missing.push("VAULT_BASE_URL");
|
|
43
|
+
|
|
44
|
+
if (missing.length > 0) {
|
|
45
|
+
throw new VaultError(
|
|
46
|
+
`[Vault SDK] Missing required configuration: ${missing.join(", ")}. ` +
|
|
47
|
+
`All of VAULT_ACCESS_KEY, VAULT_SECRET_KEY, VAULT_CLIENT_API_KEY, and VAULT_BASE_URL must be provided.`,
|
|
48
|
+
{ code: "MISSING_CONFIG", operation: "constructor" }
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
this.apiKey = VAULT_ACCESS_KEY;
|
|
53
|
+
this.apiSecret = VAULT_SECRET_KEY;
|
|
54
|
+
this.clientApiKey = VAULT_CLIENT_API_KEY;
|
|
55
|
+
this.baseUrl = VAULT_BASE_URL;
|
|
56
|
+
this.wsUrl = VAULT_WS_URL;
|
|
57
|
+
this.ws = null;
|
|
58
|
+
|
|
59
|
+
this.httpClient = axios.create({
|
|
60
|
+
baseURL: this.baseUrl,
|
|
61
|
+
headers: {
|
|
62
|
+
"Content-Type": "application/json",
|
|
63
|
+
"API-Key": this.apiKey,
|
|
64
|
+
},
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Internal: Make an authenticated HTTP request to the Vault API.
|
|
70
|
+
* Automatically generates HMAC-SHA256 signature for each request.
|
|
71
|
+
*
|
|
72
|
+
* @param {string} method - HTTP method (GET, POST, PUT, DELETE)
|
|
73
|
+
* @param {string} endpoint - API endpoint path
|
|
74
|
+
* @param {Object} [payload] - Request body
|
|
75
|
+
* @param {Object} [options] - Additional options (e.g. extra headers)
|
|
76
|
+
* @returns {Promise<Object>} Response from the API
|
|
77
|
+
* @throws {VaultError} On API or network errors
|
|
78
|
+
*/
|
|
79
|
+
async request(method, endpoint, payload, options = {}) {
|
|
80
|
+
const timestamp = Date.now().toString();
|
|
81
|
+
const signature = this.sign(timestamp);
|
|
82
|
+
|
|
83
|
+
const headers = {
|
|
84
|
+
timestamp,
|
|
85
|
+
signature,
|
|
86
|
+
apikey: this.apiKey,
|
|
87
|
+
"x-client-api-key": this.clientApiKey,
|
|
88
|
+
...options.headers,
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
try {
|
|
92
|
+
const response = await this.httpClient.request({
|
|
93
|
+
method,
|
|
94
|
+
url: endpoint,
|
|
95
|
+
headers,
|
|
96
|
+
data: payload,
|
|
97
|
+
});
|
|
98
|
+
return response;
|
|
99
|
+
} catch (error) {
|
|
100
|
+
const operation = options.operation || endpoint;
|
|
101
|
+
|
|
102
|
+
if (error.response) {
|
|
103
|
+
const status = error.response.status;
|
|
104
|
+
const data = error.response.data;
|
|
105
|
+
const serverMessage = data?.message || data?.error || "";
|
|
106
|
+
const errorInfo = HTTP_ERROR_MAP[status] || {
|
|
107
|
+
code: "UNKNOWN_ERROR",
|
|
108
|
+
description: "An unexpected error occurred.",
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
const message = serverMessage
|
|
112
|
+
? `[Vault SDK] ${operation}: ${serverMessage}`
|
|
113
|
+
: `[Vault SDK] ${operation}: ${errorInfo.description}`;
|
|
114
|
+
|
|
115
|
+
throw new VaultError(message, {
|
|
116
|
+
status,
|
|
117
|
+
code: errorInfo.code,
|
|
118
|
+
operation,
|
|
119
|
+
data,
|
|
120
|
+
});
|
|
121
|
+
} else if (error.request) {
|
|
122
|
+
throw new VaultError(
|
|
123
|
+
`[Vault SDK] ${operation}: No response received from the server. ` +
|
|
124
|
+
`Please check your network connection and ensure VAULT_BASE_URL ("${this.baseUrl}") is correct.`,
|
|
125
|
+
{ code: "NETWORK_ERROR", operation }
|
|
126
|
+
);
|
|
127
|
+
} else {
|
|
128
|
+
throw new VaultError(
|
|
129
|
+
`[Vault SDK] ${operation}: Request failed — ${error.message}`,
|
|
130
|
+
{ code: "REQUEST_SETUP_ERROR", operation }
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Internal: Generate HMAC-SHA256 signature for request authentication.
|
|
138
|
+
*
|
|
139
|
+
* @param {string} timestamp - Current timestamp string
|
|
140
|
+
* @returns {string} Hex-encoded HMAC signature
|
|
141
|
+
*/
|
|
142
|
+
sign(timestamp) {
|
|
143
|
+
const message = this.apiKey + timestamp;
|
|
144
|
+
return crypto
|
|
145
|
+
.createHmac("sha256", this.apiSecret)
|
|
146
|
+
.update(message)
|
|
147
|
+
.digest("hex");
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// ─── WebSocket ────────────────────────────────────────────────
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Establish a WebSocket connection for real-time vault events.
|
|
154
|
+
*
|
|
155
|
+
* @returns {Promise<void>} Resolves when connection is established
|
|
156
|
+
* @throws {VaultError} If VAULT_WS_URL is not configured or connection fails
|
|
157
|
+
*
|
|
158
|
+
* @example
|
|
159
|
+
* await vault.connectToWebsocket();
|
|
160
|
+
* vault.on("message", (data) => console.log("Received:", data));
|
|
161
|
+
* vault.on("stream_error", (err) => console.error("WS error:", err));
|
|
162
|
+
*/
|
|
163
|
+
async connectToWebsocket() {
|
|
164
|
+
if (!this.wsUrl) {
|
|
165
|
+
throw new VaultError(
|
|
166
|
+
"[Vault SDK] 'connectToWebsocket': VAULT_WS_URL is not configured. " +
|
|
167
|
+
"Provide it in the constructor to use WebSocket features.",
|
|
168
|
+
{ code: "MISSING_CONFIG", operation: "connectToWebsocket" }
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
const timestamp = Date.now().toString();
|
|
173
|
+
const signature = this.sign(timestamp);
|
|
174
|
+
|
|
175
|
+
return new Promise((resolve, reject) => {
|
|
176
|
+
this.ws = new WebSocket(
|
|
177
|
+
`${this.wsUrl}?apikey=${this.apiKey}&signature=${signature}×tamp=${timestamp}&clientApiKey=${this.clientApiKey}`
|
|
178
|
+
);
|
|
179
|
+
|
|
180
|
+
this.ws.onopen = () => {
|
|
181
|
+
resolve();
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
this.ws.onmessage = this.wsOnMessage.bind(this);
|
|
185
|
+
this.ws.onclose = () => {};
|
|
186
|
+
|
|
187
|
+
this.ws.onerror = (error) => {
|
|
188
|
+
reject(
|
|
189
|
+
new VaultError(
|
|
190
|
+
`[Vault SDK] 'connectToWebsocket': WebSocket connection failed — ${error.message || "Unknown error"}`,
|
|
191
|
+
{ code: "WEBSOCKET_ERROR", operation: "connectToWebsocket" }
|
|
192
|
+
)
|
|
193
|
+
);
|
|
194
|
+
};
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Internal: Handle incoming WebSocket messages.
|
|
200
|
+
* @private
|
|
201
|
+
*/
|
|
202
|
+
wsOnMessage(event) {
|
|
203
|
+
try {
|
|
204
|
+
const response = JSON.parse(event.data);
|
|
205
|
+
this.emit("message", response);
|
|
206
|
+
return response;
|
|
207
|
+
} catch (error) {
|
|
208
|
+
this.emit(
|
|
209
|
+
"stream_error",
|
|
210
|
+
`[Vault SDK] Failed to parse WebSocket message: ${error.message}`
|
|
211
|
+
);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Internal: Handle WebSocket errors.
|
|
217
|
+
* @private
|
|
218
|
+
*/
|
|
219
|
+
wsOnError(error) {
|
|
220
|
+
this.emit("stream_error", error.message);
|
|
221
|
+
return error;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// ─── File Upload ──────────────────────────────────────────────
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Upload a single file to the vault.
|
|
228
|
+
*
|
|
229
|
+
* The upload is a 3-step process:
|
|
230
|
+
* 1. Compute SHA-256 hash of the file content
|
|
231
|
+
* 2. Get a presigned S3 URL from the server
|
|
232
|
+
* 3. Upload directly to S3, then register the upload
|
|
233
|
+
*
|
|
234
|
+
* @param {Object} file - File object to upload
|
|
235
|
+
* @param {Buffer|Uint8Array} file.buffer - File content as a buffer
|
|
236
|
+
* @param {string} file.name - File name (e.g. "document.pdf")
|
|
237
|
+
* @param {string} [file.type] - MIME type (e.g. "application/pdf"). Defaults to "application/octet-stream"
|
|
238
|
+
* @param {string} vaultId - The vault ID to upload to
|
|
239
|
+
* @param {string} [parentId] - Parent folder ID (omit or null for root)
|
|
240
|
+
* @returns {Promise<Object>} Upload result with file details
|
|
241
|
+
*
|
|
242
|
+
* @throws {VaultError} If upload fails at any step
|
|
243
|
+
* @throws {ValidationError} If required parameters are missing/invalid
|
|
244
|
+
*
|
|
245
|
+
* @example
|
|
246
|
+
* import fs from "fs";
|
|
247
|
+
* const fileBuffer = fs.readFileSync("./photo.jpg");
|
|
248
|
+
* const result = await vault.uploadFile(
|
|
249
|
+
* { buffer: fileBuffer, name: "photo.jpg", type: "image/jpeg" },
|
|
250
|
+
* "your-vault-id"
|
|
251
|
+
* );
|
|
252
|
+
*/
|
|
253
|
+
async uploadFile(file, vaultId, parentId) {
|
|
254
|
+
validator.validate(
|
|
255
|
+
{
|
|
256
|
+
file: {
|
|
257
|
+
value: file,
|
|
258
|
+
type: "object",
|
|
259
|
+
message:
|
|
260
|
+
"[Vault SDK] 'uploadFile': The 'file' parameter must be an object with { buffer, name } properties.",
|
|
261
|
+
},
|
|
262
|
+
vaultId: { value: vaultId, type: "string" },
|
|
263
|
+
},
|
|
264
|
+
"uploadFile"
|
|
265
|
+
);
|
|
266
|
+
|
|
267
|
+
if (!file.buffer) {
|
|
268
|
+
throw new VaultError(
|
|
269
|
+
"[Vault SDK] 'uploadFile': file.buffer is required. Provide the file content as a Buffer or Uint8Array.",
|
|
270
|
+
{ code: "INVALID_PARAMETER", operation: "uploadFile" }
|
|
271
|
+
);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
if (!file.name || typeof file.name !== "string" || !file.name.trim()) {
|
|
275
|
+
throw new VaultError(
|
|
276
|
+
"[Vault SDK] 'uploadFile': file.name is required. Provide the file name as a non-empty string (e.g. 'document.pdf').",
|
|
277
|
+
{ code: "INVALID_PARAMETER", operation: "uploadFile" }
|
|
278
|
+
);
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
const { buffer, name, type } = file;
|
|
282
|
+
const size = buffer.length;
|
|
283
|
+
|
|
284
|
+
if (size === 0) {
|
|
285
|
+
throw new VaultError(
|
|
286
|
+
"[Vault SDK] 'uploadFile': file.buffer is empty. Cannot upload a zero-byte file.",
|
|
287
|
+
{ code: "INVALID_PARAMETER", operation: "uploadFile" }
|
|
288
|
+
);
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
// Step 1: Calculate SHA-256 hash
|
|
292
|
+
const hash = crypto.createHash("sha256").update(buffer).digest("hex");
|
|
293
|
+
|
|
294
|
+
// Step 2: Get presigned URL
|
|
295
|
+
let presignedRes;
|
|
296
|
+
try {
|
|
297
|
+
presignedRes = await this.getPresignedUrl({
|
|
298
|
+
vaultId,
|
|
299
|
+
fileName: name,
|
|
300
|
+
fileType: type || "application/octet-stream",
|
|
301
|
+
fileSize: size,
|
|
302
|
+
contentHash: hash,
|
|
303
|
+
folderId: parentId,
|
|
304
|
+
});
|
|
305
|
+
} catch (error) {
|
|
306
|
+
if (error instanceof VaultError) throw error;
|
|
307
|
+
throw new VaultError(
|
|
308
|
+
`[Vault SDK] 'uploadFile': Failed to get upload URL for "${name}" — ${error.message}`,
|
|
309
|
+
{ code: "PRESIGN_FAILED", operation: "uploadFile" }
|
|
310
|
+
);
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
const { url, key, contentType, sanitizedName } = presignedRes;
|
|
314
|
+
|
|
315
|
+
// Step 3: Upload to S3
|
|
316
|
+
try {
|
|
317
|
+
await axios.put(url, buffer, {
|
|
318
|
+
headers: {
|
|
319
|
+
"Content-Type": contentType,
|
|
320
|
+
"x-amz-meta-original-filename": sanitizedName || name,
|
|
321
|
+
"x-amz-meta-content-hash": hash,
|
|
322
|
+
"x-amz-meta-vault-id": vaultId,
|
|
323
|
+
"x-amz-meta-folder-id": parentId || "root",
|
|
324
|
+
"x-amz-meta-file-size": size.toString(),
|
|
325
|
+
},
|
|
326
|
+
});
|
|
327
|
+
} catch (error) {
|
|
328
|
+
const status = error.response?.status;
|
|
329
|
+
let detail = error.message;
|
|
330
|
+
if (status === 403)
|
|
331
|
+
detail =
|
|
332
|
+
"The presigned URL has expired or required signing headers are missing. Please try uploading again.";
|
|
333
|
+
if (status === 413)
|
|
334
|
+
detail = `File "${name}" exceeds the maximum allowed upload size.`;
|
|
335
|
+
|
|
336
|
+
throw new VaultError(
|
|
337
|
+
`[Vault SDK] 'uploadFile': Failed to upload "${name}" to storage — ${detail}`,
|
|
338
|
+
{
|
|
339
|
+
status,
|
|
340
|
+
code: "STORAGE_UPLOAD_FAILED",
|
|
341
|
+
operation: "uploadFile",
|
|
342
|
+
}
|
|
343
|
+
);
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
// Step 4: Register the upload
|
|
347
|
+
try {
|
|
348
|
+
return await this.registerUpload({
|
|
349
|
+
vaultId,
|
|
350
|
+
fileName: name,
|
|
351
|
+
filebaseKey: key,
|
|
352
|
+
fileSize: size,
|
|
353
|
+
contentHash: hash,
|
|
354
|
+
folderId: parentId,
|
|
355
|
+
});
|
|
356
|
+
} catch (error) {
|
|
357
|
+
if (error instanceof VaultError) throw error;
|
|
358
|
+
throw new VaultError(
|
|
359
|
+
`[Vault SDK] 'uploadFile': File "${name}" was uploaded to storage but failed to register. ` +
|
|
360
|
+
`Please contact support if this persists — ${error.message}`,
|
|
361
|
+
{ code: "REGISTER_FAILED", operation: "uploadFile" }
|
|
362
|
+
);
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* Upload multiple files to the vault in parallel.
|
|
368
|
+
*
|
|
369
|
+
* Each file is uploaded independently. Failed uploads do not block others.
|
|
370
|
+
*
|
|
371
|
+
* @param {Array<Object>} files - Array of file objects, each with { buffer, name, type? }
|
|
372
|
+
* @param {string} vaultId - The vault ID to upload to
|
|
373
|
+
* @param {string} [parentId] - Parent folder ID (omit or null for root)
|
|
374
|
+
* @returns {Promise<Array<Object>>} Array of results, each with status "success" or "failed"
|
|
375
|
+
*
|
|
376
|
+
* @example
|
|
377
|
+
* const results = await vault.uploadFiles(
|
|
378
|
+
* [
|
|
379
|
+
* { buffer: buf1, name: "file1.pdf", type: "application/pdf" },
|
|
380
|
+
* { buffer: buf2, name: "file2.jpg", type: "image/jpeg" },
|
|
381
|
+
* ],
|
|
382
|
+
* "your-vault-id"
|
|
383
|
+
* );
|
|
384
|
+
*/
|
|
385
|
+
async uploadFiles(files, vaultId, parentId = null) {
|
|
386
|
+
validator.validate(
|
|
387
|
+
{
|
|
388
|
+
files: {
|
|
389
|
+
value: files,
|
|
390
|
+
type: "array",
|
|
391
|
+
message:
|
|
392
|
+
"[Vault SDK] 'uploadFiles': 'files' must be a non-empty array of file objects ({ buffer, name }).",
|
|
393
|
+
},
|
|
394
|
+
vaultId: { value: vaultId, type: "string" },
|
|
395
|
+
},
|
|
396
|
+
"uploadFiles"
|
|
397
|
+
);
|
|
398
|
+
|
|
399
|
+
const uploadPromises = files.map(async (file, index) => {
|
|
400
|
+
try {
|
|
401
|
+
if (!file || typeof file !== "object") {
|
|
402
|
+
throw new VaultError(
|
|
403
|
+
`File at index ${index} is not a valid object. Each file must have { buffer, name }.`,
|
|
404
|
+
{ code: "INVALID_PARAMETER", operation: "uploadFiles" }
|
|
405
|
+
);
|
|
406
|
+
}
|
|
407
|
+
const result = await this.uploadFile(file, vaultId, parentId);
|
|
408
|
+
return { ...result, status: "success", fileName: file.name };
|
|
409
|
+
} catch (error) {
|
|
410
|
+
return {
|
|
411
|
+
status: "failed",
|
|
412
|
+
fileName: file?.name || `file[${index}]`,
|
|
413
|
+
error: error.message,
|
|
414
|
+
code: error.code || "UPLOAD_FAILED",
|
|
415
|
+
};
|
|
416
|
+
}
|
|
417
|
+
});
|
|
418
|
+
|
|
419
|
+
return await Promise.all(uploadPromises);
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
// ─── File Retrieval ───────────────────────────────────────────
|
|
423
|
+
|
|
424
|
+
/**
|
|
425
|
+
* Search for files in the vault by name or query.
|
|
426
|
+
*
|
|
427
|
+
* @param {string} vaultId - The vault ID to search in
|
|
428
|
+
* @param {string} [query=""] - Search query to filter files by name
|
|
429
|
+
* @returns {Promise<Object>} Matching files
|
|
430
|
+
*
|
|
431
|
+
* @example
|
|
432
|
+
* const files = await vault.getFiles("your-vault-id", "report");
|
|
433
|
+
*/
|
|
434
|
+
async getFiles(vaultId, query = "") {
|
|
435
|
+
validator.validate(
|
|
436
|
+
{
|
|
437
|
+
vaultId: { value: vaultId, type: "string" },
|
|
438
|
+
query: { value: query, type: "string", required: false },
|
|
439
|
+
},
|
|
440
|
+
"getFiles"
|
|
441
|
+
);
|
|
442
|
+
|
|
443
|
+
const queryString = `?vaultId=${encodeURIComponent(vaultId)}&query=${encodeURIComponent(query)}`;
|
|
444
|
+
const response = await this.request(
|
|
445
|
+
"GET",
|
|
446
|
+
`/v1/vault-sdk/get-files${queryString}`,
|
|
447
|
+
undefined,
|
|
448
|
+
{ operation: "getFiles" }
|
|
449
|
+
);
|
|
450
|
+
return response.data;
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
/**
|
|
454
|
+
* Get all files in the vault.
|
|
455
|
+
*
|
|
456
|
+
* @param {string} vaultId - The vault ID
|
|
457
|
+
* @returns {Promise<Object>} All files in the vault
|
|
458
|
+
*
|
|
459
|
+
* @example
|
|
460
|
+
* const allFiles = await vault.getAllFiles("your-vault-id");
|
|
461
|
+
*/
|
|
462
|
+
async getAllFiles(vaultId) {
|
|
463
|
+
validator.validate(
|
|
464
|
+
{
|
|
465
|
+
vaultId: { value: vaultId, type: "string" },
|
|
466
|
+
},
|
|
467
|
+
"getAllFiles"
|
|
468
|
+
);
|
|
469
|
+
|
|
470
|
+
const queryString = `?vaultId=${encodeURIComponent(vaultId)}`;
|
|
471
|
+
const response = await this.request(
|
|
472
|
+
"GET",
|
|
473
|
+
`/v1/vault-sdk/all-files${queryString}`,
|
|
474
|
+
undefined,
|
|
475
|
+
{ operation: "getAllFiles" }
|
|
476
|
+
);
|
|
477
|
+
return response.data;
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
// ─── Storage & Plans ──────────────────────────────────────────
|
|
481
|
+
|
|
482
|
+
/**
|
|
483
|
+
* Get storage usage details for the vault (used space, total space, etc.).
|
|
484
|
+
*
|
|
485
|
+
* @param {string} vaultId - The vault ID
|
|
486
|
+
* @returns {Promise<Object>} Storage usage information
|
|
487
|
+
*
|
|
488
|
+
* @example
|
|
489
|
+
* const storage = await vault.getStorageDetails("your-vault-id");
|
|
490
|
+
*/
|
|
491
|
+
async getStorageDetails(vaultId) {
|
|
492
|
+
validator.validate(
|
|
493
|
+
{
|
|
494
|
+
vaultId: { value: vaultId, type: "string" },
|
|
495
|
+
},
|
|
496
|
+
"getStorageDetails"
|
|
497
|
+
);
|
|
498
|
+
|
|
499
|
+
const queryString = `?vaultId=${encodeURIComponent(vaultId)}`;
|
|
500
|
+
const response = await this.request(
|
|
501
|
+
"GET",
|
|
502
|
+
`/v1/vault-sdk/storage-details${queryString}`,
|
|
503
|
+
undefined,
|
|
504
|
+
{ operation: "getStorageDetails" }
|
|
505
|
+
);
|
|
506
|
+
return response.data;
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
/**
|
|
510
|
+
* Get all available storage plans.
|
|
511
|
+
*
|
|
512
|
+
* @param {string} vaultId - The vault ID
|
|
513
|
+
* @returns {Promise<Object>} Available storage plans
|
|
514
|
+
*
|
|
515
|
+
* @example
|
|
516
|
+
* const plans = await vault.getAllPlans("your-vault-id");
|
|
517
|
+
*/
|
|
518
|
+
async getAllPlans(vaultId) {
|
|
519
|
+
validator.validate(
|
|
520
|
+
{
|
|
521
|
+
vaultId: { value: vaultId, type: "string" },
|
|
522
|
+
},
|
|
523
|
+
"getAllPlans"
|
|
524
|
+
);
|
|
525
|
+
|
|
526
|
+
const queryString = `?vaultId=${encodeURIComponent(vaultId)}`;
|
|
527
|
+
const response = await this.request(
|
|
528
|
+
"GET",
|
|
529
|
+
`/v1/vault-sdk/all-plans${queryString}`,
|
|
530
|
+
undefined,
|
|
531
|
+
{ operation: "getAllPlans" }
|
|
532
|
+
);
|
|
533
|
+
return response.data;
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
/**
|
|
537
|
+
* Purchase a storage plan.
|
|
538
|
+
*
|
|
539
|
+
* @param {string} vaultId - The vault ID
|
|
540
|
+
* @param {string} priceId - The price ID of the plan to purchase
|
|
541
|
+
* @returns {Promise<Object>} Purchase confirmation
|
|
542
|
+
*
|
|
543
|
+
* @example
|
|
544
|
+
* const purchase = await vault.buyPlan("your-vault-id", "price-id");
|
|
545
|
+
*/
|
|
546
|
+
async buyPlan(vaultId, priceId) {
|
|
547
|
+
validator.validate(
|
|
548
|
+
{
|
|
549
|
+
vaultId: { value: vaultId, type: "string" },
|
|
550
|
+
priceId: { value: priceId, type: "string" },
|
|
551
|
+
},
|
|
552
|
+
"buyPlan"
|
|
553
|
+
);
|
|
554
|
+
|
|
555
|
+
const response = await this.request(
|
|
556
|
+
"POST",
|
|
557
|
+
"/v1/vault-sdk/buy-plan",
|
|
558
|
+
{ vaultId, priceId },
|
|
559
|
+
{ operation: "buyPlan" }
|
|
560
|
+
);
|
|
561
|
+
return response.data;
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
/**
|
|
565
|
+
* Cancel the active subscription at period end.
|
|
566
|
+
*
|
|
567
|
+
* @param {string} vaultId - The vault ID
|
|
568
|
+
* @returns {Promise<Object>} Cancellation scheduling details
|
|
569
|
+
*
|
|
570
|
+
* @example
|
|
571
|
+
* const result = await vault.cancelSubscription("your-vault-id");
|
|
572
|
+
*/
|
|
573
|
+
async cancelSubscription(vaultId) {
|
|
574
|
+
validator.validate(
|
|
575
|
+
{
|
|
576
|
+
vaultId: { value: vaultId, type: "string" },
|
|
577
|
+
},
|
|
578
|
+
"cancelSubscription"
|
|
579
|
+
);
|
|
580
|
+
|
|
581
|
+
const response = await this.request(
|
|
582
|
+
"POST",
|
|
583
|
+
"/v1/vault-sdk/cancel-subscription",
|
|
584
|
+
{ vaultId },
|
|
585
|
+
{ operation: "cancelSubscription" }
|
|
586
|
+
);
|
|
587
|
+
return response.data;
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
/**
|
|
591
|
+
* Schedule an upcoming plan to start after current plan expiry.
|
|
592
|
+
*
|
|
593
|
+
* @param {string} vaultId - The vault ID
|
|
594
|
+
* @param {string} priceId - Stripe price ID for the upcoming plan
|
|
595
|
+
* @returns {Promise<Object>} Upcoming plan scheduling result
|
|
596
|
+
*
|
|
597
|
+
* @example
|
|
598
|
+
* const result = await vault.createUpcomingPlan("your-vault-id", "price-id");
|
|
599
|
+
*/
|
|
600
|
+
async createUpcomingPlan(vaultId, priceId) {
|
|
601
|
+
validator.validate(
|
|
602
|
+
{
|
|
603
|
+
vaultId: { value: vaultId, type: "string" },
|
|
604
|
+
priceId: { value: priceId, type: "string" },
|
|
605
|
+
},
|
|
606
|
+
"createUpcomingPlan"
|
|
607
|
+
);
|
|
608
|
+
|
|
609
|
+
const response = await this.request(
|
|
610
|
+
"POST",
|
|
611
|
+
"/v1/vault-sdk/upcoming",
|
|
612
|
+
{ vaultId, priceId },
|
|
613
|
+
{ operation: "createUpcomingPlan" }
|
|
614
|
+
);
|
|
615
|
+
return response.data;
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
/**
|
|
619
|
+
* Cancel auto-renewal for a pending upcoming plan.
|
|
620
|
+
*
|
|
621
|
+
* @param {string} vaultId - The vault ID
|
|
622
|
+
* @returns {Promise<Object>} Upcoming plan cancellation result
|
|
623
|
+
*
|
|
624
|
+
* @example
|
|
625
|
+
* const result = await vault.cancelUpcomingPlan("your-vault-id");
|
|
626
|
+
*/
|
|
627
|
+
async cancelUpcomingPlan(vaultId) {
|
|
628
|
+
validator.validate(
|
|
629
|
+
{
|
|
630
|
+
vaultId: { value: vaultId, type: "string" },
|
|
631
|
+
},
|
|
632
|
+
"cancelUpcomingPlan"
|
|
633
|
+
);
|
|
634
|
+
|
|
635
|
+
const response = await this.request(
|
|
636
|
+
"POST",
|
|
637
|
+
"/v1/vault-sdk/upcoming/cancel",
|
|
638
|
+
{ vaultId },
|
|
639
|
+
{ operation: "cancelUpcomingPlan" }
|
|
640
|
+
);
|
|
641
|
+
return response.data;
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
/**
|
|
645
|
+
* Get active subscriptions for the vault.
|
|
646
|
+
*
|
|
647
|
+
* @param {string} vaultId - The vault ID
|
|
648
|
+
* @returns {Promise<Object>} Active subscriptions
|
|
649
|
+
*
|
|
650
|
+
* @example
|
|
651
|
+
* const subs = await vault.getSubscriptions("your-vault-id");
|
|
652
|
+
*/
|
|
653
|
+
async getSubscriptions(vaultId) {
|
|
654
|
+
validator.validate(
|
|
655
|
+
{
|
|
656
|
+
vaultId: { value: vaultId, type: "string" },
|
|
657
|
+
},
|
|
658
|
+
"getSubscriptions"
|
|
659
|
+
);
|
|
660
|
+
|
|
661
|
+
const queryString = `?vaultId=${encodeURIComponent(vaultId)}`;
|
|
662
|
+
const response = await this.request(
|
|
663
|
+
"GET",
|
|
664
|
+
`/v1/vault-sdk/subscriptions${queryString}`,
|
|
665
|
+
undefined,
|
|
666
|
+
{ operation: "getSubscriptions" }
|
|
667
|
+
);
|
|
668
|
+
return response.data;
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
// ─── Folder Operations ────────────────────────────────────────
|
|
672
|
+
|
|
673
|
+
/**
|
|
674
|
+
* Create a new folder in the vault.
|
|
675
|
+
*
|
|
676
|
+
* @param {string} vaultId - The vault ID
|
|
677
|
+
* @param {string} folderName - Name for the new folder
|
|
678
|
+
* @param {string} [parentId] - Parent folder ID for nested folders (omit for root)
|
|
679
|
+
* @returns {Promise<Object>} Created folder details
|
|
680
|
+
*
|
|
681
|
+
* @example
|
|
682
|
+
* await vault.createFolder("your-vault-id", "Documents");
|
|
683
|
+
* await vault.createFolder("your-vault-id", "Invoices", "parent-folder-id");
|
|
684
|
+
*/
|
|
685
|
+
async createFolder(vaultId, folderName, parentId = null) {
|
|
686
|
+
validator.validate(
|
|
687
|
+
{
|
|
688
|
+
vaultId: { value: vaultId, type: "string" },
|
|
689
|
+
folderName: { value: folderName, type: "string" },
|
|
690
|
+
},
|
|
691
|
+
"createFolder"
|
|
692
|
+
);
|
|
693
|
+
|
|
694
|
+
const response = await this.request(
|
|
695
|
+
"POST",
|
|
696
|
+
"/v1/vault-sdk/create-folder",
|
|
697
|
+
{ vaultId, folderName, parentId },
|
|
698
|
+
{ operation: "createFolder" }
|
|
699
|
+
);
|
|
700
|
+
return response.data;
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
/**
|
|
704
|
+
* Rename a file or folder in the vault.
|
|
705
|
+
*
|
|
706
|
+
* @param {string} vaultId - The vault ID
|
|
707
|
+
* @param {string} itemId - The ID of the file or folder to rename
|
|
708
|
+
* @param {string} newName - The new name
|
|
709
|
+
* @returns {Promise<Object>} Updated item details
|
|
710
|
+
*
|
|
711
|
+
* @example
|
|
712
|
+
* await vault.renameItem("your-vault-id", "item-id", "New Name.pdf");
|
|
713
|
+
*/
|
|
714
|
+
async renameItem(vaultId, itemId, newName) {
|
|
715
|
+
validator.validate(
|
|
716
|
+
{
|
|
717
|
+
vaultId: { value: vaultId, type: "string" },
|
|
718
|
+
itemId: { value: itemId, type: "string" },
|
|
719
|
+
newName: { value: newName, type: "string" },
|
|
720
|
+
},
|
|
721
|
+
"renameItem"
|
|
722
|
+
);
|
|
723
|
+
|
|
724
|
+
const response = await this.request(
|
|
725
|
+
"POST",
|
|
726
|
+
"/v1/vault-sdk/rename",
|
|
727
|
+
{ vaultId, itemId, newName },
|
|
728
|
+
{ operation: "renameItem" }
|
|
729
|
+
);
|
|
730
|
+
return response.data;
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
/**
|
|
734
|
+
* Delete a folder from the vault.
|
|
735
|
+
*
|
|
736
|
+
* @param {string} vaultId - The vault ID
|
|
737
|
+
* @param {string} folderId - The ID of the folder to delete
|
|
738
|
+
* @returns {Promise<Object>} Deletion confirmation
|
|
739
|
+
*
|
|
740
|
+
* @example
|
|
741
|
+
* await vault.deleteFolder("your-vault-id", "folder-id");
|
|
742
|
+
*/
|
|
743
|
+
async deleteFolder(vaultId, folderId) {
|
|
744
|
+
validator.validate(
|
|
745
|
+
{
|
|
746
|
+
vaultId: { value: vaultId, type: "string" },
|
|
747
|
+
folderId: { value: folderId, type: "string" },
|
|
748
|
+
},
|
|
749
|
+
"deleteFolder"
|
|
750
|
+
);
|
|
751
|
+
|
|
752
|
+
const response = await this.request(
|
|
753
|
+
"DELETE",
|
|
754
|
+
"/v1/vault-sdk/delete-folder",
|
|
755
|
+
{ vaultId, folderId },
|
|
756
|
+
{ operation: "deleteFolder" }
|
|
757
|
+
);
|
|
758
|
+
return response.data;
|
|
759
|
+
}
|
|
760
|
+
|
|
761
|
+
/**
|
|
762
|
+
* Delete a file from the vault.
|
|
763
|
+
*
|
|
764
|
+
* @param {string} vaultId - The vault ID
|
|
765
|
+
* @param {string} fileId - The ID of the file to delete
|
|
766
|
+
* @returns {Promise<Object>} Deletion confirmation
|
|
767
|
+
*
|
|
768
|
+
* @example
|
|
769
|
+
* await vault.deleteFile("your-vault-id", "file-id");
|
|
770
|
+
*/
|
|
771
|
+
async deleteFile(vaultId, fileId) {
|
|
772
|
+
validator.validate(
|
|
773
|
+
{
|
|
774
|
+
vaultId: { value: vaultId, type: "string" },
|
|
775
|
+
fileId: { value: fileId, type: "string" },
|
|
776
|
+
},
|
|
777
|
+
"deleteFile"
|
|
778
|
+
);
|
|
779
|
+
|
|
780
|
+
const response = await this.request(
|
|
781
|
+
"DELETE",
|
|
782
|
+
"/v1/vault-sdk/delete-file",
|
|
783
|
+
{ vaultId, fileId },
|
|
784
|
+
{ operation: "deleteFile" }
|
|
785
|
+
);
|
|
786
|
+
return response.data;
|
|
787
|
+
}
|
|
788
|
+
|
|
789
|
+
// ─── Starred Files ────────────────────────────────────────────
|
|
790
|
+
|
|
791
|
+
/**
|
|
792
|
+
* Mark or unmark a file as starred.
|
|
793
|
+
*
|
|
794
|
+
* @param {string} vaultId - The vault ID
|
|
795
|
+
* @param {string} fileId - The file ID to star/unstar
|
|
796
|
+
* @param {boolean} isStarred - true to star, false to unstar
|
|
797
|
+
* @returns {Promise<Object>} Updated file details
|
|
798
|
+
*
|
|
799
|
+
* @example
|
|
800
|
+
* await vault.addToStarred("your-vault-id", "file-id", true);
|
|
801
|
+
*/
|
|
802
|
+
async addToStarred(vaultId, fileId, isStarred) {
|
|
803
|
+
validator.validate(
|
|
804
|
+
{
|
|
805
|
+
vaultId: { value: vaultId, type: "string" },
|
|
806
|
+
fileId: { value: fileId, type: "string" },
|
|
807
|
+
isStarred: { value: isStarred, type: "boolean" },
|
|
808
|
+
},
|
|
809
|
+
"addToStarred"
|
|
810
|
+
);
|
|
811
|
+
|
|
812
|
+
const response = await this.request(
|
|
813
|
+
"POST",
|
|
814
|
+
"/v1/vault-sdk/add-to-starred",
|
|
815
|
+
{ vaultId, fileId, isStarred },
|
|
816
|
+
{ operation: "addToStarred" }
|
|
817
|
+
);
|
|
818
|
+
return response.data;
|
|
819
|
+
}
|
|
820
|
+
|
|
821
|
+
/**
|
|
822
|
+
* Get all starred files in the vault.
|
|
823
|
+
*
|
|
824
|
+
* @param {string} vaultId - The vault ID
|
|
825
|
+
* @returns {Promise<Object>} Starred files
|
|
826
|
+
*
|
|
827
|
+
* @example
|
|
828
|
+
* const starred = await vault.getStarredFiles("your-vault-id");
|
|
829
|
+
*/
|
|
830
|
+
async getStarredFiles(vaultId) {
|
|
831
|
+
validator.validate(
|
|
832
|
+
{
|
|
833
|
+
vaultId: { value: vaultId, type: "string" },
|
|
834
|
+
},
|
|
835
|
+
"getStarredFiles"
|
|
836
|
+
);
|
|
837
|
+
|
|
838
|
+
const queryString = `?vaultId=${encodeURIComponent(vaultId)}`;
|
|
839
|
+
const response = await this.request(
|
|
840
|
+
"GET",
|
|
841
|
+
`/v1/vault-sdk/get-starred-files${queryString}`,
|
|
842
|
+
undefined,
|
|
843
|
+
{ operation: "getStarredFiles" }
|
|
844
|
+
);
|
|
845
|
+
return response.data;
|
|
846
|
+
}
|
|
847
|
+
|
|
848
|
+
// ─── Platform Operations ──────────────────────────────────────
|
|
849
|
+
|
|
850
|
+
/**
|
|
851
|
+
* Create a new platform user.
|
|
852
|
+
*
|
|
853
|
+
* @param {string} email - User's email address
|
|
854
|
+
* @param {string} [platformId] - Optional platform ID to create the user in
|
|
855
|
+
* @returns {Promise<Object>} Created user details
|
|
856
|
+
*
|
|
857
|
+
* @example
|
|
858
|
+
* const user = await vault.createPlatformUser("user@example.com", "platform-id");
|
|
859
|
+
* const sdkUser = await vault.createPlatformUser("user@example.com"); // platform-less SDK user link
|
|
860
|
+
*/
|
|
861
|
+
async createPlatformUser(email, platformId) {
|
|
862
|
+
const normalizedPlatformId =
|
|
863
|
+
typeof platformId === "string" ? platformId.trim() : platformId;
|
|
864
|
+
|
|
865
|
+
validator.validate(
|
|
866
|
+
{
|
|
867
|
+
email: { value: email, type: "string" },
|
|
868
|
+
platformId: {
|
|
869
|
+
value: normalizedPlatformId || undefined,
|
|
870
|
+
type: "string",
|
|
871
|
+
required: false,
|
|
872
|
+
},
|
|
873
|
+
},
|
|
874
|
+
"createPlatformUser"
|
|
875
|
+
);
|
|
876
|
+
|
|
877
|
+
const payload = { email };
|
|
878
|
+
if (normalizedPlatformId) {
|
|
879
|
+
payload.platformId = normalizedPlatformId;
|
|
880
|
+
}
|
|
881
|
+
|
|
882
|
+
const response = await this.request(
|
|
883
|
+
"POST",
|
|
884
|
+
"/v1/vault-sdk/create-user",
|
|
885
|
+
payload,
|
|
886
|
+
{ operation: "createPlatformUser" }
|
|
887
|
+
);
|
|
888
|
+
return response.data;
|
|
889
|
+
}
|
|
890
|
+
|
|
891
|
+
/**
|
|
892
|
+
* Import an existing vault into a platform.
|
|
893
|
+
*
|
|
894
|
+
* @param {string} vaultId - The vault ID to import
|
|
895
|
+
* @param {string} [platformId] - Optional target platform ID
|
|
896
|
+
* @returns {Promise<Object>} Import result
|
|
897
|
+
*
|
|
898
|
+
* @example
|
|
899
|
+
* const result = await vault.importVault("vault-id", "platform-id");
|
|
900
|
+
* const result = await vault.importVault("vault-id"); // link client + enable SDK access without a platform
|
|
901
|
+
*/
|
|
902
|
+
async importVault(vaultId, platformId) {
|
|
903
|
+
const normalizedPlatformId =
|
|
904
|
+
typeof platformId === "string" ? platformId.trim() : platformId;
|
|
905
|
+
|
|
906
|
+
validator.validate(
|
|
907
|
+
{
|
|
908
|
+
vaultId: { value: vaultId, type: "string" },
|
|
909
|
+
platformId: {
|
|
910
|
+
value: normalizedPlatformId || undefined,
|
|
911
|
+
type: "string",
|
|
912
|
+
required: false,
|
|
913
|
+
},
|
|
914
|
+
},
|
|
915
|
+
"importVault"
|
|
916
|
+
);
|
|
917
|
+
|
|
918
|
+
const payload = { vaultId };
|
|
919
|
+
if (normalizedPlatformId) {
|
|
920
|
+
payload.platformId = normalizedPlatformId;
|
|
921
|
+
}
|
|
922
|
+
|
|
923
|
+
const response = await this.request(
|
|
924
|
+
"POST",
|
|
925
|
+
"/v1/vault-sdk/import-vault",
|
|
926
|
+
payload,
|
|
927
|
+
{ operation: "importVault" }
|
|
928
|
+
);
|
|
929
|
+
return response.data;
|
|
930
|
+
}
|
|
931
|
+
|
|
932
|
+
// ─── Media ────────────────────────────────────────────────────
|
|
933
|
+
|
|
934
|
+
/**
|
|
935
|
+
* Fetch media associated with a vault ID.
|
|
936
|
+
*
|
|
937
|
+
* @param {string} vaultId - The vault ID
|
|
938
|
+
* @returns {Promise<Object>} Media data
|
|
939
|
+
*
|
|
940
|
+
* @example
|
|
941
|
+
* const media = await vault.getMedia("your-vault-id");
|
|
942
|
+
*/
|
|
943
|
+
async getMedia(vaultId) {
|
|
944
|
+
validator.validate(
|
|
945
|
+
{
|
|
946
|
+
vaultId: { value: vaultId, type: "string" },
|
|
947
|
+
},
|
|
948
|
+
"getMedia"
|
|
949
|
+
);
|
|
950
|
+
|
|
951
|
+
const queryString = `?vaultId=${encodeURIComponent(vaultId)}`;
|
|
952
|
+
const response = await this.request(
|
|
953
|
+
"GET",
|
|
954
|
+
`/v1/vault-sdk/get-media${queryString}`,
|
|
955
|
+
undefined,
|
|
956
|
+
{ operation: "getMedia" }
|
|
957
|
+
);
|
|
958
|
+
return response.data;
|
|
959
|
+
}
|
|
960
|
+
|
|
961
|
+
/**
|
|
962
|
+
* Alias for renameItem() — kept for backward compatibility.
|
|
963
|
+
* @param {string} vaultId
|
|
964
|
+
* @param {string} itemId
|
|
965
|
+
* @param {string} newName
|
|
966
|
+
* @returns {Promise<Object>}
|
|
967
|
+
*/
|
|
968
|
+
async renameFile(vaultId, itemId, newName) {
|
|
969
|
+
return this.renameItem(vaultId, itemId, newName);
|
|
970
|
+
}
|
|
971
|
+
|
|
972
|
+
// ─── Internal Helpers ─────────────────────────────────────────
|
|
973
|
+
|
|
974
|
+
/**
|
|
975
|
+
* Internal: Get a presigned S3 URL for file upload.
|
|
976
|
+
* @private
|
|
977
|
+
*/
|
|
978
|
+
async getPresignedUrl({
|
|
979
|
+
vaultId,
|
|
980
|
+
fileName,
|
|
981
|
+
fileType,
|
|
982
|
+
fileSize,
|
|
983
|
+
contentHash,
|
|
984
|
+
folderId,
|
|
985
|
+
}) {
|
|
986
|
+
validator.validate(
|
|
987
|
+
{
|
|
988
|
+
vaultId: { value: vaultId, type: "string" },
|
|
989
|
+
fileName: { value: fileName, type: "string" },
|
|
990
|
+
fileSize: { value: fileSize, type: "number" },
|
|
991
|
+
contentHash: { value: contentHash, type: "string" },
|
|
992
|
+
},
|
|
993
|
+
"getPresignedUrl"
|
|
994
|
+
);
|
|
995
|
+
|
|
996
|
+
const response = await this.request(
|
|
997
|
+
"POST",
|
|
998
|
+
"/v1/vault-sdk/get-presigned-url",
|
|
999
|
+
{
|
|
1000
|
+
vaultId,
|
|
1001
|
+
fileName,
|
|
1002
|
+
fileType: fileType || "application/octet-stream",
|
|
1003
|
+
fileSize,
|
|
1004
|
+
contentHash,
|
|
1005
|
+
folderId,
|
|
1006
|
+
},
|
|
1007
|
+
{ operation: "getPresignedUrl" }
|
|
1008
|
+
);
|
|
1009
|
+
return response.data;
|
|
1010
|
+
}
|
|
1011
|
+
|
|
1012
|
+
/**
|
|
1013
|
+
* Internal: Register a completed file upload with the backend.
|
|
1014
|
+
* @private
|
|
1015
|
+
*/
|
|
1016
|
+
async registerUpload({
|
|
1017
|
+
vaultId,
|
|
1018
|
+
fileName,
|
|
1019
|
+
filebaseKey,
|
|
1020
|
+
fileSize,
|
|
1021
|
+
contentHash,
|
|
1022
|
+
folderId,
|
|
1023
|
+
}) {
|
|
1024
|
+
validator.validate(
|
|
1025
|
+
{
|
|
1026
|
+
vaultId: { value: vaultId, type: "string" },
|
|
1027
|
+
fileName: { value: fileName, type: "string" },
|
|
1028
|
+
filebaseKey: { value: filebaseKey, type: "string" },
|
|
1029
|
+
fileSize: { value: fileSize, type: "number" },
|
|
1030
|
+
contentHash: { value: contentHash, type: "string" },
|
|
1031
|
+
},
|
|
1032
|
+
"registerUpload"
|
|
1033
|
+
);
|
|
1034
|
+
|
|
1035
|
+
const response = await this.request(
|
|
1036
|
+
"POST",
|
|
1037
|
+
"/v1/vault-sdk/register-upload",
|
|
1038
|
+
{
|
|
1039
|
+
vaultId,
|
|
1040
|
+
fileName,
|
|
1041
|
+
filebaseKey,
|
|
1042
|
+
fileSize,
|
|
1043
|
+
contentHash,
|
|
1044
|
+
folderId,
|
|
1045
|
+
},
|
|
1046
|
+
{ operation: "registerUpload" }
|
|
1047
|
+
);
|
|
1048
|
+
return response.data;
|
|
1049
|
+
}
|
|
1050
|
+
}
|
|
1051
|
+
|
|
1052
|
+
export default Vault;
|