1
1
import {
2
- createParamDecorator ,
3
- type ExecutionContext ,
2
+ Injectable ,
3
+ Param ,
4
4
type PipeTransform ,
5
5
type Type ,
6
6
} from '@nestjs/common' ;
@@ -9,6 +9,7 @@ import { Context } from '@nestjs/graphql';
9
9
import { uniq } from 'lodash' ;
10
10
import { type DateTime } from 'luxon' ;
11
11
import { NoSessionException } from '../components/authentication/no-session.exception' ;
12
+ import { SessionHost } from '../components/authentication/session.host' ;
12
13
import { type ScopedRole } from '../components/authorization/dto' ;
13
14
import { type GqlContextType } from './context.type' ;
14
15
import { UnauthenticatedException } from './exceptions' ;
@@ -49,30 +50,32 @@ export const sessionFromContext = (context: GqlContextType) => {
49
50
return session ;
50
51
} ;
51
52
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
+
52
62
export const LoggedInSession = ( ) =>
53
63
AnonSession ( { transform : loggedInSession } ) ;
54
64
55
65
export const AnonSession =
56
66
( ...pipes : Array < Type < PipeTransform > | PipeTransform > ) : ParameterDecorator =>
57
67
( ...args ) => {
58
- Context ( { transform : sessionFromContext } , ...pipes ) ( ...args ) ;
68
+ Context ( SessionPipe , ...pipes ) ( ...args ) ;
59
69
process . nextTick ( ( ) => {
60
70
// Only set this metadata if it's a controller method.
61
71
// Waiting for the next tick as class decorators execute after methods.
62
72
if ( Reflect . getMetadata ( CONTROLLER_WATERMARK , args [ 0 ] . constructor ) ) {
63
- HttpSession ( ...pipes ) ( ...args ) ;
73
+ Param ( SessionPipe , ...pipes ) ( ...args ) ;
64
74
SessionWatermark ( ...args ) ;
65
75
}
66
76
} ) ;
67
77
} ;
68
78
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
-
76
79
const SessionWatermark : ParameterDecorator = ( target , key ) =>
77
80
Reflect . defineMetadata ( 'SESSION_WATERMARK' , true , target . constructor , key ! ) ;
78
81
0 commit comments