diff --git a/Cookie.js b/Cookie.js new file mode 100644 index 0000000..7dd5652 --- /dev/null +++ b/Cookie.js @@ -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() +} \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..35c0ca8 --- /dev/null +++ b/package.json @@ -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" +}