hydrousdb 3.0.2 → 3.5.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 +350 -975
- package/dist/index.cjs +942 -534
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +643 -398
- package/dist/index.d.ts +643 -398
- package/dist/index.mjs +1581 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +7 -6
- package/dist/index.js +0 -1179
- package/dist/index.js.map +0 -1
package/dist/index.js
DELETED
|
@@ -1,1179 +0,0 @@
|
|
|
1
|
-
// src/utils/errors.ts
|
|
2
|
-
var HydrousError = class extends Error {
|
|
3
|
-
constructor(message, code, status, requestId, details) {
|
|
4
|
-
super(message);
|
|
5
|
-
this.name = "HydrousError";
|
|
6
|
-
this.code = code;
|
|
7
|
-
this.status = status;
|
|
8
|
-
this.requestId = requestId;
|
|
9
|
-
this.details = details;
|
|
10
|
-
Object.setPrototypeOf(this, new.target.prototype);
|
|
11
|
-
}
|
|
12
|
-
toString() {
|
|
13
|
-
return `HydrousError [${this.code}]: ${this.message}`;
|
|
14
|
-
}
|
|
15
|
-
};
|
|
16
|
-
var AuthError = class extends HydrousError {
|
|
17
|
-
constructor(message, code, status, requestId, details) {
|
|
18
|
-
super(message, code, status, requestId, details);
|
|
19
|
-
this.name = "AuthError";
|
|
20
|
-
}
|
|
21
|
-
};
|
|
22
|
-
var RecordError = class extends HydrousError {
|
|
23
|
-
constructor(message, code, status, requestId, details) {
|
|
24
|
-
super(message, code, status, requestId, details);
|
|
25
|
-
this.name = "RecordError";
|
|
26
|
-
}
|
|
27
|
-
};
|
|
28
|
-
var StorageError = class extends HydrousError {
|
|
29
|
-
constructor(message, code, status, requestId) {
|
|
30
|
-
super(message, code, status, requestId);
|
|
31
|
-
this.name = "StorageError";
|
|
32
|
-
}
|
|
33
|
-
};
|
|
34
|
-
var AnalyticsError = class extends HydrousError {
|
|
35
|
-
constructor(message, code, status, requestId) {
|
|
36
|
-
super(message, code, status, requestId);
|
|
37
|
-
this.name = "AnalyticsError";
|
|
38
|
-
}
|
|
39
|
-
};
|
|
40
|
-
var ValidationError = class extends HydrousError {
|
|
41
|
-
constructor(message, details) {
|
|
42
|
-
super(message, "VALIDATION_ERROR", 400, void 0, details);
|
|
43
|
-
this.name = "ValidationError";
|
|
44
|
-
}
|
|
45
|
-
};
|
|
46
|
-
var NetworkError = class extends HydrousError {
|
|
47
|
-
constructor(message, cause) {
|
|
48
|
-
super(message, "NETWORK_ERROR");
|
|
49
|
-
this.name = "NetworkError";
|
|
50
|
-
if (cause) this.cause = cause;
|
|
51
|
-
}
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
// src/utils/http.ts
|
|
55
|
-
var DEFAULT_BASE_URL = "https://db-api-82687684612.us-central1.run.app";
|
|
56
|
-
var HttpClient = class {
|
|
57
|
-
constructor(baseUrl) {
|
|
58
|
-
this.baseUrl = baseUrl.replace(/\/$/, "");
|
|
59
|
-
}
|
|
60
|
-
async request(path, apiKeyOrOpts, opts = {}) {
|
|
61
|
-
let apiKey;
|
|
62
|
-
let resolvedOpts;
|
|
63
|
-
if (typeof apiKeyOrOpts === "string") {
|
|
64
|
-
apiKey = apiKeyOrOpts;
|
|
65
|
-
resolvedOpts = opts;
|
|
66
|
-
} else {
|
|
67
|
-
apiKey = void 0;
|
|
68
|
-
resolvedOpts = apiKeyOrOpts ?? opts;
|
|
69
|
-
}
|
|
70
|
-
const {
|
|
71
|
-
method = "GET",
|
|
72
|
-
body,
|
|
73
|
-
headers = {},
|
|
74
|
-
rawBody,
|
|
75
|
-
contentType = "application/json"
|
|
76
|
-
} = resolvedOpts;
|
|
77
|
-
const url = `${this.baseUrl}${path}`;
|
|
78
|
-
const reqHeaders = {
|
|
79
|
-
...apiKey ? { "X-Api-Key": apiKey } : {},
|
|
80
|
-
...headers
|
|
81
|
-
};
|
|
82
|
-
let reqBody = null;
|
|
83
|
-
if (rawBody !== void 0) {
|
|
84
|
-
reqBody = rawBody;
|
|
85
|
-
if (contentType) reqHeaders["Content-Type"] = contentType;
|
|
86
|
-
} else if (body !== void 0) {
|
|
87
|
-
reqBody = JSON.stringify(body);
|
|
88
|
-
reqHeaders["Content-Type"] = "application/json";
|
|
89
|
-
}
|
|
90
|
-
let response;
|
|
91
|
-
try {
|
|
92
|
-
response = await fetch(url, {
|
|
93
|
-
method,
|
|
94
|
-
headers: reqHeaders,
|
|
95
|
-
body: reqBody
|
|
96
|
-
});
|
|
97
|
-
} catch (err) {
|
|
98
|
-
throw new NetworkError(
|
|
99
|
-
`Network request failed: ${err instanceof Error ? err.message : String(err)}`,
|
|
100
|
-
err
|
|
101
|
-
);
|
|
102
|
-
}
|
|
103
|
-
const ct = response.headers.get("content-type") ?? "";
|
|
104
|
-
if (!ct.includes("application/json")) {
|
|
105
|
-
if (!response.ok) {
|
|
106
|
-
throw new HydrousError(
|
|
107
|
-
`Request failed with status ${response.status}`,
|
|
108
|
-
`HTTP_${response.status}`,
|
|
109
|
-
response.status
|
|
110
|
-
);
|
|
111
|
-
}
|
|
112
|
-
const buffer = await response.arrayBuffer();
|
|
113
|
-
return buffer;
|
|
114
|
-
}
|
|
115
|
-
let responseData;
|
|
116
|
-
try {
|
|
117
|
-
responseData = await response.json();
|
|
118
|
-
} catch {
|
|
119
|
-
throw new HydrousError(
|
|
120
|
-
"Failed to parse server response as JSON",
|
|
121
|
-
"PARSE_ERROR",
|
|
122
|
-
response.status
|
|
123
|
-
);
|
|
124
|
-
}
|
|
125
|
-
if (!response.ok) {
|
|
126
|
-
const errData = responseData;
|
|
127
|
-
throw new HydrousError(
|
|
128
|
-
errData["error"] ?? `Request failed with status ${response.status}`,
|
|
129
|
-
errData["code"] ?? `HTTP_${response.status}`,
|
|
130
|
-
response.status,
|
|
131
|
-
errData["requestId"],
|
|
132
|
-
errData["details"]
|
|
133
|
-
);
|
|
134
|
-
}
|
|
135
|
-
return responseData;
|
|
136
|
-
}
|
|
137
|
-
get(path, apiKey, headers) {
|
|
138
|
-
return this.request(path, apiKey, { method: "GET", headers });
|
|
139
|
-
}
|
|
140
|
-
post(path, apiKey, body, headers) {
|
|
141
|
-
return this.request(path, apiKey, { method: "POST", body, headers });
|
|
142
|
-
}
|
|
143
|
-
put(path, apiKey, body, headers) {
|
|
144
|
-
return this.request(path, apiKey, { method: "PUT", body, headers });
|
|
145
|
-
}
|
|
146
|
-
patch(path, apiKey, body, headers) {
|
|
147
|
-
return this.request(path, apiKey, { method: "PATCH", body, headers });
|
|
148
|
-
}
|
|
149
|
-
delete(path, apiKey, body, headers) {
|
|
150
|
-
return this.request(path, apiKey, { method: "DELETE", body, headers });
|
|
151
|
-
}
|
|
152
|
-
async putToSignedUrl(signedUrl, data, mimeType, onProgress) {
|
|
153
|
-
const XHR = typeof globalThis["XMLHttpRequest"] !== "undefined" ? globalThis["XMLHttpRequest"] : void 0;
|
|
154
|
-
if (XHR && onProgress) {
|
|
155
|
-
return new Promise((resolve, reject) => {
|
|
156
|
-
const xhr = new XHR();
|
|
157
|
-
xhr.upload.onprogress = (e) => {
|
|
158
|
-
if (e.lengthComputable) {
|
|
159
|
-
onProgress(Math.round(e.loaded / e.total * 100));
|
|
160
|
-
}
|
|
161
|
-
};
|
|
162
|
-
xhr.onload = () => {
|
|
163
|
-
if (xhr.status >= 200 && xhr.status < 300) {
|
|
164
|
-
resolve();
|
|
165
|
-
} else {
|
|
166
|
-
reject(new Error(`Upload failed: ${xhr.status}`));
|
|
167
|
-
}
|
|
168
|
-
};
|
|
169
|
-
xhr.onerror = () => reject(new Error("Upload network error"));
|
|
170
|
-
xhr.open("PUT", signedUrl);
|
|
171
|
-
xhr.setRequestHeader("Content-Type", mimeType);
|
|
172
|
-
const payload = data instanceof Blob ? data : new Blob([data], { type: mimeType });
|
|
173
|
-
xhr.send(payload);
|
|
174
|
-
});
|
|
175
|
-
}
|
|
176
|
-
const fetchBody = data instanceof Blob ? data : new Blob([data], { type: mimeType });
|
|
177
|
-
const response = await fetch(signedUrl, {
|
|
178
|
-
method: "PUT",
|
|
179
|
-
headers: { "Content-Type": mimeType },
|
|
180
|
-
body: fetchBody
|
|
181
|
-
});
|
|
182
|
-
if (!response.ok) {
|
|
183
|
-
throw new NetworkError(`Signed URL upload failed with status ${response.status}`);
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
};
|
|
187
|
-
|
|
188
|
-
// src/auth/client.ts
|
|
189
|
-
var AuthClient = class {
|
|
190
|
-
constructor(http, authKey, bucketKey) {
|
|
191
|
-
this.http = http;
|
|
192
|
-
this.authKey = authKey;
|
|
193
|
-
this.basePath = `/auth/${bucketKey}`;
|
|
194
|
-
}
|
|
195
|
-
post(path, body) {
|
|
196
|
-
return this.http.post(path, this.authKey, body);
|
|
197
|
-
}
|
|
198
|
-
get(path) {
|
|
199
|
-
return this.http.get(path, this.authKey);
|
|
200
|
-
}
|
|
201
|
-
patch(path, body) {
|
|
202
|
-
return this.http.patch(path, this.authKey, body);
|
|
203
|
-
}
|
|
204
|
-
delete(path, body) {
|
|
205
|
-
return this.http.delete(path, this.authKey, body);
|
|
206
|
-
}
|
|
207
|
-
// ─── Registration & Login ─────────────────────────────────────────────────
|
|
208
|
-
async signup(options) {
|
|
209
|
-
const result = await this.post(`${this.basePath}/signup`, options);
|
|
210
|
-
return { user: result.user, session: result.session };
|
|
211
|
-
}
|
|
212
|
-
async login(options) {
|
|
213
|
-
const result = await this.post(`${this.basePath}/login`, options);
|
|
214
|
-
return { user: result.user, session: result.session };
|
|
215
|
-
}
|
|
216
|
-
async logout({ sessionId }) {
|
|
217
|
-
await this.post(`${this.basePath}/logout`, { sessionId });
|
|
218
|
-
}
|
|
219
|
-
async refreshSession({ refreshToken }) {
|
|
220
|
-
const result = await this.post(`${this.basePath}/session/refresh`, { refreshToken });
|
|
221
|
-
return result.session;
|
|
222
|
-
}
|
|
223
|
-
// ─── User Profile ─────────────────────────────────────────────────────────
|
|
224
|
-
async getUser({ userId }) {
|
|
225
|
-
const result = await this.get(`${this.basePath}/user/${userId}`);
|
|
226
|
-
return result.user;
|
|
227
|
-
}
|
|
228
|
-
async updateUser(options) {
|
|
229
|
-
const { sessionId, userId, data } = options;
|
|
230
|
-
const result = await this.patch(`${this.basePath}/user`, { sessionId, userId, ...data });
|
|
231
|
-
return result.user;
|
|
232
|
-
}
|
|
233
|
-
async deleteUser({ sessionId, userId }) {
|
|
234
|
-
await this.delete(`${this.basePath}/user`, { sessionId, userId });
|
|
235
|
-
}
|
|
236
|
-
// ─── Admin Operations ─────────────────────────────────────────────────────
|
|
237
|
-
async listUsers(options) {
|
|
238
|
-
const { sessionId, limit = 50, offset = 0 } = options;
|
|
239
|
-
const result = await this.post(
|
|
240
|
-
`${this.basePath}/users/list`,
|
|
241
|
-
{ sessionId, limit, offset }
|
|
242
|
-
);
|
|
243
|
-
return { users: result.users, total: result.total, limit: result.limit, offset: result.offset };
|
|
244
|
-
}
|
|
245
|
-
async hardDeleteUser({ sessionId, userId }) {
|
|
246
|
-
await this.delete(`${this.basePath}/user/hard`, { sessionId, userId });
|
|
247
|
-
}
|
|
248
|
-
async bulkDeleteUsers({ sessionId, userIds }) {
|
|
249
|
-
const result = await this.post(
|
|
250
|
-
`${this.basePath}/users/bulk-delete`,
|
|
251
|
-
{ sessionId, userIds }
|
|
252
|
-
);
|
|
253
|
-
return { deleted: result.deleted, failed: result.failed };
|
|
254
|
-
}
|
|
255
|
-
async lockAccount({ sessionId, userId, duration }) {
|
|
256
|
-
const result = await this.post(
|
|
257
|
-
`${this.basePath}/account/lock`,
|
|
258
|
-
{ sessionId, userId, duration }
|
|
259
|
-
);
|
|
260
|
-
return result.data;
|
|
261
|
-
}
|
|
262
|
-
async unlockAccount({ sessionId, userId }) {
|
|
263
|
-
await this.post(`${this.basePath}/account/unlock`, { sessionId, userId });
|
|
264
|
-
}
|
|
265
|
-
// ─── Password Management ──────────────────────────────────────────────────
|
|
266
|
-
async changePassword(options) {
|
|
267
|
-
await this.post(`${this.basePath}/password/change`, options);
|
|
268
|
-
}
|
|
269
|
-
async requestPasswordReset({ email }) {
|
|
270
|
-
await this.post(`${this.basePath}/password/reset/request`, { email });
|
|
271
|
-
}
|
|
272
|
-
async confirmPasswordReset({ resetToken, newPassword }) {
|
|
273
|
-
await this.post(`${this.basePath}/password/reset/confirm`, { resetToken, newPassword });
|
|
274
|
-
}
|
|
275
|
-
// ─── Email Verification ───────────────────────────────────────────────────
|
|
276
|
-
async requestEmailVerification({ userId }) {
|
|
277
|
-
await this.post(`${this.basePath}/email/verify/request`, { userId });
|
|
278
|
-
}
|
|
279
|
-
async confirmEmailVerification({ verifyToken }) {
|
|
280
|
-
await this.post(`${this.basePath}/email/verify/confirm`, { verifyToken });
|
|
281
|
-
}
|
|
282
|
-
};
|
|
283
|
-
|
|
284
|
-
// src/utils/query.ts
|
|
285
|
-
function buildQueryParams(options = {}) {
|
|
286
|
-
const params = new URLSearchParams();
|
|
287
|
-
if (options.limit !== void 0) params.set("limit", String(options.limit));
|
|
288
|
-
if (options.offset !== void 0) params.set("offset", String(options.offset));
|
|
289
|
-
if (options.orderBy !== void 0) params.set("orderBy", options.orderBy);
|
|
290
|
-
if (options.order !== void 0) params.set("order", options.order);
|
|
291
|
-
if (options.fields !== void 0) params.set("fields", options.fields);
|
|
292
|
-
if (options.startAfter !== void 0) params.set("startAfter", options.startAfter);
|
|
293
|
-
if (options.startAt !== void 0) params.set("startAt", options.startAt);
|
|
294
|
-
if (options.endAt !== void 0) params.set("endAt", options.endAt);
|
|
295
|
-
if (options.dateRange?.start !== void 0)
|
|
296
|
-
params.set("startDate", new Date(options.dateRange.start).toISOString().split("T")[0]);
|
|
297
|
-
if (options.dateRange?.end !== void 0)
|
|
298
|
-
params.set("endDate", new Date(options.dateRange.end).toISOString().split("T")[0]);
|
|
299
|
-
if (options.filters && options.filters.length > 0) {
|
|
300
|
-
params.set("filters", JSON.stringify(options.filters));
|
|
301
|
-
}
|
|
302
|
-
const str = params.toString();
|
|
303
|
-
return str ? `?${str}` : "";
|
|
304
|
-
}
|
|
305
|
-
function guessMimeType(filename) {
|
|
306
|
-
const ext = filename.split(".").pop()?.toLowerCase();
|
|
307
|
-
const map = {
|
|
308
|
-
jpg: "image/jpeg",
|
|
309
|
-
jpeg: "image/jpeg",
|
|
310
|
-
png: "image/png",
|
|
311
|
-
gif: "image/gif",
|
|
312
|
-
webp: "image/webp",
|
|
313
|
-
svg: "image/svg+xml",
|
|
314
|
-
pdf: "application/pdf",
|
|
315
|
-
mp4: "video/mp4",
|
|
316
|
-
webm: "video/webm",
|
|
317
|
-
mp3: "audio/mpeg",
|
|
318
|
-
wav: "audio/wav",
|
|
319
|
-
txt: "text/plain",
|
|
320
|
-
html: "text/html",
|
|
321
|
-
css: "text/css",
|
|
322
|
-
js: "application/javascript",
|
|
323
|
-
json: "application/json",
|
|
324
|
-
xml: "application/xml",
|
|
325
|
-
zip: "application/zip",
|
|
326
|
-
csv: "text/csv"
|
|
327
|
-
};
|
|
328
|
-
return map[ext ?? ""] ?? "application/octet-stream";
|
|
329
|
-
}
|
|
330
|
-
function assertSafeName(name, label = "name") {
|
|
331
|
-
if (!/^[a-zA-Z_][a-zA-Z0-9_.\-]{0,200}$/.test(name)) {
|
|
332
|
-
throw new Error(
|
|
333
|
-
`Invalid ${label} "${name}". Must start with a letter or underscore and contain only letters, numbers, underscores, dots, or hyphens.`
|
|
334
|
-
);
|
|
335
|
-
}
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
// src/records/client.ts
|
|
339
|
-
var RecordsClient = class {
|
|
340
|
-
constructor(http, bucketSecurityKey, bucketKey) {
|
|
341
|
-
assertSafeName(bucketKey, "bucketKey");
|
|
342
|
-
this.http = http;
|
|
343
|
-
this.bucketKey = bucketKey;
|
|
344
|
-
this.bucketKey_ = bucketSecurityKey;
|
|
345
|
-
this.basePath = `/records/${bucketKey}`;
|
|
346
|
-
}
|
|
347
|
-
get key() {
|
|
348
|
-
return this.bucketKey_;
|
|
349
|
-
}
|
|
350
|
-
// ─── Single Record Operations ─────────────────────────────────────────────
|
|
351
|
-
async create(data) {
|
|
352
|
-
const result = await this.http.post(this.basePath, this.key, data);
|
|
353
|
-
return result.record ?? result.data;
|
|
354
|
-
}
|
|
355
|
-
async get(id) {
|
|
356
|
-
const result = await this.http.get(`${this.basePath}/${id}`, this.key);
|
|
357
|
-
return result.record ?? result.data;
|
|
358
|
-
}
|
|
359
|
-
async set(id, data) {
|
|
360
|
-
const result = await this.http.put(`${this.basePath}/${id}`, this.key, data);
|
|
361
|
-
return result.record ?? result.data;
|
|
362
|
-
}
|
|
363
|
-
async patch(id, data, options = {}) {
|
|
364
|
-
const { merge = true } = options;
|
|
365
|
-
const result = await this.http.patch(
|
|
366
|
-
`${this.basePath}/${id}`,
|
|
367
|
-
this.key,
|
|
368
|
-
{ ...data, _merge: merge }
|
|
369
|
-
);
|
|
370
|
-
return result.record ?? result.data;
|
|
371
|
-
}
|
|
372
|
-
async delete(id) {
|
|
373
|
-
await this.http.delete(`${this.basePath}/${id}`, this.key);
|
|
374
|
-
}
|
|
375
|
-
// ─── Batch Operations ─────────────────────────────────────────────────────
|
|
376
|
-
async batchCreate(items) {
|
|
377
|
-
const result = await this.http.post(
|
|
378
|
-
`${this.basePath}/batch`,
|
|
379
|
-
this.key,
|
|
380
|
-
{ records: items }
|
|
381
|
-
);
|
|
382
|
-
return result.records;
|
|
383
|
-
}
|
|
384
|
-
async batchDelete(ids) {
|
|
385
|
-
const result = await this.http.post(
|
|
386
|
-
`${this.basePath}/batch/delete`,
|
|
387
|
-
this.key,
|
|
388
|
-
{ ids }
|
|
389
|
-
);
|
|
390
|
-
return { deleted: result.deleted, failed: result.failed };
|
|
391
|
-
}
|
|
392
|
-
// ─── Querying ─────────────────────────────────────────────────────────────
|
|
393
|
-
async query(options = {}) {
|
|
394
|
-
const qs = buildQueryParams(options);
|
|
395
|
-
const result = await this.http.get(`${this.basePath}${qs}`, this.key);
|
|
396
|
-
return {
|
|
397
|
-
records: result.records,
|
|
398
|
-
total: result.total,
|
|
399
|
-
hasMore: result.hasMore,
|
|
400
|
-
nextCursor: result.nextCursor
|
|
401
|
-
};
|
|
402
|
-
}
|
|
403
|
-
async getAll(options = {}) {
|
|
404
|
-
const { records } = await this.query(options);
|
|
405
|
-
return records;
|
|
406
|
-
}
|
|
407
|
-
async count(filters = []) {
|
|
408
|
-
const result = await this.http.post(
|
|
409
|
-
`${this.basePath}/count`,
|
|
410
|
-
this.key,
|
|
411
|
-
{ filters }
|
|
412
|
-
);
|
|
413
|
-
return result.count;
|
|
414
|
-
}
|
|
415
|
-
// ─── Version History ──────────────────────────────────────────────────────
|
|
416
|
-
async getHistory(id) {
|
|
417
|
-
const result = await this.http.get(`${this.basePath}/${id}/history`, this.key);
|
|
418
|
-
return result.history;
|
|
419
|
-
}
|
|
420
|
-
async restoreVersion(id, version) {
|
|
421
|
-
const result = await this.http.post(
|
|
422
|
-
`${this.basePath}/${id}/restore`,
|
|
423
|
-
this.key,
|
|
424
|
-
{ version }
|
|
425
|
-
);
|
|
426
|
-
return result.record ?? result.data;
|
|
427
|
-
}
|
|
428
|
-
};
|
|
429
|
-
|
|
430
|
-
// src/analytics/client.ts
|
|
431
|
-
var AnalyticsClient = class {
|
|
432
|
-
constructor(http, bucketSecurityKey, bucketKey) {
|
|
433
|
-
assertSafeName(bucketKey, "bucketKey");
|
|
434
|
-
this.http = http;
|
|
435
|
-
this.bucketSecurityKey = bucketSecurityKey;
|
|
436
|
-
this.basePath = `/analytics/${bucketKey}`;
|
|
437
|
-
}
|
|
438
|
-
async run(query) {
|
|
439
|
-
const result = await this.http.post(
|
|
440
|
-
this.basePath,
|
|
441
|
-
this.bucketSecurityKey,
|
|
442
|
-
query
|
|
443
|
-
);
|
|
444
|
-
return result.data;
|
|
445
|
-
}
|
|
446
|
-
async count(opts = {}) {
|
|
447
|
-
return this.run({ queryType: "count", ...opts });
|
|
448
|
-
}
|
|
449
|
-
async distribution(opts) {
|
|
450
|
-
assertSafeName(opts.field, "field");
|
|
451
|
-
return this.run({ queryType: "distribution", ...opts });
|
|
452
|
-
}
|
|
453
|
-
async sum(opts) {
|
|
454
|
-
assertSafeName(opts.field, "field");
|
|
455
|
-
if (opts.groupBy) assertSafeName(opts.groupBy, "groupBy");
|
|
456
|
-
return this.run({ queryType: "sum", ...opts });
|
|
457
|
-
}
|
|
458
|
-
async timeSeries(opts = {}) {
|
|
459
|
-
return this.run({ queryType: "timeSeries", granularity: "day", ...opts });
|
|
460
|
-
}
|
|
461
|
-
async fieldTimeSeries(opts) {
|
|
462
|
-
assertSafeName(opts.field, "field");
|
|
463
|
-
return this.run({ queryType: "fieldTimeSeries", aggregation: "sum", granularity: "day", ...opts });
|
|
464
|
-
}
|
|
465
|
-
async topN(opts) {
|
|
466
|
-
assertSafeName(opts.field, "field");
|
|
467
|
-
if (opts.labelField) assertSafeName(opts.labelField, "labelField");
|
|
468
|
-
return this.run({ queryType: "topN", n: 10, order: "desc", ...opts });
|
|
469
|
-
}
|
|
470
|
-
async stats(opts) {
|
|
471
|
-
assertSafeName(opts.field, "field");
|
|
472
|
-
return this.run({ queryType: "stats", ...opts });
|
|
473
|
-
}
|
|
474
|
-
async records(opts = {}) {
|
|
475
|
-
if (opts.orderBy) assertSafeName(opts.orderBy, "orderBy");
|
|
476
|
-
if (opts.selectFields) opts.selectFields.forEach((f) => assertSafeName(f, "selectField"));
|
|
477
|
-
return this.run({ queryType: "records", limit: 100, order: "desc", ...opts });
|
|
478
|
-
}
|
|
479
|
-
async multiMetric(opts) {
|
|
480
|
-
opts.metrics.forEach((m) => {
|
|
481
|
-
assertSafeName(m.field, "metric.field");
|
|
482
|
-
assertSafeName(m.name, "metric.name");
|
|
483
|
-
});
|
|
484
|
-
return this.run({ queryType: "multiMetric", ...opts });
|
|
485
|
-
}
|
|
486
|
-
async storageStats(opts = {}) {
|
|
487
|
-
return this.run({ queryType: "storageStats", ...opts });
|
|
488
|
-
}
|
|
489
|
-
async crossBucket(opts) {
|
|
490
|
-
assertSafeName(opts.field, "field");
|
|
491
|
-
opts.bucketKeys.forEach((k) => assertSafeName(k, "bucketKey"));
|
|
492
|
-
return this.run({ queryType: "crossBucket", aggregation: "sum", ...opts });
|
|
493
|
-
}
|
|
494
|
-
async query(query) {
|
|
495
|
-
const data = await this.run(query);
|
|
496
|
-
return { queryType: query.queryType, data };
|
|
497
|
-
}
|
|
498
|
-
};
|
|
499
|
-
|
|
500
|
-
// src/storage/manager.ts
|
|
501
|
-
var StorageManager = class {
|
|
502
|
-
constructor(http, storageKey) {
|
|
503
|
-
this.basePath = "/storage";
|
|
504
|
-
this.http = http;
|
|
505
|
-
this.storageKey = storageKey;
|
|
506
|
-
}
|
|
507
|
-
/** Headers for all storage requests — uses X-Storage-Key, not X-Api-Key. */
|
|
508
|
-
get authHeaders() {
|
|
509
|
-
return { "X-Storage-Key": this.storageKey };
|
|
510
|
-
}
|
|
511
|
-
// ─── Upload: Simple (server-buffered) ────────────────────────────────────
|
|
512
|
-
/**
|
|
513
|
-
* Upload a file to storage in one step (server-buffered, up to 500 MB).
|
|
514
|
-
* For files >10 MB or when you need upload progress, use `getUploadUrl()` instead.
|
|
515
|
-
*
|
|
516
|
-
* @param data File data as a Blob, Buffer, Uint8Array, or ArrayBuffer.
|
|
517
|
-
* @param path Destination path in your storage (e.g. `"avatars/alice.jpg"`).
|
|
518
|
-
* @param options Upload options: isPublic, overwrite, mimeType.
|
|
519
|
-
*
|
|
520
|
-
* @example
|
|
521
|
-
* ```ts
|
|
522
|
-
* // Upload a public avatar
|
|
523
|
-
* const result = await storage.upload(file, 'avatars/alice.jpg', { isPublic: true });
|
|
524
|
-
* console.log(result.publicUrl); // → https://...
|
|
525
|
-
*
|
|
526
|
-
* // Upload a private document
|
|
527
|
-
* const result = await storage.upload(pdfBuffer, 'docs/contract.pdf');
|
|
528
|
-
* console.log(result.downloadUrl); // → /storage/download/docs/contract.pdf
|
|
529
|
-
* ```
|
|
530
|
-
*/
|
|
531
|
-
async upload(data, path, options = {}) {
|
|
532
|
-
const { isPublic = false, overwrite = false, mimeType } = options;
|
|
533
|
-
const mime = mimeType ?? guessMimeType(path);
|
|
534
|
-
const formData = new FormData();
|
|
535
|
-
const blob = data instanceof Blob ? data : new Blob([data], { type: mime });
|
|
536
|
-
formData.append("file", blob, path.split("/").pop() ?? "file");
|
|
537
|
-
formData.append("path", path);
|
|
538
|
-
formData.append("mimeType", mime);
|
|
539
|
-
formData.append("isPublic", String(isPublic));
|
|
540
|
-
formData.append("overwrite", String(overwrite));
|
|
541
|
-
const result = await this.http.request(`${this.basePath}/upload`, {
|
|
542
|
-
method: "POST",
|
|
543
|
-
rawBody: formData,
|
|
544
|
-
headers: this.authHeaders
|
|
545
|
-
});
|
|
546
|
-
return {
|
|
547
|
-
path: result.path,
|
|
548
|
-
mimeType: result.mimeType,
|
|
549
|
-
size: result.size,
|
|
550
|
-
isPublic: result.isPublic,
|
|
551
|
-
publicUrl: result.publicUrl,
|
|
552
|
-
downloadUrl: result.downloadUrl
|
|
553
|
-
};
|
|
554
|
-
}
|
|
555
|
-
/**
|
|
556
|
-
* Upload raw JSON or plain text data as a file.
|
|
557
|
-
*
|
|
558
|
-
* @example
|
|
559
|
-
* ```ts
|
|
560
|
-
* const result = await storage.uploadRaw(
|
|
561
|
-
* { config: { theme: 'dark' } },
|
|
562
|
-
* 'settings/user-config.json',
|
|
563
|
-
* { isPublic: false },
|
|
564
|
-
* );
|
|
565
|
-
* ```
|
|
566
|
-
*/
|
|
567
|
-
async uploadRaw(data, path, options = {}) {
|
|
568
|
-
const { isPublic = false, overwrite = false, mimeType = "application/json" } = options;
|
|
569
|
-
const body = typeof data === "string" ? data : JSON.stringify(data);
|
|
570
|
-
const result = await this.http.request(`${this.basePath}/upload-raw`, {
|
|
571
|
-
method: "POST",
|
|
572
|
-
body: { path, data: body, mimeType, isPublic, overwrite },
|
|
573
|
-
headers: this.authHeaders
|
|
574
|
-
});
|
|
575
|
-
return {
|
|
576
|
-
path: result.path,
|
|
577
|
-
mimeType: result.mimeType,
|
|
578
|
-
size: result.size,
|
|
579
|
-
isPublic: result.isPublic,
|
|
580
|
-
publicUrl: result.publicUrl,
|
|
581
|
-
downloadUrl: result.downloadUrl
|
|
582
|
-
};
|
|
583
|
-
}
|
|
584
|
-
// ─── Upload: Direct-to-GCS (recommended for large files) ─────────────────
|
|
585
|
-
/**
|
|
586
|
-
* Step 1 of the recommended upload flow.
|
|
587
|
-
* Get a signed URL to upload directly to GCS from the client (supports progress).
|
|
588
|
-
*
|
|
589
|
-
* @example
|
|
590
|
-
* ```ts
|
|
591
|
-
* const { uploadUrl, path: confirmedPath } = await storage.getUploadUrl({
|
|
592
|
-
* path: 'videos/intro.mp4',
|
|
593
|
-
* mimeType: 'video/mp4',
|
|
594
|
-
* size: file.size,
|
|
595
|
-
* isPublic: true,
|
|
596
|
-
* });
|
|
597
|
-
*
|
|
598
|
-
* // Upload using XHR for progress tracking
|
|
599
|
-
* await storage.uploadToSignedUrl(uploadUrl, file, 'video/mp4', (pct) => {
|
|
600
|
-
* console.log(`${pct}% uploaded`);
|
|
601
|
-
* });
|
|
602
|
-
*
|
|
603
|
-
* // Step 3: confirm
|
|
604
|
-
* const result = await storage.confirmUpload({ path: confirmedPath, mimeType: 'video/mp4', isPublic: true });
|
|
605
|
-
* ```
|
|
606
|
-
*/
|
|
607
|
-
async getUploadUrl(opts) {
|
|
608
|
-
const result = await this.http.request(`${this.basePath}/upload-url`, {
|
|
609
|
-
method: "POST",
|
|
610
|
-
body: { isPublic: false, overwrite: false, expiresInSeconds: 900, ...opts },
|
|
611
|
-
headers: this.authHeaders
|
|
612
|
-
});
|
|
613
|
-
return result;
|
|
614
|
-
}
|
|
615
|
-
/**
|
|
616
|
-
* Upload data directly to a signed GCS URL (no auth headers needed).
|
|
617
|
-
* Optionally tracks progress via a callback.
|
|
618
|
-
*
|
|
619
|
-
* @param signedUrl The URL returned by `getUploadUrl()`.
|
|
620
|
-
* @param data File data.
|
|
621
|
-
* @param mimeType Must match what was used in `getUploadUrl()`.
|
|
622
|
-
* @param onProgress Optional callback called with 0–100 progress percentage.
|
|
623
|
-
*/
|
|
624
|
-
async uploadToSignedUrl(signedUrl, data, mimeType, onProgress) {
|
|
625
|
-
await this.http.putToSignedUrl(signedUrl, data, mimeType, onProgress);
|
|
626
|
-
}
|
|
627
|
-
/**
|
|
628
|
-
* Step 3 of the recommended upload flow.
|
|
629
|
-
* Confirm a direct upload and register metadata on the server.
|
|
630
|
-
*
|
|
631
|
-
* @example
|
|
632
|
-
* ```ts
|
|
633
|
-
* const result = await storage.confirmUpload({
|
|
634
|
-
* path: 'videos/intro.mp4',
|
|
635
|
-
* mimeType: 'video/mp4',
|
|
636
|
-
* isPublic: true,
|
|
637
|
-
* });
|
|
638
|
-
* console.log(result.publicUrl);
|
|
639
|
-
* ```
|
|
640
|
-
*/
|
|
641
|
-
async confirmUpload(opts) {
|
|
642
|
-
const result = await this.http.request(`${this.basePath}/confirm`, {
|
|
643
|
-
method: "POST",
|
|
644
|
-
body: { isPublic: false, ...opts },
|
|
645
|
-
headers: this.authHeaders
|
|
646
|
-
});
|
|
647
|
-
return {
|
|
648
|
-
path: result.path,
|
|
649
|
-
mimeType: result.mimeType,
|
|
650
|
-
size: result.size,
|
|
651
|
-
isPublic: result.isPublic,
|
|
652
|
-
publicUrl: result.publicUrl,
|
|
653
|
-
downloadUrl: result.downloadUrl
|
|
654
|
-
};
|
|
655
|
-
}
|
|
656
|
-
// ─── Batch Upload ─────────────────────────────────────────────────────────
|
|
657
|
-
/**
|
|
658
|
-
* Get signed upload URLs for multiple files at once.
|
|
659
|
-
*
|
|
660
|
-
* @example
|
|
661
|
-
* ```ts
|
|
662
|
-
* const { files } = await storage.getBatchUploadUrls([
|
|
663
|
-
* { path: 'images/photo1.jpg', mimeType: 'image/jpeg', size: 204800 },
|
|
664
|
-
* { path: 'images/photo2.jpg', mimeType: 'image/jpeg', size: 153600 },
|
|
665
|
-
* ]);
|
|
666
|
-
*
|
|
667
|
-
* // Upload each file and confirm
|
|
668
|
-
* for (const f of files) {
|
|
669
|
-
* await storage.uploadToSignedUrl(f.uploadUrl, blobs[f.index], f.mimeType);
|
|
670
|
-
* await storage.confirmUpload({ path: f.path, mimeType: f.mimeType });
|
|
671
|
-
* }
|
|
672
|
-
* ```
|
|
673
|
-
*/
|
|
674
|
-
async getBatchUploadUrls(files) {
|
|
675
|
-
const result = await this.http.request(
|
|
676
|
-
`${this.basePath}/batch-upload-urls`,
|
|
677
|
-
{ method: "POST", body: { files }, headers: this.authHeaders }
|
|
678
|
-
);
|
|
679
|
-
return { files: result.files };
|
|
680
|
-
}
|
|
681
|
-
/**
|
|
682
|
-
* Confirm multiple direct uploads at once.
|
|
683
|
-
*/
|
|
684
|
-
async batchConfirmUploads(items) {
|
|
685
|
-
const result = await this.http.request(
|
|
686
|
-
`${this.basePath}/batch-confirm`,
|
|
687
|
-
{ method: "POST", body: { files: items }, headers: this.authHeaders }
|
|
688
|
-
);
|
|
689
|
-
return result.files.map((r) => ({
|
|
690
|
-
path: r.path,
|
|
691
|
-
mimeType: r.mimeType,
|
|
692
|
-
size: r.size,
|
|
693
|
-
isPublic: r.isPublic,
|
|
694
|
-
publicUrl: r.publicUrl,
|
|
695
|
-
downloadUrl: r.downloadUrl
|
|
696
|
-
}));
|
|
697
|
-
}
|
|
698
|
-
// ─── Download ─────────────────────────────────────────────────────────────
|
|
699
|
-
/**
|
|
700
|
-
* Download a private file as an ArrayBuffer.
|
|
701
|
-
* For public files, use the `publicUrl` directly — no SDK needed.
|
|
702
|
-
*
|
|
703
|
-
* @example
|
|
704
|
-
* ```ts
|
|
705
|
-
* const buffer = await storage.download('docs/contract.pdf');
|
|
706
|
-
* const blob = new Blob([buffer], { type: 'application/pdf' });
|
|
707
|
-
* // Open in browser:
|
|
708
|
-
* window.open(URL.createObjectURL(blob));
|
|
709
|
-
* ```
|
|
710
|
-
*/
|
|
711
|
-
async download(path) {
|
|
712
|
-
const encoded = path.split("/").map(encodeURIComponent).join("/");
|
|
713
|
-
return this.http.request(`${this.basePath}/download/${encoded}`, {
|
|
714
|
-
method: "GET",
|
|
715
|
-
headers: this.authHeaders
|
|
716
|
-
});
|
|
717
|
-
}
|
|
718
|
-
/**
|
|
719
|
-
* Download multiple files at once, returned as a JSON map of `{ path: base64 }`.
|
|
720
|
-
*
|
|
721
|
-
* @example
|
|
722
|
-
* ```ts
|
|
723
|
-
* const files = await storage.batchDownload(['docs/a.pdf', 'docs/b.pdf']);
|
|
724
|
-
* ```
|
|
725
|
-
*/
|
|
726
|
-
async batchDownload(paths) {
|
|
727
|
-
const result = await this.http.request(
|
|
728
|
-
`${this.basePath}/batch-download`,
|
|
729
|
-
{ method: "POST", body: { paths }, headers: this.authHeaders }
|
|
730
|
-
);
|
|
731
|
-
return result.files;
|
|
732
|
-
}
|
|
733
|
-
// ─── List ─────────────────────────────────────────────────────────────────
|
|
734
|
-
/**
|
|
735
|
-
* List files and folders at a given path prefix.
|
|
736
|
-
*
|
|
737
|
-
* @example
|
|
738
|
-
* ```ts
|
|
739
|
-
* // List everything in the root
|
|
740
|
-
* const { files, folders } = await storage.list();
|
|
741
|
-
*
|
|
742
|
-
* // List a specific folder
|
|
743
|
-
* const { files, folders, hasMore, nextCursor } = await storage.list({
|
|
744
|
-
* prefix: 'avatars/',
|
|
745
|
-
* limit: 20,
|
|
746
|
-
* });
|
|
747
|
-
*
|
|
748
|
-
* // Next page
|
|
749
|
-
* const page2 = await storage.list({ prefix: 'avatars/', cursor: nextCursor });
|
|
750
|
-
* ```
|
|
751
|
-
*/
|
|
752
|
-
async list(opts = {}) {
|
|
753
|
-
const params = new URLSearchParams();
|
|
754
|
-
if (opts.prefix) params.set("prefix", opts.prefix);
|
|
755
|
-
if (opts.limit) params.set("limit", String(opts.limit));
|
|
756
|
-
if (opts.cursor) params.set("cursor", opts.cursor);
|
|
757
|
-
if (opts.recursive) params.set("recursive", "true");
|
|
758
|
-
const qs = params.toString() ? `?${params}` : "";
|
|
759
|
-
const result = await this.http.request(`${this.basePath}/list${qs}`, {
|
|
760
|
-
method: "GET",
|
|
761
|
-
headers: this.authHeaders
|
|
762
|
-
});
|
|
763
|
-
return {
|
|
764
|
-
files: result.files,
|
|
765
|
-
folders: result.folders,
|
|
766
|
-
hasMore: result.hasMore,
|
|
767
|
-
nextCursor: result.nextCursor
|
|
768
|
-
};
|
|
769
|
-
}
|
|
770
|
-
// ─── Metadata ─────────────────────────────────────────────────────────────
|
|
771
|
-
/**
|
|
772
|
-
* Get metadata for a file (size, MIME type, visibility, URLs).
|
|
773
|
-
*
|
|
774
|
-
* @example
|
|
775
|
-
* ```ts
|
|
776
|
-
* const meta = await storage.getMetadata('avatars/alice.jpg');
|
|
777
|
-
* console.log(meta.size, meta.isPublic, meta.publicUrl);
|
|
778
|
-
* ```
|
|
779
|
-
*/
|
|
780
|
-
async getMetadata(path) {
|
|
781
|
-
const encoded = path.split("/").map(encodeURIComponent).join("/");
|
|
782
|
-
const result = await this.http.request(
|
|
783
|
-
`${this.basePath}/metadata/${encoded}`,
|
|
784
|
-
{ method: "GET", headers: this.authHeaders }
|
|
785
|
-
);
|
|
786
|
-
return {
|
|
787
|
-
path: result.path,
|
|
788
|
-
size: result.size,
|
|
789
|
-
mimeType: result.mimeType,
|
|
790
|
-
isPublic: result.isPublic,
|
|
791
|
-
publicUrl: result.publicUrl,
|
|
792
|
-
downloadUrl: result.downloadUrl,
|
|
793
|
-
createdAt: result.createdAt,
|
|
794
|
-
updatedAt: result.updatedAt
|
|
795
|
-
};
|
|
796
|
-
}
|
|
797
|
-
// ─── Signed URL (time-limited share) ─────────────────────────────────────
|
|
798
|
-
/**
|
|
799
|
-
* Generate a time-limited download URL for a private file.
|
|
800
|
-
* The URL can be shared externally without requiring an `X-Storage-Key`.
|
|
801
|
-
*
|
|
802
|
-
* > **Note:** Downloads via signed URLs bypass the server, so download stats
|
|
803
|
-
* > are NOT tracked. Use `downloadUrl` for tracked downloads.
|
|
804
|
-
*
|
|
805
|
-
* @param path Path to the file.
|
|
806
|
-
* @param expiresIn URL lifetime in seconds (default 3600 = 1 hour).
|
|
807
|
-
*
|
|
808
|
-
* @example
|
|
809
|
-
* ```ts
|
|
810
|
-
* const { signedUrl, expiresAt } = await storage.getSignedUrl('docs/invoice.pdf', 1800);
|
|
811
|
-
* // Share signedUrl with the recipient — it expires in 30 minutes.
|
|
812
|
-
* ```
|
|
813
|
-
*/
|
|
814
|
-
async getSignedUrl(path, expiresIn = 3600) {
|
|
815
|
-
const result = await this.http.request(`${this.basePath}/signed-url`, {
|
|
816
|
-
method: "POST",
|
|
817
|
-
body: { path, expiresIn },
|
|
818
|
-
headers: this.authHeaders
|
|
819
|
-
});
|
|
820
|
-
return {
|
|
821
|
-
signedUrl: result.signedUrl,
|
|
822
|
-
expiresAt: result.expiresAt,
|
|
823
|
-
expiresIn: result.expiresIn,
|
|
824
|
-
path: result.path
|
|
825
|
-
};
|
|
826
|
-
}
|
|
827
|
-
// ─── Visibility ───────────────────────────────────────────────────────────
|
|
828
|
-
/**
|
|
829
|
-
* Change a file's visibility between public and private after upload.
|
|
830
|
-
*
|
|
831
|
-
* @example
|
|
832
|
-
* ```ts
|
|
833
|
-
* // Make a file public
|
|
834
|
-
* const result = await storage.setVisibility('avatars/alice.jpg', true);
|
|
835
|
-
* console.log(result.publicUrl); // CDN URL
|
|
836
|
-
*
|
|
837
|
-
* // Make a file private
|
|
838
|
-
* const result = await storage.setVisibility('avatars/alice.jpg', false);
|
|
839
|
-
* console.log(result.downloadUrl); // Auth-required URL
|
|
840
|
-
* ```
|
|
841
|
-
*/
|
|
842
|
-
async setVisibility(path, isPublic) {
|
|
843
|
-
const result = await this.http.request(`${this.basePath}/visibility`, {
|
|
844
|
-
method: "PATCH",
|
|
845
|
-
body: { path, isPublic },
|
|
846
|
-
headers: this.authHeaders
|
|
847
|
-
});
|
|
848
|
-
return {
|
|
849
|
-
path: result.path,
|
|
850
|
-
isPublic: result.isPublic,
|
|
851
|
-
publicUrl: result.publicUrl,
|
|
852
|
-
downloadUrl: result.downloadUrl
|
|
853
|
-
};
|
|
854
|
-
}
|
|
855
|
-
// ─── Folder Operations ────────────────────────────────────────────────────
|
|
856
|
-
/**
|
|
857
|
-
* Create a folder (a GCS prefix placeholder).
|
|
858
|
-
*
|
|
859
|
-
* @example
|
|
860
|
-
* ```ts
|
|
861
|
-
* await storage.createFolder('uploads/2025/');
|
|
862
|
-
* ```
|
|
863
|
-
*/
|
|
864
|
-
async createFolder(path) {
|
|
865
|
-
const result = await this.http.request(
|
|
866
|
-
`${this.basePath}/folder`,
|
|
867
|
-
{ method: "POST", body: { path }, headers: this.authHeaders }
|
|
868
|
-
);
|
|
869
|
-
return { path: result.path };
|
|
870
|
-
}
|
|
871
|
-
// ─── File Operations ──────────────────────────────────────────────────────
|
|
872
|
-
/**
|
|
873
|
-
* Delete a single file.
|
|
874
|
-
*
|
|
875
|
-
* @example
|
|
876
|
-
* ```ts
|
|
877
|
-
* await storage.deleteFile('avatars/old-avatar.jpg');
|
|
878
|
-
* ```
|
|
879
|
-
*/
|
|
880
|
-
async deleteFile(path) {
|
|
881
|
-
await this.http.request(`${this.basePath}/file`, {
|
|
882
|
-
method: "DELETE",
|
|
883
|
-
body: { path },
|
|
884
|
-
headers: this.authHeaders
|
|
885
|
-
});
|
|
886
|
-
}
|
|
887
|
-
/**
|
|
888
|
-
* Delete a folder and all its contents recursively.
|
|
889
|
-
*
|
|
890
|
-
* @example
|
|
891
|
-
* ```ts
|
|
892
|
-
* await storage.deleteFolder('temp/');
|
|
893
|
-
* ```
|
|
894
|
-
*/
|
|
895
|
-
async deleteFolder(path) {
|
|
896
|
-
await this.http.request(`${this.basePath}/folder`, {
|
|
897
|
-
method: "DELETE",
|
|
898
|
-
body: { path },
|
|
899
|
-
headers: this.authHeaders
|
|
900
|
-
});
|
|
901
|
-
}
|
|
902
|
-
/**
|
|
903
|
-
* Move or rename a file.
|
|
904
|
-
*
|
|
905
|
-
* @example
|
|
906
|
-
* ```ts
|
|
907
|
-
* // Rename
|
|
908
|
-
* await storage.move('docs/draft.pdf', 'docs/final.pdf');
|
|
909
|
-
* // Move to a different folder
|
|
910
|
-
* await storage.move('inbox/report.xlsx', 'archive/2025/report.xlsx');
|
|
911
|
-
* ```
|
|
912
|
-
*/
|
|
913
|
-
async move(from, to) {
|
|
914
|
-
const result = await this.http.request(
|
|
915
|
-
`${this.basePath}/move`,
|
|
916
|
-
{ method: "POST", body: { from, to }, headers: this.authHeaders }
|
|
917
|
-
);
|
|
918
|
-
return { from: result.from, to: result.to };
|
|
919
|
-
}
|
|
920
|
-
/**
|
|
921
|
-
* Copy a file to a new path.
|
|
922
|
-
*
|
|
923
|
-
* @example
|
|
924
|
-
* ```ts
|
|
925
|
-
* await storage.copy('templates/base.html', 'sites/my-site/index.html');
|
|
926
|
-
* ```
|
|
927
|
-
*/
|
|
928
|
-
async copy(from, to) {
|
|
929
|
-
const result = await this.http.request(
|
|
930
|
-
`${this.basePath}/copy`,
|
|
931
|
-
{ method: "POST", body: { from, to }, headers: this.authHeaders }
|
|
932
|
-
);
|
|
933
|
-
return { from: result.from, to: result.to };
|
|
934
|
-
}
|
|
935
|
-
// ─── Stats ────────────────────────────────────────────────────────────────
|
|
936
|
-
/**
|
|
937
|
-
* Get storage statistics for your key: total files, bytes, operation counts.
|
|
938
|
-
*
|
|
939
|
-
* @example
|
|
940
|
-
* ```ts
|
|
941
|
-
* const stats = await storage.getStats();
|
|
942
|
-
* console.log(`${stats.totalFiles} files, ${(stats.totalBytes / 1e6).toFixed(1)} MB`);
|
|
943
|
-
* ```
|
|
944
|
-
*/
|
|
945
|
-
async getStats() {
|
|
946
|
-
const result = await this.http.request(`${this.basePath}/stats`, {
|
|
947
|
-
method: "GET",
|
|
948
|
-
headers: this.authHeaders
|
|
949
|
-
});
|
|
950
|
-
return result.stats;
|
|
951
|
-
}
|
|
952
|
-
// ─── Info (no auth) ───────────────────────────────────────────────────────
|
|
953
|
-
/**
|
|
954
|
-
* Ping the storage service. No authentication required.
|
|
955
|
-
*
|
|
956
|
-
* @example
|
|
957
|
-
* ```ts
|
|
958
|
-
* const info = await storage.info();
|
|
959
|
-
* // → { ok: true, storageRoot: 'hydrous-storage' }
|
|
960
|
-
* ```
|
|
961
|
-
*/
|
|
962
|
-
async info() {
|
|
963
|
-
return this.http.get(`${this.basePath}/info`);
|
|
964
|
-
}
|
|
965
|
-
};
|
|
966
|
-
|
|
967
|
-
// src/storage/scoped.ts
|
|
968
|
-
var ScopedStorage = class _ScopedStorage {
|
|
969
|
-
constructor(manager, prefix) {
|
|
970
|
-
this.manager = manager;
|
|
971
|
-
this.prefix = prefix.replace(/\/+$/, "") + "/";
|
|
972
|
-
}
|
|
973
|
-
scopedPath(userPath) {
|
|
974
|
-
return `${this.prefix}${userPath.replace(/^\/+/, "")}`;
|
|
975
|
-
}
|
|
976
|
-
/** Upload a file within the scoped folder. */
|
|
977
|
-
upload(data, path, options) {
|
|
978
|
-
return this.manager.upload(data, this.scopedPath(path), options);
|
|
979
|
-
}
|
|
980
|
-
/** Upload raw JSON or text within the scoped folder. */
|
|
981
|
-
uploadRaw(data, path, options) {
|
|
982
|
-
return this.manager.uploadRaw(data, this.scopedPath(path), options);
|
|
983
|
-
}
|
|
984
|
-
/** Get a signed upload URL for a file within the scoped folder. */
|
|
985
|
-
getUploadUrl(opts) {
|
|
986
|
-
return this.manager.getUploadUrl({ ...opts, path: this.scopedPath(opts.path) });
|
|
987
|
-
}
|
|
988
|
-
/** Confirm a direct upload within the scoped folder. */
|
|
989
|
-
confirmUpload(opts) {
|
|
990
|
-
return this.manager.confirmUpload({ ...opts, path: this.scopedPath(opts.path) });
|
|
991
|
-
}
|
|
992
|
-
/** Download a file within the scoped folder. */
|
|
993
|
-
download(path) {
|
|
994
|
-
return this.manager.download(this.scopedPath(path));
|
|
995
|
-
}
|
|
996
|
-
/**
|
|
997
|
-
* List files within the scoped folder.
|
|
998
|
-
* `prefix` in options is relative to the scope.
|
|
999
|
-
*/
|
|
1000
|
-
list(opts = {}) {
|
|
1001
|
-
const scopedOpts = {
|
|
1002
|
-
...opts,
|
|
1003
|
-
prefix: this.scopedPath(opts.prefix ?? "")
|
|
1004
|
-
};
|
|
1005
|
-
return this.manager.list(scopedOpts);
|
|
1006
|
-
}
|
|
1007
|
-
/** Get metadata for a file within the scoped folder. */
|
|
1008
|
-
getMetadata(path) {
|
|
1009
|
-
return this.manager.getMetadata(this.scopedPath(path));
|
|
1010
|
-
}
|
|
1011
|
-
/** Get a time-limited signed URL for a file within the scoped folder. */
|
|
1012
|
-
getSignedUrl(path, expiresIn) {
|
|
1013
|
-
return this.manager.getSignedUrl(this.scopedPath(path), expiresIn);
|
|
1014
|
-
}
|
|
1015
|
-
/** Change visibility of a file within the scoped folder. */
|
|
1016
|
-
setVisibility(path, isPublic) {
|
|
1017
|
-
return this.manager.setVisibility(this.scopedPath(path), isPublic);
|
|
1018
|
-
}
|
|
1019
|
-
/** Delete a file within the scoped folder. */
|
|
1020
|
-
deleteFile(path) {
|
|
1021
|
-
return this.manager.deleteFile(this.scopedPath(path));
|
|
1022
|
-
}
|
|
1023
|
-
/** Delete a sub-folder within the scoped folder. */
|
|
1024
|
-
deleteFolder(path) {
|
|
1025
|
-
return this.manager.deleteFolder(this.scopedPath(path));
|
|
1026
|
-
}
|
|
1027
|
-
/** Move a file within the scoped folder. */
|
|
1028
|
-
move(from, to) {
|
|
1029
|
-
return this.manager.move(this.scopedPath(from), this.scopedPath(to));
|
|
1030
|
-
}
|
|
1031
|
-
/** Copy a file within the scoped folder. */
|
|
1032
|
-
copy(from, to) {
|
|
1033
|
-
return this.manager.copy(this.scopedPath(from), this.scopedPath(to));
|
|
1034
|
-
}
|
|
1035
|
-
/** Create a sub-folder within the scoped folder. */
|
|
1036
|
-
createFolder(path) {
|
|
1037
|
-
return this.manager.createFolder(this.scopedPath(path));
|
|
1038
|
-
}
|
|
1039
|
-
/**
|
|
1040
|
-
* Create a further-scoped instance nested within this scope.
|
|
1041
|
-
*
|
|
1042
|
-
* @example
|
|
1043
|
-
* ```ts
|
|
1044
|
-
* const uploads = db.storage.scope('user-uploads');
|
|
1045
|
-
* const images = uploads.scope('images'); // → "user-uploads/images/"
|
|
1046
|
-
* ```
|
|
1047
|
-
*/
|
|
1048
|
-
scope(subPrefix) {
|
|
1049
|
-
return new _ScopedStorage(this.manager, this.scopedPath(subPrefix));
|
|
1050
|
-
}
|
|
1051
|
-
};
|
|
1052
|
-
|
|
1053
|
-
// src/client.ts
|
|
1054
|
-
var HydrousClient = class {
|
|
1055
|
-
constructor(config) {
|
|
1056
|
-
this._recordsCache = /* @__PURE__ */ new Map();
|
|
1057
|
-
this._authCache = /* @__PURE__ */ new Map();
|
|
1058
|
-
this._analyticsCache = /* @__PURE__ */ new Map();
|
|
1059
|
-
this._storageCache = /* @__PURE__ */ new Map();
|
|
1060
|
-
if (!config.authKey) {
|
|
1061
|
-
throw new Error("[HydrousDB] authKey is required. Get yours from https://hydrousdb.com/dashboard.");
|
|
1062
|
-
}
|
|
1063
|
-
if (!config.bucketSecurityKey) {
|
|
1064
|
-
throw new Error("[HydrousDB] bucketSecurityKey is required. Get yours from https://hydrousdb.com/dashboard.");
|
|
1065
|
-
}
|
|
1066
|
-
if (!config.storageKeys || Object.keys(config.storageKeys).length === 0) {
|
|
1067
|
-
throw new Error("[HydrousDB] storageKeys is required. Define at least one storage key from https://hydrousdb.com/dashboard.");
|
|
1068
|
-
}
|
|
1069
|
-
const baseUrl = config.baseUrl ?? DEFAULT_BASE_URL;
|
|
1070
|
-
this.http = new HttpClient(baseUrl);
|
|
1071
|
-
this.authKey_ = config.authKey;
|
|
1072
|
-
this.bucketSecurityKey_ = config.bucketSecurityKey;
|
|
1073
|
-
this.storageKeys_ = config.storageKeys;
|
|
1074
|
-
}
|
|
1075
|
-
// ─── Records ─────────────────────────────────────────────────────────────
|
|
1076
|
-
/**
|
|
1077
|
-
* Get a typed records client for the named bucket.
|
|
1078
|
-
* Uses your `bucketSecurityKey` automatically.
|
|
1079
|
-
*
|
|
1080
|
-
* @example
|
|
1081
|
-
* ```ts
|
|
1082
|
-
* interface Post { title: string; published: boolean }
|
|
1083
|
-
* const posts = db.records<Post>('blog-posts');
|
|
1084
|
-
* const post = await posts.create({ title: 'Hello', published: false });
|
|
1085
|
-
* ```
|
|
1086
|
-
*/
|
|
1087
|
-
records(bucketKey) {
|
|
1088
|
-
if (!this._recordsCache.has(bucketKey)) {
|
|
1089
|
-
this._recordsCache.set(
|
|
1090
|
-
bucketKey,
|
|
1091
|
-
new RecordsClient(this.http, this.bucketSecurityKey_, bucketKey)
|
|
1092
|
-
);
|
|
1093
|
-
}
|
|
1094
|
-
return this._recordsCache.get(bucketKey);
|
|
1095
|
-
}
|
|
1096
|
-
// ─── Auth ─────────────────────────────────────────────────────────────────
|
|
1097
|
-
/**
|
|
1098
|
-
* Get an auth client for the named user bucket.
|
|
1099
|
-
* Uses your `authKey` automatically.
|
|
1100
|
-
*
|
|
1101
|
-
* @example
|
|
1102
|
-
* ```ts
|
|
1103
|
-
* const auth = db.auth('app-users');
|
|
1104
|
-
* const { user, session } = await auth.login({ email: '…', password: '…' });
|
|
1105
|
-
* ```
|
|
1106
|
-
*/
|
|
1107
|
-
auth(bucketKey) {
|
|
1108
|
-
if (!this._authCache.has(bucketKey)) {
|
|
1109
|
-
this._authCache.set(bucketKey, new AuthClient(this.http, this.authKey_, bucketKey));
|
|
1110
|
-
}
|
|
1111
|
-
return this._authCache.get(bucketKey);
|
|
1112
|
-
}
|
|
1113
|
-
// ─── Analytics ────────────────────────────────────────────────────────────
|
|
1114
|
-
/**
|
|
1115
|
-
* Get an analytics client for the named bucket.
|
|
1116
|
-
* Uses your `bucketSecurityKey` automatically.
|
|
1117
|
-
*
|
|
1118
|
-
* @example
|
|
1119
|
-
* ```ts
|
|
1120
|
-
* const analytics = db.analytics('orders');
|
|
1121
|
-
* const { count } = await analytics.count();
|
|
1122
|
-
* ```
|
|
1123
|
-
*/
|
|
1124
|
-
analytics(bucketKey) {
|
|
1125
|
-
if (!this._analyticsCache.has(bucketKey)) {
|
|
1126
|
-
this._analyticsCache.set(
|
|
1127
|
-
bucketKey,
|
|
1128
|
-
new AnalyticsClient(this.http, this.bucketSecurityKey_, bucketKey)
|
|
1129
|
-
);
|
|
1130
|
-
}
|
|
1131
|
-
return this._analyticsCache.get(bucketKey);
|
|
1132
|
-
}
|
|
1133
|
-
// ─── Storage ──────────────────────────────────────────────────────────────
|
|
1134
|
-
/**
|
|
1135
|
-
* Get a storage manager for the named storage key.
|
|
1136
|
-
* The name must match a key you defined in `storageKeys` when calling `createClient`.
|
|
1137
|
-
* Uses the corresponding `ssk_…` key automatically via `X-Storage-Key` header.
|
|
1138
|
-
*
|
|
1139
|
-
* @param keyName The name of the storage key (e.g. `"avatars"`, `"documents"`, `"main"`).
|
|
1140
|
-
*
|
|
1141
|
-
* @example
|
|
1142
|
-
* ```ts
|
|
1143
|
-
* const avatars = db.storage('avatars');
|
|
1144
|
-
* const documents = db.storage('documents');
|
|
1145
|
-
*
|
|
1146
|
-
* // Upload to avatars bucket
|
|
1147
|
-
* await avatars.upload(file, `${userId}.jpg`, { isPublic: true });
|
|
1148
|
-
*
|
|
1149
|
-
* // Scope to a sub-folder
|
|
1150
|
-
* const userDocs = db.storage('documents').scope(`users/${userId}`);
|
|
1151
|
-
* await userDocs.upload(pdfBuffer, 'contract.pdf');
|
|
1152
|
-
* ```
|
|
1153
|
-
*/
|
|
1154
|
-
storage(keyName) {
|
|
1155
|
-
const ssk = this.storageKeys_[keyName];
|
|
1156
|
-
if (!ssk) {
|
|
1157
|
-
const available = Object.keys(this.storageKeys_).join(", ");
|
|
1158
|
-
throw new Error(
|
|
1159
|
-
`[HydrousDB] Unknown storage key name "${keyName}". Available keys: ${available}. Add it to storageKeys in your createClient() config.`
|
|
1160
|
-
);
|
|
1161
|
-
}
|
|
1162
|
-
if (!this._storageCache.has(keyName)) {
|
|
1163
|
-
this._storageCache.set(keyName, new StorageManager(this.http, ssk));
|
|
1164
|
-
}
|
|
1165
|
-
const mgr = this._storageCache.get(keyName);
|
|
1166
|
-
const extended = mgr;
|
|
1167
|
-
if (!extended.scope) {
|
|
1168
|
-
extended.scope = (prefix) => new ScopedStorage(mgr, prefix);
|
|
1169
|
-
}
|
|
1170
|
-
return extended;
|
|
1171
|
-
}
|
|
1172
|
-
};
|
|
1173
|
-
function createClient(config) {
|
|
1174
|
-
return new HydrousClient(config);
|
|
1175
|
-
}
|
|
1176
|
-
|
|
1177
|
-
export { AnalyticsClient, AnalyticsError, AuthClient, AuthError, HydrousClient, HydrousError, NetworkError, RecordError, RecordsClient, ScopedStorage, StorageError, StorageManager, ValidationError, createClient };
|
|
1178
|
-
//# sourceMappingURL=index.js.map
|
|
1179
|
-
//# sourceMappingURL=index.js.map
|