@3846masa/axios-cookiejar-support
Advanced tools
Comparing version 0.0.2 to 0.0.3
@@ -16,3 +16,4 @@ 'use strict'; | ||
}) | ||
.then(({ data }) => { | ||
.then((response) => { | ||
const data = response.data; | ||
console.log(data.headers.cookie); | ||
@@ -19,0 +20,0 @@ }) |
@@ -14,3 +14,4 @@ 'use strict'; | ||
axios.get('https://google.com') | ||
.then(({ config }) => { | ||
.then((response) => { | ||
const config = response.config; | ||
// axios.defaults.jar === config.jar | ||
@@ -17,0 +18,0 @@ console.log(config.jar.toJSON()); |
@@ -15,3 +15,4 @@ 'use strict'; | ||
}) | ||
.then(({ config }) => { | ||
.then((response) => { | ||
const config = response.config; | ||
console.log(config.jar.toJSON()); | ||
@@ -18,0 +19,0 @@ }) |
14
index.js
@@ -19,15 +19,19 @@ 'use strict'; | ||
instance.request = function request (config) { | ||
config._COOKIEJAR_SUPPORT_LOCAL = | ||
config._COOKIEJAR_SUPPORT_LOCAL || {}; | ||
const local = config._COOKIEJAR_SUPPORT_LOCAL; | ||
if (instance.defaults.jar === true) { | ||
instance.defaults.jar = new tough.CookieJar(); | ||
} | ||
if (!config.$$__jar) { | ||
if (!local.jar) { | ||
if (config.jar === true) { | ||
config.$$__jar = (instance.defaults.jar || new tough.CookieJar()); | ||
local.jar = (instance.defaults.jar || new tough.CookieJar()); | ||
} else if (config.jar === false) { | ||
config.$$__jar = false; | ||
local.jar = false; | ||
} else { | ||
config.$$__jar = (config.jar || instance.defaults.jar); | ||
local.jar = (config.jar || instance.defaults.jar); | ||
} | ||
} | ||
return origRequest(config); | ||
@@ -34,0 +38,0 @@ }; |
@@ -5,23 +5,29 @@ 'use strict'; | ||
return Promise.resolve(config) | ||
.then(function backupOriginalOptions (config) { | ||
config.$$__orig = config.$$__orig || {}; | ||
.then(function backupOriginalConfigs (config) { | ||
const local = config._COOKIEJAR_SUPPORT_LOCAL; | ||
local.backupOptions = local.backupOptions || {}; | ||
return config; | ||
}) | ||
.then(function redirectSetup (config) { | ||
config.$$__redirectCount = | ||
const local = config._COOKIEJAR_SUPPORT_LOCAL; | ||
local.redirectCount = | ||
isFinite(config.maxRedirects) ? config.maxRedirects : 5; | ||
config.$$__orig.maxRedirects = | ||
config.$$__orig.maxRedirects || config.maxRedirects; | ||
local.backupOptions.maxRedirects = | ||
local.backupOptions.maxRedirects || config.maxRedirects; | ||
config.$$__orig.validateStatus = | ||
config.$$__orig.validateStatus || config.validateStatus; | ||
local.backupOptions.validateStatus = | ||
local.backupOptions.validateStatus || config.validateStatus; | ||
config.maxRedirects = 0; | ||
config.validateStatus = undefined; | ||
return config; | ||
}) | ||
.then(function cookieSetup (config) { | ||
if (config.$$__jar && config.withCredentials) { | ||
const cookieString = config.$$__jar.getCookieStringSync(config.url); | ||
const local = config._COOKIEJAR_SUPPORT_LOCAL; | ||
if (local.jar && config.withCredentials) { | ||
const cookieString = local.jar.getCookieStringSync(config.url); | ||
if (cookieString) { | ||
@@ -34,2 +40,3 @@ config.headers['Cookie'] = | ||
} | ||
return config; | ||
@@ -36,0 +43,0 @@ }); |
@@ -9,3 +9,4 @@ 'use strict'; | ||
const config = response.config; | ||
if (!config.$$__jar || !response.headers['set-cookie']) { | ||
const local = config._COOKIEJAR_SUPPORT_LOCAL; | ||
if (!local.jar || !response.headers['set-cookie']) { | ||
return response; | ||
@@ -17,7 +18,7 @@ } | ||
cookies.forEach(function (cookie) { | ||
return config.$$__jar.setCookieSync(cookie, config.url); | ||
local.jar.setCookieSync(cookie, config.url); | ||
}); | ||
} else { | ||
const cookie = response.headers['set-cookie']; | ||
config.$$__jar.setCookieSync(cookie, config.url); | ||
local.jar.setCookieSync(cookie, config.url); | ||
} | ||
@@ -29,9 +30,10 @@ | ||
const config = response.config; | ||
const local = config._COOKIEJAR_SUPPORT_LOCAL; | ||
config.$$__orig.baseURL = | ||
config.$$__orig.baseURL || config.baseURL; | ||
config.$$__orig.url = | ||
config.$$__orig.url || config.url; | ||
config.$$__orig.method = | ||
config.$$__orig.method || config.method; | ||
local.backupOptions.baseURL = | ||
local.backupOptions.baseURL || config.baseURL; | ||
local.backupOptions.url = | ||
local.backupOptions.url || config.url; | ||
local.backupOptions.method = | ||
local.backupOptions.method || config.method; | ||
@@ -41,3 +43,3 @@ config.baseURL = undefined; | ||
config.$$__redirectCount--; | ||
local.redirectCount--; | ||
@@ -48,4 +50,5 @@ return response; | ||
const config = response.config; | ||
const local = config._COOKIEJAR_SUPPORT_LOCAL; | ||
if (config.$$__redirectCount < 0 || !response.headers['location']) { | ||
if (local.redirectCount < 0 || !response.headers['location']) { | ||
return response; | ||
@@ -61,8 +64,9 @@ } | ||
const config = response.config; | ||
if (!config.$$__jar) return response; | ||
const local = config._COOKIEJAR_SUPPORT_LOCAL; | ||
if (!local || !local.jar) return response; | ||
if (instance.defaults.jar && (!config.jar || config.jar === true)) { | ||
instance.defaults.jar = config.$$__jar; | ||
instance.defaults.jar = local.jar; | ||
} | ||
config.jar = config.$$__jar; | ||
config.jar = local.jar; | ||
@@ -73,13 +77,15 @@ return response; | ||
const config = response.config; | ||
const local = config._COOKIEJAR_SUPPORT_LOCAL; | ||
if (!local) return response; | ||
for (let key in config.$$__orig) { | ||
config[key] = config.$$__orig[key]; | ||
for (let key in local.backupOptions) { | ||
config[key] = local.backupOptions[key]; | ||
} | ||
for (let key in config.$$__orig) { | ||
if (!key.match(/^\$\$__/)) continue; | ||
delete config[key]; | ||
} | ||
return response; | ||
}) | ||
.then(function deleteLocals (response) { | ||
delete response.config._COOKIEJAR_SUPPORT_LOCAL; | ||
return response; | ||
}) | ||
.then(function validate (response) { | ||
@@ -86,0 +92,0 @@ return new Promise(function (resolve, reject) { |
{ | ||
"name": "@3846masa/axios-cookiejar-support", | ||
"description": "Add tough-cookie support to axios.", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"author": "3846masa", | ||
@@ -52,4 +52,5 @@ "browser": { | ||
"postpublish": "greenkeeper-postpublish", | ||
"postversion": "git commit --amend --allow-empty -m \"[skip ci] `git log -1 --pretty=%s`\"", | ||
"test": "mocha --require intelli-espower-loader" | ||
} | ||
} |
@@ -9,10 +9,24 @@ # axios-cookiejar-support | ||
------ | ||
[![NPM](https://img.shields.io/npm/v/@3846masa/axios-cookiejar-support.svg?style=flat-square)][npm] | ||
[![LICENSE](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)][license] | ||
[![CircleCI](https://img.shields.io/circleci/project/3846masa/axios-cookiejar-support/master.svg?style=flat-square)][circleci] | ||
[![NPM][npm-badge]][npm] | ||
[![LICENSE][license-badge]][license] | ||
[![CircleCI][circleci-badge]][circleci] | ||
[![dependencies][dependencies-badge]][dependencies-david] | ||
[![peerdependencies][peerdependencies-badge]][peerdependencies-david] | ||
[![devdependencies][devdependencies-badge]][devdependencies-david] | ||
[npm]: https://www.npmjs.com/package/@3846masa/axios-cookiejar-support | ||
[license]: https://3846masa.mit-license.org | ||
[circleci]: https://circleci.com/gh/3846masa/axios-cookiejar-support | ||
[dependencies-david]: https://david-dm.org/3846masa/axios-cookiejar-support?view=list | ||
[peerdependencies-david]: https://david-dm.org/3846masa/axios-cookiejar-support?type=peer&view=list | ||
[devdependencies-david]: https://david-dm.org/3846masa/axios-cookiejar-support?type=dev&view=list | ||
[npm-badge]: https://img.shields.io/npm/v/@3846masa/axios-cookiejar-support.svg?style=flat-square&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgBAMAAACBVGfHAAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAK7OHOkAAAAbUExURcwAAOeIiP////G7u/ri4tIZGdpFReJsbPC3t075sZwAAAAvSURBVCjPY2CgDWAThIMEsACjEhwIUCZg0dGCIqASwMAxMgXAgSzOwMAOC2TqAwBvzR4JxLaP0gAAAABJRU5ErkJggg== | ||
[license-badge]: https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQBAMAAADt3eJSAAAAIGNIUk0AAHomAACAhAAA%2BgAAAIDoAAB1MAAA6mAAADqYAAAXcJy6UTwAAAAVUExURSBTICJcIiNgIiZoJTuhNyt3Kf///%2BCqxSgAAAAGdFJOUwpclbn%2B4Fj6/H8AAAABYktHRAZhZrh9AAAACXBIWXMAAA3XAAAN1wFCKJt4AAAAB3RJTUUH4AkEEjEV7MDQQwAAAGBJREFUCNc1TUEKgDAMi07vE/Q%2BRD8g%2B4BbvAvi/79iMjDQJm1CC6BbDzRsZI3incIpYeYFhCaYnLiyPYnYkwWZFWoFHrSuttCmmbwXh0eJQYVON4JthZTxCzzAmyb8%2BAAKXBRyN6RyZQAAAABJRU5ErkJggg== | ||
[circleci-badge]: https://img.shields.io/circleci/project/3846masa/axios-cookiejar-support/master.svg?style=flat-square&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQEAYAAABPYyMiAAAAIGNIUk0AAHomAACAhAAA%2BgAAAIDoAAB1MAAA6mAAADqYAAAXcJy6UTwAAAAGYktHRP///////wlY99wAAAAHdElNRQfgCQQSJS8EYt6kAAAAiklEQVRIx2M41nqs9Xi9WDQh%2BjQQnDnz%2BS5x9KS9xJrLgN/CSXtJs5h0BzHQ1mLCDmEgL4jJpyl0AOG4JTYE4Q6gdqIi1UMM2F1OrE9xBy2xDmGgrs8wHU5nB2CGBNEOQPcBoaigThQA08AgS4QDkA3pXRChe4SBskqHiiUhreoEsmtDyhxEfKIFAG4yoGuqR9fTAAAAAElFTkSuQmCC | ||
[dependencies-badge]: https://img.shields.io/david/3846masa/axios-cookiejar-support.svg?style=flat-square&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgBAMAAACBVGfHAAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAK7OHOkAAAAbUExURcwAAOeIiP////G7u/ri4tIZGdpFReJsbPC3t075sZwAAAAvSURBVCjPY2CgDWAThIMEsACjEhwIUCZg0dGCIqASwMAxMgXAgSzOwMAOC2TqAwBvzR4JxLaP0gAAAABJRU5ErkJggg== | ||
[peerdependencies-badge]: https://img.shields.io/david/peer/3846masa/axios-cookiejar-support.svg?style=flat-square&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgBAMAAACBVGfHAAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAK7OHOkAAAAbUExURcwAAOeIiP////G7u/ri4tIZGdpFReJsbPC3t075sZwAAAAvSURBVCjPY2CgDWAThIMEsACjEhwIUCZg0dGCIqASwMAxMgXAgSzOwMAOC2TqAwBvzR4JxLaP0gAAAABJRU5ErkJggg== | ||
[devdependencies-badge]: https://img.shields.io/david/dev/3846masa/axios-cookiejar-support.svg?style=flat-square&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgBAMAAACBVGfHAAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAK7OHOkAAAAbUExURcwAAOeIiP////G7u/ri4tIZGdpFReJsbPC3t075sZwAAAAvSURBVCjPY2CgDWAThIMEsACjEhwIUCZg0dGCIqASwMAxMgXAgSzOwMAOC2TqAwBvzR4JxLaP0gAAAABJRU5ErkJggg== | ||
## Install | ||
@@ -44,2 +58,20 @@ | ||
See [examples](./example). | ||
### Extended Request Config | ||
c.f.) https://github.com/mzabriskie/axios#request-config | ||
```js | ||
{ | ||
// `jar` is tough.CookieJar instance or boolean. | ||
// If true, axios create CookieJar automatically. | ||
jar: undefined, // default | ||
// **IMPORTANT** | ||
// If false, axios DONOT send cookies from cookiejar. | ||
withCredentials: false // default | ||
} | ||
``` | ||
### Browser | ||
@@ -65,3 +97,5 @@ | ||
![3846masa icon](https://www.gravatar.com/avatar/cfeae69aae4f4fc102960f01d35d2d86?s=50) | ||
![3846masa icon][3846masa-icon] | ||
[3846masa](https://github.com/3846masa) | ||
[3846masa-icon]: https://www.gravatar.com/avatar/cfeae69aae4f4fc102960f01d35d2d86?s=50 |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
26641
535
99
0