-
Notifications
You must be signed in to change notification settings - Fork 2
/
cookies.js
35 lines (30 loc) · 899 Bytes
/
cookies.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
var middleware = require('./middleware')
function storeCookies (cookies, url, header) {
if (header) {
var headers =
header instanceof Array
? header
: [header]
headers.forEach(function (setCookieHeader) {
cookies.setCookieSync(setCookieHeader, url)
})
}
}
module.exports = middleware('cookies', function (request, next, client) {
var cookies
if (client._options.cookies === true) {
var toughCookie = require('tough-cookie')
cookies = request.options.cookies = client._options.cookies = new toughCookie.CookieJar()
} else {
cookies = request.options.cookies
}
if (cookies) {
request.headers.cookie = cookies.getCookieStringSync(request.url)
return next().then(function (response) {
storeCookies(cookies, response.url, response.headers['set-cookie'])
return response
})
} else {
return next()
}
})