This repository has been archived by the owner on Dec 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 272
[core] add parseMethod
#5
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
21b5074
[core] expose 'parseMethod' in SupersetClient and callApi for text re…
williaster 397240e
[parseResponse][test] fix parseMethod test
williaster 560f30b
[monorepo] gitignore .npmrc
williaster b01e770
[core][deps] @data-ui/build-config => ^0.0.14
williaster e138fc2
[monorepo] tweak npm scripts
williaster a19871c
[core] clean up parseResponse
williaster c8c3558
[core] clean up parseResponse more
williaster fc7e7db
[core] don't fallback to text parsing if response is supposed to be j…
williaster 85a1bdd
[core][tests] fix borken tests, return promises instead of calling do…
williaster File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
.eslintrc.js | ||
.idea | ||
.npm | ||
.npmrc | ||
.prettierignore | ||
.yarnclean | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,25 @@ | ||
export default function parseResponse(apiPromise) { | ||
return apiPromise.then(apiResponse => | ||
// 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 | ||
.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, | ||
}); | ||
} | ||
const PARSERS = { | ||
json: response => response.json().then(json => ({ json, response })), | ||
text: response => response.text().then(text => ({ response, text })), | ||
}; | ||
|
||
return typeof text === 'undefined' ? { json, response } : { response, text }; | ||
}), | ||
); | ||
export default function parseResponse(apiPromise, parseMethod = 'json') { | ||
if (parseMethod === null) return apiPromise; | ||
|
||
const responseParser = PARSERS[parseMethod] || PARSERS.json; | ||
|
||
return apiPromise | ||
.then(response => { | ||
if (!response.ok) { | ||
return Promise.reject({ | ||
error: response.error || 'An error occurred', | ||
response, | ||
status: response.status, | ||
statusText: response.statusText, | ||
}); | ||
} | ||
|
||
return response; | ||
}) | ||
.then(responseParser); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.