diff --git a/.taprc b/.taprc index c5acd1e..071f034 100644 --- a/.taprc +++ b/.taprc @@ -1 +1,3 @@ 100: true +check-coverage: true +coverage: true diff --git a/package.json b/package.json index 51e15e7..d5bbbd1 100644 --- a/package.json +++ b/package.json @@ -7,8 +7,12 @@ "scripts": { "lint": "standard | snazzy", "lint:ci": "standard", - "test": "tap test/*.test.js && npm run typescript", - "typescript": "tsd" + "lint:fix": "standard --fix", + "test": "npm run unit && npm run typescript", + "typescript": "tsd", + "unit": "tap -J \"test/*.test.js\"", + "unit:report": "npm run unit -- --coverage-report=html", + "unit:verbose": "npm run unit -- -Rspec" }, "precommit": [ "lint", @@ -35,19 +39,19 @@ }, "homepage": "https://github.com/fastify/fastify-cookie#readme", "devDependencies": { - "@types/node": "^17.0.8", - "fastify": "^3.25.3", + "@types/node": "^17.0.16", + "fastify": "^3.27.1", "pre-commit": "^1.2.2", - "sinon": "^13.0.0", + "sinon": "^13.0.1", "snazzy": "^9.0.0", "standard": "^16.0.4", "tap": "^15.1.6", "tsd": "^0.19.1", - "typescript": "^4.5.4" + "typescript": "^4.5.5" }, "dependencies": { "cookie-signature": "^1.1.0", - "fastify-plugin": "^3.0.0" + "fastify-plugin": "^3.0.1" }, "tsd": { "directory": "test" diff --git a/plugin.d.ts b/plugin.d.ts index b1e8afa..37e274f 100644 --- a/plugin.d.ts +++ b/plugin.d.ts @@ -1,4 +1,4 @@ -/// +/// import { FastifyPluginCallback } from 'fastify'; @@ -8,9 +8,7 @@ declare module 'fastify' { * Unsigns the specified cookie using the secret provided. * @param value Cookie value */ - unsignCookie( - value: string, - ): { + unsignCookie(value: string): { valid: boolean; renew: boolean; value: string | null; @@ -20,9 +18,7 @@ declare module 'fastify' { * @docs https://github.com/fastify/fastify-cookie#manual-cookie-parsing * @param cookieHeader Raw cookie header value */ - parseCookie( - cookieHeader: string - ): { + parseCookie(cookieHeader: string): { [key: string]: string; }; } @@ -37,52 +33,50 @@ declare module 'fastify' { * Unsigns the specified cookie using the secret provided. * @param value Cookie value */ - unsignCookie( - value: string, - ): { + unsignCookie(value: string): { valid: boolean; renew: boolean; value: string | null; }; } - type setCookieWrapper = ( + export type setCookieWrapper = ( name: string, value: string, options?: CookieSerializeOptions - ) => FastifyReply + ) => FastifyReply; interface FastifyReply { - /** - * Set response cookie - * @name setCookie - * @param name Cookie name - * @param value Cookie value - * @param options Serialize options - */ - setCookie: setCookieWrapper; + /** + * Set response cookie + * @name setCookie + * @param name Cookie name + * @param value Cookie value + * @param options Serialize options + */ + setCookie( + name: string, + value: string, + options?: CookieSerializeOptions + ): this; /** * @alias setCookie */ - cookie: setCookieWrapper + cookie(name: string, value: string, options?: CookieSerializeOptions): this; + /** * clear response cookie * @param name Cookie name * @param options Serialize options */ - clearCookie( - name: string, - options?: CookieSerializeOptions - ): FastifyReply; + clearCookie(name: string, options?: CookieSerializeOptions): this; /** * Unsigns the specified cookie using the secret provided. * @param value Cookie value */ - unsignCookie( - value: string, - ): { + unsignCookie(value: string): { valid: boolean; renew: boolean; value: string | null; @@ -110,4 +104,4 @@ export interface FastifyCookieOptions { declare const fastifyCookie: FastifyPluginCallback>; export default fastifyCookie; -export { fastifyCookie } +export { fastifyCookie }; diff --git a/test/plugin.test-d.ts b/test/plugin.test-d.ts index c4a5bb8..cd71114 100644 --- a/test/plugin.test-d.ts +++ b/test/plugin.test-d.ts @@ -1,16 +1,15 @@ -import fastify, { - FastifyInstance, - FastifyPluginCallback, - setCookieWrapper -} from 'fastify'; +import fastify, { FastifyInstance, FastifyPluginCallback, FastifyReply, setCookieWrapper } from 'fastify'; import { Server } from 'http'; import { expectType } from 'tsd'; -import * as fastifyCookieStar from '../'; -import fastifyCookieDefault, { fastifyCookie as fastifyCookieNamed } from '../'; +import * as fastifyCookieStar from '..'; +import fastifyCookieDefault, { + CookieSerializeOptions, + fastifyCookie as fastifyCookieNamed +} from '..'; import cookie, { FastifyCookieOptions } from '../plugin'; -import fastifyCookieCjsImport = require('../'); -const fastifyCookieCjs = require('../'); +import fastifyCookieCjsImport = require('..'); +const fastifyCookieCjs = require('..'); const app: FastifyInstance = fastify(); app.register(fastifyCookieNamed); @@ -24,9 +23,7 @@ app.register(fastifyCookieStar.fastifyCookie); expectType>(fastifyCookieNamed); expectType>(fastifyCookieDefault); expectType>(fastifyCookieCjsImport.default); -expectType>( - fastifyCookieCjsImport.fastifyCookie -); +expectType>(fastifyCookieCjsImport.fastifyCookie); expectType>(fastifyCookieStar.default); expectType>( fastifyCookieStar.fastifyCookie @@ -46,19 +43,21 @@ server.after((_err) => { server.get('/', (request, reply) => { const test = request.cookies.test; - expectType(reply.cookie) - expectType(reply.setCookie) + expectType(reply.cookie); + expectType(reply.setCookie); - reply - .setCookie('test', test, { domain: 'example.com', path: '/' }) - .clearCookie('foo') - .send({ hello: 'world' }); - }) + expectType( + reply + .setCookie('test', test, { domain: 'example.com', path: '/' }) + .clearCookie('foo') + .send({ hello: 'world' }) + ); + }); }); const serverWithHttp2 = fastify({ http2: true }); -serverWithHttp2.register(cookie) +serverWithHttp2.register(cookie); serverWithHttp2.after(() => { serverWithHttp2.get('/', (request, reply) => { @@ -67,108 +66,98 @@ serverWithHttp2.after(() => { .setCookie('test', test, { domain: 'example.com', path: '/' }) .clearCookie('foo') .send({ hello: 'world' }); - }) + }); }); const testSamesiteOptionsApp = fastify(); -testSamesiteOptionsApp.register(cookie) +testSamesiteOptionsApp.register(cookie); testSamesiteOptionsApp.after(() => { server.get('/test-samesite-option-true', (request, reply) => { const test = request.cookies.test; - reply - .setCookie('test', test, { sameSite: true }) - .send({ hello: 'world' }); - }) + reply.setCookie('test', test, { sameSite: true }).send({ hello: 'world' }); + }); server.get('/test-samesite-option-false', (request, reply) => { const test = request.cookies.test; - reply - .setCookie('test', test, { sameSite: false }) - .send({ hello: 'world' }); - }) + reply.setCookie('test', test, { sameSite: false }).send({ hello: 'world' }); + }); server.get('/test-samesite-option-lax', (request, reply) => { const test = request.cookies.test; - reply - .setCookie('test', test, { sameSite: 'lax' }) - .send({ hello: 'world' }); - }) + reply.setCookie('test', test, { sameSite: 'lax' }).send({ hello: 'world' }); + }); server.get('/test-samesite-option-strict', (request, reply) => { const test = request.cookies.test; reply .setCookie('test', test, { sameSite: 'strict' }) .send({ hello: 'world' }); - }) + }); server.get('/test-samesite-option-none', (request, reply) => { const test = request.cookies.test; reply .setCookie('test', test, { sameSite: 'none' }) .send({ hello: 'world' }); - }) + }); }); const appWithImplicitHttpSigned = fastify(); -appWithImplicitHttpSigned - .register(cookie, { - secret: 'testsecret' - }) +appWithImplicitHttpSigned.register(cookie, { + secret: 'testsecret', +}); appWithImplicitHttpSigned.after(() => { server.get('/', (request, reply) => { - appWithImplicitHttpSigned.unsignCookie(request.cookies.test) - appWithImplicitHttpSigned.unsignCookie('test') + appWithImplicitHttpSigned.unsignCookie(request.cookies.test); + appWithImplicitHttpSigned.unsignCookie('test'); - reply.unsignCookie(request.cookies.test) - reply.unsignCookie('test') + reply.unsignCookie(request.cookies.test); + reply.unsignCookie('test'); request.unsignCookie(request.cookies.anotherTest); request.unsignCookie('anotherTest'); - reply - .send({ hello: 'world' }); - }) + reply.send({ hello: 'world' }); + }); }); -const appWithRotationSecret = fastify() +const appWithRotationSecret = fastify(); -appWithRotationSecret - .register(cookie, { - secret: ['testsecret'] - }) +appWithRotationSecret.register(cookie, { + secret: ['testsecret'], +}); appWithRotationSecret.after(() => { server.get('/', (request, reply) => { - reply.unsignCookie(request.cookies.test) - const { valid, renew, value } = reply.unsignCookie('test') + reply.unsignCookie(request.cookies.test); + const { valid, renew, value } = reply.unsignCookie('test'); - expectType(valid) - expectType(renew) - expectType(value) + expectType(valid); + expectType(renew); + expectType(value); - reply - .send({ hello: 'world' }); - }) -}) + reply.send({ hello: 'world' }); + }); +}); const appWithParseOptions = fastify(); const parseOptions: fastifyCookieStar.CookieSerializeOptions = { - domain: "example.com", + domain: 'example.com', encode: (value: string) => value, expires: new Date(), httpOnly: true, maxAge: 3600, - path: "/", - sameSite: "lax", + path: '/', + sameSite: 'lax', secure: true, signed: true, }; expectType(parseOptions); appWithParseOptions.register(cookie, { - secret: "testsecret", + secret: 'testsecret', parseOptions, }); appWithParseOptions.after(() => { - server.get("/", (request, reply) => { + server.get('/', (request, reply) => { const { valid, renew, value } = reply.unsignCookie(request.cookies.test); expectType(valid);