Skip to content

Commit

Permalink
fix: stricter types for unsigned cookies (#292)
Browse files Browse the repository at this point in the history
The success and failure cases are distinct and the types can reflect
this.
  • Loading branch information
ojeytonwilliams authored Jul 3, 2024
1 parent cd9f415 commit d2b6413
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
10 changes: 7 additions & 3 deletions types/plugin.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,14 @@ declare namespace fastifyCookie {
export type Unsign = (input: string, secret: string | Buffer, algorithm?: string) => UnsignResult;
export type SignerFactory = (secrets: string | string[] | Buffer | Buffer[], algorithm?: string) => SignerBase;

export interface UnsignResult {
valid: boolean;
export type UnsignResult = {
valid: true;
renew: boolean;
value: string | null;
value: string;
} | {
valid: false;
renew: false;
value: null;
}

export const signerFactory: SignerFactory;
Expand Down
36 changes: 24 additions & 12 deletions types/plugin.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,15 @@ appWithRotationSecret.register(cookie, {
appWithRotationSecret.after(() => {
server.get('/', (request, reply) => {
reply.unsignCookie(request.cookies.test!);
const { valid, renew, value } = reply.unsignCookie('test');
const unsigned = reply.unsignCookie('test');

expectType<boolean>(valid);
expectType<boolean>(renew);
expectType<string | null>(value);
expectType<boolean>(unsigned.valid);
if (unsigned.valid) {
expectType<string>(unsigned.value);
} else {
expectType<null>(unsigned.value);
}
expectType<boolean>(unsigned.renew);

reply.send({ hello: 'world' });
});
Expand Down Expand Up @@ -182,11 +186,15 @@ appWithParseOptions.register(cookie, {
});
appWithParseOptions.after(() => {
server.get('/', (request, reply) => {
const { valid, renew, value } = reply.unsignCookie(request.cookies.test!);
const unsigned = reply.unsignCookie(request.cookies.test!);

expectType<boolean>(valid);
expectType<boolean>(renew);
expectType<string | null>(value);
expectType<boolean>(unsigned.valid);
if (unsigned.valid) {
expectType<string>(unsigned.value);
} else {
expectType<null>(unsigned.value);
}
expectType<boolean>(unsigned.renew);
});
});

Expand All @@ -204,11 +212,15 @@ appWithCustomSigner.register(cookie, {
appWithCustomSigner.after(() => {
server.get('/', (request, reply) => {
reply.unsignCookie(request.cookies.test!)
const { valid, renew, value } = reply.unsignCookie('test')
const unsigned = reply.unsignCookie('test')

expectType<boolean>(valid)
expectType<boolean>(renew)
expectType<string | null>(value)
expectType<boolean>(unsigned.valid);
if (unsigned.valid) {
expectType<string>(unsigned.value);
} else {
expectType<null>(unsigned.value);
}
expectType<boolean>(unsigned.renew);

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

0 comments on commit d2b6413

Please sign in to comment.