diff --git a/src/request/RequestClient.ts b/src/request/RequestClient.ts index eb6aac2..9f29dcd 100644 --- a/src/request/RequestClient.ts +++ b/src/request/RequestClient.ts @@ -429,8 +429,8 @@ export class RequestClient implements HttpClient { throw new Error('Valid JSON could not be extracted from response.') } - isValidJson(weboutResponse) - parsedResponse = JSON.parse(weboutResponse) + const jsonResponse = isValidJson(weboutResponse) + parsedResponse = jsonResponse } catch { parsedResponse = response.data } diff --git a/src/test/utils/isValidJson.spec.ts b/src/test/utils/isValidJson.spec.ts index 8f6c0c9..e8a63f2 100644 --- a/src/test/utils/isValidJson.spec.ts +++ b/src/test/utils/isValidJson.spec.ts @@ -6,15 +6,15 @@ describe.only('jsonValidator', () => { test: 'test' } - expect(isValidJson(json)).not.toThrowError + expect(isValidJson(json)).toBe(json) }) it('should not throw an error with an valid json string', () => { - const json = JSON.stringify({ + const json = { test: 'test' - }) + } - expect(isValidJson(json)).not.toThrowError + expect(isValidJson(JSON.stringify(json))).toStrictEqual(json) }) it('should throw an error with an invalid json', () => { diff --git a/src/utils/isValidJson.ts b/src/utils/isValidJson.ts index d6cc549..253440f 100644 --- a/src/utils/isValidJson.ts +++ b/src/utils/isValidJson.ts @@ -4,9 +4,9 @@ */ export const isValidJson = (str: string | object) => { try { - if (typeof str === 'object') return + if (typeof str === 'object') return str - str = JSON.parse(str) + return JSON.parse(str) } catch (e) { throw new Error('Invalid JSON response.') }