Skip to content

Commit 1b82937

Browse files
authored
Merge pull request #142 from supabase/refactor/storage-client
refactor: SupabaseStorageClient
2 parents 2e32bc2 + 68798a8 commit 1b82937

File tree

8 files changed

+137
-130
lines changed

8 files changed

+137
-130
lines changed

example/next-storage/components/Account.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,11 @@ export default function Account({
3939
const file = event.target.files[0]
4040
const fileExt = file.name.split('.').pop()
4141
const fileName = `${session?.user.id}${Math.random()}.${fileExt}`
42-
const filePath = `${DEFAULT_AVATARS_BUCKET}/${fileName}`
42+
const filePath = `${fileName}`
4343

44-
let { error: uploadError } = await supabase.storage.uploadFile(filePath, file)
44+
let { error: uploadError } = await supabase.storage
45+
.from(DEFAULT_AVATARS_BUCKET)
46+
.upload(filePath, file)
4547

4648
if (uploadError) {
4749
throw uploadError

example/next-storage/components/Avatar.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { useEffect, useState } from 'react'
22
import { supabase } from '../lib/api'
3+
import { DEFAULT_AVATARS_BUCKET } from '../lib/constants'
34

45
export default function Avatar({ url, size }: { url: string | null; size: number }) {
56
const [avatarUrl, setAvatarUrl] = useState<string | null>(null)
@@ -10,7 +11,7 @@ export default function Avatar({ url, size }: { url: string | null; size: number
1011

1112
async function downloadImage(path: string) {
1213
try {
13-
const { data, error } = await supabase.storage.downloadFile(path)
14+
const { data, error } = await supabase.storage.from(DEFAULT_AVATARS_BUCKET).download(path)
1415
if (error) {
1516
throw error
1617
}

src/SupabaseClient.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export default class SupabaseClient {
7171
* Supabase Storage allows you to manage user-generated content, such as photos or videos.
7272
*/
7373
get storage() {
74-
return this._initStorageClient()
74+
return new SupabaseStorageClient(this.storageUrl, this._getAuthHeaders())
7575
}
7676

7777
/**
@@ -167,10 +167,6 @@ export default class SupabaseClient {
167167
})
168168
}
169169

170-
private _initStorageClient() {
171-
return new SupabaseStorageClient(this.storageUrl, this._getAuthHeaders())
172-
}
173-
174170
private _getAuthHeaders(): { [key: string]: string } {
175171
const headers: { [key: string]: string } = {}
176172
const authBearer = this.auth.session()?.access_token ?? this.supabaseKey

src/lib/SupabaseStorageClient.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
1-
import { StorageApi } from './storage'
1+
import { StorageBucketApi, StorageFileApi } from './storage'
22

3-
export class SupabaseStorageClient extends StorageApi {
3+
export class SupabaseStorageClient extends StorageBucketApi {
44
constructor(url: string, headers: { [key: string]: string } = {}) {
55
super(url, headers)
66
}
7+
8+
/**
9+
* Perform file operation in a bucket.
10+
*
11+
* @param id The bucket id to operate on.
12+
*/
13+
from(id: string): StorageFileApi {
14+
return new StorageFileApi(this.url, this.headers, id)
15+
}
716
}

src/lib/storage/StorageBucketApi.ts

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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

Comments
 (0)