-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Handle BigInt numbers from API #920
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
{ | ||
"extends": "react-app" | ||
"extends": "react-app", | ||
"env": { | ||
"es2020": true | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -86,7 +86,7 @@ describe('stats', () => { | |
const fetchMock = (global.fetch = jest.fn().mockImplementation(async () => { | ||
return { | ||
ok: true, | ||
json: async () => TABLE_TEST.output | ||
text: async () => JSON.stringify(TABLE_TEST.output) | ||
}; | ||
})); | ||
|
||
|
@@ -117,7 +117,7 @@ describe('stats', () => { | |
const fetchMock = (global.fetch = jest.fn().mockImplementation(async () => { | ||
return { | ||
ok: true, | ||
json: async () => QUERY_TEST.output | ||
text: async () => JSON.stringify(QUERY_TEST.output) | ||
}; | ||
})); | ||
|
||
|
@@ -149,7 +149,7 @@ describe('stats', () => { | |
const fetchMock = (global.fetch = jest.fn().mockImplementation(async () => { | ||
return { | ||
ok: true, | ||
json: async () => QUERY_TEST.output | ||
text: async () => JSON.stringify(QUERY_TEST.output) | ||
}; | ||
})); | ||
|
||
|
@@ -187,7 +187,7 @@ describe('stats', () => { | |
const fetchMock = (global.fetch = jest.fn().mockImplementation(async () => { | ||
return { | ||
ok: true, | ||
json: async () => QUERY_TEST.output | ||
text: async () => JSON.stringify(QUERY_TEST.output) | ||
}; | ||
})); | ||
|
||
|
@@ -225,5 +225,20 @@ describe('stats', () => { | |
expect(res).toEqual(TILESET_TEST.output); | ||
expect(mockedGetTileJson).toBeCalledWith({ source: TILESET_TEST.input.source }); | ||
}); | ||
|
||
test('BigInt numbers are correctly parsed', async () => { | ||
const jsonWithBigInt = '{"rows": [{"quadbin":5256683984082960383}]}'; | ||
|
||
const fetchMock = (global.fetch = jest.fn().mockImplementation(async () => { | ||
return { | ||
ok: true, | ||
text: async () => jsonWithBigInt | ||
}; | ||
})); | ||
|
||
const res = await getStats({ source: QUERY_SOURCE, column: 'injuries' }); | ||
|
||
expect(res.rows[0].quadbin).toEqual(BigInt(5256683984082960383)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Careful here! Writing this out as BigInt(5256683984082960383) === BigInt(5256683984082960383n) // false! |
||
}); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,13 @@ | ||
import { InvalidColumnError } from '@carto/react-core/'; | ||
|
||
// Widgets API may return BigInt values, and JSON.parse does not handle them properly, so we need to use a reviver | ||
const bigIntReviver = (key, value) => { | ||
if (typeof value === 'number' && value > Number.MAX_SAFE_INTEGER) { | ||
return BigInt(value); | ||
} | ||
return value; | ||
}; | ||
Comment on lines
+4
to
+9
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See above — unfortunately I think that it's necessary to receive the value as a string and not an integer. By the time the reviver receives a numeric value, I think it will be too late to recover the original integer. Possible solution in https://stackoverflow.com/a/69644630, though maybe this is complex enough that we want a dependency to help... |
||
|
||
/** | ||
* Return more descriptive error from API | ||
*/ | ||
|
@@ -51,7 +59,9 @@ export async function makeCall({ url, credentials, opts }) { | |
signal: opts?.abortController?.signal, | ||
...opts?.otherOptions | ||
}); | ||
data = await response.json(); | ||
|
||
const text = await response.text(); | ||
data = JSON.parse(text, bigIntReviver); | ||
} catch (error) { | ||
if (error.name === 'AbortError') throw error; | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Otherwise,
BigInt
constructor gives an ESLint error 🤦