Skip to content

Commit

Permalink
Add parseBigInt as an option in fetchJson
Browse files Browse the repository at this point in the history
  • Loading branch information
Krithika Sundararajan committed Oct 21, 2021
1 parent c093e94 commit 557a89f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
2 changes: 1 addition & 1 deletion ui/packages/lib/src/utils/fetchJson.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const fetchData = async (url, authCtx, options) => {
export default async (url, authCtx, options = {}) => {
return fetchData(url, authCtx, options)
.then(response =>
parseJson(response).then(result => {
parseJson(response, !!options.parseBigInt).then(result => {
var headers = {};
for (var pair of response.headers.entries()) {
headers[pair[0]] = pair[1];
Expand Down
12 changes: 8 additions & 4 deletions ui/packages/lib/src/utils/parseJson.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
const jsonBig = require(`json-bigint`);

export const parseJson = response => {
const parseJsonHandleBigInt = (response, parseBigInt) => {
return parseBigInt
? response.text().then(text => jsonBig.parse(text))
: response.json();
}

export const parseJson = (response, parseBigInt) => {
// using the json-bigint library to parse the data instead of the Response.json() Web API,
// to parse BigInt without losing precision.
return response.text().then(
text => jsonBig.parse(text)
).catch(error => {
return parseJsonHandleBigInt(response, parseBigInt).catch(error => {
// for responses without body
if (error.name === "SyntaxError") {
return {};
Expand Down

0 comments on commit 557a89f

Please sign in to comment.