-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSQLiteCloudClient.ts
105 lines (92 loc) · 3.32 KB
/
SQLiteCloudClient.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { Database } from '../drivers/database'
import { Fetch, fetchWithAuth } from './utils/fetch'
import { PubSubClient } from './_pubsub/PubSubClient'
import { WebliteClient } from './weblite/WebliteClient'
import { StorageClient } from './_storage/StorageClient'
import { SQLiteCloudDataTypes, SQLiteCloudError } from '../drivers/types'
import { cleanConnectionString } from './utils'
import { FunctionsClient } from './_functions/FunctionsClient'
import { SQLiteCloudClientConfig } from './types'
import { DEFAULT_HEADERS } from '../drivers/constants'
const validateConfig = (config: SQLiteCloudClientConfig | string) => {
if (!(config)) throw new SQLiteCloudError('No configuration provided')
if (typeof config === 'string') {
if (!config.includes('sqlitecloud://')) throw new SQLiteCloudError('Invalid connection string')
}
if (typeof config === 'object') {
if (!config.connectionString) throw new SQLiteCloudError('No connection string provided')
if (!config.connectionString.includes('sqlitecloud://')) throw new SQLiteCloudError('Invalid connection string')
}
}
export class SQLiteCloudClient {
protected connectionString: string
protected fetch: Fetch
protected _globalHeaders: Record<string, string>
protected _db: Database | null
protected _pubSub: PubSubClient | null
protected _weblite: WebliteClient | null
constructor(config: SQLiteCloudClientConfig | string) {
try {
validateConfig(config)
let connectionString: string
let customFetch: Fetch | undefined
let globalHeaders: Record<string, string> = {}
if (typeof config === 'string') {
connectionString = cleanConnectionString(config)
globalHeaders = DEFAULT_HEADERS
} else {
connectionString = config.connectionString
customFetch = config.global?.fetch
globalHeaders = config.global?.headers ? { ...DEFAULT_HEADERS, ...config.global.headers } : DEFAULT_HEADERS
}
this.connectionString = connectionString
this.fetch = fetchWithAuth(this.connectionString, customFetch)
this._globalHeaders = globalHeaders
this._db = null
this._pubSub = null
this._weblite = null
} catch (error) {
throw new SQLiteCloudError('failed to initialize SQLiteCloudClient')
}
}
// Defaults to HTTP API
async sql(sql: TemplateStringsArray | string, ...values: SQLiteCloudDataTypes[]) {
return await this.weblite.sql(sql, ...values)
}
get pubSub() {
if (!this._pubSub) {
this._pubSub = new PubSubClient(this.db.getConfiguration())
}
return this._pubSub
}
get db() {
if (!this._db) {
this._db = new Database(this.connectionString)
}
return this._db
}
get weblite() {
if (!this._weblite) {
this._weblite = new WebliteClient(this.connectionString, {
fetch: this.fetch,
headers: this._globalHeaders
})
}
return this._weblite
}
get files() {
return new StorageClient(this.connectionString, {
customFetch: this.fetch,
headers: this._globalHeaders
})
}
get functions() {
return new FunctionsClient(this.connectionString, {
customFetch: this.fetch,
headers: this._globalHeaders
})
}
}
export function createClient(config: SQLiteCloudClientConfig | string): SQLiteCloudClient {
return new SQLiteCloudClient(config)
}