@supabase/storage-js 1.7.3 → 1.8.0-next.3

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 +33 -14
  9. package/dist/main/lib/StorageBucketApi.js.map +1 -1
  10. package/dist/main/lib/StorageFileApi.d.ts +50 -29
  11. package/dist/main/lib/StorageFileApi.d.ts.map +1 -1
  12. package/dist/main/lib/StorageFileApi.js +88 -38
  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 +15 -3
  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 +26 -7
  36. package/dist/module/lib/StorageBucketApi.js.map +1 -1
  37. package/dist/module/lib/StorageFileApi.d.ts +50 -29
  38. package/dist/module/lib/StorageFileApi.d.ts.map +1 -1
  39. package/dist/module/lib/StorageFileApi.js +80 -30
  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 +6 -0
  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 -3
  56. package/src/index.ts +1 -0
  57. package/src/lib/StorageBucketApi.ts +94 -13
  58. package/src/lib/StorageFileApi.ts +195 -53
  59. package/src/lib/errors.ts +40 -0
  60. package/src/lib/fetch.ts +13 -10
  61. package/src/lib/helpers.ts +8 -0
  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,16 @@ 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
+ }
282
+ | {
283
+ data: null
284
+ error: StorageError
285
+ }
286
+ > {
223
287
  try {
224
288
  const _path = this._getFinalPath(path)
225
289
  let data = await post(
@@ -228,11 +292,15 @@ export class StorageFileApi {
228
292
  { expiresIn },
229
293
  { headers: this.headers }
230
294
  )
231
- const signedURL = `${this.url}${data.signedURL}`
295
+ const signedURL = encodeURI(`${this.url}${data.signedURL}`)
232
296
  data = { signedURL }
233
- return { data, error: null, signedURL }
297
+ return { data, error: null }
234
298
  } catch (error) {
235
- return { data: null, error, signedURL: null }
299
+ if (isStorageError(error)) {
300
+ return { data: null, error }
301
+ }
302
+
303
+ throw error
236
304
  }
237
305
  }
238
306
 
@@ -245,10 +313,16 @@ export class StorageFileApi {
245
313
  async createSignedUrls(
246
314
  paths: string[],
247
315
  expiresIn: number
248
- ): Promise<{
249
- data: { error: string | null; path: string | null; signedURL: string }[] | null
250
- error: Error | null
251
- }> {
316
+ ): Promise<
317
+ | {
318
+ data: { error: string | null; path: string | null; signedURL: string }[]
319
+ error: null
320
+ }
321
+ | {
322
+ data: null
323
+ error: StorageError
324
+ }
325
+ > {
252
326
  try {
253
327
  const data = await post(
254
328
  this.fetch,
@@ -259,12 +333,16 @@ export class StorageFileApi {
259
333
  return {
260
334
  data: data.map((datum: { signedURL: string }) => ({
261
335
  ...datum,
262
- signedURL: datum.signedURL ? `${this.url}${datum.signedURL}` : null,
336
+ signedURL: datum.signedURL ? encodeURI(`${this.url}${datum.signedURL}`) : null,
263
337
  })),
264
338
  error: null,
265
339
  }
266
340
  } catch (error) {
267
- return { data: null, error }
341
+ if (isStorageError(error)) {
342
+ return { data: null, error }
343
+ }
344
+
345
+ throw error
268
346
  }
269
347
  }
270
348
 
@@ -273,7 +351,18 @@ export class StorageFileApi {
273
351
  *
274
352
  * @param path The file path to be downloaded, including the path and file name. For example `folder/image.png`.
275
353
  */
276
- async download(path: string): Promise<{ data: Blob | null; error: Error | null }> {
354
+ async download(
355
+ path: string
356
+ ): Promise<
357
+ | {
358
+ data: Blob
359
+ error: null
360
+ }
361
+ | {
362
+ data: null
363
+ error: StorageError
364
+ }
365
+ > {
277
366
  try {
278
367
  const _path = this._getFinalPath(path)
279
368
  const res = await get(this.fetch, `${this.url}/object/${_path}`, {
@@ -283,30 +372,22 @@ export class StorageFileApi {
283
372
  const data = await res.blob()
284
373
  return { data, error: null }
285
374
  } catch (error) {
286
- return { data: null, error }
375
+ if (isStorageError(error)) {
376
+ return { data: null, error }
377
+ }
378
+
379
+ throw error
287
380
  }
288
381
  }
289
382
 
290
383
  /**
291
- * Retrieve URLs for assets in public buckets
384
+ * Retrieve URLs for assets in public buckets and encapsulates it in a return object
292
385
  *
293
386
  * @param path The file path to be downloaded, including the path and file name. For example `folder/image.png`.
294
387
  */
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
- }
388
+ getPublicUrl(path: string): string {
389
+ const _path = this._getFinalPath(path)
390
+ return encodeURI(`${this.url}/object/public/${_path}`)
310
391
  }
311
392
 
312
393
  /**
@@ -314,7 +395,18 @@ export class StorageFileApi {
314
395
  *
315
396
  * @param paths An array of files to be deleted, including the path and file name. For example [`folder/image.png`].
316
397
  */
317
- async remove(paths: string[]): Promise<{ data: FileObject[] | null; error: Error | null }> {
398
+ async remove(
399
+ paths: string[]
400
+ ): Promise<
401
+ | {
402
+ data: FileObject[]
403
+ error: null
404
+ }
405
+ | {
406
+ data: null
407
+ error: StorageError
408
+ }
409
+ > {
318
410
  try {
319
411
  const data = await remove(
320
412
  this.fetch,
@@ -324,7 +416,11 @@ export class StorageFileApi {
324
416
  )
325
417
  return { data, error: null }
326
418
  } catch (error) {
327
- return { data: null, error }
419
+ if (isStorageError(error)) {
420
+ return { data: null, error }
421
+ }
422
+
423
+ throw error
328
424
  }
329
425
  }
330
426
 
@@ -332,12 +428,27 @@ export class StorageFileApi {
332
428
  * Get file metadata
333
429
  * @param id the file id to retrieve metadata
334
430
  */
335
- // async getMetadata(id: string): Promise<{ data: Metadata | null; error: Error | null }> {
431
+ // async getMetadata(
432
+ // id: string
433
+ // ): Promise<
434
+ // | {
435
+ // data: Metadata
436
+ // error: null
437
+ // }
438
+ // | {
439
+ // data: null
440
+ // error: StorageError
441
+ // }
442
+ // > {
336
443
  // try {
337
- // const data = await get(`${this.url}/metadata/${id}`, { headers: this.headers })
444
+ // const data = await get(this.fetch, `${this.url}/metadata/${id}`, { headers: this.headers })
338
445
  // return { data, error: null }
339
446
  // } catch (error) {
340
- // return { data: null, error }
447
+ // if (isStorageError(error)) {
448
+ // return { data: null, error }
449
+ // }
450
+
451
+ // throw error
341
452
  // }
342
453
  // }
343
454
 
@@ -349,12 +460,30 @@ export class StorageFileApi {
349
460
  // async updateMetadata(
350
461
  // id: string,
351
462
  // meta: Metadata
352
- // ): Promise<{ data: Metadata | null; error: Error | null }> {
463
+ // ): Promise<
464
+ // | {
465
+ // data: Metadata
466
+ // error: null
467
+ // }
468
+ // | {
469
+ // data: null
470
+ // error: StorageError
471
+ // }
472
+ // > {
353
473
  // try {
354
- // const data = await post(`${this.url}/metadata/${id}`, { ...meta }, { headers: this.headers })
474
+ // const data = await post(
475
+ // this.fetch,
476
+ // `${this.url}/metadata/${id}`,
477
+ // { ...meta },
478
+ // { headers: this.headers }
479
+ // )
355
480
  // return { data, error: null }
356
481
  // } catch (error) {
357
- // return { data: null, error }
482
+ // if (isStorageError(error)) {
483
+ // return { data: null, error }
484
+ // }
485
+
486
+ // throw error
358
487
  // }
359
488
  // }
360
489
 
@@ -368,7 +497,16 @@ export class StorageFileApi {
368
497
  path?: string,
369
498
  options?: SearchOptions,
370
499
  parameters?: FetchParameters
371
- ): Promise<{ data: FileObject[] | null; error: Error | null }> {
500
+ ): Promise<
501
+ | {
502
+ data: FileObject[]
503
+ error: null
504
+ }
505
+ | {
506
+ data: null
507
+ error: StorageError
508
+ }
509
+ > {
372
510
  try {
373
511
  const body = { ...DEFAULT_SEARCH_OPTIONS, ...options, prefix: path || '' }
374
512
  const data = await post(
@@ -380,7 +518,11 @@ export class StorageFileApi {
380
518
  )
381
519
  return { data, error: null }
382
520
  } catch (error) {
383
- return { data: null, error }
521
+ if (isStorageError(error)) {
522
+ return { data: null, error }
523
+ }
524
+
525
+ throw error
384
526
  }
385
527
  }
386
528
 
@@ -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))
@@ -11,3 +11,11 @@ export const resolveFetch = (customFetch?: Fetch): Fetch => {
11
11
  }
12
12
  return (...args) => _fetch(...args)
13
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 = '1.7.3'
2
+ export const version = '1.8.0-next.3'