mirror of
https://github.com/cupcakearmy/koa-cookie.git
synced 2024-12-22 16:16:29 +00:00
Added Package.json
This commit is contained in:
parent
f67a950229
commit
bdcf84f686
53
Cookie.js
Normal file
53
Cookie.js
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
module.exports = async(c, n) => {
|
||||||
|
let cObj = {}
|
||||||
|
if (typeof c.request.header.cookie === 'string')
|
||||||
|
c.request.header.cookie.split(';').forEach(v => {
|
||||||
|
const tmp = v.trim().split('=')
|
||||||
|
cObj[unescape(tmp[0])] = unescape(tmp[1])
|
||||||
|
})
|
||||||
|
|
||||||
|
// Save them to the context
|
||||||
|
c.request.cookies = cObj
|
||||||
|
|
||||||
|
// Getter and Setter
|
||||||
|
c.request.cookie = {
|
||||||
|
get: (key) => {
|
||||||
|
return c.request.cookies[key]
|
||||||
|
},
|
||||||
|
/**
|
||||||
|
* {String} key
|
||||||
|
* {String} value
|
||||||
|
* {String} path/
|
||||||
|
* {String} maxAge seconds | if not set cookie will be deleted when the browser get closed
|
||||||
|
* {String} httpOnly bool
|
||||||
|
* {String} secure bool
|
||||||
|
* {String} key
|
||||||
|
*/
|
||||||
|
set: (opt) => {
|
||||||
|
// Add the minimal information
|
||||||
|
let cookie = `${opt.key}=${opt.value}`
|
||||||
|
|
||||||
|
// Optional Flags
|
||||||
|
// Max-Age
|
||||||
|
if (opt.maxAge !== undefined && parseInt(opt.maxAge) > 0)
|
||||||
|
cookie += `; Max-Age=${parseInt(opt.maxAge)}`
|
||||||
|
// Domain
|
||||||
|
if (opt.domain !== undefined)
|
||||||
|
cookie += `; Domain=${opt.domain}`
|
||||||
|
// Path
|
||||||
|
if (opt.path !== undefined)
|
||||||
|
cookie += `; Path=${opt.path}`
|
||||||
|
// HttpOnly
|
||||||
|
if (opt.httpOnly !== undefined && Boolean(opt.httpOnly))
|
||||||
|
cookie += `; HttpOnly`
|
||||||
|
// Secure
|
||||||
|
if (opt.secure !== undefined && Boolean(opt.secure))
|
||||||
|
cookie += `; Secure`
|
||||||
|
|
||||||
|
// Set The final cookie
|
||||||
|
c.set('Set-Cookie', cookie)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
await n()
|
||||||
|
}
|
24
package.json
Normal file
24
package.json
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
{
|
||||||
|
"name": "cca-koa-cookie",
|
||||||
|
"version": "0.1.0",
|
||||||
|
"description": "Cookie Parser and Setter for Koa",
|
||||||
|
"main": "Cookie.js",
|
||||||
|
"scripts": {
|
||||||
|
"test": "echo \"Error: no test specified\" && exit 1"
|
||||||
|
},
|
||||||
|
"repository": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "git+https://github.com/CupCakeArmy/koa-cookie.git"
|
||||||
|
},
|
||||||
|
"keywords": [
|
||||||
|
"Koa",
|
||||||
|
"Cookie",
|
||||||
|
"cca"
|
||||||
|
],
|
||||||
|
"author": "Niccolo Borgioli",
|
||||||
|
"license": "ISC",
|
||||||
|
"bugs": {
|
||||||
|
"url": "https://github.com/CupCakeArmy/koa-cookie/issues"
|
||||||
|
},
|
||||||
|
"homepage": "https://github.com/CupCakeArmy/koa-cookie#readme"
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user