Skip to content

Commit 09d80e7

Browse files
authored
Merge pull request #4 from planetscale/no-result
Remove Result type
2 parents 434cd8d + b1248e8 commit 09d80e7

File tree

2 files changed

+53
-151
lines changed

2 files changed

+53
-151
lines changed

src/api.ts

Lines changed: 0 additions & 74 deletions
This file was deleted.

src/index.ts

Lines changed: 53 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { Result, apiResult, unwrap, apiMessage, ClientError } from './api.js'
21
import { utf8Encode } from './text.js'
32

43
export interface Credentials {
@@ -79,95 +78,72 @@ export default class Client {
7978
}
8079

8180
export class Connection {
82-
client: Client
83-
session: QuerySession | null
81+
private client: Client
82+
private session: QuerySession | null
8483

8584
constructor(client: Client) {
8685
this.client = client
8786
this.session = null
8887
}
8988

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
10295
}
10396
}
10497

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}`)
122121
}
123122
}
124123

125124
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
171147
}
172148
}
173149
}

0 commit comments

Comments
 (0)