-
Notifications
You must be signed in to change notification settings - Fork 14k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* [core] expose 'parseMethod' in SupersetClient and callApi for text responses * [parseResponse][test] fix parseMethod test * [monorepo] gitignore .npmrc * [core][deps] @data-ui/build-config => ^0.0.14 * [monorepo] tweak npm scripts * [core] clean up parseResponse * [core] clean up parseResponse more * [core] don't fallback to text parsing if response is supposed to be json, allow for null responseParser * [core][tests] fix borken tests, return promises instead of calling done()
- Loading branch information
1 parent
f8c38e9
commit 53c1eb9
Showing
12 changed files
with
319 additions
and
300 deletions.
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
47 changes: 23 additions & 24 deletions
47
.../temporary_superset_ui/superset-ui/packages/superset-ui-core/src/callApi/parseResponse.js
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.