Skip to content

Commit 4e601b3

Browse files
committed
clean up storageclient
1 parent 4f3ba4f commit 4e601b3

File tree

1 file changed

+37
-6
lines changed

1 file changed

+37
-6
lines changed

src/packages/storage/StorageClient.ts

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,40 @@ import { SQLiteCloudError } from "../../drivers/types"
33
import { getAPIUrl } from "../utils"
44
import { Fetch, fetchWithAuth } from "../utils/fetch"
55

6+
// TODO: add consistent return types
7+
8+
9+
/**
10+
* StorageResponse
11+
* @param data - The data returned from the operation.
12+
* @param error - The error that occurred.
13+
*/
614
interface StorageResponse {
715
data: any
816
error: any
917
}
1018

19+
/**
20+
* Storage
21+
* @param createBucket - Create a bucket.
22+
* @param getBucket - Get a bucket.
23+
* @param deleteBucket - Delete a bucket.
24+
* @param listBuckets - List all buckets.
25+
* @param upload - Upload a file.
26+
* @param download - Download a file.
27+
* @param remove - Remove a file.
28+
* @param list - List all files in a bucket.
29+
*/
1130
interface Storage {
1231
createBucket(bucket: string): Promise<StorageResponse>
1332
getBucket(bucket: string): Promise<StorageResponse>
1433
deleteBucket(bucket: string): Promise<StorageResponse>
1534
listBuckets(): Promise<StorageResponse>
16-
upload(bucket: string, pathname: string, file: File | Buffer | Blob | string, options: { contentType: string }): Promise<StorageResponse>
35+
upload(bucket: string, pathname: string, file: File | Buffer | Blob | string, options: { headers?: Record<string, string> }): Promise<StorageResponse>
1736
download(bucket: string, pathname: string): Promise<StorageResponse>
1837
remove(bucket: string, pathName: string): Promise<StorageResponse>
1938
list(bucket: string): Promise<StorageResponse>
2039
}
21-
2240
export class StorageClient implements Storage {
2341
protected filesUrl: string
2442
protected webliteSQLUrl: string
@@ -100,10 +118,24 @@ export class StorageClient implements Storage {
100118
}
101119
}
102120

103-
async upload(bucket: string, pathname: string, file: File | Buffer | Blob | string, options: { contentType: string }) {
121+
async upload(bucket: string, pathname: string, file: File | Buffer | Blob | string, options: { headers?: Record<string, string> }) {
104122
const url = `${this.filesUrl}/${bucket}/${pathname}`;
123+
let _headers: Record<string, string> = {}
124+
if (file instanceof File) {
125+
_headers['Content-Type'] = file.type
126+
} else if (file instanceof Blob) {
127+
_headers['Content-Type'] = file.type
128+
} else if (file instanceof Buffer) {
129+
_headers['Content-Type'] = 'application/octet-stream'
130+
} else if (typeof file === 'string') {
131+
_headers['Content-Type'] = 'text/plain'
132+
} else {
133+
_headers['Content-Type'] = 'application/json'
134+
}
105135
const headers = {
106-
'Content-Type': options?.contentType || 'application/octet-stream'
136+
..._headers,
137+
...options.headers,
138+
...this.headers
107139
}
108140
try {
109141
const response = await this.fetch(url, { method: 'POST', body: file, headers })
@@ -125,12 +157,11 @@ export class StorageClient implements Storage {
125157
}
126158
let responseType = (response.headers.get('Content-Type') ?? 'text/plain').split(';')[0].trim()
127159
let data: any
160+
// TODO: add appropriate headers based on response type in Gateway
128161
if (responseType === 'application/json') {
129162
data = await response.json()
130163
} else if (responseType === 'application/octet-stream') {
131164
data = await response.blob()
132-
} else if (responseType === 'text/event-stream') {
133-
data = response
134165
} else if (responseType === 'multipart/form-data') {
135166
data = await response.formData()
136167
} else {

0 commit comments

Comments
 (0)