Skip to content

Commit 1de8f25

Browse files
committed
Session decorators: Inject from SessionHost
1 parent 10f8e67 commit 1de8f25

File tree

3 files changed

+16
-18
lines changed

3 files changed

+16
-18
lines changed

src/common/session.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {
2-
createParamDecorator,
3-
type ExecutionContext,
2+
Injectable,
3+
Param,
44
type PipeTransform,
55
type Type,
66
} from '@nestjs/common';
@@ -9,6 +9,7 @@ import { Context } from '@nestjs/graphql';
99
import { uniq } from 'lodash';
1010
import { type DateTime } from 'luxon';
1111
import { NoSessionException } from '../components/authentication/no-session.exception';
12+
import { SessionHost } from '../components/authentication/session.host';
1213
import { type ScopedRole } from '../components/authorization/dto';
1314
import { type GqlContextType } from './context.type';
1415
import { UnauthenticatedException } from './exceptions';
@@ -49,30 +50,32 @@ export const sessionFromContext = (context: GqlContextType) => {
4950
return session;
5051
};
5152

53+
@Injectable()
54+
export class SessionPipe implements PipeTransform {
55+
constructor(private readonly sessionHost: SessionHost) {}
56+
57+
transform() {
58+
return this.sessionHost.current$.value;
59+
}
60+
}
61+
5262
export const LoggedInSession = () =>
5363
AnonSession({ transform: loggedInSession });
5464

5565
export const AnonSession =
5666
(...pipes: Array<Type<PipeTransform> | PipeTransform>): ParameterDecorator =>
5767
(...args) => {
58-
Context({ transform: sessionFromContext }, ...pipes)(...args);
68+
Context(SessionPipe, ...pipes)(...args);
5969
process.nextTick(() => {
6070
// Only set this metadata if it's a controller method.
6171
// Waiting for the next tick as class decorators execute after methods.
6272
if (Reflect.getMetadata(CONTROLLER_WATERMARK, args[0].constructor)) {
63-
HttpSession(...pipes)(...args);
73+
Param(SessionPipe, ...pipes)(...args);
6474
SessionWatermark(...args);
6575
}
6676
});
6777
};
6878

69-
// Using Nest's custom decorator so that we can pass pipes.
70-
const HttpSession = createParamDecorator(
71-
(_data: unknown, ctx: ExecutionContext) => {
72-
return ctx.switchToHttp().getRequest().session;
73-
},
74-
);
75-
7679
const SessionWatermark: ParameterDecorator = (target, key) =>
7780
Reflect.defineMetadata('SESSION_WATERMARK', true, target.constructor, key!);
7881

src/components/authentication/authentication.module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { forwardRef, Global, Module } from '@nestjs/common';
22
import { APP_INTERCEPTOR } from '@nestjs/core';
3+
import { SessionPipe } from '~/common/session';
34
import { splitDb } from '~/core';
45
import { AuthorizationModule } from '../authorization/authorization.module';
56
import { UserModule } from '../user/user.module';
@@ -43,6 +44,7 @@ import { SessionResolver } from './session.resolver';
4344
GelCurrentUserProvider,
4445
{ provide: APP_INTERCEPTOR, useExisting: GelCurrentUserProvider },
4546
{ provide: SessionHost, useClass: SessionHostImpl },
47+
SessionPipe,
4648
],
4749
exports: [
4850
SessionHost,

src/core/http/types.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import type {
44
FastifyReply as Response,
55
RouteShorthandOptions,
66
} from 'fastify';
7-
import type { Session } from '~/common';
87

98
// Exporting with I prefix to avoid ambiguity with web global types
109
export type { Request as IRequest, Response as IResponse };
@@ -19,12 +18,6 @@ export type HttpHooks = Required<{
1918
export type { FastifyCorsOptions as CorsOptions } from '@fastify/cors';
2019
export type { SerializeOptions as CookieOptions } from '@fastify/cookie';
2120

22-
declare module 'fastify' {
23-
export interface FastifyRequest {
24-
session?: Session;
25-
}
26-
}
27-
2821
declare module '@nestjs/common/interfaces/features/arguments-host.interface' {
2922
export interface HttpArgumentsHost {
3023
getRequest(): Request;

0 commit comments

Comments
 (0)