@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.
- 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 +10 -8
- package/dist/main/lib/StorageBucketApi.js.map +1 -1
- package/dist/main/lib/StorageFileApi.d.ts +19 -5
- package/dist/main/lib/StorageFileApi.d.ts.map +1 -1
- package/dist/main/lib/StorageFileApi.js +35 -11
- package/dist/main/lib/StorageFileApi.js.map +1 -1
- package/dist/main/lib/constants.d.ts +4 -0
- package/dist/main/lib/constants.d.ts.map +1 -0
- package/dist/main/lib/constants.js +6 -0
- package/dist/main/lib/constants.js.map +1 -0
- 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/main/lib/index.d.ts +1 -0
- package/dist/main/lib/index.d.ts.map +1 -1
- package/dist/main/lib/index.js +1 -0
- package/dist/main/lib/index.js.map +1 -1
- package/dist/main/lib/version.d.ts +2 -0
- package/dist/main/lib/version.d.ts.map +1 -0
- package/dist/main/lib/version.js +6 -0
- package/dist/main/lib/version.js.map +1 -0
- 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 +10 -8
- package/dist/module/lib/StorageBucketApi.js.map +1 -1
- package/dist/module/lib/StorageFileApi.d.ts +19 -5
- package/dist/module/lib/StorageFileApi.d.ts.map +1 -1
- package/dist/module/lib/StorageFileApi.js +35 -11
- package/dist/module/lib/StorageFileApi.js.map +1 -1
- package/dist/module/lib/constants.d.ts +4 -0
- package/dist/module/lib/constants.d.ts.map +1 -0
- package/dist/module/lib/constants.js +3 -0
- package/dist/module/lib/constants.js.map +1 -0
- 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/module/lib/index.d.ts +1 -0
- package/dist/module/lib/index.d.ts.map +1 -1
- package/dist/module/lib/index.js +1 -0
- package/dist/module/lib/index.js.map +1 -1
- package/dist/module/lib/version.d.ts +2 -0
- package/dist/module/lib/version.d.ts.map +1 -0
- package/dist/module/lib/version.js +3 -0
- package/dist/module/lib/version.js.map +1 -0
- package/dist/umd/supabase.js +1 -1
- package/package.json +5 -3
- package/src/SupabaseStorageClient.ts +4 -3
- package/src/lib/StorageBucketApi.ts +22 -7
- package/src/lib/StorageFileApi.ts +48 -8
- package/src/lib/constants.ts +2 -0
- package/src/lib/fetch.ts +13 -6
- package/src/lib/index.ts +1 -0
- 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(
|
|
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(
|
|
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(
|
|
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 }
|
|
@@ -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
|
|
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
|
}
|
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
|
}
|
package/src/lib/index.ts
CHANGED