From 325c77b47adf078c154e59705e8aba2e0f7f69eb Mon Sep 17 00:00:00 2001 From: Kyle Rush Date: Mon, 19 Dec 2022 18:51:13 -0500 Subject: [PATCH 1/5] Allow null Session in SessionCallback when there is an error. --- types/types.d.ts | 7 ++++++- types/types.test-d.ts | 6 ++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/types/types.d.ts b/types/types.d.ts index d1eed51..300bba8 100644 --- a/types/types.d.ts +++ b/types/types.d.ts @@ -26,7 +26,12 @@ type FastifySession = FastifyPluginCallback void; -type CallbackSession = (err: Error | null, result: Fastify.Session) => void; +//type CallbackSession = (err: Error | null, result: Fastify.Session) => void; + +interface CallbackSession { + (err: null, result: Fastify.Session): void + (err: Error, result: null): void +} interface SessionData extends ExpressSessionData { sessionId: string; diff --git a/types/types.test-d.ts b/types/types.test-d.ts index 7643f7f..4861f67 100644 --- a/types/types.test-d.ts +++ b/types/types.test-d.ts @@ -88,8 +88,10 @@ app.route({ request.sessionStore.set('session-set-test', request.session, () => {}); request.sessionStore.get('', (err, session) => { expectType(err); - expectType(session); - expectType<{ id: number } | undefined>(session.user); + expectType(session); + if (session) { + expectType<{ id: number } | undefined>(session.user); + } }); expectType(request.session.set('foo', 'bar')); expectType(request.session.get('foo')); From 38386edc32a7e94b650898f6b3a0c5e1530bf2eb Mon Sep 17 00:00:00 2001 From: Kyle Rush Date: Tue, 20 Dec 2022 06:25:50 -0500 Subject: [PATCH 2/5] Update types/types.d.ts Co-authored-by: KaKa --- types/types.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/types.d.ts b/types/types.d.ts index 300bba8..ae68e0c 100644 --- a/types/types.d.ts +++ b/types/types.d.ts @@ -30,7 +30,7 @@ type Callback = (err?: Error) => void; interface CallbackSession { (err: null, result: Fastify.Session): void - (err: Error, result: null): void + (err: Error): void } interface SessionData extends ExpressSessionData { From 7ff6422e860c38af95d2af366491d402fef5c825 Mon Sep 17 00:00:00 2001 From: Kyle Rush Date: Tue, 20 Dec 2022 06:52:10 -0500 Subject: [PATCH 3/5] Export SessionCallback type and allow session argument to be undefined in store.get callback function. --- types/types.d.ts | 8 ++------ types/types.test-d.ts | 7 ++----- 2 files changed, 4 insertions(+), 11 deletions(-) diff --git a/types/types.d.ts b/types/types.d.ts index ae68e0c..c6e58ce 100644 --- a/types/types.d.ts +++ b/types/types.d.ts @@ -26,12 +26,6 @@ type FastifySession = FastifyPluginCallback void; -//type CallbackSession = (err: Error | null, result: Fastify.Session) => void; - -interface CallbackSession { - (err: null, result: Fastify.Session): void - (err: Error): void -} interface SessionData extends ExpressSessionData { sessionId: string; @@ -85,6 +79,8 @@ interface Signer { } declare namespace fastifySession { + export type CallbackSession = (err: null | Error, result: undefined | Fastify.Session) => void + export interface SessionStore { set( sessionId: string, diff --git a/types/types.test-d.ts b/types/types.test-d.ts index 4861f67..3b37697 100644 --- a/types/types.test-d.ts +++ b/types/types.test-d.ts @@ -86,12 +86,9 @@ app.route({ expectError(request.session.doesNotExist()); expectType<{ id: number } | undefined>(request.session.user); request.sessionStore.set('session-set-test', request.session, () => {}); - request.sessionStore.get('', (err, session) => { + request.sessionStore.get('', (err, result) => { expectType(err); - expectType(session); - if (session) { - expectType<{ id: number } | undefined>(session.user); - } + expectType(result); }); expectType(request.session.set('foo', 'bar')); expectType(request.session.get('foo')); From 6a0b1a90b19584fac5a133e784618df78622eacf Mon Sep 17 00:00:00 2001 From: Kyle Rush Date: Tue, 20 Dec 2022 06:57:22 -0500 Subject: [PATCH 4/5] Add session.user type test back in. --- types/types.test-d.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/types/types.test-d.ts b/types/types.test-d.ts index 3b37697..20d26eb 100644 --- a/types/types.test-d.ts +++ b/types/types.test-d.ts @@ -86,9 +86,12 @@ app.route({ expectError(request.session.doesNotExist()); expectType<{ id: number } | undefined>(request.session.user); request.sessionStore.set('session-set-test', request.session, () => {}); - request.sessionStore.get('', (err, result) => { + request.sessionStore.get('', (err, session) => { expectType(err); - expectType(result); + expectType(session); + if (session) { + expectType<{ id: number } | undefined>(session.user); + } }); expectType(request.session.set('foo', 'bar')); expectType(request.session.get('foo')); From 5d65a0b3f33313827e10a0a55e3b913b064a2e45 Mon Sep 17 00:00:00 2001 From: Kyle Rush Date: Mon, 26 Dec 2022 11:17:50 -0500 Subject: [PATCH 5/5] Update types/types.d.ts Co-authored-by: KaKa --- types/types.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/types.d.ts b/types/types.d.ts index c6e58ce..91d199b 100644 --- a/types/types.d.ts +++ b/types/types.d.ts @@ -79,7 +79,7 @@ interface Signer { } declare namespace fastifySession { - export type CallbackSession = (err: null | Error, result: undefined | Fastify.Session) => void + export type CallbackSession = ((err: Error) => void) | ((err: null, result: Fastify.Session) => void) export interface SessionStore { set(