@supabase/storage-js 1.4.0 → 1.5.2

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 +19 -5
  10. package/dist/main/lib/StorageFileApi.d.ts.map +1 -1
  11. package/dist/main/lib/StorageFileApi.js +35 -11
  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 +19 -5
  38. package/dist/module/lib/StorageFileApi.d.ts.map +1 -1
  39. package/dist/module/lib/StorageFileApi.js +35 -11
  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 +5 -3
  59. package/src/SupabaseStorageClient.ts +4 -3
  60. package/src/lib/StorageBucketApi.ts +22 -7
  61. package/src/lib/StorageFileApi.ts +48 -8
  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
  /**
@@ -77,8 +84,10 @@ export class StorageFileApi {
77
84
  headers['content-type'] = options.contentType as string
78
85
  }
79
86
 
80
- const _path = this._getFinalPath(path)
81
- const res = await fetch(`${this.url}/object/${_path}`, {
87
+ const cleanPath = this._removeEmptyFolders(path)
88
+ const _path = this._getFinalPath(cleanPath)
89
+ const fetcher = this.fetch ?? fetch
90
+ const res = await fetcher(`${this.url}/object/${_path}`, {
82
91
  method,
83
92
  body: body as BodyInit,
84
93
  headers,
@@ -154,10 +163,10 @@ export class StorageFileApi {
154
163
  }
155
164
 
156
165
  /**
157
- * Moves an existing file, optionally renaming it at the same time.
166
+ * Moves an existing file.
158
167
  *
159
168
  * @param fromPath The original file path, including the current file name. For example `folder/image.png`.
160
- * @param toPath The new file path, including the new file name. For example `folder/image-copy.png`.
169
+ * @param toPath The new file path, including the new file name. For example `folder/image-new.png`.
161
170
  */
162
171
  async move(
163
172
  fromPath: string,
@@ -165,6 +174,7 @@ export class StorageFileApi {
165
174
  ): Promise<{ data: { message: string } | null; error: Error | null }> {
166
175
  try {
167
176
  const data = await post(
177
+ this.fetch,
168
178
  `${this.url}/object/move`,
169
179
  { bucketId: this.bucketId, sourceKey: fromPath, destinationKey: toPath },
170
180
  { headers: this.headers }
@@ -175,6 +185,29 @@ export class StorageFileApi {
175
185
  }
176
186
  }
177
187
 
188
+ /**
189
+ * Copies an existing file.
190
+ *
191
+ * @param fromPath The original file path, including the current file name. For example `folder/image.png`.
192
+ * @param toPath The new file path, including the new file name. For example `folder/image-copy.png`.
193
+ */
194
+ async copy(
195
+ fromPath: string,
196
+ toPath: string
197
+ ): Promise<{ data: { message: string } | null; error: Error | null }> {
198
+ try {
199
+ const data = await post(
200
+ this.fetch,
201
+ `${this.url}/object/copy`,
202
+ { bucketId: this.bucketId, sourceKey: fromPath, destinationKey: toPath },
203
+ { headers: this.headers }
204
+ )
205
+ return { data, error: null }
206
+ } catch (error) {
207
+ return { data: null, error }
208
+ }
209
+ }
210
+
178
211
  /**
179
212
  * Create signed url to download file without requiring permissions. This URL can be valid for a set number of seconds.
180
213
  *
@@ -192,6 +225,7 @@ export class StorageFileApi {
192
225
  try {
193
226
  const _path = this._getFinalPath(path)
194
227
  let data = await post(
228
+ this.fetch,
195
229
  `${this.url}/object/sign/${_path}`,
196
230
  { expiresIn },
197
231
  { headers: this.headers }
@@ -212,7 +246,7 @@ export class StorageFileApi {
212
246
  async download(path: string): Promise<{ data: Blob | null; error: Error | null }> {
213
247
  try {
214
248
  const _path = this._getFinalPath(path)
215
- const res = await get(`${this.url}/object/${_path}`, {
249
+ const res = await get(this.fetch, `${this.url}/object/${_path}`, {
216
250
  headers: this.headers,
217
251
  noResolveJson: true,
218
252
  })
@@ -248,11 +282,12 @@ export class StorageFileApi {
248
282
  /**
249
283
  * Deletes files within the same bucket
250
284
  *
251
- * @param paths An array of files to be deletes, including the path and file name. For example [`folder/image.png`].
285
+ * @param paths An array of files to be deleted, including the path and file name. For example [`folder/image.png`].
252
286
  */
253
287
  async remove(paths: string[]): Promise<{ data: FileObject[] | null; error: Error | null }> {
254
288
  try {
255
289
  const data = await remove(
290
+ this.fetch,
256
291
  `${this.url}/object/${this.bucketId}`,
257
292
  { prefixes: paths },
258
293
  { headers: this.headers }
@@ -307,6 +342,7 @@ export class StorageFileApi {
307
342
  try {
308
343
  const body = { ...DEFAULT_SEARCH_OPTIONS, ...options, prefix: path || '' }
309
344
  const data = await post(
345
+ this.fetch,
310
346
  `${this.url}/object/list/${this.bucketId}`,
311
347
  body,
312
348
  { headers: this.headers },
@@ -321,4 +357,8 @@ export class StorageFileApi {
321
357
  _getFinalPath(path: string) {
322
358
  return `${this.bucketId}/${path}`
323
359
  }
360
+
361
+ _removeEmptyFolders(path: string) {
362
+ return path.replace(/^\/|\/$/g, '').replace(/\/+/g, '/')
363
+ }
324
364
  }
@@ -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'