Skip to content
This repository has been archived by the owner on Oct 21, 2022. It is now read-only.

Commit

Permalink
investigating the validateStatus function
Browse files Browse the repository at this point in the history
  • Loading branch information
taylordowns2000 committed Feb 4, 2021
1 parent 31fe0ec commit 2ded71f
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 29 deletions.
26 changes: 15 additions & 11 deletions lib/Adaptor.js
Expand Up @@ -131,32 +131,36 @@ axios.interceptors.request.use(config => {
});

function handleCookies(response) {
var _response$config;
const {
config,
data,
headers
} = response;

if ((_response$config = response.config) === null || _response$config === void 0 ? void 0 : _response$config.keepCookie) {
if (config.keepCookie) {
let cookies;
let keepCookies = [];

if (response.headers['set-cookie']) {
var _response$headers$set2;
if (headers['set-cookie']) {
var _headers$setCookie2;

if (response.headers['set-cookie'] instanceof Array) {
var _response$headers$set;
if (headers['set-cookie'] instanceof Array) {
var _headers$setCookie;

cookies = (_response$headers$set = response.headers['set-cookie']) === null || _response$headers$set === void 0 ? void 0 : _response$headers$set.map(Cookie.parse);
cookies = (_headers$setCookie = headers['set-cookie']) === null || _headers$setCookie === void 0 ? void 0 : _headers$setCookie.map(Cookie.parse);
} else {
cookies = [Cookie.parse(response.headers['set-cookie'])];
cookies = [Cookie.parse(headers['set-cookie'])];
}

(_response$headers$set2 = response.headers['set-cookie']) === null || _response$headers$set2 === void 0 ? void 0 : _response$headers$set2.forEach(c => {
cookiejar.setCookie(Cookie.parse(c), response.config.url, (err, cookie) => {
(_headers$setCookie2 = headers['set-cookie']) === null || _headers$setCookie2 === void 0 ? void 0 : _headers$setCookie2.forEach(c => {
cookiejar.setCookie(Cookie.parse(c), config.url, (err, cookie) => {
keepCookies === null || keepCookies === void 0 ? void 0 : keepCookies.push(cookie === null || cookie === void 0 ? void 0 : cookie.cookieString());
});
});
}

return { ...response,
data: { ...response.data,
data: { ...data,
__cookie: (keepCookies === null || keepCookies === void 0 ? void 0 : keepCookies.length) === 1 ? keepCookies[0] : keepCookies,
__headers: response.headers
}
Expand Down
6 changes: 4 additions & 2 deletions lib/Utils.js
Expand Up @@ -111,7 +111,7 @@ function mapToAxiosConfig(requestConfig) {
}
}

return { ...requestConfig,
const finalConfig = { ...requestConfig,
url: (_requestConfig$url = (_requestConfig8 = requestConfig) === null || _requestConfig8 === void 0 ? void 0 : _requestConfig8.url) !== null && _requestConfig$url !== void 0 ? _requestConfig$url : (_requestConfig9 = requestConfig) === null || _requestConfig9 === void 0 ? void 0 : _requestConfig9.uri,
// https:
// requestConfig?.https ??
Expand Down Expand Up @@ -140,9 +140,10 @@ function mapToAxiosConfig(requestConfig) {
// onDownloadProgress,
// maxContentLength,
// maxBodyLength,
validateStatus: function (status) {
validateStatus: status => {
var _requestConfig20, _requestConfig20$opti, _requestConfig20$opti2;

console.log('Status received by axios', status);
return status >= 200 && status < 300 || ((_requestConfig20 = requestConfig) === null || _requestConfig20 === void 0 ? void 0 : (_requestConfig20$opti = _requestConfig20.options) === null || _requestConfig20$opti === void 0 ? void 0 : (_requestConfig20$opti2 = _requestConfig20$opti.successCodes) === null || _requestConfig20$opti2 === void 0 ? void 0 : _requestConfig20$opti2.includes(status));
},
maxRedirects: (_requestConfig$maxRed = (_requestConfig21 = requestConfig) === null || _requestConfig21 === void 0 ? void 0 : _requestConfig21.maxRedirects) !== null && _requestConfig$maxRed !== void 0 ? _requestConfig$maxRed : ((_requestConfig22 = requestConfig) === null || _requestConfig22 === void 0 ? void 0 : _requestConfig22.followAllRedirects) === false ? 0 : 5 // socketPath,
Expand All @@ -153,4 +154,5 @@ function mapToAxiosConfig(requestConfig) {
// decompress,

};
return finalConfig;
}
25 changes: 11 additions & 14 deletions src/Adaptor.js
Expand Up @@ -56,32 +56,29 @@ axios.interceptors.request.use(config => {
});

function handleCookies(response) {
if (response.config?.keepCookie) {
const { config, data, headers } = response;
if (config.keepCookie) {
let cookies;
let keepCookies = [];

if (response.headers['set-cookie']) {
if (response.headers['set-cookie'] instanceof Array) {
cookies = response.headers['set-cookie']?.map(Cookie.parse);
if (headers['set-cookie']) {
if (headers['set-cookie'] instanceof Array) {
cookies = headers['set-cookie']?.map(Cookie.parse);
} else {
cookies = [Cookie.parse(response.headers['set-cookie'])];
cookies = [Cookie.parse(headers['set-cookie'])];
}

response.headers['set-cookie']?.forEach(c => {
cookiejar.setCookie(
Cookie.parse(c),
response.config.url,
(err, cookie) => {
keepCookies?.push(cookie?.cookieString());
}
);
headers['set-cookie']?.forEach(c => {
cookiejar.setCookie(Cookie.parse(c), config.url, (err, cookie) => {
keepCookies?.push(cookie?.cookieString());
});
});
}

return {
...response,
data: {
...response.data,
...data,
__cookie: keepCookies?.length === 1 ? keepCookies[0] : keepCookies,
__headers: response.headers,
},
Expand Down
7 changes: 5 additions & 2 deletions src/Utils.js
Expand Up @@ -79,7 +79,7 @@ export function mapToAxiosConfig(requestConfig) {
}
}

return {
const finalConfig = {
...requestConfig,
url: requestConfig?.url ?? requestConfig?.uri,
// https:
Expand Down Expand Up @@ -111,7 +111,8 @@ export function mapToAxiosConfig(requestConfig) {
// onDownloadProgress,
// maxContentLength,
// maxBodyLength,
validateStatus: function (status) {
validateStatus: status => {
console.log('Status received by axios', status);
return (
(status >= 200 && status < 300) ||
requestConfig?.options?.successCodes?.includes(status)
Expand All @@ -127,4 +128,6 @@ export function mapToAxiosConfig(requestConfig) {
// cancelToken,
// decompress,
};

return finalConfig;
}

0 comments on commit 2ded71f

Please sign in to comment.