From 082e6b37b1028b4840cea8bb6693387f8b0c476d Mon Sep 17 00:00:00 2001 From: uzlopak Date: Tue, 14 Nov 2023 21:41:04 +0100 Subject: [PATCH 1/5] export integrated cookie functionality --- cookie.js | 18 ++++++++++-------- plugin.js | 2 ++ types/plugin.d.ts | 6 ++++++ types/plugin.test-d.ts | 3 +++ 4 files changed, 21 insertions(+), 8 deletions(-) diff --git a/cookie.js b/cookie.js index 349db26..5b55a4c 100644 --- a/cookie.js +++ b/cookie.js @@ -28,14 +28,6 @@ 'use strict' -/** - * Module exports. - * @public - */ - -exports.parse = parse -exports.serialize = serialize - /** * Module variables. * @private @@ -223,3 +215,13 @@ function tryDecode (str, decode) { return str } } + +/** + * Module exports. + * @public + */ + +module.exports = { + parse, + serialize +} diff --git a/plugin.js b/plugin.js index 81e7432..0953e88 100644 --- a/plugin.js +++ b/plugin.js @@ -214,6 +214,8 @@ module.exports = fastifyCookie module.exports.default = fastifyCookie // supersedes fastifyCookie.default = fastifyCookie module.exports.fastifyCookie = fastifyCookie // supersedes fastifyCookie.fastifyCookie = fastifyCookie +module.exports.cookie = cookie + module.exports.signerFactory = Signer module.exports.Signer = Signer module.exports.sign = sign diff --git a/types/plugin.d.ts b/types/plugin.d.ts index 23e7500..fcd79ab 100644 --- a/types/plugin.d.ts +++ b/types/plugin.d.ts @@ -139,6 +139,10 @@ declare namespace fastifyCookie { signed?: boolean; } + export interface ParseOptions { + decode?: (encodedURIComponent: string) => string; + } + type HookType = 'onRequest' | 'preParsing' | 'preValidation' | 'preHandler' | 'preSerialization'; export interface FastifyCookieOptions { @@ -162,6 +166,8 @@ declare namespace fastifyCookie { export const unsign: Unsign; export interface FastifyCookie extends FastifyCookiePlugin { + parse: (cookieHeader: string, opts?: ParseOptions) => { [key: string]: string }; + serialize: (name: string, value: string, opts?: SerializeOptions) => string; signerFactory: SignerFactory; Signer: Signer; sign: Sign; diff --git a/types/plugin.test-d.ts b/types/plugin.test-d.ts index 12ac223..048c6e9 100644 --- a/types/plugin.test-d.ts +++ b/types/plugin.test-d.ts @@ -231,3 +231,6 @@ appWithHook.register(cookie, { hook: 'preSerialization' }); appWithHook.register(cookie, { hook: 'preValidation' }); expectError(appWithHook.register(cookie, { hook: true })); expectError(appWithHook.register(cookie, { hook: 'false' })); + +expectType<(cookieHeader: string, opts?: fastifyCookieStar.ParseOptions) => { [key: string]: string }>(fastifyCookieDefault.parse); +expectType<(name: string, value: string, opts?: fastifyCookieStar.SerializeOptions) => string>(fastifyCookieDefault.serialize); From 135e434bad4a9b4e4e92c219dbfb68254b207f77 Mon Sep 17 00:00:00 2001 From: uzlopak Date: Wed, 15 Nov 2023 11:50:19 +0100 Subject: [PATCH 2/5] fix --- plugin.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/plugin.js b/plugin.js index 0953e88..01a64ca 100644 --- a/plugin.js +++ b/plugin.js @@ -214,7 +214,8 @@ module.exports = fastifyCookie module.exports.default = fastifyCookie // supersedes fastifyCookie.default = fastifyCookie module.exports.fastifyCookie = fastifyCookie // supersedes fastifyCookie.fastifyCookie = fastifyCookie -module.exports.cookie = cookie +module.exports.serialize = cookie.serialize +module.exports.parse = cookie.parse module.exports.signerFactory = Signer module.exports.Signer = Signer From 975b772a326aa7be39546c21effdf4b8d1bcbb3b Mon Sep 17 00:00:00 2001 From: Aras Abbasi Date: Mon, 20 Nov 2023 11:34:55 +0100 Subject: [PATCH 3/5] require --- test/cookie-module.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/cookie-module.test.js b/test/cookie-module.test.js index e8e10f7..e35a496 100644 --- a/test/cookie-module.test.js +++ b/test/cookie-module.test.js @@ -3,7 +3,7 @@ const tap = require('tap') const test = tap.test -const cookie = require('../cookie') +const cookie = require('..') test('parse: argument validation', (t) => { t.plan(2) From a3398c77d1505b35a7f8fb7c6db847d2ab21459b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCrg=C3=BCn=20Day=C4=B1o=C4=9Flu?= Date: Fri, 24 Nov 2023 15:48:38 +0100 Subject: [PATCH 4/5] add readme --- README.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/README.md b/README.md index 740b6ca..23ea85c 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,8 @@ This plugin's cookie parsing works via Fastify's `onRequest` hook. Therefore, you should register it prior to any other `onRequest` hooks that will depend upon this plugin's actions. +It is also possible to import the low-level cookie parsing and serialization functions. + `@fastify/cookie` [v2.x](https://github.com/fastify/fastify-cookie/tree/v2.x) supports both Fastify@1 and Fastify@2. `@fastify/cookie` v3 only supports Fastify@2. @@ -70,6 +72,23 @@ app.register(cookie, { } as FastifyCookieOptions) ``` +## Importing `serialize` and `parse` + +```js +const { serialize, parse } = require('@fastify/cookie') +const fastify = require('fastify')() + +fastify.get('/', (req, reply) => { + const cookie = serialize('lang', 'en', { + maxAge: 60_000, + }) + + reply.header('Set-Cookie', cookie) + + reply.send('Language set!') +}) +``` + ## Options - `secret` (`String` | `Array` | `Buffer` | `Object`): From 8cf5dfe9e5c05d2f0c45b0172c546390f3b04b5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCrg=C3=BCn=20Day=C4=B1o=C4=9Flu?= Date: Sat, 25 Nov 2023 14:56:08 +0100 Subject: [PATCH 5/5] add link --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 23ea85c..8044e48 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ This plugin's cookie parsing works via Fastify's `onRequest` hook. Therefore, you should register it prior to any other `onRequest` hooks that will depend upon this plugin's actions. -It is also possible to import the low-level cookie parsing and serialization functions. +It is also possible to [import the low-level cookie parsing and serialization functions](#importing-serialize-and-parse). `@fastify/cookie` [v2.x](https://github.com/fastify/fastify-cookie/tree/v2.x) supports both Fastify@1 and Fastify@2.