-
-
Notifications
You must be signed in to change notification settings - Fork 269
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #142 from supabase/refactor/storage-client
refactor: SupabaseStorageClient
- Loading branch information
Showing
8 changed files
with
137 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,16 @@ | ||
import { StorageApi } from './storage' | ||
import { StorageBucketApi, StorageFileApi } from './storage' | ||
|
||
export class SupabaseStorageClient extends StorageApi { | ||
export class SupabaseStorageClient extends StorageBucketApi { | ||
constructor(url: string, headers: { [key: string]: string } = {}) { | ||
super(url, headers) | ||
} | ||
|
||
/** | ||
* Perform file operation in a bucket. | ||
* | ||
* @param id The bucket id to operate on. | ||
*/ | ||
from(id: string): StorageFileApi { | ||
return new StorageFileApi(this.url, this.headers, id) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { get, post, remove } from './fetch' | ||
import { Bucket } from './types' | ||
|
||
export class StorageBucketApi { | ||
protected url: string | ||
protected headers: { [key: string]: string } | ||
|
||
constructor(url: string, headers: { [key: string]: string } = {}) { | ||
this.url = url | ||
this.headers = headers | ||
} | ||
|
||
/** | ||
* Retrieves the details of all Storage buckets within an existing product. | ||
*/ | ||
async listBuckets(): Promise<{ data: Bucket[] | null; error: Error | null }> { | ||
try { | ||
const data = await get(`${this.url}/bucket`, { headers: this.headers }) | ||
return { data, error: null } | ||
} catch (error) { | ||
return { data: null, error } | ||
} | ||
} | ||
|
||
/** | ||
* Retrieves the details of an existing Storage bucket. | ||
* | ||
* @param id The unique identifier of the bucket you would like to retrieve. | ||
*/ | ||
async getBucket(id: string): Promise<{ data: Bucket | null; error: Error | null }> { | ||
try { | ||
const data = await get(`${this.url}/bucket/${id}`, { headers: this.headers }) | ||
return { data, error: null } | ||
} catch (error) { | ||
return { data: null, error } | ||
} | ||
} | ||
|
||
/** | ||
* Retrieves the details of an existing Storage bucket. | ||
* | ||
* @param id A unique identifier for the bucket you are creating. | ||
*/ | ||
async createBucket(id: string): Promise<{ data: Bucket | null; error: Error | null }> { | ||
try { | ||
const data = await post(`${this.url}/bucket`, { id, name: id }, { headers: this.headers }) | ||
return { data, error: null } | ||
} catch (error) { | ||
return { data: null, error } | ||
} | ||
} | ||
|
||
/** | ||
* Removes all objects inside a single bucket. | ||
* | ||
* @param id The unique identifier of the bucket you would like to empty. | ||
*/ | ||
async emptyBucket( | ||
id: string | ||
): Promise<{ data: { message: string } | null; error: Error | null }> { | ||
try { | ||
const data = await post(`${this.url}/bucket/${id}/empty`, {}, { headers: this.headers }) | ||
return { data, error: null } | ||
} catch (error) { | ||
return { data: null, error } | ||
} | ||
} | ||
|
||
/** | ||
* Deletes an existing bucket. A bucket can't be deleted with existing objects inside it. | ||
* You must first `empty()` the bucket. | ||
* | ||
* @param id The unique identifier of the bucket you would like to delete. | ||
*/ | ||
async deleteBucket( | ||
id: string | ||
): Promise<{ data: { message: string } | null; error: Error | null }> { | ||
try { | ||
const data = await remove(`${this.url}/bucket/${id}`, {}, { headers: this.headers }) | ||
return { data, error: null } | ||
} catch (error) { | ||
return { data: null, error } | ||
} | ||
} | ||
} |
Oops, something went wrong.