@supabase/storage-js 2.6.0 → 2.7.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 (39) hide show
  1. package/dist/main/lib/fetch.d.ts +2 -1
  2. package/dist/main/lib/fetch.d.ts.map +1 -1
  3. package/dist/main/lib/fetch.js +13 -5
  4. package/dist/main/lib/fetch.js.map +1 -1
  5. package/dist/main/lib/helpers.d.ts +1 -0
  6. package/dist/main/lib/helpers.d.ts.map +1 -1
  7. package/dist/main/lib/helpers.js +16 -1
  8. package/dist/main/lib/helpers.js.map +1 -1
  9. package/dist/main/lib/types.d.ts +28 -0
  10. package/dist/main/lib/types.d.ts.map +1 -1
  11. package/dist/main/lib/version.d.ts +1 -1
  12. package/dist/main/lib/version.js +1 -1
  13. package/dist/main/packages/StorageFileApi.d.ts +25 -1
  14. package/dist/main/packages/StorageFileApi.d.ts.map +1 -1
  15. package/dist/main/packages/StorageFileApi.js +68 -1
  16. package/dist/main/packages/StorageFileApi.js.map +1 -1
  17. package/dist/module/lib/fetch.d.ts +2 -1
  18. package/dist/module/lib/fetch.d.ts.map +1 -1
  19. package/dist/module/lib/fetch.js +11 -4
  20. package/dist/module/lib/fetch.js.map +1 -1
  21. package/dist/module/lib/helpers.d.ts +1 -0
  22. package/dist/module/lib/helpers.d.ts.map +1 -1
  23. package/dist/module/lib/helpers.js +14 -0
  24. package/dist/module/lib/helpers.js.map +1 -1
  25. package/dist/module/lib/types.d.ts +28 -0
  26. package/dist/module/lib/types.d.ts.map +1 -1
  27. package/dist/module/lib/version.d.ts +1 -1
  28. package/dist/module/lib/version.js +1 -1
  29. package/dist/module/packages/StorageFileApi.d.ts +25 -1
  30. package/dist/module/packages/StorageFileApi.d.ts.map +1 -1
  31. package/dist/module/packages/StorageFileApi.js +71 -4
  32. package/dist/module/packages/StorageFileApi.js.map +1 -1
  33. package/dist/umd/supabase.js +1 -1
  34. package/package.json +3 -3
  35. package/src/lib/fetch.ts +30 -5
  36. package/src/lib/helpers.ts +16 -0
  37. package/src/lib/types.ts +34 -0
  38. package/src/lib/version.ts +1 -1
  39. package/src/packages/StorageFileApi.ts +103 -4
