Skip to content

Commit

Permalink
export integrated cookie functionality (#263)
Browse files Browse the repository at this point in the history
  • Loading branch information
Uzlopak authored Nov 25, 2023
1 parent 5f66b9a commit 0c7351c
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 9 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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`):
Expand Down
18 changes: 10 additions & 8 deletions cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,6 @@

'use strict'

/**
* Module exports.
* @public
*/

exports.parse = parse
exports.serialize = serialize

/**
* Module variables.
* @private
Expand Down Expand Up @@ -229,3 +221,13 @@ function tryDecode (str, decode) {
return str
}
}

/**
* Module exports.
* @public
*/

module.exports = {
parse,
serialize
}
3 changes: 3 additions & 0 deletions plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/cookie-module.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions types/plugin.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions types/plugin.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,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);

0 comments on commit 0c7351c

Please sign in to comment.