-
Notifications
You must be signed in to change notification settings - Fork 272
[core] add parseMethod
#5
Changes from 7 commits
21b5074
397240e
560f30b
b01e770
e138fc2
a19871c
c8c3558
fc7e7db
85a1bdd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
.eslintrc.js | ||
.idea | ||
.npm | ||
.npmrc | ||
.prettierignore | ||
.yarnclean | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,29 @@ | ||
export default function parseResponse(apiPromise) { | ||
return apiPromise.then(apiResponse => | ||
const PARSERS = { | ||
json: response => | ||
// first try to parse as json, and fall back to text (e.g., in the case of HTML stacktrace) | ||
// cannot fall back to .text() without cloning the response because body is single-use | ||
apiResponse | ||
response | ||
.clone() | ||
.json() | ||
.catch(() => /* jsonParseError */ apiResponse.text().then(textPayload => ({ textPayload }))) | ||
.then(maybeJson => ({ | ||
json: maybeJson.textPayload ? undefined : maybeJson, | ||
response: apiResponse, | ||
text: maybeJson.textPayload, | ||
})) | ||
.then(({ response, json, text }) => { | ||
if (!response.ok) { | ||
return Promise.reject({ | ||
error: response.error || (json && json.error) || text || 'An error occurred', | ||
status: response.status, | ||
statusText: response.statusText, | ||
}); | ||
} | ||
.then(json => ({ json, response })) | ||
.catch(() => /* jsonParseError */ response.text().then(text => ({ response, text }))), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this mean the developer always have to check if Also, generally, does one expect this Promise to reject when fail to parse or resolve text of the supposed-to-be json? IMO, I would expect it to fail and give me the text in the error message for debugging. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah now that we expose the parse method and are explicit about There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this broke a lot of tests, which were falling back to |
||
|
||
return typeof text === 'undefined' ? { json, response } : { response, text }; | ||
}), | ||
); | ||
text: response => response.text().then(text => ({ response, text })), | ||
}; | ||
|
||
export default function parseResponse(apiPromise, parseMethod = 'json') { | ||
const responseParser = PARSERS[parseMethod] || PARSERS.json; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can developer pass There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought about that but am not sure a free-form parser makes sense. There are only a few available on the Response object, of which we are exposing It doesn't seem worthwhile to copy / mirror the full response API to me (would begin questioning the need for this lib). One option could be to allow There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sgtm |
||
|
||
return apiPromise.then(responseParser).then(({ json, response, text }) => { | ||
// HTTP 404 or 500 are not rejected, ok is just set to false | ||
if (!response.ok) { | ||
return Promise.reject({ | ||
error: response.error || (json && json.error) || text || 'An error occurred', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps should separate error handling for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. return apiPromise
.then((response) => {
if (!response.ok) {
return Promise.reject({
error: response.error,
status: response.status,
statusText: response.statusText,
});
}
return response;
})
.then(responseParser) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I dig the latter^ |
||
status: response.status, | ||
statusText: response.statusText, | ||
}); | ||
} | ||
|
||
return { json, response, text }; | ||
}); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It starts to get long, do you want to separate into one param per line?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh just notice
post
is already doing that, then maybe good to spread this one out too.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's all based on prettier config, it auto wraps if it's over the 100 char limit (which
post
is butget
is not). we could decrease the char limit if you feel strongly about it tho.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nope. this is pretty minor. I'll leave it to you.