Skip to content

Commit 5fc7362

Browse files
authored
Merge pull request #137 from supabase/chore/storage-refactor
Chore/storage refactor
2 parents 47e4593 + 5cdcb8e commit 5fc7362

File tree

2 files changed

+60
-44
lines changed

2 files changed

+60
-44
lines changed

src/lib/storage/StorageApi.ts

Lines changed: 55 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ export class StorageApi {
2121
}
2222

2323
/**
24-
* Gets all buckets details
24+
* Retrieves the details of all Storage buckets within an existing product.
2525
*/
26-
async getAllBuckets(): Promise<{ data: Bucket[] | null; error: Error | null }> {
26+
async listBuckets(): Promise<{ data: Bucket[] | null; error: Error | null }> {
2727
try {
2828
const data = await get(`${this.url}/bucket`, { headers: this.headers })
2929
return { data, error: null }
@@ -33,8 +33,9 @@ export class StorageApi {
3333
}
3434

3535
/**
36-
* Get details of a bucket
37-
* @param id the bucket id to retrieve
36+
* Retrieves the details of an existing Storage bucket.
37+
*
38+
* @param id The unique identifier of the bucket you would like to retrieve.
3839
*/
3940
async getBucket(id: string): Promise<{ data: Bucket | null; error: Error | null }> {
4041
try {
@@ -46,8 +47,9 @@ export class StorageApi {
4647
}
4748

4849
/**
49-
* Create a bucket
50-
* @param name the new bucket name
50+
* Retrieves the details of an existing Storage bucket.
51+
*
52+
* @param name A name of the bucket you are creating.
5153
*/
5254
async createBucket(name: string): Promise<{ data: Bucket | null; error: Error | null }> {
5355
try {
@@ -59,8 +61,9 @@ export class StorageApi {
5961
}
6062

6163
/**
62-
* Empty a bucket
63-
* @param id the bucket id to empty
64+
* Removes all objects inside a single bucket.
65+
*
66+
* @param id The unique identifier of the bucket you would like to empty.
6467
*/
6568
async emptyBucket(
6669
id: string
@@ -74,8 +77,10 @@ export class StorageApi {
7477
}
7578

7679
/**
77-
* Delete a bucket
78-
* @param id the bucket id to be deleted
80+
* Deletes an existing bucket. A bucket can't be deleted with existing objects inside it.
81+
* You must first `empty()` the bucket.
82+
*
83+
* @param id The unique identifier of the bucket you would like to delete.
7984
*/
8085
async deleteBucket(
8186
id: string
@@ -89,9 +94,10 @@ export class StorageApi {
8994
}
9095

9196
/**
92-
* Upload a file
93-
* @param path the relative file path
94-
* @param file the File content
97+
* Uploads a file to an existing bucket.
98+
*
99+
* @param path The relative file path including the bucket ID. Should be of the format `bucket/folder/subfolder`. The bucket already exist before attempting to upload.
100+
* @param file The File object to be stored in the bucket.
95101
*/
96102
async uploadFile(
97103
path: string,
@@ -123,9 +129,10 @@ export class StorageApi {
123129
}
124130

125131
/**
126-
* Update a file
127-
* @param path the relative file path
128-
* @param file the File content
132+
* Replaces an existing file at the specified path with a new one.
133+
*
134+
* @param path The relative file path including the bucket ID. Should be of the format `bucket/folder/subfolder`. The bucket already exist before attempting to upload.
135+
* @param file The file object to be stored in the bucket.
129136
*/
130137
async updateFile(
131138
path: string,
@@ -157,20 +164,21 @@ export class StorageApi {
157164
}
158165

159166
/**
160-
* Rename a file
161-
* @param bucketName
162-
* @param fromPath original relative file path
163-
* @param toPath the new relative file path
167+
* Moves an existing file, optionally renaming it at the same time.
168+
*
169+
* @param bucketId The bucket which contains the file.
170+
* @param fromPath The original file path, including the current file name. For example `folder/image.png`.
171+
* @param toPath The new file path, including the new file name. For example `folder/image-copy.png`.
164172
*/
165-
async renameFile(
166-
bucketName: string,
173+
async moveFile(
174+
bucketId: string,
167175
fromPath: string,
168176
toPath: string
169177
): Promise<{ data: { message: string } | null; error: Error | null }> {
170178
try {
171179
const data = await post(
172-
`${this.url}/object/rename`,
173-
{ bucketName, sourceKey: fromPath, destinationKey: toPath },
180+
`${this.url}/object/move`,
181+
{ bucketId, sourceKey: fromPath, destinationKey: toPath },
174182
{ headers: this.headers }
175183
)
176184
return { data, error: null }
@@ -180,9 +188,10 @@ export class StorageApi {
180188
}
181189

182190
/**
183-
* Create signed url to download file
184-
* @param path the relative file path to be downloaded
185-
* @param expiresIn seconds until the signed URL expires
191+
* Create signed url to download file.
192+
*
193+
* @param path The file path to be downloaded, including the current file name. For example `folder/image.png`.
194+
* @param expiresIn The number of seconds until the signed URL expires. For example, `60` for a URL which is valid for one minute.
186195
*/
187196
async createSignedUrl(
188197
path: string,
@@ -201,8 +210,9 @@ export class StorageApi {
201210
}
202211

203212
/**
204-
* Download a file
205-
* @param path the relative file path to be downloaded
213+
* Downloads a file.
214+
*
215+
* @param path The file path to be downloaded, including the path and file name. For example `folder/image.png`.
206216
*/
207217
async downloadFile(path: string): Promise<{ data: Blob | null; error: Error | null }> {
208218
try {
@@ -218,8 +228,9 @@ export class StorageApi {
218228
}
219229

220230
/**
221-
* Delete a file
222-
* @param path the relative file path to be deleted
231+
* Deletes a file.
232+
*
233+
* @param path The file path to be deleted, including the path and file name. For example `folder/image.png`.
223234
*/
224235
async deleteFile(
225236
path: string
@@ -233,17 +244,18 @@ export class StorageApi {
233244
}
234245

235246
/**
236-
* Delete multiple files on the same bucket
237-
* @param bucketName
238-
* @param paths the relative file paths to be deleted excluded the bucket name
247+
* Deletes multiple files within the same bucket
248+
*
249+
* @param bucketId The bucket which contains the files.
250+
* @param paths An array of files to be deletes, including the path and file name. For example [`folder/image.png`].
239251
*/
240252
async deleteFiles(
241-
bucketName: string,
253+
bucketId: string,
242254
paths: string[]
243255
): Promise<{ data: FileObject[] | null; error: Error | null }> {
244256
try {
245257
const data = await remove(
246-
`${this.url}/object/${bucketName}`,
258+
`${this.url}/object/${bucketId}`,
247259
{ prefixes: paths },
248260
{ headers: this.headers }
249261
)
@@ -284,19 +296,19 @@ export class StorageApi {
284296
// }
285297

286298
/**
287-
* Use to fetch folder contents
288-
* @param bucketName
289-
* @param path the relative folder path excluded the bucket name
290-
* @param options
299+
* Lists all the files within a bucket.
300+
* @param bucketId The bucket which contains the files.
301+
* @param path The folder path.
302+
* @param options Search options, including `limit`, `offset`, and `sortBy`.
291303
*/
292-
async search(
293-
bucketName: string,
304+
async listFiles(
305+
bucketId: string,
294306
path?: string,
295307
options?: SearchOptions
296308
): Promise<{ data: FileObject[] | null; error: Error | null }> {
297309
try {
298310
const body = { ...DEFAULT_SEARCH_OPTIONS, ...options, prefix: path || '' }
299-
const data = await post(`${this.url}/search/${bucketName}`, body, { headers: this.headers })
311+
const data = await post(`${this.url}/object/list/${bucketId}`, body, { headers: this.headers })
300312
return { data, error: null }
301313
} catch (error) {
302314
return { data: null, error }

src/lib/storage/types.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,14 @@ export interface SortBy {
2222
column?: string
2323
order?: string
2424
}
25-
2625
export interface SearchOptions {
26+
/** The number of files you want to be returned. */
2727
limit?: number
28+
29+
/** The starting position. */
2830
offset?: number
31+
32+
/** The column to sort by. Can be any column inside a FileObject. */
2933
sortBy?: SortBy
3034
}
3135

0 commit comments

Comments
 (0)