@supabase/storage-js 1.4.1 → 1.6.0
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.
- package/dist/main/SupabaseStorageClient.d.ts +2 -1
- package/dist/main/SupabaseStorageClient.d.ts.map +1 -1
- package/dist/main/SupabaseStorageClient.js +3 -3
- package/dist/main/SupabaseStorageClient.js.map +1 -1
- package/dist/main/lib/StorageBucketApi.d.ts +3 -1
- package/dist/main/lib/StorageBucketApi.d.ts.map +1 -1
- package/dist/main/lib/StorageBucketApi.js +8 -7
- package/dist/main/lib/StorageBucketApi.js.map +1 -1
- package/dist/main/lib/StorageFileApi.d.ts +33 -6
- package/dist/main/lib/StorageFileApi.d.ts.map +1 -1
- package/dist/main/lib/StorageFileApi.js +53 -12
- package/dist/main/lib/StorageFileApi.js.map +1 -1
- package/dist/main/lib/fetch.d.ts +5 -4
- package/dist/main/lib/fetch.d.ts.map +1 -1
- package/dist/main/lib/fetch.js +10 -10
- package/dist/main/lib/fetch.js.map +1 -1
- package/dist/module/SupabaseStorageClient.d.ts +2 -1
- package/dist/module/SupabaseStorageClient.d.ts.map +1 -1
- package/dist/module/SupabaseStorageClient.js +3 -3
- package/dist/module/SupabaseStorageClient.js.map +1 -1
- package/dist/module/lib/StorageBucketApi.d.ts +3 -1
- package/dist/module/lib/StorageBucketApi.d.ts.map +1 -1
- package/dist/module/lib/StorageBucketApi.js +8 -7
- package/dist/module/lib/StorageBucketApi.js.map +1 -1
- package/dist/module/lib/StorageFileApi.d.ts +33 -6
- package/dist/module/lib/StorageFileApi.d.ts.map +1 -1
- package/dist/module/lib/StorageFileApi.js +53 -12
- package/dist/module/lib/StorageFileApi.js.map +1 -1
- package/dist/module/lib/fetch.d.ts +5 -4
- package/dist/module/lib/fetch.d.ts.map +1 -1
- package/dist/module/lib/fetch.js +11 -11
- package/dist/module/lib/fetch.js.map +1 -1
- package/dist/umd/supabase.js +1 -1
- package/package.json +2 -2
- package/src/SupabaseStorageClient.ts +4 -3
- package/src/lib/StorageBucketApi.ts +20 -6
- package/src/lib/StorageFileApi.ts +75 -9
- package/src/lib/fetch.ts +13 -6
|
@@ -1,14 +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
3
|
import { DEFAULT_HEADERS } from './constants'
|
|
4
4
|
|
|
5
5
|
export class StorageBucketApi {
|
|
6
6
|
protected url: string
|
|
7
7
|
protected headers: { [key: string]: string }
|
|
8
|
+
protected fetch?: Fetch
|
|
8
9
|
|
|
9
|
-
constructor(url: string, headers: { [key: string]: string } = {}) {
|
|
10
|
+
constructor(url: string, headers: { [key: string]: string } = {}, fetch?: Fetch) {
|
|
10
11
|
this.url = url
|
|
11
12
|
this.headers = { ...DEFAULT_HEADERS, ...headers }
|
|
13
|
+
this.fetch = fetch
|
|
12
14
|
}
|
|
13
15
|
|
|
14
16
|
/**
|
|
@@ -16,7 +18,7 @@ export class StorageBucketApi {
|
|
|
16
18
|
*/
|
|
17
19
|
async listBuckets(): Promise<{ data: Bucket[] | null; error: Error | null }> {
|
|
18
20
|
try {
|
|
19
|
-
const data = await get(`${this.url}/bucket`, { headers: this.headers })
|
|
21
|
+
const data = await get(this.fetch, `${this.url}/bucket`, { headers: this.headers })
|
|
20
22
|
return { data, error: null }
|
|
21
23
|
} catch (error) {
|
|
22
24
|
return { data: null, error }
|
|
@@ -30,7 +32,7 @@ export class StorageBucketApi {
|
|
|
30
32
|
*/
|
|
31
33
|
async getBucket(id: string): Promise<{ data: Bucket | null; error: Error | null }> {
|
|
32
34
|
try {
|
|
33
|
-
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 })
|
|
34
36
|
return { data, error: null }
|
|
35
37
|
} catch (error) {
|
|
36
38
|
return { data: null, error }
|
|
@@ -49,6 +51,7 @@ export class StorageBucketApi {
|
|
|
49
51
|
): Promise<{ data: string | null; error: Error | null }> {
|
|
50
52
|
try {
|
|
51
53
|
const data = await post(
|
|
54
|
+
this.fetch,
|
|
52
55
|
`${this.url}/bucket`,
|
|
53
56
|
{ id, name: id, public: options.public },
|
|
54
57
|
{ headers: this.headers }
|
|
@@ -70,6 +73,7 @@ export class StorageBucketApi {
|
|
|
70
73
|
): Promise<{ data: { message: string } | null; error: Error | null }> {
|
|
71
74
|
try {
|
|
72
75
|
const data = await put(
|
|
76
|
+
this.fetch,
|
|
73
77
|
`${this.url}/bucket/${id}`,
|
|
74
78
|
{ id, name: id, public: options.public },
|
|
75
79
|
{ headers: this.headers }
|
|
@@ -89,7 +93,12 @@ export class StorageBucketApi {
|
|
|
89
93
|
id: string
|
|
90
94
|
): Promise<{ data: { message: string } | null; error: Error | null }> {
|
|
91
95
|
try {
|
|
92
|
-
const data = await post(
|
|
96
|
+
const data = await post(
|
|
97
|
+
this.fetch,
|
|
98
|
+
`${this.url}/bucket/${id}/empty`,
|
|
99
|
+
{},
|
|
100
|
+
{ headers: this.headers }
|
|
101
|
+
)
|
|
93
102
|
return { data, error: null }
|
|
94
103
|
} catch (error) {
|
|
95
104
|
return { data: null, error }
|
|
@@ -106,7 +115,12 @@ export class StorageBucketApi {
|
|
|
106
115
|
id: string
|
|
107
116
|
): Promise<{ data: { message: string } | null; error: Error | null }> {
|
|
108
117
|
try {
|
|
109
|
-
const data = await remove(
|
|
118
|
+
const data = await remove(
|
|
119
|
+
this.fetch,
|
|
120
|
+
`${this.url}/bucket/${id}`,
|
|
121
|
+
{},
|
|
122
|
+
{ headers: this.headers }
|
|
123
|
+
)
|
|
110
124
|
return { data, error: null }
|
|
111
125
|
} catch (error) {
|
|
112
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(
|
|
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
|
|
81
|
-
const
|
|
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
|
|
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-
|
|
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 }
|
|
@@ -176,7 +186,30 @@ export class StorageFileApi {
|
|
|
176
186
|
}
|
|
177
187
|
|
|
178
188
|
/**
|
|
179
|
-
*
|
|
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
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Create signed URL to download file without requiring permissions. This URL can be valid for a set number of seconds.
|
|
180
213
|
*
|
|
181
214
|
* @param path The file path to be downloaded, including the current file name. For example `folder/image.png`.
|
|
182
215
|
* @param expiresIn The number of seconds until the signed URL expires. For example, `60` for a URL which is valid for one minute.
|
|
@@ -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 }
|
|
@@ -204,6 +238,32 @@ export class StorageFileApi {
|
|
|
204
238
|
}
|
|
205
239
|
}
|
|
206
240
|
|
|
241
|
+
/**
|
|
242
|
+
* Create signed URLs to download files without requiring permissions. These URLs can be valid for a set number of seconds.
|
|
243
|
+
*
|
|
244
|
+
* @param paths The file paths to be downloaded, including the current file names. For example [`folder/image.png`, 'folder2/image2.png'].
|
|
245
|
+
* @param expiresIn The number of seconds until the signed URLs expire. For example, `60` for URLs which are valid for one minute.
|
|
246
|
+
*/
|
|
247
|
+
async createSignedUrls(
|
|
248
|
+
paths: string[],
|
|
249
|
+
expiresIn: number
|
|
250
|
+
): Promise<{
|
|
251
|
+
data: { path: string; signedURL: string }[] | null
|
|
252
|
+
error: Error | null
|
|
253
|
+
}> {
|
|
254
|
+
try {
|
|
255
|
+
const data = await post(
|
|
256
|
+
this.fetch,
|
|
257
|
+
`${this.url}/object/sign/${this.bucketId}`,
|
|
258
|
+
{ expiresIn, paths },
|
|
259
|
+
{ headers: this.headers }
|
|
260
|
+
)
|
|
261
|
+
return { data, error: null }
|
|
262
|
+
} catch (error) {
|
|
263
|
+
return { data: null, error }
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
207
267
|
/**
|
|
208
268
|
* Downloads a file.
|
|
209
269
|
*
|
|
@@ -212,7 +272,7 @@ export class StorageFileApi {
|
|
|
212
272
|
async download(path: string): Promise<{ data: Blob | null; error: Error | null }> {
|
|
213
273
|
try {
|
|
214
274
|
const _path = this._getFinalPath(path)
|
|
215
|
-
const res = await get(`${this.url}/object/${_path}`, {
|
|
275
|
+
const res = await get(this.fetch, `${this.url}/object/${_path}`, {
|
|
216
276
|
headers: this.headers,
|
|
217
277
|
noResolveJson: true,
|
|
218
278
|
})
|
|
@@ -248,11 +308,12 @@ export class StorageFileApi {
|
|
|
248
308
|
/**
|
|
249
309
|
* Deletes files within the same bucket
|
|
250
310
|
*
|
|
251
|
-
* @param paths An array of files to be
|
|
311
|
+
* @param paths An array of files to be deleted, including the path and file name. For example [`folder/image.png`].
|
|
252
312
|
*/
|
|
253
313
|
async remove(paths: string[]): Promise<{ data: FileObject[] | null; error: Error | null }> {
|
|
254
314
|
try {
|
|
255
315
|
const data = await remove(
|
|
316
|
+
this.fetch,
|
|
256
317
|
`${this.url}/object/${this.bucketId}`,
|
|
257
318
|
{ prefixes: paths },
|
|
258
319
|
{ headers: this.headers }
|
|
@@ -307,6 +368,7 @@ export class StorageFileApi {
|
|
|
307
368
|
try {
|
|
308
369
|
const body = { ...DEFAULT_SEARCH_OPTIONS, ...options, prefix: path || '' }
|
|
309
370
|
const data = await post(
|
|
371
|
+
this.fetch,
|
|
310
372
|
`${this.url}/object/list/${this.bucketId}`,
|
|
311
373
|
body,
|
|
312
374
|
{ headers: this.headers },
|
|
@@ -321,4 +383,8 @@ export class StorageFileApi {
|
|
|
321
383
|
_getFinalPath(path: string) {
|
|
322
384
|
return `${this.bucketId}/${path}`
|
|
323
385
|
}
|
|
386
|
+
|
|
387
|
+
_removeEmptyFolders(path: string) {
|
|
388
|
+
return path.replace(/^\/|\/$/g, '').replace(/\/+/g, '/')
|
|
389
|
+
}
|
|
324
390
|
}
|
package/src/lib/fetch.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import
|
|
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
|
-
|
|
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
|
}
|