mirror of
https://github.com/sasjs/adapter.git
synced 2026-01-05 11:40:06 +00:00
14773
package-lock.json
generated
14773
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -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
|
||||||
}
|
}
|
||||||
|
|||||||
31
src/test/utils/isValidJson.spec.ts
Normal file
31
src/test/utils/isValidJson.spec.ts
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
import { isValidJson } from '../../utils'
|
||||||
|
|
||||||
|
describe('jsonValidator', () => {
|
||||||
|
it('should not throw an error with an valid json', () => {
|
||||||
|
const json = {
|
||||||
|
test: 'test'
|
||||||
|
}
|
||||||
|
|
||||||
|
expect(isValidJson(json)).toBe(json)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should not throw an error with an valid json string', () => {
|
||||||
|
const json = {
|
||||||
|
test: 'test'
|
||||||
|
}
|
||||||
|
|
||||||
|
expect(isValidJson(JSON.stringify(json))).toStrictEqual(json)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should throw an error with an invalid json', () => {
|
||||||
|
const json = `{\"test\":\"test\"\"test2\":\"test\"}`
|
||||||
|
|
||||||
|
expect(() => {
|
||||||
|
try {
|
||||||
|
isValidJson(json)
|
||||||
|
} catch (err) {
|
||||||
|
throw new Error()
|
||||||
|
}
|
||||||
|
}).toThrowError
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -2,9 +2,11 @@
|
|||||||
* Checks if string is in valid JSON format else throw error.
|
* Checks if string is in valid JSON format else throw error.
|
||||||
* @param str - string to check.
|
* @param str - string to check.
|
||||||
*/
|
*/
|
||||||
export const isValidJson = (str: string) => {
|
export const isValidJson = (str: string | object) => {
|
||||||
try {
|
try {
|
||||||
JSON.parse(str)
|
if (typeof str === 'object') return 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