@supabase/storage-js 1.7.1 → 1.8.0-next.1

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.
Files changed (62) hide show
  1. package/README.md +11 -1
  2. package/dist/main/index.d.ts +1 -0
  3. package/dist/main/index.d.ts.map +1 -1
  4. package/dist/main/index.js +6 -1
  5. package/dist/main/index.js.map +1 -1
  6. package/dist/main/lib/StorageBucketApi.d.ts +31 -12
  7. package/dist/main/lib/StorageBucketApi.d.ts.map +1 -1
  8. package/dist/main/lib/StorageBucketApi.js +32 -13
  9. package/dist/main/lib/StorageBucketApi.js.map +1 -1
  10. package/dist/main/lib/StorageFileApi.d.ts +53 -30
  11. package/dist/main/lib/StorageFileApi.d.ts.map +1 -1
  12. package/dist/main/lib/StorageFileApi.js +86 -36
  13. package/dist/main/lib/StorageFileApi.js.map +1 -1
  14. package/dist/main/lib/errors.d.ts +19 -0
  15. package/dist/main/lib/errors.d.ts.map +1 -0
  16. package/dist/main/lib/errors.js +39 -0
  17. package/dist/main/lib/errors.js.map +1 -0
  18. package/dist/main/lib/fetch.d.ts.map +1 -1
  19. package/dist/main/lib/fetch.js +13 -11
  20. package/dist/main/lib/fetch.js.map +1 -1
  21. package/dist/main/lib/helpers.d.ts +6 -0
  22. package/dist/main/lib/helpers.d.ts.map +1 -1
  23. package/dist/main/lib/helpers.js +42 -6
  24. package/dist/main/lib/helpers.js.map +1 -1
  25. package/dist/main/lib/index.js +5 -1
  26. package/dist/main/lib/index.js.map +1 -1
  27. package/dist/main/lib/version.d.ts +1 -1
  28. package/dist/main/lib/version.js +1 -1
  29. package/dist/module/index.d.ts +1 -0
  30. package/dist/module/index.d.ts.map +1 -1
  31. package/dist/module/index.js +1 -0
  32. package/dist/module/index.js.map +1 -1
  33. package/dist/module/lib/StorageBucketApi.d.ts +31 -12
  34. package/dist/module/lib/StorageBucketApi.d.ts.map +1 -1
  35. package/dist/module/lib/StorageBucketApi.js +25 -6
  36. package/dist/module/lib/StorageBucketApi.js.map +1 -1
  37. package/dist/module/lib/StorageFileApi.d.ts +53 -30
  38. package/dist/module/lib/StorageFileApi.d.ts.map +1 -1
  39. package/dist/module/lib/StorageFileApi.js +78 -28
  40. package/dist/module/lib/StorageFileApi.js.map +1 -1
  41. package/dist/module/lib/errors.d.ts +19 -0
  42. package/dist/module/lib/errors.d.ts.map +1 -0
  43. package/dist/module/lib/errors.js +32 -0
  44. package/dist/module/lib/errors.js.map +1 -0
  45. package/dist/module/lib/fetch.d.ts.map +1 -1
  46. package/dist/module/lib/fetch.js +13 -11
  47. package/dist/module/lib/fetch.js.map +1 -1
  48. package/dist/module/lib/helpers.d.ts +6 -0
  49. package/dist/module/lib/helpers.d.ts.map +1 -1
  50. package/dist/module/lib/helpers.js +16 -2
  51. package/dist/module/lib/helpers.js.map +1 -1
  52. package/dist/module/lib/version.d.ts +1 -1
  53. package/dist/module/lib/version.js +1 -1
  54. package/dist/umd/supabase.js +1 -1
  55. package/package.json +3 -8
  56. package/src/index.ts +1 -0
  57. package/src/lib/StorageBucketApi.ts +93 -12
  58. package/src/lib/StorageFileApi.ts +195 -51
  59. package/src/lib/errors.ts +40 -0
  60. package/src/lib/fetch.ts +13 -10
  61. package/src/lib/helpers.ts +9 -3
  62. package/src/lib/version.ts +1 -1
@@ -1,3 +1,4 @@
1
+ import { isStorageError, StorageError } from './errors'
1
2
  import { Fetch, FetchParameters, get, post, remove } from './fetch'