@@ -1,6 +1,6 @@
1
- import { isStorageError, StorageError } from '../lib/errors'
2
- import { Fetch, get, post, remove } from '../lib/fetch'
3
- import { resolveFetch } from '../lib/helpers'
1
+ import { isStorageError, StorageError, StorageUnknownError } from '../lib/errors'
2
+ import { Fetch, get, head, post, remove } from '../lib/fetch'
3
+ import { recursiveToCamel, resolveFetch } from '../lib/helpers'
4
4
  import {
5
5
  FileObject,
6
6
  FileOptions,
@@ -8,6 +8,8 @@ import {
8
8
  FetchParameters,
9
9
  TransformOptions,
10
10
  DestinationOptions,
11
+ FileObjectV2,
12
+ Camelize,
11
13
  } from '../lib/types'
12
14
 
13
15
  const DEFAULT_SEARCH_OPTIONS = {
@@ -80,22 +82,38 @@ export default class StorageFileApi {
80
82
  try {
81
83
  let body
82
84
  const options = { ...DEFAULT_FILE_OPTIONS, ...fileOptions }
83
- const headers: Record<string, string> = {
85
+ let headers: Record<string, string> = {
84
86
  ...this.headers,
85
87
  ...(method === 'POST' && { 'x-upsert': String(options.upsert as boolean) }),
86
88
  }
87
89
 
90
+ const metadata = options.metadata
91
+
88
92
  if (typeof Blob !== 'undefined' && fileBody instanceof Blob) {
89
93
  body = new FormData()
90
94
  body.append('cacheControl', options.cacheControl as string)
95
+ if (metadata) {
96
+ body.append('metadata', this.encodeMetadata(metadata))
97
+ }
91
98
  body.append('', fileBody)
92
99
  } else if (typeof FormData !== 'undefined' && fileBody instanceof FormData) {
93
100
  body = fileBody
94
101
  body.append('cacheControl', options.cacheControl as string)
102
+ if (metadata) {
103
+ body.append('metadata', this.encodeMetadata(metadata))
104
+ }
95
105
  } else {
96
106
  body = fileBody
97
107
  headers['cache-control'] = `max-age=${options.cacheControl}`
98
108
  headers['content-type'] = options.contentType as string
109
+
110
+ if (metadata) {
111
+ headers['x-metadata'] = this.toBase64(this.encodeMetadata(metadata))
112
+ }
113
+ }
114
+
115
+ if (fileOptions?.headers) {
116
+ headers = { ...headers, ...fileOptions.headers }
99
117
  }
100
118
 
101
119
  const cleanPath = this._removeEmptyFolders(path)
@@ -525,6 +543,76 @@ export default class StorageFileApi {
525
543
  }
526
544
  }
527
545
 
546
+ /**
547
+ * Retrieves the details of an existing file.
548
+ * @param path
549
+ */
550
+ async info(
551
+ path: string
552
+ ): Promise<
553
+ | {
554
+ data: Camelize<FileObjectV2>
555
+ error: null
556
+ }
557
+ | {
558
+ data: null
559
+ error: StorageError
560
+ }
561
+ > {
562
+ const _path = this._getFinalPath(path)
563
+
564
+ try {
565
+ const data = await get(this.fetch, `${this.url}/object/info/${_path}`, {
566
+ headers: this.headers,
567
+ })
568
+
569
+ return { data: recursiveToCamel(data) as Camelize<FileObjectV2>, error: null }
570
+ } catch (error) {
571
+ if (isStorageError(error)) {
572
+ return { data: null, error }
573
+ }
574
+
575
+ throw error
576
+ }
577
+ }
578
+
579
+ /**
580
+ * Checks the existence of a file.
581
+ * @param path
582
+ */
583
+ async exists(
584
+ path: string
585
+ ): Promise<
586
+ | {
587
+ data: boolean
588
+ error: null
589
+ }
590
+ | {
591
+ data: boolean
592
+ error: StorageError
593
+ }
594
+ > {
595
+ const _path = this._getFinalPath(path)
596
+
597
+ try {
598
+ await head(this.fetch, `${this.url}/object/${_path}`, {
599
+ headers: this.headers,
600
+ })
601
+
602
+ return { data: true, error: null }
603
+ } catch (error) {
604
+ if (isStorageError(error) && error instanceof StorageUnknownError) {
605
+ const originalError = (error.originalError as unknown) as { status: number }
606
+
607
+ if ([400, 404].includes(originalError?.status)) {
608
+ return { data: false, error }
609
+ }
610
+ }
611
+
612
+ throw error
613
+ }
614
+ }
615
+
528
616
  /**
529
617
  * A simple convenience function to get the URL for an asset in a public bucket. If you do not want to use this function, you can construct the public URL by concatenating the bucket URL with the path to the asset.
530
618
  * This function does not verify if the bucket is public. If a public URL is created for a bucket which is not public, you will not be able to download the asset.
@@ -700,6 +788,17 @@ export default class StorageFileApi {
700
788
  }
701
789
  }
702
790
 
791
+ protected encodeMetadata(metadata: Record<string, any>) {
792
+ return JSON.stringify(metadata)
793
+ }
794
+
795
+ toBase64(data: string) {
796
+ if (typeof Buffer !== 'undefined') {
797
+ return Buffer.from(data).toString('base64')
798
+ }
799
+ return btoa(data)
800
+ }
801
+
703
802
  private _getFinalPath(path: string) {
704
803
  return `${this.bucketId}/${path}`
705
804
  }