Skip to content

Commit 2838977

Browse files
committed
formatting
1 parent 9b74e4b commit 2838977

File tree

8 files changed

+232
-101
lines changed

8 files changed

+232
-101
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@sqlitecloud/drivers",
3-
"version": "1.0.355",
3+
"version": "1.0.357",
44
"description": "SQLiteCloud drivers for Typescript/Javascript in edge, web and node clients",
55
"main": "./lib/index.js",
66
"types": "./lib/index.d.ts",

src/SQLiteCloudClient.ts

Lines changed: 60 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,117 +1,126 @@
1-
import { Database } from './drivers/database';
2-
import { SQLiteCloudError } from './drivers/types';
3-
import { Fetch, fetchWithAuth } from './drivers/fetch';
4-
import { DEFAULT_HEADERS, DEFAULT_WEBLITE_VERSION, WEBLITE_PORT } from './drivers/constants';
1+
import { Database } from './drivers/database'
2+
import { SQLiteCloudError, UploadOptions } from './drivers/types'
3+
import { Fetch, fetchWithAuth } from './drivers/fetch'
4+
import { DEFAULT_HEADERS, DEFAULT_WEBLITE_VERSION, WEBLITE_PORT } from './drivers/constants'
5+
import { PubSub } from './drivers/pubsub-refactor'
56

67
interface SQLiteCloudClientConfig {
7-
connectionString: string;
8-
fetch?: Fetch;
8+
connectionString: string
9+
fetch?: Fetch
10+
}
11+
12+
interface ISQLiteCloudClient {
13+
pubSub: PubSub
14+
db: Database
15+
upload(databaseName: string, file: File | Buffer | Blob | string, opts?: UploadOptions): Promise<Response>
16+
download(databaseName: string): Promise<ArrayBuffer | Blob>
17+
delete(databaseName: string): Promise<Response>
18+
listDatabases(): Promise<any>
919
}
1020

1121
const parseConnectionString = (connectionString: string) => {
12-
const url = new URL(connectionString);
22+
const url = new URL(connectionString)
1323
return {
1424
host: url.hostname,
1525
port: url.port,
1626
database: url.pathname.slice(1),
17-
apiKey: url.searchParams.get('apikey'),
27+
apiKey: url.searchParams.get('apikey')
1828
}
1929
}
2030

