@supabase/storage-js 1.3.0 → 1.5.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 (65) hide show
  1. package/dist/main/SupabaseStorageClient.d.ts +2 -1
  2. package/dist/main/SupabaseStorageClient.d.ts.map +1 -1
  3. package/dist/main/SupabaseStorageClient.js +3 -3
  4. package/dist/main/SupabaseStorageClient.js.map +1 -1
  5. package/dist/main/lib/StorageBucketApi.d.ts +3 -1
  6. package/dist/main/lib/StorageBucketApi.d.ts.map +1 -1
  7. package/dist/main/lib/StorageBucketApi.js +10 -8
  8. package/dist/main/lib/StorageBucketApi.js.map +1 -1
  9. package/dist/main/lib/StorageFileApi.d.ts +8 -5
  10. package/dist/main/lib/StorageFileApi.d.ts.map +1 -1
  11. package/dist/main/lib/StorageFileApi.js +14 -9
  12. package/dist/main/lib/StorageFileApi.js.map +1 -1
  13. package/dist/main/lib/constants.d.ts +4 -0
  14. package/dist/main/lib/constants.d.ts.map +1 -0
  15. package/dist/main/lib/constants.js +6 -0
  16. package/dist/main/lib/constants.js.map +1 -0
  17. package/dist/main/lib/fetch.d.ts +5 -4
  18. package/dist/main/lib/fetch.d.ts.map +1 -1
  19. package/dist/main/lib/fetch.js +10 -10
  20. package/dist/main/lib/fetch.js.map +1 -1
  21. package/dist/main/lib/index.d.ts +1 -0
  22. package/dist/main/lib/index.d.ts.map +1 -1
  23. package/dist/main/lib/index.js +1 -0
  24. package/dist/main/lib/index.js.map +1 -1
  25. package/dist/main/lib/version.d.ts +2 -0
  26. package/dist/main/lib/version.d.ts.map +1 -0
  27. package/dist/main/lib/version.js +6 -0
  28. package/dist/main/lib/version.js.map +1 -0
  29. package/dist/module/SupabaseStorageClient.d.ts +2 -1
  30. package/dist/module/SupabaseStorageClient.d.ts.map +1 -1
  31. package/dist/module/SupabaseStorageClient.js +3 -3
  32. package/dist/module/SupabaseStorageClient.js.map +1 -1
  33. package/dist/module/lib/StorageBucketApi.d.ts +3 -1
  34. package/dist/module/lib/StorageBucketApi.d.ts.map +1 -1
  35. package/dist/module/lib/StorageBucketApi.js +10 -8
  36. package/dist/module/lib/StorageBucketApi.js.map +1 -1
  37. package/dist/module/lib/StorageFileApi.d.ts +8 -5
  38. package/dist/module/lib/StorageFileApi.d.ts.map +1 -1
  39. package/dist/module/lib/StorageFileApi.js +14 -9
  40. package/dist/module/lib/StorageFileApi.js.map +1 -1
  41. package/dist/module/lib/constants.d.ts +4 -0
  42. package/dist/module/lib/constants.d.ts.map +1 -0
  43. package/dist/module/lib/constants.js +3 -0
  44. package/dist/module/lib/constants.js.map +1 -0
  45. package/dist/module/lib/fetch.d.ts +5 -4
  46. package/dist/module/lib/fetch.d.ts.map +1 -1
  47. package/dist/module/lib/fetch.js +11 -11
  48. package/dist/module/lib/fetch.js.map +1 -1
  49. package/dist/module/lib/index.d.ts +1 -0
  50. package/dist/module/lib/index.d.ts.map +1 -1
  51. package/dist/module/lib/index.js +1 -0
  52. package/dist/module/lib/index.js.map +1 -1
  53. package/dist/module/lib/version.d.ts +2 -0
  54. package/dist/module/lib/version.d.ts.map +1 -0
  55. package/dist/module/lib/version.js +3 -0
  56. package/dist/module/lib/version.js.map +1 -0
  57. package/dist/umd/supabase.js +1 -1
  58. package/package.json +4 -2
  59. package/src/SupabaseStorageClient.ts +4 -3
  60. package/src/lib/StorageBucketApi.ts +22 -7
  61. package/src/lib/StorageFileApi.ts +28 -6
  62. package/src/lib/constants.ts +2 -0
  63. package/src/lib/fetch.ts +13 -6
  64. package/src/lib/index.ts +1 -0
  65. package/src/lib/version.ts +2 -0
