diff --git a/package.json b/package.json index f681032..56114c6 100644 --- a/package.json +++ b/package.json @@ -4,13 +4,13 @@ "description": "Plugin for fastify to add support for cookies", "main": "plugin.js", "type": "commonjs", - "types": "types/plugin.d.ts", + "types": "types/index.d.ts", "scripts": { "coverage": "npm run test:unit -- --coverage-report=html", "lint": "eslint", "lint:fix": "eslint --fix", "test": "npm run test:unit && npm run test:typescript", - "test:typescript": "tsd", + "test:typescript": "tstyche", "test:unit": "c8 --100 node --test", "test:unit:verbose": "npm run test:unit -- -Rspec" }, @@ -65,16 +65,13 @@ "fastify": "^5.0.0", "neostandard": "^0.13.0", "sinon": "^21.0.0", - "tsd": "^0.33.0" + "tstyche": "^7.0.0" }, "dependencies": { "cookie": "^1.0.0", "fastify-plugin": "^5.0.0" }, - "tsd": { - "directory": "test" - }, "publishConfig": { "access": "public" } -} +} \ No newline at end of file diff --git a/types/plugin.d.ts b/types/index.d.ts similarity index 100% rename from types/plugin.d.ts rename to types/index.d.ts diff --git a/types/plugin.test-d.ts b/types/index.tst.ts similarity index 55% rename from types/plugin.test-d.ts rename to types/index.tst.ts index f610b3e..aad6adb 100644 --- a/types/plugin.test-d.ts +++ b/types/index.tst.ts @@ -1,8 +1,12 @@ -import { expectError, expectType } from 'tsd' +import { expect } from 'tstyche' import * as fastifyCookieStar from '..' import fastifyCookieCjsImport = require('..') import cookie, { fastifyCookie as fastifyCookieNamed, Signer } from '..' -import fastify, { FastifyInstance, FastifyReply, setCookieWrapper } from 'fastify' +import fastify, { + type FastifyInstance, + type FastifyReply, + type setCookieWrapper +} from 'fastify' const fastifyCookieCjs = require('..') @@ -15,61 +19,72 @@ app.register(fastifyCookieCjsImport.fastifyCookie) app.register(fastifyCookieStar.default) app.register(fastifyCookieStar.fastifyCookie) -expectType(fastifyCookieNamed) -expectType(cookie) -expectType(fastifyCookieCjsImport.default) -expectType(fastifyCookieCjsImport.fastifyCookie) -expectType(fastifyCookieStar.default) -expectType( +expect(fastifyCookieNamed).type.toBe() +expect(cookie).type.toBe() +expect( + fastifyCookieCjsImport.default +).type.toBe() +expect( + fastifyCookieCjsImport.fastifyCookie +).type.toBe() +expect(fastifyCookieStar.default).type.toBe() +expect( fastifyCookieStar.fastifyCookie -) -expectType(fastifyCookieCjs) +).type.toBe() +expect(fastifyCookieCjs).type.toBe() -expectType(cookie.sign) -expectType(cookie.unsign) -expectType(cookie.signerFactory) +expect(cookie.sign).type.toBe() +expect(cookie.unsign).type.toBe() +expect(cookie.signerFactory).type.toBe() -expectType(fastifyCookieNamed.sign) -expectType(fastifyCookieNamed.unsign) -expectType(fastifyCookieNamed.signerFactory) +expect(fastifyCookieNamed.sign).type.toBe() +expect(fastifyCookieNamed.unsign).type.toBe() +expect( + fastifyCookieNamed.signerFactory +).type.toBe() const server = fastify() server.register(cookie) server.after((_err) => { - expectType< string >( + expect( server.serializeCookie('sessionId', 'aYb4uTIhdBXC') - ) + ).type.toBe() - expectType<{ [key: string]: string }>( - // See https://github.com/fastify/fastify-cookie#manual-cookie-parsing - server.parseCookie('sessionId=aYb4uTIhdBXC') - ) + expect(server.parseCookie('sessionId=aYb4uTIhdBXC')).type.toBe<{ + [key: string]: string; + }>() server.get('/', (request, reply) => { const test = request.cookies.test - expectType(test) + expect(test).type.toBe() - expectType(reply.cookie) - expectType(reply.setCookie) + expect(reply.cookie).type.toBe() + expect(reply.setCookie).type.toBe() - expectType( + expect( reply .setCookie('test', test!, { domain: 'example.com', path: '/' }) .clearCookie('foo') .send({ hello: 'world' }) - ) + ).type.toBe() }) - expectType<(value: string) => string>(server.signCookie) - expectType<(value: string) => fastifyCookieStar.UnsignResult>(server.unsignCookie) + expect(server.signCookie).type.toBe<(value: string) => string>() + expect(server.unsignCookie).type.toBe< + (value: string) => fastifyCookieStar.UnsignResult + >() server.get('/', (request, reply) => { - expectType<(value: string) => string>(request.signCookie) - expectType<(value: string) => string>(reply.signCookie) - expectType<(value: string) => fastifyCookieStar.UnsignResult>(request.unsignCookie) - expectType<(value: string) => fastifyCookieStar.UnsignResult>(reply.unsignCookie) + expect(request.signCookie).type.toBe<(value: string) => string>() + expect(reply.signCookie).type.toBe<(value: string) => string>() + expect(request.unsignCookie).type.toBe< + (value: string) => fastifyCookieStar.UnsignResult + >() + expect(reply.unsignCookie).type.toBe< + (value: string) => fastifyCookieStar.UnsignResult + >() }) }) @@ -97,11 +112,15 @@ testSamesiteOptionsApp.after(() => { }) 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 @@ -120,7 +139,7 @@ testSamesiteOptionsApp.after(() => { const appWithImplicitHttpSigned = fastify() appWithImplicitHttpSigned.register(cookie, { - secret: 'testsecret', + secret: 'testsecret' }) appWithImplicitHttpSigned.register(cookie, { secret: 'testsecret', @@ -144,20 +163,20 @@ appWithImplicitHttpSigned.after(() => { const appWithRotationSecret = fastify() appWithRotationSecret.register(cookie, { - secret: ['testsecret'], + secret: ['testsecret'] }) appWithRotationSecret.after(() => { server.get('/', (request, reply) => { reply.unsignCookie(request.cookies.test!) const unsigned = reply.unsignCookie('test') - expectType(unsigned.valid) + expect(unsigned.valid).type.toBe() if (unsigned.valid) { - expectType(unsigned.value) + expect(unsigned.value).type.toBe() } else { - expectType(unsigned.value) + expect(unsigned.value).type.toBe() } - expectType(unsigned.renew) + expect(unsigned.renew).type.toBe() reply.send({ hello: 'world' }) }) @@ -175,25 +194,25 @@ const parseOptions: fastifyCookieStar.CookieSerializeOptions = { sameSite: 'lax', secure: true, signed: true, - partitioned: false, + partitioned: false } -expectType(parseOptions) +expect(parseOptions).type.toBe() appWithParseOptions.register(cookie, { secret: 'testsecret', - parseOptions, + parseOptions }) appWithParseOptions.after(() => { server.get('/', (request, reply) => { const unsigned = reply.unsignCookie(request.cookies.test!) - expectType(unsigned.valid) + expect(unsigned.valid).type.toBe() if (unsigned.valid) { - expectType(unsigned.value) + expect(unsigned.value).type.toBe() } else { - expectType(unsigned.value) + expect(unsigned.value).type.toBe() } - expectType(unsigned.renew) + expect(unsigned.renew).type.toBe() }) }) @@ -203,7 +222,9 @@ appWithCustomSigner.register(cookie, { secret: { sign: (x) => x + '.signed', unsign: (x) => { - if (x.endsWith('.signed')) { return { renew: false, valid: true, value: x.slice(0, -7) } } + if (x.endsWith('.signed')) { + return { renew: false, valid: true, value: x.slice(0, -7) } + } return { renew: false, valid: false, value: null } } } @@ -213,22 +234,28 @@ appWithCustomSigner.after(() => { reply.unsignCookie(request.cookies.test!) const unsigned = reply.unsignCookie('test') - expectType(unsigned.valid) + expect(unsigned.valid).type.toBe() if (unsigned.valid) { - expectType(unsigned.value) + expect(unsigned.value).type.toBe() } else { - expectType(unsigned.value) + expect(unsigned.value).type.toBe() } - expectType(unsigned.renew) + expect(unsigned.renew).type.toBe() reply.send({ hello: 'world' }) }) }) -expectType(new fastifyCookieStar.Signer('secretString')) -expectType(new fastifyCookieStar.Signer(['secretStringInArray'])) -expectType(new fastifyCookieStar.Signer(Buffer.from('secretString'))) -expectType(new fastifyCookieStar.Signer([Buffer.from('secretStringInArray')])) +expect(new fastifyCookieStar.Signer('secretString')).type.toBe() +expect( + new fastifyCookieStar.Signer(['secretStringInArray']) +).type.toBe() +expect( + new fastifyCookieStar.Signer(Buffer.from('secretString')) +).type.toBe() +expect( + new fastifyCookieStar.Signer([Buffer.from('secretStringInArray')]) +).type.toBe() const signer = new fastifyCookieStar.Signer(['secretStringInArray'], 'sha256') signer.sign('Lorem Ipsum') @@ -242,8 +269,22 @@ appWithHook.register(cookie, { hook: 'preHandler' }) appWithHook.register(cookie, { hook: 'preParsing' }) 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 }>(cookie.parse) -expectType<(name: string, value: string, opts?: fastifyCookieStar.SerializeOptions) => string>(cookie.serialize) +expect(appWithHook.register) + .type.not.toBeCallableWith(cookie, { hook: true }) +expect(appWithHook.register) + .type.not.toBeCallableWith(cookie, { hook: 'false' }) + +expect(cookie.parse).type.toBe< + ( + cookieHeader: string, + opts?: fastifyCookieStar.ParseOptions + ) => { [key: string]: string } +>() +expect(cookie.serialize).type.toBe< + ( + name: string, + value: string, + opts?: fastifyCookieStar.SerializeOptions + ) => string +>()