@data-loom/storage-js 0.4.4-alpha.1 → 0.4.4-alpha.10
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 +398 -156
- package/package.json +9 -3
package/README.md
CHANGED
|
@@ -7,7 +7,7 @@ Dataloom 存储服务 JavaScript SDK,基于 Supabase Storage 修改而来,
|
|
|
7
7
|
- 📁 **文件上传和下载** - 支持多种文件格式的上传和下载
|
|
8
8
|
- 🔗 **签名URL** - 创建具有时效性的安全文件访问链接
|
|
9
9
|
- 🗑️ **文件管理** - 删除、列表等文件操作
|
|
10
|
-
-
|
|
10
|
+
- 🌐 **URL上传** - 从公共URL下载文件并上传到存储桶
|
|
11
11
|
- 📊 **批量操作** - 支持批量创建签名URL等批量操作
|
|
12
12
|
- 🔍 **文件搜索** - 支持文件搜索和分页
|
|
13
13
|
- 🛡️ **错误处理** - 完善的错误处理机制
|
|
@@ -77,69 +77,179 @@ constructor(
|
|
|
77
77
|
)
|
|
78
78
|
```
|
|
79
79
|
|
|
80
|
+
---
|
|
81
|
+
|
|
80
82
|
### 文件上传
|
|
81
83
|
|
|
82
|
-
#### `
|
|
84
|
+
#### `upload(path, fileBody, fileOptions?)` - 重载1
|
|
83
85
|
|
|
84
|
-
|
|
86
|
+
上传文件到指定的存储桶路径。
|
|
85
87
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
88
|
+
**入参:**
|
|
89
|
+
```typescript
|
|
90
|
+
function upload(
|
|
91
|
+
path: string, // 文件路径,格式为 `folder/subfolder/filename.png`
|
|
92
|
+
fileBody: FileBody, // 文件内容
|
|
93
|
+
fileOptions?: FileOptions // 上传选项
|
|
94
|
+
): Promise<UploadResult>
|
|
95
|
+
```
|
|
89
96
|
|
|
90
|
-
|
|
97
|
+
**出参:**
|
|
98
|
+
```typescript
|
|
99
|
+
type UploadResult =
|
|
100
|
+
| { data: UploadFileData; error: null }
|
|
101
|
+
| { data: null; error: StorageError }
|
|
102
|
+
```
|
|
91
103
|
|
|
92
104
|
**示例:**
|
|
93
105
|
```javascript
|
|
94
|
-
const { data, error } = await storage.
|
|
95
|
-
filePath: 'documents/report.pdf',
|
|
106
|
+
const { data, error } = await storage.upload('avatars/user-123.jpg', file, {
|
|
96
107
|
cacheControl: 3600,
|
|
108
|
+
contentType: 'image/jpeg',
|
|
109
|
+
upsert: false
|
|
110
|
+
});
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
---
|
|
114
|
+
|
|
115
|
+
#### `upload(fileBody, fileOptions?)` - 重载2
|
|
116
|
+
|
|
117
|
+
使用 FileOptionsV2 上传文件,路径通过 options.filePath 指定。
|
|
118
|
+
|
|
119
|
+
**入参:**
|
|
120
|
+
```typescript
|
|
121
|
+
function upload(
|
|
122
|
+
fileBody: FileBody, // 文件内容
|
|
123
|
+
fileOptions?: FileOptionsV2 // 上传选项(包含 filePath)
|
|
124
|
+
): Promise<UploadResult>
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
**出参:**
|
|
128
|
+
```typescript
|
|
129
|
+
type UploadResult =
|
|
130
|
+
| { data: UploadFileData; error: null }
|
|
131
|
+
| { data: null; error: StorageError }
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
**示例:**
|
|
135
|
+
```javascript
|
|
136
|
+
const { data, error } = await storage.upload(file, {
|
|
137
|
+
filePath: 'documents/report.pdf',
|
|
97
138
|
contentType: 'application/pdf',
|
|
98
139
|
contentDisposition: 'attachment; filename="report.pdf"'
|
|
99
140
|
});
|
|
100
141
|
```
|
|
101
142
|
|
|
102
|
-
|
|
143
|
+
---
|
|
103
144
|
|
|
104
|
-
|
|
145
|
+
#### `uploadFile(fileBody, fileOptions?)`
|
|
105
146
|
|
|
106
|
-
|
|
107
|
-
- `path` (string): 文件路径,格式为 `folder/subfolder/filename.png`
|
|
108
|
-
- `fileBody` (FileBody): 文件内容,支持多种格式
|
|
109
|
-
- `fileOptions` (FileOptions, 可选): 文件选项
|
|
147
|
+
使用文件选项上传文件(推荐,与 upload 重载2 功能相同)。
|
|
110
148
|
|
|
111
|
-
|
|
149
|
+
**入参:**
|
|
112
150
|
```typescript
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
151
|
+
function uploadFile(
|
|
152
|
+
fileBody: FileBody, // 文件内容
|
|
153
|
+
fileOptions?: FileOptionsV2 // 上传选项
|
|
154
|
+
): Promise<UploadResult>
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
**出参:**
|
|
158
|
+
```typescript
|
|
159
|
+
type UploadResult =
|
|
160
|
+
| { data: UploadFileData; error: null }
|
|
161
|
+
| { data: null; error: StorageError }
|
|
120
162
|
```
|
|
121
163
|
|
|
122
164
|
**示例:**
|
|
123
165
|
```javascript
|
|
124
|
-
const
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
contentType: 'image/jpeg',
|
|
130
|
-
upsert: false
|
|
166
|
+
const { data, error } = await storage.uploadFile(file, {
|
|
167
|
+
filePath: 'documents/report.pdf',
|
|
168
|
+
cacheControl: 3600,
|
|
169
|
+
contentType: 'application/pdf',
|
|
170
|
+
contentDisposition: 'attachment; filename="report.pdf"'
|
|
131
171
|
});
|
|
132
172
|
```
|
|
133
173
|
|
|
174
|
+
---
|
|
175
|
+
|
|
134
176
|
### 文件更新
|
|
135
177
|
|
|
136
178
|
#### `update(path, fileBody, fileOptions?)`
|
|
137
179
|
|
|
138
180
|
替换指定路径的现有文件。
|
|
139
181
|
|
|
140
|
-
|
|
182
|
+
**入参:**
|
|
183
|
+
```typescript
|
|
184
|
+
function update(
|
|
185
|
+
path: string, // 文件路径
|
|
186
|
+
fileBody: FileBody, // 文件内容
|
|
187
|
+
fileOptions?: FileOptions // 上传选项
|
|
188
|
+
): Promise<UploadResult>
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
**出参:**
|
|
192
|
+
```typescript
|
|
193
|
+
type UploadResult =
|
|
194
|
+
| { data: UploadFileData; error: null }
|
|
195
|
+
| { data: null; error: StorageError }
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
---
|
|
199
|
+
|
|
200
|
+
### 从URL上传文件
|
|
141
201
|
|
|
142
|
-
|
|
202
|
+
#### `uploadFromUrl(url)`
|
|
203
|
+
|
|
204
|
+
从公共URL上传文件到存储桶。自动透传响应头(content-type、content-disposition、cache-control)到上传请求。对于火山引擎北京区域的 TOS URL,会使用服务端复制优化性能。
|
|
205
|
+
|
|
206
|
+
**入参:**
|
|
207
|
+
```typescript
|
|
208
|
+
function uploadFromUrl(
|
|
209
|
+
url: string // 公共文件URL(如 CDN 链接)
|
|
210
|
+
): Promise<UploadFromUrlResult>
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
**出参:**
|
|
214
|
+
```typescript
|
|
215
|
+
type UploadFromUrlResult =
|
|
216
|
+
| { data: UploadFileData; error: null }
|
|
217
|
+
| { data: null; error: StorageError }
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
**功能特性:**
|
|
221
|
+
- ✅ 只允许 `http://` 和 `https://` 协议
|
|
222
|
+
- ✅ 自动透传响应头到上传请求(contentType、contentDisposition、cacheControl)
|
|
223
|
+
- ✅ 检测并拒绝空文件
|
|
224
|
+
- ✅ 火山引擎北京区域 TOS URL 使用服务端复制优化
|
|
225
|
+
|
|
226
|
+
**示例:**
|
|
227
|
+
```javascript
|
|
228
|
+
// 基本用法
|
|
229
|
+
const { data, error } = await storage.uploadFromUrl(
|
|
230
|
+
'https://cdn.example.com/images/photo.jpg'
|
|
231
|
+
);
|
|
232
|
+
|
|
233
|
+
if (data) {
|
|
234
|
+
console.log('文件上传成功:', data.download_url);
|
|
235
|
+
}
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
**错误处理:**
|
|
239
|
+
```javascript
|
|
240
|
+
const { data, error } = await storage.uploadFromUrl('https://example.com/file.pdf');
|
|
241
|
+
|
|
242
|
+
if (error) {
|
|
243
|
+
// 可能的错误类型:
|
|
244
|
+
// - 'Invalid URL provided' - URL格式无效
|
|
245
|
+
// - 'Only http and https protocols are supported' - 协议不支持
|
|
246
|
+
// - 'Failed to download file from URL: 404 Not Found' - 下载失败
|
|
247
|
+
// - 'Downloaded file is empty' - 下载的文件为空
|
|
248
|
+
console.error(error.message);
|
|
249
|
+
}
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
---
|
|
143
253
|
|
|
144
254
|
### 创建签名URL
|
|
145
255
|
|
|
@@ -147,22 +257,23 @@ const { data, error } = await storage.upload('avatars/user-123.jpg', file, {
|
|
|
147
257
|
|
|
148
258
|
创建具有时效性的安全文件访问链接。
|
|
149
259
|
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
}
|
|
260
|
+
**入参:**
|
|
261
|
+
```typescript
|
|
262
|
+
function createSignedUrl(
|
|
263
|
+
path: string, // 文件路径或 download_url
|
|
264
|
+
expiresIn: number, // 过期时间(秒)
|
|
265
|
+
options?: {
|
|
266
|
+
download?: string | boolean; // 是否触发下载
|
|
267
|
+
transform?: TransformOptions; // 图片转换选项
|
|
268
|
+
}
|
|
269
|
+
): Promise<SignedUrlResult>
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
**出参:**
|
|
273
|
+
```typescript
|
|
274
|
+
type SignedUrlResult =
|
|
275
|
+
| { data: { signedUrl: string }; error: null }
|
|
276
|
+
| { data: null; error: StorageError }
|
|
166
277
|
```
|
|
167
278
|
|
|
168
279
|
**示例:**
|
|
@@ -181,80 +292,99 @@ const { data, error } = await storage.createSignedUrl('images/photo.jpg', 3600,
|
|
|
181
292
|
});
|
|
182
293
|
```
|
|
183
294
|
|
|
295
|
+
---
|
|
296
|
+
|
|
184
297
|
#### `createSignedUrls(paths, expiresIn, options?)`
|
|
185
298
|
|
|
186
299
|
批量创建签名URL。
|
|
187
300
|
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
error: string | null;
|
|
198
|
-
path: string | null;
|
|
199
|
-
signedUrl: string;
|
|
200
|
-
}>;
|
|
201
|
-
error: null;
|
|
202
|
-
} | {
|
|
203
|
-
data: null;
|
|
204
|
-
error: StorageError;
|
|
205
|
-
}
|
|
301
|
+
**入参:**
|
|
302
|
+
```typescript
|
|
303
|
+
function createSignedUrls(
|
|
304
|
+
paths: string[], // 文件路径或 download_url 数组
|
|
305
|
+
expiresIn: number, // 过期时间(秒)
|
|
306
|
+
options?: {
|
|
307
|
+
download?: string | boolean;
|
|
308
|
+
}
|
|
309
|
+
): Promise<SignedUrlsResult>
|
|
206
310
|
```
|
|
207
311
|
|
|
312
|
+
**出参:**
|
|
313
|
+
```typescript
|
|
314
|
+
type SignedUrlsResult =
|
|
315
|
+
| {
|
|
316
|
+
data: Array<{
|
|
317
|
+
error: string | null;
|
|
318
|
+
path: string | null;
|
|
319
|
+
signedUrl: string;
|
|
320
|
+
}>;
|
|
321
|
+
error: null
|
|
322
|
+
}
|
|
323
|
+
| { data: null; error: StorageError }
|
|
324
|
+
```
|
|
325
|
+
|
|
326
|
+
**示例:**
|
|
327
|
+
```javascript
|
|
328
|
+
const { data, error } = await storage.createSignedUrls(
|
|
329
|
+
['file1.pdf', 'file2.pdf', 'file3.pdf'],
|
|
330
|
+
3600
|
|
331
|
+
);
|
|
332
|
+
```
|
|
333
|
+
|
|
334
|
+
---
|
|
335
|
+
|
|
208
336
|
### 文件下载
|
|
209
337
|
|
|
210
338
|
#### `download(path, options?)`
|
|
211
339
|
|
|
212
340
|
从私有存储桶下载文件。
|
|
213
341
|
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
342
|
+
**入参:**
|
|
343
|
+
```typescript
|
|
344
|
+
function download(
|
|
345
|
+
path: string, // 文件路径
|
|
346
|
+
options?: {
|
|
347
|
+
transform?: TransformOptions; // 图片转换选项
|
|
348
|
+
}
|
|
349
|
+
): Promise<DownloadResult>
|
|
350
|
+
```
|
|
218
351
|
|
|
219
|
-
|
|
352
|
+
**出参:**
|
|
220
353
|
```typescript
|
|
221
|
-
|
|
222
|
-
data: Blob;
|
|
223
|
-
|
|
224
|
-
} | {
|
|
225
|
-
data: null;
|
|
226
|
-
error: StorageError;
|
|
227
|
-
}
|
|
354
|
+
type DownloadResult =
|
|
355
|
+
| { data: Blob; error: null }
|
|
356
|
+
| { data: null; error: StorageError }
|
|
228
357
|
```
|
|
229
358
|
|
|
230
359
|
**示例:**
|
|
231
360
|
```javascript
|
|
232
361
|
const { data, error } = await storage.download('documents/report.pdf');
|
|
233
362
|
if (data) {
|
|
234
|
-
// 处理下载的文件数据
|
|
235
363
|
const url = URL.createObjectURL(data);
|
|
236
364
|
window.open(url);
|
|
237
365
|
}
|
|
238
366
|
```
|
|
239
367
|
|
|
368
|
+
---
|
|
369
|
+
|
|
240
370
|
### 文件删除
|
|
241
371
|
|
|
242
372
|
#### `remove(paths)`
|
|
243
373
|
|
|
244
374
|
删除指定路径的文件。
|
|
245
375
|
|
|
246
|
-
|
|
247
|
-
|
|
376
|
+
**入参:**
|
|
377
|
+
```typescript
|
|
378
|
+
function remove(
|
|
379
|
+
paths: string[] // 文件路径或 download_url 数组
|
|
380
|
+
): Promise<RemoveResult>
|
|
381
|
+
```
|
|
248
382
|
|
|
249
|
-
|
|
383
|
+
**出参:**
|
|
250
384
|
```typescript
|
|
251
|
-
|
|
252
|
-
data: FileObject[];
|
|
253
|
-
|
|
254
|
-
} | {
|
|
255
|
-
data: null;
|
|
256
|
-
error: StorageError;
|
|
257
|
-
}
|
|
385
|
+
type RemoveResult =
|
|
386
|
+
| { data: FileObject[]; error: null }
|
|
387
|
+
| { data: null; error: StorageError }
|
|
258
388
|
```
|
|
259
389
|
|
|
260
390
|
**示例:**
|
|
@@ -265,26 +395,28 @@ const { data, error } = await storage.remove([
|
|
|
265
395
|
]);
|
|
266
396
|
```
|
|
267
397
|
|
|
398
|
+
---
|
|
399
|
+
|
|
268
400
|
### 文件列表
|
|
269
401
|
|
|
270
402
|
#### `list(path?, options?, parameters?)`
|
|
271
403
|
|
|
272
404
|
列出存储桶中的文件。
|
|
273
405
|
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
406
|
+
**入参:**
|
|
407
|
+
```typescript
|
|
408
|
+
function list(
|
|
409
|
+
path?: string, // 文件夹路径
|
|
410
|
+
options?: SearchOptions, // 搜索选项
|
|
411
|
+
parameters?: FetchParameters // 请求参数
|
|
412
|
+
): Promise<ListResult>
|
|
413
|
+
```
|
|
278
414
|
|
|
279
|
-
|
|
415
|
+
**出参:**
|
|
280
416
|
```typescript
|
|
281
|
-
|
|
282
|
-
data: FileObject[];
|
|
283
|
-
|
|
284
|
-
} | {
|
|
285
|
-
data: null;
|
|
286
|
-
error: StorageError;
|
|
287
|
-
}
|
|
417
|
+
type ListResult =
|
|
418
|
+
| { data: FileObject[]; error: null }
|
|
419
|
+
| { data: null; error: StorageError }
|
|
288
420
|
```
|
|
289
421
|
|
|
290
422
|
**示例:**
|
|
@@ -304,99 +436,208 @@ const { data, error } = await storage.list('documents', {
|
|
|
304
436
|
});
|
|
305
437
|
```
|
|
306
438
|
|
|
439
|
+
---
|
|
440
|
+
|
|
307
441
|
## 类型定义
|
|
308
442
|
|
|
309
443
|
### FileBody
|
|
444
|
+
|
|
310
445
|
支持以下文件格式:
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
446
|
+
|
|
447
|
+
```typescript
|
|
448
|
+
type FileBody =
|
|
449
|
+
| ArrayBuffer
|
|
450
|
+
| ArrayBufferView
|
|
451
|
+
| Blob
|
|
452
|
+
| Buffer
|
|
453
|
+
| File
|
|
454
|
+
| FormData
|
|
455
|
+
| NodeJS.ReadableStream
|
|
456
|
+
| ReadableStream<Uint8Array>
|
|
457
|
+
| URLSearchParams
|
|
458
|
+
| string;
|
|
459
|
+
```
|
|
321
460
|
|
|
322
461
|
### FileOptions
|
|
462
|
+
|
|
323
463
|
```typescript
|
|
324
464
|
interface FileOptions {
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
465
|
+
/** 缓存控制时间(秒),设置 Cache-Control: max-age=<seconds> 头 */
|
|
466
|
+
cacheControl?: string | number;
|
|
467
|
+
/** Content-Type 头值 */
|
|
468
|
+
contentType?: string;
|
|
469
|
+
/** 是否覆盖已存在文件,默认 false */
|
|
470
|
+
upsert?: boolean;
|
|
471
|
+
/** 双工流选项 */
|
|
472
|
+
duplex?: string;
|
|
473
|
+
/** 文件元数据 */
|
|
474
|
+
metadata?: Record<string, any>;
|
|
475
|
+
/** 额外请求头 */
|
|
476
|
+
headers?: Record<string, string>;
|
|
331
477
|
}
|
|
332
478
|
```
|
|
333
479
|
|
|
334
480
|
### FileOptionsV2
|
|
335
|
-
```typescript
|
|
336
|
-
interface FileOptionsV2 {
|
|
337
|
-
filePath?: string; // 文件路径(用于 uploadFile)
|
|
338
|
-
cacheControl?: string | number; // 缓存控制(秒)
|
|
339
|
-
contentType?: string; // 内容类型
|
|
340
|
-
upsert?: boolean; // 是否覆盖已存在文件
|
|
341
|
-
contentDisposition?: string; // 内容处置
|
|
342
|
-
}
|
|
343
|
-
```
|
|
344
481
|
|
|
345
|
-
### TransformOptions
|
|
346
482
|
```typescript
|
|
347
|
-
interface
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
483
|
+
interface FileOptionsV2 {
|
|
484
|
+
/** 文件上传路径 */
|
|
485
|
+
filePath?: string;
|
|
486
|
+
/** 缓存控制时间(秒) */
|
|
487
|
+
cacheControl?: string | number;
|
|
488
|
+
/** Content-Type 头值 */
|
|
489
|
+
contentType?: string;
|
|
490
|
+
/** 是否覆盖已存在文件,默认 false */
|
|
491
|
+
upsert?: boolean;
|
|
492
|
+
/** Content-Disposition 响应头值 */
|
|
493
|
+
contentDisposition?: string;
|
|
353
494
|
}
|
|
354
495
|
```
|
|
355
496
|
|
|
356
497
|
### UploadFileData
|
|
498
|
+
|
|
357
499
|
```typescript
|
|
358
500
|
interface UploadFileData {
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
501
|
+
/** 文件ID */
|
|
502
|
+
id: string;
|
|
503
|
+
/** 文件路径 */
|
|
504
|
+
file_path: string;
|
|
505
|
+
/** 存储桶ID */
|
|
506
|
+
bucket_id: string;
|
|
507
|
+
/** 下载URL */
|
|
508
|
+
download_url: string;
|
|
363
509
|
}
|
|
364
510
|
```
|
|
365
511
|
|
|
366
512
|
### FileObject
|
|
513
|
+
|
|
367
514
|
```typescript
|
|
368
515
|
interface FileObject {
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
516
|
+
/** 文件名 */
|
|
517
|
+
name: string;
|
|
518
|
+
/** 存储桶ID */
|
|
519
|
+
bucket_id: string;
|
|
520
|
+
/** 所有者 */
|
|
521
|
+
owner: string;
|
|
522
|
+
/** 文件ID */
|
|
523
|
+
id: string;
|
|
524
|
+
/** 更新时间 */
|
|
525
|
+
updated_at: string;
|
|
526
|
+
/** 创建时间 */
|
|
527
|
+
created_at: string;
|
|
528
|
+
/** 创建者 */
|
|
529
|
+
created_by: string;
|
|
530
|
+
/** 更新者 */
|
|
531
|
+
updated_by: string;
|
|
532
|
+
/** 最后访问时间 */
|
|
533
|
+
last_accessed_at?: string;
|
|
534
|
+
/** 元数据 */
|
|
535
|
+
metadata: Record<string, any>;
|
|
536
|
+
/** 存储桶信息 */
|
|
537
|
+
buckets: Bucket;
|
|
538
|
+
}
|
|
539
|
+
```
|
|
540
|
+
|
|
541
|
+
### Bucket
|
|
542
|
+
|
|
543
|
+
```typescript
|
|
544
|
+
interface Bucket {
|
|
545
|
+
/** 存储桶ID */
|
|
546
|
+
id: string;
|
|
547
|
+
/** 存储桶类型 */
|
|
548
|
+
type?: 'STANDARD' | 'ANALYTICS';
|
|
549
|
+
/** 存储桶名称 */
|
|
550
|
+
name: string;
|
|
551
|
+
/** 所有者 */
|
|
552
|
+
owner: string;
|
|
553
|
+
/** 文件大小限制 */
|
|
554
|
+
file_size_limit?: number;
|
|
555
|
+
/** 允许的MIME类型 */
|
|
556
|
+
allowed_mime_types?: string[];
|
|
557
|
+
/** 创建时间 */
|
|
558
|
+
created_at: string;
|
|
559
|
+
/** 更新时间 */
|
|
560
|
+
updated_at: string;
|
|
561
|
+
/** 是否公开 */
|
|
562
|
+
public: boolean;
|
|
380
563
|
}
|
|
381
564
|
```
|
|
382
565
|
|
|
383
566
|
### SearchOptions
|
|
567
|
+
|
|
384
568
|
```typescript
|
|
385
569
|
interface SearchOptions {
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
570
|
+
/** 返回文件数量,默认 100 */
|
|
571
|
+
limit?: number;
|
|
572
|
+
/** 起始位置 */
|
|
573
|
+
offset?: number;
|
|
574
|
+
/** 排序选项 */
|
|
575
|
+
sortBy?: SortBy;
|
|
576
|
+
/** 搜索字符串 */
|
|
577
|
+
search?: string;
|
|
578
|
+
}
|
|
579
|
+
```
|
|
580
|
+
|
|
581
|
+
### SortBy
|
|
582
|
+
|
|
583
|
+
```typescript
|
|
584
|
+
interface SortBy {
|
|
585
|
+
/** 排序列名 */
|
|
586
|
+
column?: string;
|
|
587
|
+
/** 排序顺序:'asc' | 'desc' */
|
|
588
|
+
order?: string;
|
|
589
|
+
}
|
|
590
|
+
```
|
|
591
|
+
|
|
592
|
+
### TransformOptions
|
|
593
|
+
|
|
594
|
+
```typescript
|
|
595
|
+
interface TransformOptions {
|
|
596
|
+
/** 宽度(像素) */
|
|
597
|
+
width?: number;
|
|
598
|
+
/** 高度(像素) */
|
|
599
|
+
height?: number;
|
|
600
|
+
/** 调整大小模式 */
|
|
601
|
+
resize?: 'cover' | 'contain' | 'fill';
|
|
602
|
+
/** 质量(20-100) */
|
|
603
|
+
quality?: number;
|
|
604
|
+
/** 格式 */
|
|
605
|
+
format?: 'origin';
|
|
390
606
|
}
|
|
391
607
|
```
|
|
392
608
|
|
|
393
609
|
### FetchParameters
|
|
610
|
+
|
|
394
611
|
```typescript
|
|
395
612
|
interface FetchParameters {
|
|
396
|
-
|
|
613
|
+
/** AbortController 信号,用于取消请求 */
|
|
614
|
+
signal?: AbortSignal;
|
|
615
|
+
}
|
|
616
|
+
```
|
|
617
|
+
|
|
618
|
+
### StorageError
|
|
619
|
+
|
|
620
|
+
```typescript
|
|
621
|
+
class StorageError extends Error {
|
|
622
|
+
message: string;
|
|
397
623
|
}
|
|
398
624
|
```
|
|
399
625
|
|
|
626
|
+
### OnRequestErrorType
|
|
627
|
+
|
|
628
|
+
```typescript
|
|
629
|
+
interface OnRequestErrorType {
|
|
630
|
+
code: number;
|
|
631
|
+
details: string;
|
|
632
|
+
hint: string | null;
|
|
633
|
+
message: string;
|
|
634
|
+
status: number;
|
|
635
|
+
statusText: string;
|
|
636
|
+
}
|
|
637
|
+
```
|
|
638
|
+
|
|
639
|
+
---
|
|
640
|
+
|
|
400
641
|
## 错误处理
|
|
401
642
|
|
|
402
643
|
所有API方法都返回包含 `data` 和 `error` 的对象。当操作成功时,`error` 为 `null`,`data` 包含返回数据;当操作失败时,`data` 为 `null`,`error` 包含错误信息。
|
|
@@ -406,13 +647,13 @@ const { data, error } = await storage.upload('test.jpg', file);
|
|
|
406
647
|
|
|
407
648
|
if (error) {
|
|
408
649
|
console.error('操作失败:', error.message);
|
|
409
|
-
// 错误处理
|
|
410
650
|
} else {
|
|
411
651
|
console.log('操作成功:', data);
|
|
412
|
-
// 处理返回数据
|
|
413
652
|
}
|
|
414
653
|
```
|
|
415
654
|
|
|
655
|
+
---
|
|
656
|
+
|
|
416
657
|
## 高级用法
|
|
417
658
|
|
|
418
659
|
### 日志记录
|
|
@@ -444,7 +685,6 @@ const storage = new StorageFileApi(
|
|
|
444
685
|
false,
|
|
445
686
|
(error) => {
|
|
446
687
|
console.error('请求错误:', error);
|
|
447
|
-
// 自定义错误处理
|
|
448
688
|
}
|
|
449
689
|
);
|
|
450
690
|
```
|
|
@@ -468,6 +708,8 @@ const storage = new StorageFileApi(
|
|
|
468
708
|
);
|
|
469
709
|
```
|
|
470
710
|
|
|
711
|
+
---
|
|
712
|
+
|
|
471
713
|
## 许可证
|
|
472
714
|
|
|
473
715
|
MIT License
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@data-loom/storage-js",
|
|
3
|
-
"version": "0.4.4-alpha.
|
|
3
|
+
"version": "0.4.4-alpha.10",
|
|
4
4
|
"description": "Dataloom service, modified from supabase/storage-js",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"service",
|
|
@@ -14,6 +14,11 @@
|
|
|
14
14
|
"main": "dist/main/index.js",
|
|
15
15
|
"module": "dist/module/index.js",
|
|
16
16
|
"types": "dist/module/index.d.ts",
|
|
17
|
+
"husky": {
|
|
18
|
+
"hooks": {
|
|
19
|
+
"pre-commit": "npm run test:precommit"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
17
22
|
"scripts": {
|
|
18
23
|
"clean": "rimraf dist docs/v2",
|
|
19
24
|
"format": "prettier --write \"{src,test}/**/**/*.ts\"",
|
|
@@ -30,10 +35,11 @@
|
|
|
30
35
|
"test:types": "run-s build:module && tsd --files test/*.test-d.ts",
|
|
31
36
|
"test:cov": "exit 0",
|
|
32
37
|
"lint": "exit 0",
|
|
33
|
-
"test:types:watch": "run-s build && tsd --files 'test/**/*.test-d.ts' --watch"
|
|
38
|
+
"test:types:watch": "run-s build && tsd --files 'test/**/*.test-d.ts' --watch",
|
|
39
|
+
"test:precommit": "jest --testPathIgnorePatterns='storageFileApi.test.ts|storageApi.test.ts|storageFileApiNode.test.ts' --passWithNoTests"
|
|
34
40
|
},
|
|
35
41
|
"dependencies": {
|
|
36
|
-
"@data-loom/node-fetch": "
|
|
42
|
+
"@data-loom/node-fetch": "0.4.3"
|
|
37
43
|
},
|
|
38
44
|
"devDependencies": {
|
|
39
45
|
"@types/jest": "^26.0.13",
|