21-
type UploadOptions = {
22-
replace?: boolean;
23-
}
24-
25-
class SQLiteCloudClient {
31+
export class SQLiteCloudClient implements ISQLiteCloudClient {
2632
// TODO: Add support for custom fetch
2733
private fetch: Fetch
2834
private connectionString: string
2935
private webliteUrl: string
30-
public db: Database
36+
private _db: Database
37+
private _pubSub: PubSub
3138

3239
constructor(config: SQLiteCloudClientConfig | string) {
33-
let connectionString: string;
40+
let connectionString: string
3441
if (typeof config === 'string') {
35-
connectionString = config;
42+
connectionString = config
3643
} else {
37-
connectionString = config.connectionString;
44+
connectionString = config.connectionString
3845
}
39-
// TODO: validate connection string
40-
this.connectionString = connectionString;
41-
this.db = new Database(this.connectionString);
4246

43-
const {
44-
host,
45-
apiKey,
46-
} = parseConnectionString(this.connectionString);
47+
this.connectionString = connectionString
48+
this._db = new Database(this.connectionString)
49+
this._pubSub = new PubSub(this.db)
50+
this.fetch = fetchWithAuth(this.connectionString)
4751

48-
if (!apiKey) {
49-
throw new SQLiteCloudError('apiKey is required');
50-
}
52+
const { host } = parseConnectionString(this.connectionString)
53+
54+
this.webliteUrl = `https://${host}:${WEBLITE_PORT}/${DEFAULT_WEBLITE_VERSION}/weblite`
55+
}
56+
57+
get pubSub() {
58+
return this._pubSub
59+
}
5160

52-
this.fetch = fetchWithAuth(this.connectionString);
53-
this.webliteUrl = `https://${host}:${WEBLITE_PORT}/${DEFAULT_WEBLITE_VERSION}/weblite`;
61+
get db() {
62+
return this._db
5463
}
5564

5665
async upload(databaseName: string, file: File | Buffer | Blob | string, opts: UploadOptions = {}) {
57-
const url = `${this.webliteUrl}/${databaseName}`;
58-
let body;
66+
const url = `${this.webliteUrl}/${databaseName}`
67+
let body
5968
if (file instanceof File) {
60-
body = file;
69+
body = file
6170
} else if (file instanceof Buffer) {
62-
body = file;
71+
body = file
6372
} else if (file instanceof Blob) {
64-
body = file;
73+
body = file
6574
} else {
6675
// string
67-
body = new Blob([file]);
76+
body = new Blob([file])
6877
}
6978

7079
const headers = {
7180
'Content-Type': 'application/octet-stream',
72-
'X-Client-Info': DEFAULT_HEADERS['X-Client-Info'],
81+
'X-Client-Info': DEFAULT_HEADERS['X-Client-Info']
7382
}
7483

75-
const method = opts.replace ? 'PATCH' : 'POST';
84+
const method = opts.replace ? 'PATCH' : 'POST'
7685

7786
const response = await this.fetch(url, { method, body, headers })
7887

7988
if (!response.ok) {
80-
throw new SQLiteCloudError(`Failed to upload database: ${response.statusText}`);
89+
throw new SQLiteCloudError(`Failed to upload database: ${response.statusText}`)
8190
}
8291

83-
return response;
92+
return response
8493
}
8594

8695
async download(databaseName: string) {
87-
const url = `${this.webliteUrl}/${databaseName}`;
96+
const url = `${this.webliteUrl}/${databaseName}`
8897
const response = await this.fetch(url, { method: 'GET' })
8998
if (!response.ok) {
90-
throw new SQLiteCloudError(`Failed to download database: ${response.statusText}`);
99+
throw new SQLiteCloudError(`Failed to download database: ${response.statusText}`)
91100
}
92-
const isNode = typeof window === 'undefined';
93-
return isNode ? await response.arrayBuffer() : await response.blob();
101+
const isNode = typeof window === 'undefined'
102+
return isNode ? await response.arrayBuffer() : await response.blob()
94103
}
95104

96105
async delete(databaseName: string) {
97-
const url = `${this.webliteUrl}/${databaseName}`;
106+
const url = `${this.webliteUrl}/${databaseName}`
98107
const response = await this.fetch(url, { method: 'DELETE' })
99108
if (!response.ok) {
100-
throw new SQLiteCloudError(`Failed to delete database: ${response.statusText}`);
109+
throw new SQLiteCloudError(`Failed to delete database: ${response.statusText}`)
101110
}
102-
return response;
111+
return response
103112
}
104113

105114
async listDatabases() {
106-
const url = `${this.webliteUrl}/databases`;
115+
const url = `${this.webliteUrl}/databases`
107116
const response = await this.fetch(url, { method: 'GET' })
108117
if (!response.ok) {
109-
throw new SQLiteCloudError(`Failed to list databases: ${response.statusText}`);
118+
throw new SQLiteCloudError(`Failed to list databases: ${response.statusText}`)
110119
}
111-
return await response.json();
120+
return await response.json()
112121
}
113122
}
114123

115-
export function createClient(config: SQLiteCloudClientConfig | string) {
116-
return new SQLiteCloudClient(config);
124+
export function createClient(config: SQLiteCloudClientConfig | string): SQLiteCloudClient {
125+
return new SQLiteCloudClient(config)
117126
}

src/drivers/constants.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
const version = '0.0.1'
32
let JS_ENV = ''
43
// @ts-ignore
@@ -14,8 +13,8 @@ if (typeof Deno !== 'undefined') {
1413

1514
export const DEFAULT_HEADERS = { 'X-Client-Info': `sqlitecloud-js-${JS_ENV}/${version}` }
1615
export const DEFAULT_GLOBAL_OPTIONS = {
17-
headers: DEFAULT_HEADERS,
16+
headers: DEFAULT_HEADERS
1817
}
1918

2019
export const DEFAULT_WEBLITE_VERSION = 'v2'
21-
export const WEBLITE_PORT = 8090
20+
export const WEBLITE_PORT = 8090

src/drivers/database.ts

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import { popCallback } from './utilities'
1414
import { ErrorCallback, ResultsCallback, RowCallback, RowsCallback } from './types'
1515
import EventEmitter from 'eventemitter3'
1616
import { isBrowser } from './utilities'
17-
import { PubSub } from './pubsub'
1817
import { Statement } from './statement'
1918

2019
// Uses eventemitter3 instead of node events for browser compatibility
@@ -479,24 +478,4 @@ export class Database extends EventEmitter {
479478
})
480479
})
481480
}
482-
483-
/**
484-
* PubSub class provides a Pub/Sub real-time updates and notifications system to
485-
* allow multiple applications to communicate with each other asynchronously.
486-
* It allows applications to subscribe to tables and receive notifications whenever
487-
* data changes in the database table. It also enables sending messages to anyone
488-
* subscribed to a specific channel.
489-
* @returns {PubSub} A PubSub object
490-
*/
491-
public async getPubSub(): Promise<PubSub> {
492-
return new Promise((resolve, reject) => {
493-
this.getConnection((error, connection) => {
494-
if (error || !connection) {
495-
reject(error)
496-
} else {
497-
resolve(new PubSub(connection))
498-
}
499-
})
500-
})
501-
}
502481
}

src/drivers/fetch.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
import nodeFetch, { Headers as NodeFetchHeaders } from 'node-fetch'
32

43
export type Fetch = typeof fetch
@@ -23,14 +22,12 @@ export const resolveHeadersConstructor = () => {
2322
return Headers
2423
}
2524

26-
2725
export const fetchWithAuth = (authorization: string, customFetch?: Fetch): Fetch => {
28-
const fetch = resolveFetch(customFetch);
29-
const HeadersConstructor = resolveHeadersConstructor();
30-
26+
const fetch = resolveFetch(customFetch)
27+
const HeadersConstructor = resolveHeadersConstructor()
3128

3229
return async (input, init) => {
33-
const headers = new HeadersConstructor(init?.headers);
30+
const headers = new HeadersConstructor(init?.headers)
3431
if (!headers.has('Authorization')) {
3532
headers.set('Authorization', `Bearer ${authorization}`)
3633
}

0 commit comments

Comments
 (0)