@@ -1,13 +1,16 @@
1
- import { get, post, put, remove } from './fetch'
1
+ import { Fetch, get, post, put, remove } from './fetch'
2
2
  import { Bucket } from './types'
3
+ import { DEFAULT_HEADERS } from './constants'
3
4
 
4
5
  export class StorageBucketApi {
5
6
  protected url: string
6
7
  protected headers: { [key: string]: string }
8
+ protected fetch?: Fetch
7
9
 
8
- constructor(url: string, headers: { [key: string]: string } = {}) {
10
+ constructor(url: string, headers: { [key: string]: string } = {}, fetch?: Fetch) {
9
11
  this.url = url
10
- this.headers = headers
12
+ this.headers = { ...DEFAULT_HEADERS, ...headers }
13
+ this.fetch = fetch
11
14
  }
12
15
 
13
16
  /**
@@ -15,7 +18,7 @@ export class StorageBucketApi {
15
18
  */
16
19
  async listBuckets(): Promise<{ data: Bucket[] | null; error: Error | null }> {
17
20
  try {
18
- const data = await get(`${this.url}/bucket`, { headers: this.headers })
21
+ const data = await get(this.fetch, `${this.url}/bucket`, { headers: this.headers })
19
22
  return { data, error: null }
20
23
  } catch (error) {
21
24
  return { data: null, error }
@@ -29,7 +32,7 @@ export class StorageBucketApi {
29
32
  */
30
33
  async getBucket(id: string): Promise<{ data: Bucket | null; error: Error | null }> {
31
34
  try {
32
- const data = await get(`${this.url}/bucket/${id}`, { headers: this.headers })
35
+ const data = await get(this.fetch, `${this.url}/bucket/${id}`, { headers: this.headers })
33
36
  return { data, error: null }
34
37
  } catch (error) {
35
38
  return { data: null, error }
@@ -48,6 +51,7 @@ export class StorageBucketApi {
48
51
  ): Promise<{ data: string | null; error: Error | null }> {
49
52
  try {
50
53
  const data = await post(
54
+ this.fetch,
51
55
  `${this.url}/bucket`,
52
56
  { id, name: id, public: options.public },
53
57
  { headers: this.headers }
@@ -69,6 +73,7 @@ export class StorageBucketApi {
69
73
  ): Promise<{ data: { message: string } | null; error: Error | null }> {
70
74
  try {
71
75
  const data = await put(
76
+ this.fetch,
72
77
  `${this.url}/bucket/${id}`,
73
78
  { id, name: id, public: options.public },
74
79
  { headers: this.headers }
@@ -88,7 +93,12 @@ export class StorageBucketApi {
88
93
  id: string
89
94
  ): Promise<{ data: { message: string } | null; error: Error | null }> {
90
95
  try {
91
- const data = await post(`${this.url}/bucket/${id}/empty`, {}, { headers: this.headers })
96
+ const data = await post(
97
+ this.fetch,
98
+ `${this.url}/bucket/${id}/empty`,
99
+ {},
100
+ { headers: this.headers }
101
+ )
92
102
  return { data, error: null }
93
103
  } catch (error) {
94
104
  return { data: null, error }
@@ -105,7 +115,12 @@ export class StorageBucketApi {
105
115
  id: string
106
116
  ): Promise<{ data: { message: string } | null; error: Error | null }> {
107
117
  try {
108
- const data = await remove(`${this.url}/bucket/${id}`, {}, { headers: this.headers })
118
+ const data = await remove(
119
+ this.fetch,
120
+ `${this.url}/bucket/${id}`,
121
+ {},
122
+ { headers: this.headers }
123
+ )
109
124
  return { data, error: null }
110
125
  } catch (error) {
111
126
  return { data: null, error }
@@ -1,4 +1,4 @@
1
- import { FetchParameters, get, post, remove } from './fetch'
1
+ import { Fetch, FetchParameters, get, post, remove } from './fetch'
2
2
  import { isBrowser } from './helpers'
3
3
  import { FileObject, FileOptions, SearchOptions } from './types'
4
4
  import fetch from 'cross-fetch'
@@ -22,11 +22,18 @@ export class StorageFileApi {
22
22
  protected url: string
23
23
  protected headers: { [key: string]: string }
24
24
  protected bucketId?: string
25
+ protected fetch?: Fetch
25
26
 
26
- constructor(url: string, headers: { [key: string]: string } = {}, bucketId?: string) {
27
+ constructor(
28
+ url: string,
29
+ headers: { [key: string]: string } = {},
30
+ bucketId?: string,
31
+ fetch?: Fetch
32
+ ) {
27
33
  this.url = url
28
34
  this.headers = headers
29
35
  this.bucketId = bucketId
36
+ this.fetch = fetch
30
37
  }
31
38
 
32
39
  /**
@@ -47,8 +54,10 @@ export class StorageFileApi {
47
54
  | ArrayBuffer
48
55
  | ArrayBufferView
49
56
  | Blob
57
+ | Buffer
50
58
  | File
51
59
  | FormData
60
+ | NodeJS.ReadableStream
52
61
  | ReadableStream<Uint8Array>
53
62
  | URLSearchParams
54
63
  | string,
@@ -75,10 +84,11 @@ export class StorageFileApi {
75
84
  headers['content-type'] = options.contentType as string
76
85
  }
77
86
 
78
- const _path = this._getFinalPath(path)
87
+ const cleanPath = this._removeEmptyFolders(path)
88
+ const _path = this._getFinalPath(cleanPath)
79
89
  const res = await fetch(`${this.url}/object/${_path}`, {
80
90
  method,
81
- body,
91
+ body: body as BodyInit,
82
92
  headers,
83
93
  })
84
94
 
@@ -111,8 +121,10 @@ export class StorageFileApi {
111
121
  | ArrayBuffer
112
122
  | ArrayBufferView
113
123
  | Blob
124
+ | Buffer
114
125
  | File
115
126
  | FormData
127
+ | NodeJS.ReadableStream
116
128
  | ReadableStream<Uint8Array>
117
129
  | URLSearchParams
118
130
  | string,
@@ -137,8 +149,10 @@ export class StorageFileApi {
137
149
  | ArrayBuffer
138
150
  | ArrayBufferView
139
151
  | Blob
152
+ | Buffer
140
153
  | File
141
154
  | FormData
155
+ | NodeJS.ReadableStream
142
156
  | ReadableStream<Uint8Array>
143
157
  | URLSearchParams
144
158
  | string,
@@ -159,6 +173,7 @@ export class StorageFileApi {
159
173
  ): Promise<{ data: { message: string } | null; error: Error | null }> {
160
174
  try {
161
175
  const data = await post(
176
+ this.fetch,
162
177
  `${this.url}/object/move`,
163
178
  { bucketId: this.bucketId, sourceKey: fromPath, destinationKey: toPath },
164
179
  { headers: this.headers }
@@ -186,6 +201,7 @@ export class StorageFileApi {
186
201
  try {
187
202
  const _path = this._getFinalPath(path)
188
203
  let data = await post(
204
+ this.fetch,
189
205
  `${this.url}/object/sign/${_path}`,
190
206
  { expiresIn },
191
207
  { headers: this.headers }
@@ -206,7 +222,7 @@ export class StorageFileApi {
206
222
  async download(path: string): Promise<{ data: Blob | null; error: Error | null }> {
207
223
  try {
208
224
  const _path = this._getFinalPath(path)
209
- const res = await get(`${this.url}/object/${_path}`, {
225
+ const res = await get(this.fetch, `${this.url}/object/${_path}`, {
210
226
  headers: this.headers,
211
227
  noResolveJson: true,
212
228
  })
@@ -242,11 +258,12 @@ export class StorageFileApi {
242
258
  /**
243
259
  * Deletes files within the same bucket
244
260
  *
245
- * @param paths An array of files to be deletes, including the path and file name. For example [`folder/image.png`].
261
+ * @param paths An array of files to be deleted, including the path and file name. For example [`folder/image.png`].
246
262
  */
247
263
  async remove(paths: string[]): Promise<{ data: FileObject[] | null; error: Error | null }> {
248
264
  try {
249
265
  const data = await remove(
266
+ this.fetch,
250
267
  `${this.url}/object/${this.bucketId}`,
251
268
  { prefixes: paths },
252
269
  { headers: this.headers }
@@ -301,6 +318,7 @@ export class StorageFileApi {
301
318
  try {
302
319
  const body = { ...DEFAULT_SEARCH_OPTIONS, ...options, prefix: path || '' }
303
320
  const data = await post(
321
+ this.fetch,
304
322
  `${this.url}/object/list/${this.bucketId}`,
305
323
  body,
306
324
  { headers: this.headers },
@@ -315,4 +333,8 @@ export class StorageFileApi {
315
333
  _getFinalPath(path: string) {
316
334
  return `${this.bucketId}/${path}`
317
335
  }
336
+
337
+ _removeEmptyFolders(path: string) {
338
+ return path.replace(/^\/|\/$/g, '').replace(/\/+/g, '/')
339
+ }
318
340
  }
@@ -0,0 +1,2 @@
1
+ import { version } from './version'
2
+ export const DEFAULT_HEADERS = { 'X-Client-Info': `storage-js/${version}` }
package/src/lib/fetch.ts CHANGED
@@ -1,4 +1,6 @@
1
- import fetch from 'cross-fetch'
1
+ import crossFetch from 'cross-fetch'
2
+
3
+ export type Fetch = typeof fetch
2
4
 
3
5
  export interface FetchOptions {
4
6
  headers?: {
@@ -46,6 +48,7 @@ const _getRequestParams = (
46
48
  }
47
49
 
48
50
  async function _handleRequest(
51
+ fetcher: Fetch = crossFetch,
49
52
  method: RequestMethodType,
50
53
  url: string,
51
54
  options?: FetchOptions,
@@ -53,7 +56,7 @@ async function _handleRequest(
53
56
  body?: object
54
57
  ): Promise<any> {
55
58
  return new Promise((resolve, reject) => {
56
- fetch(url, _getRequestParams(method, options, parameters, body))
59
+ fetcher(url, _getRequestParams(method, options, parameters, body))
57
60
  .then((result) => {
58
61
  if (!result.ok) throw result
59
62
  if (options?.noResolveJson) return resolve(result)
@@ -65,36 +68,40 @@ async function _handleRequest(
65
68
  }
66
69
 
67
70
  export async function get(
71
+ fetcher: Fetch | undefined,
68
72
  url: string,
69
73
  options?: FetchOptions,
70
74
  parameters?: FetchParameters
71
75
  ): Promise<any> {
72
- return _handleRequest('GET', url, options, parameters)
76
+ return _handleRequest(fetcher, 'GET', url, options, parameters)
73
77
  }
74
78
 
75
79
  export async function post(
80
+ fetcher: Fetch | undefined,
76
81
  url: string,
77
82
  body: object,
78
83
  options?: FetchOptions,
79
84
  parameters?: FetchParameters
80
85
  ): Promise<any> {
81
- return _handleRequest('POST', url, options, parameters, body)
86
+ return _handleRequest(fetcher, 'POST', url, options, parameters, body)
82
87
  }
83
88
 
84
89
  export async function put(
90
+ fetcher: Fetch | undefined,
85
91
  url: string,
86
92
  body: object,
87
93
  options?: FetchOptions,
88
94
  parameters?: FetchParameters
89
95
  ): Promise<any> {
90
- return _handleRequest('PUT', url, options, parameters, body)
96
+ return _handleRequest(fetcher, 'PUT', url, options, parameters, body)
91
97
  }
92
98
 
93
99
  export async function remove(
100
+ fetcher: Fetch | undefined,
94
101
  url: string,
95
102
  body: object,
96
103
  options?: FetchOptions,
97
104
  parameters?: FetchParameters
98
105
  ): Promise<any> {
99
- return _handleRequest('DELETE', url, options, parameters, body)
106
+ return _handleRequest(fetcher, 'DELETE', url, options, parameters, body)
100
107
  }
package/src/lib/index.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export * from './StorageBucketApi'
2
2
  export * from './StorageFileApi'
3
3
  export * from './types'
4
+ export * from './constants'
@@ -0,0 +1,2 @@
1
+ // generated by genversion
2
+ export const version = '0.0.0'