From 863def20a2fa19073bf0cee19c79a93ad20fd63e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20S=C3=A1nchez-Gallego?= Date: Tue, 25 Jun 2024 19:09:07 -0700 Subject: [PATCH 1/5] Use json-bigint to parse query response --- package-lock.json | 9 +++++++++ package.json | 1 + src/views/Search.vue | 17 ++++++++++++----- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/package-lock.json b/package-lock.json index 4e69b7a..f7a9ebe 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,6 +13,7 @@ "axios": "^1.6.0", "core-js": "^3.29.0", "json-big": "^1.0.2", + "json-bigint": "^1.0.0", "pinia": "^2.0.0", "pinia-plugin-persistedstate": "^3.2.1", "roboto-fontface": "*", @@ -3714,6 +3715,14 @@ "bignumber.js": "^9.0.0" } }, + "node_modules/json-bigint": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/json-bigint/-/json-bigint-1.0.0.tgz", + "integrity": "sha512-SiPv/8VpZuWbvLSMtTDU8hEfrZWg/mH/nV/b4o0CYbSxu1UIQPLdwKOCIyLQX+VIPO5vrLX3i8qtqFyhdPSUSQ==", + "dependencies": { + "bignumber.js": "^9.0.0" + } + }, "node_modules/json-buffer": { "version": "3.0.1", "resolved": "https://registry.npmjs.org/json-buffer/-/json-buffer-3.0.1.tgz", diff --git a/package.json b/package.json index 49e0c56..dad47c8 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "axios": "^1.6.0", "core-js": "^3.29.0", "json-big": "^1.0.2", + "json-bigint": "^1.0.0", "pinia": "^2.0.0", "pinia-plugin-persistedstate": "^3.2.1", "roboto-fontface": "*", diff --git a/src/views/Search.vue b/src/views/Search.vue index d6acc8c..7441ca7 100644 --- a/src/views/Search.vue +++ b/src/views/Search.vue @@ -86,11 +86,13 @@ import TextInput from '@/components/TextInput.vue' import DropdownSelect from '@/components/DropdownSelect.vue'; import { useRouter } from 'vue-router'; import { useAppStore } from '@/store/app' +import JSONbigint from 'json-bigint' // get the application state store and router const store = useAppStore() const router = useRouter() + // set up a form reference, is the name in v-form ref="form" let form = ref(null); @@ -164,23 +166,29 @@ async function submit_form(this: any) { console.log('submitting', formData.value) // submit the POST request to Valis + axios.interceptors.request.use((request) => { + request.transformResponse = [data => data] + return request + }) + await axios.post(import.meta.env.VITE_API_URL + '/query/main', formData.value, {headers: {'Content-Type': 'application/json'}}) .then((response) => { // handle the initial response - console.log(response.data) + const data = JSONbigint.parse(response.data); + console.log(data) // check for good status in response - if (response.data['status'] != 'success') { - let msg = `Response status failed: ${response.data['msg']}` + if (data['status'] != 'success') { + let msg = `Response status failed: ${data['msg']}` set_fail(msg) throw new Error(msg) } // return the actual data fail.value = false - return response.data['data'] + return data['data'] }) .then((data) => { // handle the actual data results @@ -277,4 +285,3 @@ onMounted(() => { // }, { immediate: true, deep: true }) - From e74550d97ef2965221bc64f2a9235485c4b12b2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20S=C3=A1nchez-Gallego?= Date: Wed, 26 Jun 2024 09:13:32 -0700 Subject: [PATCH 2/5] Add API axios instance with JSONbig handling --- src/api.ts | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 src/api.ts diff --git a/src/api.ts b/src/api.ts new file mode 100644 index 0000000..e33a215 --- /dev/null +++ b/src/api.ts @@ -0,0 +1,22 @@ +import axios from 'axios' +import JSONbig from 'json-big' + +// Defines the API instance in Axios with special handling for big integers. +export const apiInstance = axios.create({ + baseURL: import.meta.env.VITE_API_URL, + timeout: 5000, + transformResponse: [ + function transform(data) { + // Replacing the default transformResponse in axios because this uses JSON.parse and causes problems + // with precision of big numbers. + if (typeof data === 'string') { + try { + data = JSONbig.parse(data) + } catch (e) { + /* Ignore */ + } + } + return data + } + ] +}) From bc28107af54013dfd52c3088339ceb203f10893d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20S=C3=A1nchez-Gallego?= Date: Wed, 26 Jun 2024 09:14:00 -0700 Subject: [PATCH 3/5] Use apiInstance in Target and Search --- src/views/Search.vue | 25 ++++++++----------------- src/views/Target.vue | 23 ++++------------------- 2 files changed, 12 insertions(+), 36 deletions(-) diff --git a/src/views/Search.vue b/src/views/Search.vue index 7441ca7..7419690 100644 --- a/src/views/Search.vue +++ b/src/views/Search.vue @@ -80,13 +80,12 @@