|
| 1 | +import { DEFAULT_HEADERS } from "../../drivers/constants" |
| 2 | +import { SQLiteCloudError } from "../../drivers/types" |
| 3 | +import { getAPIUrl } from "../utils" |
| 4 | +import { Fetch, fetchWithAuth } from "../utils/fetch" |
| 5 | + |
| 6 | +interface StorageResponse { |
| 7 | + data: any |
| 8 | + error: any |
| 9 | +} |
| 10 | + |
| 11 | +interface Storage { |
| 12 | + createBucket(bucket: string): Promise<StorageResponse> |
| 13 | + getBucket(bucket: string): Promise<StorageResponse> |
| 14 | + deleteBucket(bucket: string): Promise<StorageResponse> |
| 15 | + listBuckets(): Promise<StorageResponse> |
| 16 | + upload(bucket: string, pathname: string, file: File | Buffer | Blob | string, options: { contentType: string }): Promise<StorageResponse> |
| 17 | + download(bucket: string, pathname: string): Promise<StorageResponse> |
| 18 | + remove(bucket: string, pathName: string): Promise<StorageResponse> |
| 19 | + list(bucket: string): Promise<StorageResponse> |
| 20 | +} |
| 21 | + |
| 22 | +export class StorageClient implements Storage { |
| 23 | + protected filesUrl: string |
| 24 | + protected webliteSQLUrl: string |
| 25 | + protected headers: Record<string, string> |
| 26 | + protected fetch: Fetch |
| 27 | + |
| 28 | + constructor( |
| 29 | + connectionString: string, |
| 30 | + options: { |
| 31 | + customFetch?: Fetch, |
| 32 | + headers?: Record<string, string> |
| 33 | + } = {}) { |
| 34 | + this.filesUrl = getAPIUrl(connectionString, 'files') |
| 35 | + this.webliteSQLUrl = getAPIUrl(connectionString, 'weblite/sql') |
| 36 | + this.fetch = options.customFetch || fetchWithAuth(connectionString) |
| 37 | + this.headers = options.headers ? { ...DEFAULT_HEADERS, ...options.headers } : { ...DEFAULT_HEADERS } |
| 38 | + } |
| 39 | + |
| 40 | + async createBucket(bucket: string) { |
| 41 | + const sql = `USE DATABASE files; INSERT INTO files (Bucket) VALUES ('${bucket}');` |
| 42 | + |
| 43 | + try { |
| 44 | + const response = await this.fetch(this.webliteSQLUrl, { |
| 45 | + method: 'POST', |
| 46 | + body: JSON.stringify({ sql }), |
| 47 | + headers: this.headers |
| 48 | + }) |
| 49 | + |
| 50 | + if (!response.ok) { |
| 51 | + throw new SQLiteCloudError(`Failed to create bucket: ${response.statusText}`) |
| 52 | + } |
| 53 | + |
| 54 | + return { data: await response.json(), error: null } |
| 55 | + } catch (error) { |
| 56 | + return { data: null, error } |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + async getBucket(bucket: string) { |
| 61 | + const url = `${this.filesUrl}/${bucket}` |
| 62 | + const response = await this.fetch(url, { method: 'GET', headers: this.headers }) |
| 63 | + if (!response.ok) { |
| 64 | + throw new SQLiteCloudError(`Failed to get bucket: ${response.statusText}`) |
| 65 | + } |
| 66 | + |
| 67 | + return { data: await response.json(), error: null } |
| 68 | + } |
| 69 | + |
| 70 | + async deleteBucket(bucket: string) { |
| 71 | + const url = `${this.filesUrl}/${bucket}` |
| 72 | + try { |
| 73 | + const response = await this.fetch(url, { method: 'DELETE', headers: this.headers }) |
| 74 | + if (!response.ok) { |
| 75 | + throw new SQLiteCloudError(`Failed to delete bucket: ${response.statusText}`) |
| 76 | + } |
| 77 | + return { data: await response.json(), error: null } |
| 78 | + } catch (error) { |
| 79 | + return { data: null, error } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + async listBuckets() { |
| 84 | + const sql = `USE DATABASE files.sqlite; SELECT * FROM files;` |
| 85 | + try { |
| 86 | + const response = await this.fetch(this.webliteSQLUrl, { |
| 87 | + method: 'POST', |
| 88 | + body: JSON.stringify({ sql }), |
| 89 | + headers: this.headers |
| 90 | + }) |
| 91 | + if (!response.ok) { |
| 92 | + throw new SQLiteCloudError(`Failed to list buckets: ${response.statusText}`) |
| 93 | + } |
| 94 | + return { data: await response.json(), error: null } |
| 95 | + } catch (error) { |
| 96 | + return { |
| 97 | + data: null, |
| 98 | + error |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + async upload(bucket: string, pathname: string, file: File | Buffer | Blob | string, options: { contentType: string }) { |
| 104 | + const url = `${this.filesUrl}/${bucket}/${pathname}`; |
| 105 | + const headers = { |
| 106 | + 'Content-Type': options?.contentType || 'application/octet-stream' |
| 107 | + } |
| 108 | + try { |
| 109 | + const response = await this.fetch(url, { method: 'POST', body: file, headers }) |
| 110 | + if (!response.ok) { |
| 111 | + throw new SQLiteCloudError(`Failed to upload file: ${response.statusText}`) |
| 112 | + } |
| 113 | + return { data: await response.json(), error: null } |
| 114 | + } catch (error) { |
| 115 | + return { data: null, error } |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + async download(bucket: string, pathname: string) { |
| 120 | + const url = `${this.filesUrl}/${bucket}/${pathname}`; |
| 121 | + try { |
| 122 | + const response = await this.fetch(url, { method: 'GET' }) |
| 123 | + if (!response.ok) { |
| 124 | + throw new SQLiteCloudError(`Failed to download file: ${response.statusText}`) |
| 125 | + } |
| 126 | + let responseType = (response.headers.get('Content-Type') ?? 'text/plain').split(';')[0].trim() |
| 127 | + let data: any |
| 128 | + if (responseType === 'application/json') { |
| 129 | + data = await response.json() |
| 130 | + } else if (responseType === 'application/octet-stream') { |
| 131 | + data = await response.blob() |
| 132 | + } else if (responseType === 'text/event-stream') { |
| 133 | + data = response |
| 134 | + } else if (responseType === 'multipart/form-data') { |
| 135 | + data = await response.formData() |
| 136 | + } else { |
| 137 | + // default to text |
| 138 | + data = await response.text() |
| 139 | + } |
| 140 | + return { data, error: null } |
| 141 | + } catch (error) { |
| 142 | + return { data: null, error } |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + async remove(bucket: string, pathName: string) { |
| 147 | + const url = `${this.filesUrl}/${bucket}/${pathName}` |
| 148 | + try { |
| 149 | + const response = await this.fetch(url, { method: 'DELETE' }) |
| 150 | + if (!response.ok) { |
| 151 | + throw new SQLiteCloudError(`Failed to remove file: ${response.statusText}`) |
| 152 | + } |
| 153 | + return { data: response.json(), error: null } |
| 154 | + } catch (error) { |
| 155 | + return { data: null, error } |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + async list(bucket: string) { |
| 160 | + const sql = `USE DATABASE files.sqlite; SELECT * FROM files WHERE bucket = '${bucket}'` |
| 161 | + try { |
| 162 | + const response = await this.fetch(this.webliteSQLUrl, { |
| 163 | + method: 'POST', |
| 164 | + body: JSON.stringify({ sql }), |
| 165 | + headers: this.headers |
| 166 | + }) |
| 167 | + if (!response.ok) { |
| 168 | + throw new SQLiteCloudError(`Failed to list files: ${response.statusText}`) |
| 169 | + } |
| 170 | + return { data: await response.json(), error: null } |
| 171 | + } catch (error) { |
| 172 | + return { data: null, error } |
| 173 | + } |
| 174 | + } |
| 175 | +} |
0 commit comments