mirror of
https://github.com/sasjs/adapter.git
synced 2026-01-03 18:50:05 +00:00
21 lines
617 B
TypeScript
21 lines
617 B
TypeScript
import { JsonParseArrayError, InvalidJsonError } from '../types/errors'
|
|
|
|
/**
|
|
* if string passed then parse the string to json else if throw error for all other types unless it is not a valid json object.
|
|
* @param str - string to check.
|
|
*/
|
|
export const getValidJson = (str: string | object) => {
|
|
try {
|
|
if (str === null || str === undefined) throw new InvalidJsonError()
|
|
|
|
if (Array.isArray(str)) throw new JsonParseArrayError()
|
|
|
|
if (typeof str === 'object') return str
|
|
|
|
return JSON.parse(str)
|
|
} catch (e) {
|
|
if (e instanceof JsonParseArrayError) throw e
|
|
throw new InvalidJsonError()
|
|
}
|
|
}
|