core/middleware/cookie.js

39 lines
730 B
JavaScript
Raw Normal View History

2020-09-15 18:35:00 +08:00
/**
*
* @authors yutent (yutent@doui.cc)
* @date 2018-05-26 00:01:00
* @version $Id$
*/
2020-09-16 14:08:06 +08:00
import Cookie from 'http.cookie'
2020-09-15 18:35:00 +08:00
2020-09-16 14:08:06 +08:00
export default function(req, res, next) {
2020-09-15 18:35:00 +08:00
var cookie = new Cookie(req.origin.req, req.origin.res)
var domain = this.get('domain')
this.__INSTANCE__.cookie = function(key, val, opt) {
if (typeof key !== 'string') {
throw new Error(
`argument key must be a string in cookie() ${typeof key} given`
)
}
if (arguments.length === 1) {
return cookie.get(key)
}
if (!opt) {
opt = {}
}
opt.domain = opt.domain || domain
val += ''
if (!val) {
opt.expires = opt.maxAge = -1
}
cookie.set(key, val, opt)
}
next()
}