2
3
  import { resolveFetch } from './helpers'
3
4
  import { FileObject, FileOptions, SearchOptions } from './types'
@@ -61,7 +62,16 @@ export class StorageFileApi {
61
62
  | URLSearchParams
62
63
  | string,
63
64
  fileOptions?: FileOptions
64
- ): Promise<{ data: { Key: string } | null; error: Error | null }> {
65
+ ): Promise<
66
+ | {
67
+ data: { path: string }
68
+ error: null
69
+ }
70
+ | {
71
+ data: null
72
+ error: StorageError
73
+ }
74
+ > {
65
75
  try {
66
76
  let body
67
77
  const options = { ...DEFAULT_FILE_OPTIONS, ...fileOptions }
@@ -92,15 +102,20 @@ export class StorageFileApi {
92
102
  })
93
103
 
94
104
  if (res.ok) {
95
- // const data = await res.json()
96
- // temporary fix till backend is updated to the latest storage-api version
97
- return { data: { Key: _path }, error: null }
105
+ return {
106
+ data: { path: cleanPath },
107
+ error: null,
108
+ }
98
109
  } else {
99
110
  const error = await res.json()
100
111
  return { data: null, error }
101
112
  }
102
113
  } catch (error) {
103
- return { data: null, error }
114
+ if (isStorageError(error)) {
115
+ return { data: null, error }
116
+ }
117
+
118
+ throw error
104
119
  }
105
120
  }
106
121
 
@@ -128,7 +143,16 @@ export class StorageFileApi {
128
143
  | URLSearchParams
129
144
  | string,
130
145
  fileOptions?: FileOptions
131
- ): Promise<{ data: { Key: string } | null; error: Error | null }> {
146
+ ): Promise<
147
+ | {
148
+ data: { path: string }
149
+ error: null
150
+ }
151
+ | {
152
+ data: null
153
+ error: StorageError
154
+ }
155
+ > {
132
156
  return this.uploadOrUpdate('POST', path, fileBody, fileOptions)
133
157
  }
134
158
 
@@ -156,7 +180,16 @@ export class StorageFileApi {
156
180
  | URLSearchParams
157
181
  | string,
158
182
  fileOptions?: FileOptions
159
- ): Promise<{ data: { Key: string } | null; error: Error | null }> {
183
+ ): Promise<
184
+ | {
185
+ data: { path: string }
186
+ error: null
187
+ }
188
+ | {
189
+ data: null
190
+ error: StorageError
191
+ }
192
+ > {
160
193
  return this.uploadOrUpdate('PUT', path, fileBody, fileOptions)
161
194
  }
162
195
 
