Skip to content
This repository has been archived by the owner on Dec 10, 2021. It is now read-only.

[core] add parseMethod #5

Merged
merged 9 commits into from
Sep 18, 2018
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 }) {
Copy link
Contributor

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?

Copy link
Contributor

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.

Copy link
Contributor Author

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 but get is not). we could decrease the char limit if you feel strongly about it tho.

Copy link
Contributor

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.

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