|
1 |
| -import { Result, apiResult, unwrap, apiMessage, ClientError } from './api.js' |
2 | 1 | import { utf8Encode } from './text.js'
|
3 | 2 |
|
4 | 3 | export interface Credentials {
|
@@ -79,95 +78,72 @@ export default class Client {
|
79 | 78 | }
|
80 | 79 |
|
81 | 80 | export class Connection {
|
82 |
| - client: Client |
83 |
| - session: QuerySession | null |
| 81 | + private client: Client |
| 82 | + private session: QuerySession | null |
84 | 83 |
|
85 | 84 | constructor(client: Client) {
|
86 | 85 | this.client = client
|
87 | 86 | this.session = null
|
88 | 87 | }
|
89 | 88 |
|
90 |
| - async createSession(): Promise<QuerySession> { |
91 |
| - const saved = await this.postJSON<QueryExecuteResponse>( |
92 |
| - `${this.client.credentials.mysqlAddress}/psdb.v1alpha1.Database/CreateSession`, |
93 |
| - {} |
94 |
| - ) |
95 |
| - if (saved.ok && !saved.ok.error) { |
96 |
| - if (saved.ok.session) { |
97 |
| - this.session = saved.ok.session |
98 |
| - } |
99 |
| - return saved.ok.session |
100 |
| - } else { |
101 |
| - throw saved.err |
| 89 | + async refresh(): Promise<boolean> { |
| 90 | + try { |
| 91 | + const session = await this.createSession() |
| 92 | + return !!session |
| 93 | + } catch { |
| 94 | + return false |
102 | 95 | }
|
103 | 96 | }
|
104 | 97 |
|
105 |
| - async postJSON<T>(url: string, body?: unknown): Promise<Result<T>> { |
106 |
| - try { |
107 |
| - const result = await apiResult<T>( |
108 |
| - fetch(url, { |
109 |
| - method: 'POST', |
110 |
| - body: body ? JSON.stringify(body) : undefined, |
111 |
| - headers: { |
112 |
| - 'Content-Type': 'application/json', |
113 |
| - ...this.client.authorizationHeader() |
114 |
| - }, |
115 |
| - credentials: 'include' |
116 |
| - }) |
117 |
| - ) |
118 |
| - |
119 |
| - return result.err ? { err: result.err } : { ok: unwrap(result.ok) } |
120 |
| - } catch (e: any) { |
121 |
| - return { err: e } |
| 98 | + private async createSession(): Promise<QuerySession> { |
| 99 | + const url = `${this.client.credentials.mysqlAddress}/psdb.v1alpha1.Database/CreateSession` |
| 100 | + const { session } = await this.postJSON<QueryExecuteResponse>(url) |
| 101 | + this.session = session |
| 102 | + return session |
| 103 | + } |
| 104 | + |
| 105 | + private async postJSON<T>(url: string, body = {}): Promise<T> { |
| 106 | + const response = await fetch(url, { |
| 107 | + method: 'POST', |
| 108 | + body: JSON.stringify(body), |
| 109 | + headers: { |
| 110 | + 'Content-Type': 'application/json', |
| 111 | + ...this.client.authorizationHeader() |
| 112 | + }, |
| 113 | + credentials: 'include' |
| 114 | + }) |
| 115 | + |
| 116 | + if (response.ok) { |
| 117 | + const result = await response.json() |
| 118 | + return result |
| 119 | + } else { |
| 120 | + throw new Error(`${response.status} ${response.statusText}`) |
122 | 121 | }
|
123 | 122 | }
|
124 | 123 |
|
125 | 124 | async execute(query: string): Promise<ExecutedQuery> {
|
126 |
| - const startTime = new Date().getTime() |
127 |
| - const saved = await this.postJSON<QueryExecuteResponse>( |
128 |
| - `${this.client.credentials.mysqlAddress}/psdb.v1alpha1.Database/Execute`, |
129 |
| - { |
130 |
| - query: query, |
131 |
| - session: this.session |
132 |
| - } |
133 |
| - ) |
134 |
| - const endTime = new Date().getTime() |
135 |
| - const elapsedTime = endTime - startTime |
136 |
| - if (saved.ok && !saved.ok.error) { |
137 |
| - const body = saved.ok |
138 |
| - const result = body.result |
139 |
| - const rows = result ? parse(result) : null |
140 |
| - const headers = result?.fields?.map((f) => f.name) |
141 |
| - |
142 |
| - this.session = body.session |
143 |
| - |
144 |
| - // Transform response into something we understand, this matches our |
145 |
| - // console's `QueryConsole` response format. |
146 |
| - return { |
147 |
| - headers, |
148 |
| - rows, |
149 |
| - size: rows.length, |
150 |
| - statement: query, |
151 |
| - time: elapsedTime |
152 |
| - } |
153 |
| - } else if (saved.ok && saved.ok.error) { |
154 |
| - return { |
155 |
| - statement: query, |
156 |
| - errorMessage: saved.ok.error.message, |
157 |
| - time: elapsedTime |
158 |
| - } |
159 |
| - } else { |
160 |
| - let errorCode: string | null = null |
161 |
| - if (saved.err instanceof ClientError) { |
162 |
| - errorCode = saved.err.body.code |
163 |
| - } |
164 |
| - |
165 |
| - return { |
166 |
| - statement: query, |
167 |
| - errorCode: errorCode, |
168 |
| - errorMessage: apiMessage(saved.err), |
169 |
| - time: elapsedTime |
170 |
| - } |
| 125 | + const startTime = Date.now() |
| 126 | + const url = `${this.client.credentials.mysqlAddress}/psdb.v1alpha1.Database/Execute` |
| 127 | + const saved = await this.postJSON<QueryExecuteResponse>(url, { |
| 128 | + query: query, |
| 129 | + session: this.session |
| 130 | + }) |
| 131 | + const time = Date.now() - startTime |
| 132 | + |
| 133 | + const { result, session, error } = saved |
| 134 | + if (error) throw new Error(error.message) |
| 135 | + |
| 136 | + this.session = session |
| 137 | + |
| 138 | + const rows = result ? parse(result) : [] |
| 139 | + const headers = result ? result.fields?.map((f) => f.name) : [] |
| 140 | + |
| 141 | + return { |
| 142 | + headers, |
| 143 | + rows, |
| 144 | + size: rows.length, |
| 145 | + statement: query, |
| 146 | + time |
171 | 147 | }
|
172 | 148 | }
|
173 | 149 | }
|
|
0 commit comments