Skip to content

Commit 2649e63

Browse files
authored
Merge pull request #1175 from arty-name/1165-jsYaml
only use YAML parser for YAML responses
2 parents 50d87a3 + e97f426 commit 2649e63

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/http.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@ export default function http(url, request = {}) {
6767
// exported for testing
6868
export const shouldDownloadAsText = (contentType = '') => /json|xml|yaml|text/.test(contentType)
6969

70+
function parseBody(body) {
71+
if (/^\s*\{/.test(body)) {
72+
return JSON.parse(body)
73+
}
74+
return jsYaml.safeLoad(body)
75+
}
76+
7077
// Serialize the response, returns a promise with headers and the body part of the hash
7178
export function serializeRes(oriRes, url, {loadSpec = false} = {}) {
7279
const res = {
@@ -88,8 +95,7 @@ export function serializeRes(oriRes, url, {loadSpec = false} = {}) {
8895

8996
if (useText) {
9097
try {
91-
// Optimistically try to convert all bodies
92-
const obj = jsYaml.safeLoad(body)
98+
const obj = parseBody(body)
9399
res.body = obj
94100
res.obj = obj
95101
}

0 commit comments

Comments
 (0)