Skip to content

Commit

Permalink
[core] add parseMethod (apache#5)
Browse files Browse the repository at this point in the history
* [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
williaster authored Sep 18, 2018
1 parent c65d008 commit b0790f2
Show file tree
Hide file tree
Showing 12 changed files with 319 additions and 300 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
.eslintrc.js
.idea
.npm
.npmrc
.prettierignore
.yarnclean

Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
"private": true,
"scripts": {
"build": "lerna run build",
"jest": "lerna run test",
"lint": "lerna run lint",
"lint:fix": "lerna run lint:fix",
"prerelease": "yarn run build",
"prepare-release": "git checkout master && git pull --rebase origin master && yarn run test",
"prepare-release": "git checkout master && git pull --rebase origin master && lerna bootstrap && yarn run lint && yarn run test",
"release": "yarn run prepare-release && lerna publish && lerna run gh-pages",
"test": "lerna bootstrap && yarn run lint && yarn run jest"
"test": "lerna run test"
},
"repository": "https://github.com/apache-superset/superset-ui.git",
"keywords": [
Expand Down
2 changes: 1 addition & 1 deletion packages/superset-ui-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
},
"homepage": "https://github.com/apache-superset/superset-ui#readme",
"devDependencies": {
"@data-ui/build-config": "^0.0.12",
"@data-ui/build-config": "^0.0.14",
"fetch-mock": "^6.5.2",
"node-fetch": "^2.2.0"
},
Expand Down
13 changes: 8 additions & 5 deletions packages/superset-ui-core/src/SupersetClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,15 @@ class SupersetClient {
);
}

get({ host, url, endpoint, mode, credentials, headers, body, timeout, signal }) {
get({ body, credentials, headers, host, endpoint, mode, parseMethod, signal, timeout, url }) {
return this.ensureAuth().then(() =>
callApi({
body,
credentials: credentials || this.credentials,
headers: { ...this.headers, ...headers },
method: 'GET',
mode: mode || this.mode,
parseMethod,
signal,
timeout: timeout || this.timeout,
url: url || this.getUrl({ endpoint, host: host || this.host }),
Expand All @@ -91,23 +92,25 @@ class SupersetClient {
}

post({
credentials,
headers,
host,
endpoint,
url,
mode,
credentials,
headers,
parseMethod,
postPayload,
timeout,
signal,
stringify,
timeout,
url,
}) {
return this.ensureAuth().then(() =>
callApi({
credentials: credentials || this.credentials,
headers: { ...this.headers, ...headers },
method: 'POST',
mode: mode || this.mode,
parseMethod,
postPayload,
signal,
stringify,
Expand Down
1 change: 0 additions & 1 deletion packages/superset-ui-core/src/callApi/callApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export default function callApi({
postPayload,
stringify = true,
redirect = 'follow', // manual, follow, error
timeoutId,
signal, // used for aborting
}) {
let request = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import callApi from './callApi';
import rejectAfterTimeout from './rejectAfterTimeout';
import parseResponse from './parseResponse';

export default function callApiAndParseWithTimeout({ timeout, ...rest }) {
export default function callApiAndParseWithTimeout({ timeout, parseMethod, ...rest }) {
const apiPromise = callApi(rest);

const racedPromise =
typeof timeout === 'number' && timeout > 0
? Promise.race([rejectAfterTimeout(timeout), apiPromise])
: apiPromise;

return parseResponse(racedPromise);
return parseResponse(racedPromise, parseMethod);
}
47 changes: 23 additions & 24 deletions packages/superset-ui-core/src/callApi/parseResponse.js
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);
}
Loading

0 comments on commit b0790f2

Please sign in to comment.