|
| 1 | +import { get, post, remove } from './fetch' |
| 2 | +import { Bucket } from './types' |
| 3 | + |
| 4 | +export class StorageBucketApi { |
| 5 | + protected url: string |
| 6 | + protected headers: { [key: string]: string } |
| 7 | + |
| 8 | + constructor(url: string, headers: { [key: string]: string } = {}) { |
| 9 | + this.url = url |
| 10 | + this.headers = headers |
| 11 | + } |
| 12 | + |
| 13 | + /** |
| 14 | + * Retrieves the details of all Storage buckets within an existing product. |
| 15 | + */ |
| 16 | + async listBuckets(): Promise<{ data: Bucket[] | null; error: Error | null }> { |
| 17 | + try { |
| 18 | + const data = await get(`${this.url}/bucket`, { headers: this.headers }) |
| 19 | + return { data, error: null } |
| 20 | + } catch (error) { |
| 21 | + return { data: null, error } |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * Retrieves the details of an existing Storage bucket. |
| 27 | + * |
| 28 | + * @param id The unique identifier of the bucket you would like to retrieve. |
| 29 | + */ |
| 30 | + async getBucket(id: string): Promise<{ data: Bucket | null; error: Error | null }> { |
| 31 | + try { |
| 32 | + const data = await get(`${this.url}/bucket/${id}`, { headers: this.headers }) |
| 33 | + return { data, error: null } |
| 34 | + } catch (error) { |
| 35 | + return { data: null, error } |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Retrieves the details of an existing Storage bucket. |
| 41 | + * |
| 42 | + * @param id A unique identifier for the bucket you are creating. |
| 43 | + */ |
| 44 | + async createBucket(id: string): Promise<{ data: Bucket | null; error: Error | null }> { |
| 45 | + try { |
| 46 | + const data = await post(`${this.url}/bucket`, { id, name: id }, { headers: this.headers }) |
| 47 | + return { data, error: null } |
| 48 | + } catch (error) { |
| 49 | + return { data: null, error } |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Removes all objects inside a single bucket. |
| 55 | + * |
| 56 | + * @param id The unique identifier of the bucket you would like to empty. |
| 57 | + */ |
| 58 | + async emptyBucket( |
| 59 | + id: string |
| 60 | + ): Promise<{ data: { message: string } | null; error: Error | null }> { |
| 61 | + try { |
| 62 | + const data = await post(`${this.url}/bucket/${id}/empty`, {}, { headers: this.headers }) |
| 63 | + return { data, error: null } |
| 64 | + } catch (error) { |
| 65 | + return { data: null, error } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Deletes an existing bucket. A bucket can't be deleted with existing objects inside it. |
| 71 | + * You must first `empty()` the bucket. |
| 72 | + * |
| 73 | + * @param id The unique identifier of the bucket you would like to delete. |
| 74 | + */ |
| 75 | + async deleteBucket( |
| 76 | + id: string |
| 77 | + ): Promise<{ data: { message: string } | null; error: Error | null }> { |
| 78 | + try { |
| 79 | + const data = await remove(`${this.url}/bucket/${id}`, {}, { headers: this.headers }) |
| 80 | + return { data, error: null } |
| 81 | + } catch (error) { |
| 82 | + return { data: null, error } |
| 83 | + } |
| 84 | + } |
| 85 | +} |
0 commit comments