mirror of
https://github.com/sasjs/adapter.git
synced 2026-01-03 10:40:06 +00:00
fix: isValidJson function returns the JSON parsed
This commit is contained in:
@@ -429,8 +429,8 @@ export class RequestClient implements HttpClient {
|
|||||||
throw new Error('Valid JSON could not be extracted from response.')
|
throw new Error('Valid JSON could not be extracted from response.')
|
||||||
}
|
}
|
||||||
|
|
||||||
isValidJson(weboutResponse)
|
const jsonResponse = isValidJson(weboutResponse)
|
||||||
parsedResponse = JSON.parse(weboutResponse)
|
parsedResponse = jsonResponse
|
||||||
} catch {
|
} catch {
|
||||||
parsedResponse = response.data
|
parsedResponse = response.data
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,15 +6,15 @@ describe.only('jsonValidator', () => {
|
|||||||
test: 'test'
|
test: 'test'
|
||||||
}
|
}
|
||||||
|
|
||||||
expect(isValidJson(json)).not.toThrowError
|
expect(isValidJson(json)).toBe(json)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should not throw an error with an valid json string', () => {
|
it('should not throw an error with an valid json string', () => {
|
||||||
const json = JSON.stringify({
|
const json = {
|
||||||
test: 'test'
|
test: 'test'
|
||||||
})
|
}
|
||||||
|
|
||||||
expect(isValidJson(json)).not.toThrowError
|
expect(isValidJson(JSON.stringify(json))).toStrictEqual(json)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should throw an error with an invalid json', () => {
|
it('should throw an error with an invalid json', () => {
|
||||||
|
|||||||
@@ -4,9 +4,9 @@
|
|||||||
*/
|
*/
|
||||||
export const isValidJson = (str: string | object) => {
|
export const isValidJson = (str: string | object) => {
|
||||||
try {
|
try {
|
||||||
if (typeof str === 'object') return
|
if (typeof str === 'object') return str
|
||||||
|
|
||||||
str = JSON.parse(str)
|
return JSON.parse(str)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
throw new Error('Invalid JSON response.')
|
throw new Error('Invalid JSON response.')
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user