|
1 | 1 | import type { Bucket, FileListResponse, GetFilesOptions } from "../types.js"; |
2 | 2 | import { ModuleBase } from "../base.js"; |
| 3 | +import { ensure0xPrefix } from "@storagehub-sdk/core"; |
3 | 4 |
|
4 | 5 | export class BucketsModule extends ModuleBase { |
| 6 | + /** Recursively fix hex prefixes in FileTree structures */ |
| 7 | + private fixFileTree(item: any): any { |
| 8 | + if (item.type === "file") { |
| 9 | + return { |
| 10 | + name: item.name, |
| 11 | + type: item.type, |
| 12 | + sizeBytes: item.sizeBytes, |
| 13 | + fileKey: ensure0xPrefix(item.fileKey), |
| 14 | + status: item.status |
| 15 | + }; |
| 16 | + } else { |
| 17 | + return { |
| 18 | + name: item.name, |
| 19 | + type: item.type, |
| 20 | + children: item.children?.map((child: any) => this.fixFileTree(child)) || [] |
| 21 | + }; |
| 22 | + } |
| 23 | + } |
5 | 24 | /** List all buckets for the current authenticated user */ |
6 | 25 | async listBuckets(signal?: AbortSignal): Promise<Bucket[]> { |
7 | 26 | const headers = await this.withAuth(); |
8 | | - return this.ctx.http.get<Bucket[]>("/buckets", { |
| 27 | + const wire = await this.ctx.http.get< |
| 28 | + Array<{ |
| 29 | + bucketId: string; |
| 30 | + name: string; |
| 31 | + root: string; |
| 32 | + isPublic: boolean; |
| 33 | + sizeBytes: number; |
| 34 | + valuePropId: string; |
| 35 | + fileCount: number; |
| 36 | + }> |
| 37 | + >("/buckets", { |
9 | 38 | ...(headers ? { headers } : {}), |
10 | 39 | ...(signal ? { signal } : {}) |
11 | 40 | }); |
| 41 | + |
| 42 | + return wire.map((bucket) => ({ |
| 43 | + bucketId: ensure0xPrefix(bucket.bucketId), |
| 44 | + name: bucket.name, |
| 45 | + root: ensure0xPrefix(bucket.root), |
| 46 | + isPublic: bucket.isPublic, |
| 47 | + sizeBytes: bucket.sizeBytes, |
| 48 | + valuePropId: bucket.valuePropId, // "unknown" string, not hex |
| 49 | + fileCount: bucket.fileCount |
| 50 | + })); |
12 | 51 | } |
13 | 52 |
|
14 | 53 | /** Get a specific bucket's metadata by its bucket ID */ |
15 | 54 | async getBucket(bucketId: string, signal?: AbortSignal): Promise<Bucket> { |
16 | 55 | const headers = await this.withAuth(); |
17 | 56 | const path = `/buckets/${encodeURIComponent(bucketId)}`; |
18 | | - return this.ctx.http.get<Bucket>(path, { |
| 57 | + |
| 58 | + const wire = await this.ctx.http.get<{ |
| 59 | + bucketId: string; |
| 60 | + name: string; |
| 61 | + root: string; |
| 62 | + isPublic: boolean; |
| 63 | + sizeBytes: number; |
| 64 | + valuePropId: string; |
| 65 | + fileCount: number; |
| 66 | + }>(path, { |
19 | 67 | ...(headers ? { headers } : {}), |
20 | 68 | ...(signal ? { signal } : {}) |
21 | 69 | }); |
| 70 | + |
| 71 | + return { |
| 72 | + bucketId: ensure0xPrefix(wire.bucketId), |
| 73 | + name: wire.name, |
| 74 | + root: ensure0xPrefix(wire.root), |
| 75 | + isPublic: wire.isPublic, |
| 76 | + sizeBytes: wire.sizeBytes, |
| 77 | + valuePropId: wire.valuePropId, // "unknown" string, not hex |
| 78 | + fileCount: wire.fileCount |
| 79 | + }; |
22 | 80 | } |
23 | 81 |
|
24 | 82 | /** List files/folders under a path for a bucket (root if no path) */ |
25 | 83 | async getFiles(bucketId: string, options?: GetFilesOptions): Promise<FileListResponse> { |
26 | 84 | const headers = await this.withAuth(); |
27 | 85 | const path = `/buckets/${encodeURIComponent(bucketId)}/files`; |
28 | | - return this.ctx.http.get<FileListResponse>(path, { |
| 86 | + |
| 87 | + const wire = await this.ctx.http.get<{ |
| 88 | + bucketId: string; |
| 89 | + files: Array<{ |
| 90 | + name: string; |
| 91 | + type: "file" | "folder"; |
| 92 | + sizeBytes?: number; |
| 93 | + fileKey?: string; |
| 94 | + status?: string; |
| 95 | + children?: Array<any>; |
| 96 | + }>; |
| 97 | + }>(path, { |
29 | 98 | ...(headers ? { headers } : {}), |
30 | 99 | ...(options?.signal ? { signal: options.signal } : {}), |
31 | 100 | ...(options?.path ? { query: { path: this.normalizePath(options.path) } } : {}) |
32 | 101 | }); |
| 102 | + |
| 103 | + return { |
| 104 | + bucketId: ensure0xPrefix(wire.bucketId), |
| 105 | + files: wire.files.map((file) => this.fixFileTree(file)) |
| 106 | + }; |
33 | 107 | } |
34 | 108 | } |
0 commit comments