From c2a919e17815d9260432e397196e9d5061349c53 Mon Sep 17 00:00:00 2001 From: Andrew Bastin Date: Tue, 19 May 2020 02:34:00 -0400 Subject: [PATCH] Fixed axios parsing JSON for non-JSON content types --- functions/strategies/AxiosStrategy.js | 24 +++++++++++++++++++++++- package-lock.json | 2 +- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/functions/strategies/AxiosStrategy.js b/functions/strategies/AxiosStrategy.js index 3aa7f8271..f38a94064 100644 --- a/functions/strategies/AxiosStrategy.js +++ b/functions/strategies/AxiosStrategy.js @@ -9,7 +9,29 @@ const axiosWithProxy = async (req, { state }) => { } const axiosWithoutProxy = async (req, _store) => { - const res = await axios(req) + const res = await axios({ + ...req, + transformResponse: [ + (data, headers) => { + // If the response has a JSON content type, try parsing it + if ( + headers["content-type"].startsWith("application/json") || + headers["content-type"].startsWith("application/vnd.api+json") || + headers["content-type"].startsWith("application/hal+json") + ) { + try { + const jsonData = JSON.parse(data) + return jsonData + } catch (e) { + return data + } + } + + // Else return the string itself without any transformations + return data + }, + ], + }) return res } diff --git a/package-lock.json b/package-lock.json index 1ee5d8021..159b535ba 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "postwoman", - "version": "1.9.5", + "version": "1.9.7", "lockfileVersion": 1, "requires": true, "dependencies": {