Skip to content

Commit

Permalink
Clean up useless code and scopes
Browse files Browse the repository at this point in the history
  • Loading branch information
jsumners committed Nov 16, 2017
1 parent f13b9ec commit 7053ed1
Showing 1 changed file with 14 additions and 18 deletions.
32 changes: 14 additions & 18 deletions plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,22 @@
const fp = require('fastify-plugin')
const cookie = require('cookie')

function plugin (fastify, options, next) {
fastify.decorateRequest('cookies', [])

fastify.decorateReply('setCookie', function (name, value, options) {
const seriaized = cookie.serialize(name, value, options || {})
this.header('Set-Cookie', seriaized)
return this
})

fastify.addHook('preHandler', (fastifyReq, fastifyRes, done) => {
if (fastifyReq.cookies.length > 0) done()
if (!fastifyReq.req.headers) fastifyReq.req.headers = {}

const cookieHeader = fastifyReq.req.headers.cookie
const cookies = (cookieHeader) ? cookie.parse(cookieHeader) : []
fastifyReq.cookies = cookies
function fastifyCookieSetCookie (name, value, options) {
const seriaized = cookie.serialize(name, value, options || {})
this.header('Set-Cookie', seriaized)
return this
}

done()
})
function fastifyCookiePreHandler (fastifyReq, fastifyRes, done) {
const cookieHeader = fastifyReq.req.headers.cookie
fastifyReq.cookies = (cookieHeader) ? cookie.parse(cookieHeader) : {}
done()
}

function plugin (fastify, options, next) {
fastify.decorateRequest('cookies', {})
fastify.decorateReply('setCookie', fastifyCookieSetCookie)
fastify.addHook('preHandler', fastifyCookiePreHandler)
next()
}

Expand Down

0 comments on commit 7053ed1

Please sign in to comment.