Skip to content

Commit d2b6413

Browse files
fix: stricter types for unsigned cookies (#292)
The success and failure cases are distinct and the types can reflect this.
1 parent cd9f415 commit d2b6413

2 files changed

Lines changed: 31 additions & 15 deletions

File tree

types/plugin.d.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,14 @@ declare namespace fastifyCookie {
155155
export type Unsign = (input: string, secret: string | Buffer, algorithm?: string) => UnsignResult;
156156
export type SignerFactory = (secrets: string | string[] | Buffer | Buffer[], algorithm?: string) => SignerBase;
157157

158-
export interface UnsignResult {
159-
valid: boolean;
158+
export type UnsignResult = {
159+
valid: true;
160160
renew: boolean;
161-
value: string | null;
161+
value: string;
162+
} | {
163+
valid: false;
164+
renew: false;
165+
value: null;
162166
}
163167

164168
export const signerFactory: SignerFactory;

types/plugin.test-d.ts

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,15 @@ appWithRotationSecret.register(cookie, {
150150
appWithRotationSecret.after(() => {
151151
server.get('/', (request, reply) => {
152152
reply.unsignCookie(request.cookies.test!);
153-
const { valid, renew, value } = reply.unsignCookie('test');
153+
const unsigned = reply.unsignCookie('test');
154154

155-
expectType<boolean>(valid);
156-
expectType<boolean>(renew);
157-
expectType<string | null>(value);
155+
expectType<boolean>(unsigned.valid);
156+
if (unsigned.valid) {
157+
expectType<string>(unsigned.value);
158+
} else {
159+
expectType<null>(unsigned.value);
160+
}
161+
expectType<boolean>(unsigned.renew);
158162

159163
reply.send({ hello: 'world' });
160164
});
@@ -182,11 +186,15 @@ appWithParseOptions.register(cookie, {
182186
});
183187
appWithParseOptions.after(() => {
184188
server.get('/', (request, reply) => {
185-
const { valid, renew, value } = reply.unsignCookie(request.cookies.test!);
189+
const unsigned = reply.unsignCookie(request.cookies.test!);
186190

187-
expectType<boolean>(valid);
188-
expectType<boolean>(renew);
189-
expectType<string | null>(value);
191+
expectType<boolean>(unsigned.valid);
192+
if (unsigned.valid) {
193+
expectType<string>(unsigned.value);
194+
} else {
195+
expectType<null>(unsigned.value);
196+
}
197+
expectType<boolean>(unsigned.renew);
190198
});
191199
});
192200

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

209-
expectType<boolean>(valid)
210-
expectType<boolean>(renew)
211-
expectType<string | null>(value)
217+
expectType<boolean>(unsigned.valid);
218+
if (unsigned.valid) {
219+
expectType<string>(unsigned.value);
220+
} else {
221+
expectType<null>(unsigned.value);
222+
}
223+
expectType<boolean>(unsigned.renew);
212224

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

0 commit comments

Comments
 (0)