From 7053ed1620e68efe6749a6b56cd5d7ca3c6d8f17 Mon Sep 17 00:00:00 2001 From: James Sumners Date: Thu, 16 Nov 2017 14:32:13 -0500 Subject: [PATCH] Clean up useless code and scopes --- plugin.js | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/plugin.js b/plugin.js index e0ac064..2109192 100644 --- a/plugin.js +++ b/plugin.js @@ -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() }