@omnibase/core-js 0.5.10 → 0.7.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.
@@ -0,0 +1,152 @@
1
+ // src/storage/index.ts
2
+ var Bucket = class {
3
+ constructor(client, name) {
4
+ this.client = client;
5
+ this.name = name;
6
+ }
7
+ /**
8
+ * Upload a file to the bucket
9
+ *
10
+ * @param path - Path within the bucket (will be prefixed with tenant_id)
11
+ * @param file - File or Blob to upload
12
+ * @param options - Upload options including custom metadata
13
+ *
14
+ * @example
15
+ * ```typescript
16
+ * const result = await storage.bucket('user-uploads').upload(
17
+ * 'documents/report.pdf',
18
+ * file,
19
+ * {
20
+ * metadata: {
21
+ * department: 'engineering',
22
+ * project: 'Q4-review',
23
+ * tags: ['important', 'quarterly']
24
+ * }
25
+ * }
26
+ * );
27
+ *
28
+ * // Upload file to S3 using pre-signed URL
29
+ * await fetch(result.upload_url, {
30
+ * method: 'PUT',
31
+ * body: file
32
+ * });
33
+ * ```
34
+ */
35
+ async upload(path, file, options) {
36
+ const metadata = {
37
+ // File metadata
38
+ filename: file instanceof File ? file.name : "blob",
39
+ size: file.size,
40
+ mime_type: file.type,
41
+ uploaded_at: (/* @__PURE__ */ new Date()).toISOString(),
42
+ // Merge custom metadata
43
+ ...options?.metadata || {}
44
+ };
45
+ const response = await this.client.fetch("/api/v1/storage/upload", {
46
+ method: "POST",
47
+ headers: {
48
+ "Content-Type": "application/json"
49
+ },
50
+ body: JSON.stringify({
51
+ bucket: this.name,
52
+ path,
53
+ metadata
54
+ })
55
+ });
56
+ if (!response.ok) {
57
+ const error = await response.json().catch(() => ({ error: "Upload failed" }));
58
+ throw new Error(error.error || "Failed to get upload URL");
59
+ }
60
+ const responseData = await response.json();
61
+ const result = responseData.data;
62
+ const uploadResponse = await fetch(result.upload_url, {
63
+ method: "PUT",
64
+ body: file,
65
+ headers: {
66
+ "Content-Type": file.type
67
+ }
68
+ });
69
+ if (!uploadResponse.ok) {
70
+ throw new Error("Failed to upload file to storage");
71
+ }
72
+ return result;
73
+ }
74
+ /**
75
+ * Download a file from the bucket
76
+ *
77
+ * @param path - Path to the file (including tenant_id prefix)
78
+ *
79
+ * @example
80
+ * ```typescript
81
+ * const { download_url } = await storage.bucket('user-uploads')
82
+ * .download('tenant-123/documents/report.pdf');
83
+ *
84
+ * // Download the file
85
+ * const response = await fetch(download_url);
86
+ * const blob = await response.blob();
87
+ * ```
88
+ */
89
+ async download(path) {
90
+ const response = await this.client.fetch("/api/v1/storage/download", {
91
+ method: "POST",
92
+ headers: {
93
+ "Content-Type": "application/json"
94
+ },
95
+ body: JSON.stringify({
96
+ bucket: this.name,
97
+ path
98
+ })
99
+ });
100
+ if (!response.ok) {
101
+ const error = await response.json().catch(() => ({ error: "Download failed" }));
102
+ throw new Error(error.error || "Failed to get download URL");
103
+ }
104
+ const responseData = await response.json();
105
+ return responseData.data;
106
+ }
107
+ /**
108
+ * Delete a file from the bucket
109
+ *
110
+ * @param path - Path to the file (including tenant_id prefix)
111
+ *
112
+ * @example
113
+ * ```typescript
114
+ * await storage.bucket('user-uploads')
115
+ * .delete('tenant-123/documents/report.pdf');
116
+ * ```
117
+ */
118
+ async delete(path) {
119
+ const response = await this.client.fetch("/api/v1/storage/object", {
120
+ method: "DELETE",
121
+ headers: {
122
+ "Content-Type": "application/json"
123
+ },
124
+ body: JSON.stringify({
125
+ bucket: this.name,
126
+ path
127
+ })
128
+ });
129
+ if (!response.ok) {
130
+ const error = await response.json().catch(() => ({ error: "Delete failed" }));
131
+ throw new Error(error.error || "Failed to delete file");
132
+ }
133
+ }
134
+ };
135
+ var StorageClient = class {
136
+ constructor(client) {
137
+ this.client = client;
138
+ }
139
+ /**
140
+ * Get a bucket reference for file operations
141
+ *
142
+ * @param name - Bucket name (e.g., 'public', 'user-uploads', 'avatars')
143
+ */
144
+ bucket(name) {
145
+ return new Bucket(this.client, name);
146
+ }
147
+ };
148
+
149
+ export {
150
+ Bucket,
151
+ StorageClient
152
+ };