diff --git a/README.md b/README.md index 740b6ca..8044e48 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](#importing-serialize-and-parse). + `@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`): 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..01a64ca 100644 --- a/plugin.js +++ b/plugin.js @@ -214,6 +214,9 @@ module.exports = fastifyCookie module.exports.default = fastifyCookie // supersedes fastifyCookie.default = fastifyCookie module.exports.fastifyCookie = fastifyCookie // supersedes fastifyCookie.fastifyCookie = fastifyCookie +module.exports.serialize = cookie.serialize +module.exports.parse = cookie.parse + module.exports.signerFactory = Signer module.exports.Signer = Signer module.exports.sign = sign 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) 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);