Skip to content

Commit b9ed52f

Browse files
committed
fix(client): parse Presto JSON response with custom reviver and BigInts
fix #24
1 parent c114b01 commit b9ed52f

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

.nvmrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
18.17.1
1+
21.0.0

presto-client/src/client.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ import {
99
Table,
1010
} from './types'
1111

12+
function digitsToBigInt(_: string, value: unknown, { source }: { source: string }) {
13+
return /^\d+$/.test(source) ? BigInt(source) : value
14+
}
15+
1216
export class PrestoClient {
1317
private baseUrl: string
1418
private catalog?: string
@@ -270,7 +274,7 @@ export class PrestoClient {
270274
throw new Error(`Query failed: ${JSON.stringify(await response.text())}`)
271275
}
272276

273-
const prestoResponse = (await response.json()) as PrestoResponse
277+
const prestoResponse = (await this.prestoConversionToJSON({ response })) as PrestoResponse
274278
if (!prestoResponse) {
275279
throw new Error(`Query failed with an empty response from the server.`)
276280
}
@@ -334,6 +338,13 @@ export class PrestoClient {
334338
method,
335339
})
336340
}
341+
342+
private async prestoConversionToJSON({ response }: { response: Response }): Promise<unknown> {
343+
const text = await response.text()
344+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
345+
// @ts-ignore JSON.parse with a 3 argument reviver is a stage 3 proposal with some support, allow it here.
346+
return JSON.parse(text, digitsToBigInt)
347+
}
337348
}
338349

339350
export default PrestoClient

0 commit comments

Comments
 (0)