From f4a6eb53bc6f476f3abc4361aaf3819247beee7d Mon Sep 17 00:00:00 2001 From: spencerwooo Date: Thu, 10 Feb 2022 22:00:40 +0800 Subject: [PATCH] fix for axios parsing file content as json by default --- utils/fetchOnMount.ts | 6 ++++-- utils/getFileIcon.ts | 21 +++++++++++++++------ 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/utils/fetchOnMount.ts b/utils/fetchOnMount.ts index 3a0a67f..314639f 100644 --- a/utils/fetchOnMount.ts +++ b/utils/fetchOnMount.ts @@ -9,8 +9,10 @@ export default function useAxiosGet(fetchUrl: string): { response: any; error: s useEffect(() => { axios - .get(fetchUrl) - .then(res => setResponse(res.data)) + // Using 'blob' as response type to get the response as a raw file blob, which is later parsed as a string. + // Axios defaults response parsing to JSON, which causes issues when parsing JSON files. + .get(fetchUrl, { responseType: 'blob' }) + .then(async res => setResponse(await res.data.text())) .catch(e => setError(e.message)) .finally(() => { setValidating(false) diff --git a/utils/getFileIcon.ts b/utils/getFileIcon.ts index b2e230d..9d345dc 100644 --- a/utils/getFileIcon.ts +++ b/utils/getFileIcon.ts @@ -58,14 +58,23 @@ const extensions = { tar: icons.archive, zip: icons.archive, - css: icons.code, - py: icons.code, - html: icons.code, - js: icons.code, - ts: icons.code, c: icons.code, - rb: icons.code, cpp: icons.code, + js: icons.code, + jsx: icons.code, + java: icons.code, + sh: icons.code, + cs: icons.code, + py: icons.code, + css: icons.code, + html: icons.code, + ts: icons.code, + tsx: icons.code, + rs: icons.code, + vue: icons.code, + json: icons.code, + yaml: icons.code, + toml: icons.code, txt: icons.text, rtf: icons.text,