@@ -169,7 +202,16 @@ export class StorageFileApi {
169
202
  async move(
170
203
  fromPath: string,
171
204
  toPath: string
172
- ): Promise<{ data: { message: string } | null; error: Error | null }> {
205
+ ): Promise<
206
+ | {
207
+ data: { message: string }
208
+ error: null
209
+ }
210
+ | {
211
+ data: null
212
+ error: StorageError
213
+ }
214
+ > {
173
215
  try {
174
216
  const data = await post(
175
217
  this.fetch,
@@ -179,7 +221,11 @@ export class StorageFileApi {
179
221
  )
180
222
  return { data, error: null }
181
223
  } catch (error) {
182
- return { data: null, error }
224
+ if (isStorageError(error)) {
225
+ return { data: null, error }
226
+ }
227
+
228
+ throw error
183
229
  }
184
230
  }
185
231
 
@@ -192,7 +238,16 @@ export class StorageFileApi {
192
238
  async copy(
193
239
  fromPath: string,
194
240
  toPath: string
195
- ): Promise<{ data: { message: string } | null; error: Error | null }> {
241
+ ): Promise<
242
+ | {
243
+ data: { message: string }
244
+ error: null
245
+ }
246
+ | {
247
+ data: null
248
+ error: StorageError
249
+ }
250
+ > {
196
251
  try {
197
252
  const data = await post(
198
253
  this.fetch,
@@ -202,7 +257,11 @@ export class StorageFileApi {
202
257
  )
203
258
  return { data, error: null }
204
259
  } catch (error) {
205
- return { data: null, error }
260
+ if (isStorageError(error)) {
261
+ return { data: null, error }
262
+ }
263
+
264
+ throw error
206
265
  }
207
266
  }
208
267
 
@@ -215,11 +274,18 @@ export class StorageFileApi {
215
274
  async createSignedUrl(
216
275
  path: string,
217
276
  expiresIn: number
218
- ): Promise<{
219
- data: { signedURL: string } | null
220
- error: Error | null
221
- signedURL: string | null
222
- }> {
277
+ ): Promise<
278
+ | {
279
+ data: { signedURL: string }
280
+ error: null
281
+ signedURL: string
282
+ }
283
+ | {
284
+ data: null
285
+ error: StorageError
286
+ signedURL: null
287
+ }
288
+ > {
223
289
  try {
224
290
  const _path = this._getFinalPath(path)
225
291
  let data = await post(
@@ -232,7 +298,11 @@ export class StorageFileApi {
232
298
  data = { signedURL }
233
299
  return { data, error: null, signedURL }
234
300
  } catch (error) {
235
- return { data: null, error, signedURL: null }
301
+ if (isStorageError(error)) {
302
+ return { data: null, error, signedURL: null }
303
+ }
304
+
305
+ throw error
236
306
  }
237
307
  }
238
308
 
@@ -245,10 +315,16 @@ export class StorageFileApi {
245
315
  async createSignedUrls(
246
316
  paths: string[],
247
317
  expiresIn: number
248
- ): Promise<{
249
- data: { error: string | null; path: string | null; signedURL: string }[] | null
250
- error: Error | null
251
- }> {
318
+ ): Promise<
319
+ | {
320
+ data: { error: string | null; path: string | null; signedURL: string }[]
321
+ error: null
322
+ }
323
+ | {
324
+ data: null
325
+ error: StorageError
326
+ }
327
+ > {
252
328
  try {
253
329
  const data = await post(
254
330
  this.fetch,
@@ -264,7 +340,11 @@ export class StorageFileApi {
264
340
  error: null,
265
341
  }
266
342
  } catch (error) {
267
- return { data: null, error }
343
+ if (isStorageError(error)) {
344
+ return { data: null, error }
345
+ }
346
+
347
+ throw error
268
348
  }
269
349
  }
270
350
 
@@ -273,7 +353,18 @@ export class StorageFileApi {
273
353
  *
274
354
  * @param path The file path to be downloaded, including the path and file name. For example `folder/image.png`.
275
355
  */
276
- async download(path: string): Promise<{ data: Blob | null; error: Error | null }> {
356
+ async download(
357
+ path: string
358
+ ): Promise<
359
+ | {
360
+ data: Blob
361
+ error: null
362
+ }
363
+ | {
364
+ data: null
365
+ error: StorageError
366
+ }
367
+ > {
277
368
  try {
278
369
  const _path = this._getFinalPath(path)
279
370
  const res = await get(this.fetch, `${this.url}/object/${_path}`, {
@@ -283,30 +374,22 @@ export class StorageFileApi {
283
374
  const data = await res.blob()
284
375
  return { data, error: null }
285
376
  } catch (error) {
286
- return { data: null, error }
377
+ if (isStorageError(error)) {
378
+ return { data: null, error }
379
+ }
380
+
381
+ throw error
287
382
  }
288
383
  }
289
384
 
290
385
  /**
291
- * Retrieve URLs for assets in public buckets
386
+ * Retrieve URLs for assets in public buckets and encapsulates it in a return object
292
387
  *
293
388
  * @param path The file path to be downloaded, including the path and file name. For example `folder/image.png`.
294
389
  */
295
- getPublicUrl(
296
- path: string
297
- ): {
298
- data: { publicURL: string } | null
299
- error: Error | null
300
- publicURL: string | null
301
- } {
302
- try {
303
- const _path = this._getFinalPath(path)
304
- const publicURL = `${this.url}/object/public/${_path}`
305
- const data = { publicURL }
306
- return { data, error: null, publicURL }
307
- } catch (error) {
308
- return { data: null, error, publicURL: null }
309
- }
390
+ getPublicUrl(path: string): string {
391
+ const _path = this._getFinalPath(path)
392
+ return `${this.url}/object/public/${_path}`
310
393
  }
311
394
 
312
395
  /**
@@ -314,7 +397,18 @@ export class StorageFileApi {
314
397
  *
315
398
  * @param paths An array of files to be deleted, including the path and file name. For example [`folder/image.png`].
316
399
  */
317
- async remove(paths: string[]): Promise<{ data: FileObject[] | null; error: Error | null }> {
400
+ async remove(
401
+ paths: string[]
402
+ ): Promise<
403
+ | {
404
+ data: FileObject[]
405
+ error: null
406
+ }
407
+ | {
408
+ data: null
409
+ error: StorageError
410
+ }
411
+ > {
318
412
  try {
319
413
  const data = await remove(
320
414
  this.fetch,
@@ -324,7 +418,11 @@ export class StorageFileApi {
324
418
  )
325
419
  return { data, error: null }
326
420
  } catch (error) {
327
- return { data: null, error }
421
+ if (isStorageError(error)) {
422
+ return { data: null, error }
423
+ }
424
+
425
+ throw error
328
426
  }
329
427
  }
330
428
 
@@ -332,12 +430,27 @@ export class StorageFileApi {
332
430
  * Get file metadata
333
431
  * @param id the file id to retrieve metadata
334
432
  */
335
- // async getMetadata(id: string): Promise<{ data: Metadata | null; error: Error | null }> {
433
+ // async getMetadata(
434
+ // id: string
435
+ // ): Promise<
436
+ // | {
437
+ // data: Metadata
438
+ // error: null
439
+ // }
440
+ // | {
441
+ // data: null
442
+ // error: StorageError
443
+ // }
444
+ // > {
336
445
  // try {
337
- // const data = await get(`${this.url}/metadata/${id}`, { headers: this.headers })
446
+ // const data = await get(this.fetch, `${this.url}/metadata/${id}`, { headers: this.headers })
338
447
  // return { data, error: null }
339
448
  // } catch (error) {
340
- // return { data: null, error }
449
+ // if (isStorageError(error)) {
450
+ // return { data: null, error }
451
+ // }
452
+
453
+ // throw error
341
454
  // }
342
455
  // }
343
456
 
@@ -349,26 +462,53 @@ export class StorageFileApi {
349
462
  // async updateMetadata(
350
463
  // id: string,
351
464
  // meta: Metadata
352
- // ): Promise<{ data: Metadata | null; error: Error | null }> {
465
+ // ): Promise<
466
+ // | {
467
+ // data: Metadata
468
+ // error: null
469
+ // }
470
+ // | {
471
+ // data: null
472
+ // error: StorageError
473
+ // }
474
+ // > {
353
475
  // try {
354
- // const data = await post(`${this.url}/metadata/${id}`, { ...meta }, { headers: this.headers })
476
+ // const data = await post(
477
+ // this.fetch,
478
+ // `${this.url}/metadata/${id}`,
479
+ // { ...meta },
480
+ // { headers: this.headers }
481
+ // )
355
482
  // return { data, error: null }
356
483
  // } catch (error) {
357
- // return { data: null, error }
484
+ // if (isStorageError(error)) {
485
+ // return { data: null, error }
486
+ // }
487
+
488
+ // throw error
358
489
  // }
359
490
  // }
360
491
 
361
492
  /**
362
493
  * Lists all the files within a bucket.
363
494
  * @param path The folder path.
364
- * @param options Search options, including `limit`, `offset`, and `sortBy`.
495
+ * @param options Search options, including `limit`, `offset`, `sortBy`, and `search`.
365
496
  * @param parameters Fetch parameters, currently only supports `signal`, which is an AbortController's signal
366
497
  */
367
498
  async list(
368
499
  path?: string,
369
500
  options?: SearchOptions,
370
501
  parameters?: FetchParameters
371
- ): Promise<{ data: FileObject[] | null; error: Error | null }> {
502
+ ): Promise<
503
+ | {
504
+ data: FileObject[]
505
+ error: null
506
+ }
507
+ | {
508
+ data: null
509
+ error: StorageError
510
+ }
511
+ > {
372
512
  try {
373
513
  const body = { ...DEFAULT_SEARCH_OPTIONS, ...options, prefix: path || '' }
374
514
  const data = await post(
@@ -380,7 +520,11 @@ export class StorageFileApi {
380
520
  )
381
521
  return { data, error: null }
382
522
  } catch (error) {
383
- return { data: null, error }
523
+ if (isStorageError(error)) {
524
+ return { data: null, error }
525
+ }
526
+
527
+ throw error
384
528
  }
385
529
  }
386
530
 
@@ -0,0 +1,40 @@
1
+ export class StorageError extends Error {
2
+ protected __isStorageError = true
3
+
4
+ constructor(message: string) {
5
+ super(message)
6
+ this.name = 'StorageError'
7
+ }
8
+ }
9
+
10
+ export function isStorageError(error: unknown): error is StorageError {
11
+ return typeof error === 'object' && error !== null && '__isStorageError' in error
12
+ }
13
+
14
+ export class StorageApiError extends StorageError {
15
+ status: number
16
+
17
+ constructor(message: string, status: number) {
18
+ super(message)
19
+ this.name = 'StorageApiError'
20
+ this.status = status
21
+ }
22
+
23
+ toJSON() {
24
+ return {
25
+ name: this.name,
26
+ message: this.message,
27
+ status: this.status,
28
+ }
29
+ }
30
+ }
31
+
32
+ export class StorageUnknownError extends StorageError {
33
+ originalError: unknown
34
+
35
+ constructor(message: string, originalError: unknown) {
36
+ super(message)
37
+ this.name = 'StorageUnknownError'
38
+ this.originalError = originalError
39
+ }
40
+ }
package/src/lib/fetch.ts CHANGED
@@ -1,3 +1,6 @@
1
+ import { StorageApiError, StorageUnknownError } from './errors'
2
+ import { resolveResponse } from './helpers'
3
+
1
4
  export type Fetch = typeof fetch
2
5
 
3
6
  export interface FetchOptions {
@@ -16,16 +19,16 @@ export type RequestMethodType = 'GET' | 'POST' | 'PUT' | 'DELETE'
16
19
  const _getErrorMessage = (err: any): string =>
17
20
  err.msg || err.message || err.error_description || err.error || JSON.stringify(err)
18
21
 
19
- const handleError = (error: any, reject: any) => {
20
- if (typeof error.json !== 'function') {
21
- return reject(error)
22
- }
23
- error.json().then((err: any) => {
24
- return reject({
25
- message: _getErrorMessage(err),
26
- status: error?.status || 500,
22
+ const handleError = async (error: unknown, reject: (reason?: any) => void) => {
23
+ const Res = await resolveResponse()
24
+
25
+ if (error instanceof Res) {
26
+ error.json().then((err) => {
27
+ reject(new StorageApiError(_getErrorMessage(err), error.status || 500))
27
28
  })
28
- })
29
+ } else {
30
+ reject(new StorageUnknownError(_getErrorMessage(error), error))
31
+ }
29
32
  }
30
33
 
31
34
  const _getRequestParams = (
@@ -57,7 +60,7 @@ async function _handleRequest(
57
60
  fetcher(url, _getRequestParams(method, options, parameters, body))
58
61
  .then((result) => {
59
62
  if (!result.ok) throw result
60
- if (options?.noResolveJson) return resolve(result)
63
+ if (options?.noResolveJson) return result
61
64
  return result.json()
62
65
  })
63
66
  .then((data) => resolve(data))
@@ -1,5 +1,3 @@
1
- import crossFetch from 'cross-fetch'
2
-
3
1
  type Fetch = typeof fetch
4
2
 
5
3
  export const resolveFetch = (customFetch?: Fetch): Fetch => {
@@ -7,9 +5,17 @@ export const resolveFetch = (customFetch?: Fetch): Fetch => {
7
5
  if (customFetch) {
8
6
  _fetch = customFetch
9
7
  } else if (typeof fetch === 'undefined') {
10
- _fetch = (crossFetch as unknown) as Fetch
8
+ _fetch = async (...args) => await (await import('cross-fetch')).fetch(...args)
11
9
  } else {
12
10
  _fetch = fetch
13
11
  }
14
12
  return (...args) => _fetch(...args)
15
13
  }
14
+
15
+ export const resolveResponse = async () => {
16
+ if (typeof Response === 'undefined') {
17
+ return (await import('cross-fetch')).Response
18
+ }
19
+
20
+ return Response
21
+ }
@@ -1,2 +1,2 @@
1
1
  // generated by genversion
2
- export const version = '0.0.0'
2
+ export const version = '1.8.0-next.1'