Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down Expand Up @@ -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"
}
}
}
File renamed without changes.
163 changes: 102 additions & 61 deletions types/plugin.test-d.ts → types/index.tst.ts
Original file line number Diff line number Diff line change
@@ -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('..')

Expand All @@ -15,61 +19,72 @@ app.register(fastifyCookieCjsImport.fastifyCookie)
app.register(fastifyCookieStar.default)
app.register(fastifyCookieStar.fastifyCookie)

expectType<fastifyCookieStar.FastifyCookie>(fastifyCookieNamed)
expectType<fastifyCookieStar.FastifyCookie>(cookie)
expectType<fastifyCookieStar.FastifyCookie>(fastifyCookieCjsImport.default)
expectType<fastifyCookieStar.FastifyCookie>(fastifyCookieCjsImport.fastifyCookie)
expectType<fastifyCookieStar.FastifyCookie>(fastifyCookieStar.default)
expectType<fastifyCookieStar.FastifyCookie>(
expect(fastifyCookieNamed).type.toBe<fastifyCookieStar.FastifyCookie>()
expect(cookie).type.toBe<fastifyCookieStar.FastifyCookie>()
expect(
fastifyCookieCjsImport.default
).type.toBe<fastifyCookieStar.FastifyCookie>()
expect(
fastifyCookieCjsImport.fastifyCookie
).type.toBe<fastifyCookieStar.FastifyCookie>()
expect(fastifyCookieStar.default).type.toBe<fastifyCookieStar.FastifyCookie>()
expect(
fastifyCookieStar.fastifyCookie
)
expectType<any>(fastifyCookieCjs)
).type.toBe<fastifyCookieStar.FastifyCookie>()
expect(fastifyCookieCjs).type.toBe<any>()

expectType<fastifyCookieStar.Sign>(cookie.sign)
expectType<fastifyCookieStar.Unsign>(cookie.unsign)
expectType<fastifyCookieStar.SignerFactory >(cookie.signerFactory)
expect(cookie.sign).type.toBe<fastifyCookieStar.Sign>()
expect(cookie.unsign).type.toBe<fastifyCookieStar.Unsign>()
expect(cookie.signerFactory).type.toBe<fastifyCookieStar.SignerFactory>()

expectType<fastifyCookieStar.Sign>(fastifyCookieNamed.sign)
expectType<fastifyCookieStar.Unsign>(fastifyCookieNamed.unsign)
expectType<fastifyCookieStar.SignerFactory >(fastifyCookieNamed.signerFactory)
expect(fastifyCookieNamed.sign).type.toBe<fastifyCookieStar.Sign>()
expect(fastifyCookieNamed.unsign).type.toBe<fastifyCookieStar.Unsign>()
expect(
fastifyCookieNamed.signerFactory
).type.toBe<fastifyCookieStar.SignerFactory>()

const server = fastify()

server.register(cookie)

server.after((_err) => {
expectType< string >(
expect(
server.serializeCookie('sessionId', 'aYb4uTIhdBXC')
)
).type.toBe<string>()

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<string | undefined>(test)
expect(test).type.toBe<string | undefined>()

expectType<setCookieWrapper>(reply.cookie)
expectType<setCookieWrapper>(reply.setCookie)
expect(reply.cookie).type.toBe<setCookieWrapper>()
expect(reply.setCookie).type.toBe<setCookieWrapper>()

expectType<FastifyReply>(
expect(
reply
.setCookie('test', test!, { domain: 'example.com', path: '/' })
.clearCookie('foo')
.send({ hello: 'world' })
)
).type.toBe<FastifyReply>()
})

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
>()
})
})

Expand Down Expand Up @@ -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
Expand All @@ -120,7 +139,7 @@ testSamesiteOptionsApp.after(() => {
const appWithImplicitHttpSigned = fastify()

appWithImplicitHttpSigned.register(cookie, {
secret: 'testsecret',
secret: 'testsecret'
})
appWithImplicitHttpSigned.register(cookie, {
secret: 'testsecret',
Expand All @@ -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<boolean>(unsigned.valid)
expect(unsigned.valid).type.toBe<boolean>()
if (unsigned.valid) {
expectType<string>(unsigned.value)
expect(unsigned.value).type.toBe<string>()
} else {
expectType<null>(unsigned.value)
expect(unsigned.value).type.toBe<null>()
}
expectType<boolean>(unsigned.renew)
expect(unsigned.renew).type.toBe<boolean>()

reply.send({ hello: 'world' })
})
Expand All @@ -175,25 +194,25 @@ const parseOptions: fastifyCookieStar.CookieSerializeOptions = {
sameSite: 'lax',
secure: true,
signed: true,
partitioned: false,
partitioned: false
}
expectType<fastifyCookieStar.CookieSerializeOptions>(parseOptions)
expect(parseOptions).type.toBe<fastifyCookieStar.CookieSerializeOptions>()

appWithParseOptions.register(cookie, {
secret: 'testsecret',
parseOptions,
parseOptions
})
appWithParseOptions.after(() => {
server.get('/', (request, reply) => {
const unsigned = reply.unsignCookie(request.cookies.test!)

expectType<boolean>(unsigned.valid)
expect(unsigned.valid).type.toBe<boolean>()
if (unsigned.valid) {
expectType<string>(unsigned.value)
expect(unsigned.value).type.toBe<string>()
} else {
expectType<null>(unsigned.value)
expect(unsigned.value).type.toBe<null>()
}
expectType<boolean>(unsigned.renew)
expect(unsigned.renew).type.toBe<boolean>()
})
})

Expand All @@ -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 }
}
}
Expand All @@ -213,22 +234,28 @@ appWithCustomSigner.after(() => {
reply.unsignCookie(request.cookies.test!)
const unsigned = reply.unsignCookie('test')

expectType<boolean>(unsigned.valid)
expect(unsigned.valid).type.toBe<boolean>()
if (unsigned.valid) {
expectType<string>(unsigned.value)
expect(unsigned.value).type.toBe<string>()
} else {
expectType<null>(unsigned.value)
expect(unsigned.value).type.toBe<null>()
}
expectType<boolean>(unsigned.renew)
expect(unsigned.renew).type.toBe<boolean>()

reply.send({ hello: 'world' })
})
})

expectType<Signer>(new fastifyCookieStar.Signer('secretString'))
expectType<Signer>(new fastifyCookieStar.Signer(['secretStringInArray']))
expectType<Signer>(new fastifyCookieStar.Signer(Buffer.from('secretString')))
expectType<Signer>(new fastifyCookieStar.Signer([Buffer.from('secretStringInArray')]))
expect(new fastifyCookieStar.Signer('secretString')).type.toBe<Signer>()
expect(
new fastifyCookieStar.Signer(['secretStringInArray'])
).type.toBe<Signer>()
expect(
new fastifyCookieStar.Signer(Buffer.from('secretString'))
).type.toBe<Signer>()
expect(
new fastifyCookieStar.Signer([Buffer.from('secretStringInArray')])
).type.toBe<Signer>()

const signer = new fastifyCookieStar.Signer(['secretStringInArray'], 'sha256')
signer.sign('Lorem Ipsum')
Expand All @@ -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
>()
Loading