@@ -2,6 +2,7 @@ import type { CallHandler, ExecutionContext, HttpException, NestInterceptor } fr
2
2
import { applyDecorators , mixin , NotFoundException , UseInterceptors } from '@nestjs/common' ;
3
3
import { getClientForEvaluation } from './utils' ;
4
4
import type { EvaluationContext } from '@openfeature/server-sdk' ;
5
+ import type { ContextFactory } from './context-factory' ;
5
6
6
7
type RequiredFlag = {
7
8
flagKey : string ;
@@ -37,6 +38,13 @@ interface RequireFlagsEnabledProps {
37
38
* @see {@link OpenFeature#setContext }
38
39
*/
39
40
context ?: EvaluationContext ;
41
+
42
+ /**
43
+ * A factory function for creating an OpenFeature {@link EvaluationContext} from Nest {@link ExecutionContext}.
44
+ * For example, this can be used to get header info from an HTTP request or information from a gRPC call to be used in the {@link EvaluationContext}.
45
+ * @see {@link ContextFactory }
46
+ */
47
+ contextFactory ?: ContextFactory ;
40
48
}
41
49
42
50
/**
@@ -56,6 +64,11 @@ interface RequireFlagsEnabledProps {
56
64
* context: { // Optional, defaults to the global OpenFeature Context
57
65
* targetingKey: 'user-id',
58
66
* },
67
+ * contextFactory: (context: ExecutionContext) => { // Optional, defaults to the global OpenFeature Context. Takes precedence over the context option.
68
+ * return {
69
+ * targetingKey: context.switchToHttp().getRequest().headers['x-user-id'],
70
+ * };
71
+ * },
59
72
* })
60
73
* @Get ('/')
61
74
* public async handleGetRequest()
@@ -72,7 +85,8 @@ const FlagsEnabledInterceptor = (props: RequireFlagsEnabledProps) => {
72
85
73
86
async intercept ( context : ExecutionContext , next : CallHandler ) {
74
87
const req = context . switchToHttp ( ) . getRequest ( ) ;
75
- const client = getClientForEvaluation ( props . domain , props . context ) ;
88
+ const evaluationContext = props . contextFactory ? await props . contextFactory ( context ) : props . context ;
89
+ const client = getClientForEvaluation ( props . domain , evaluationContext ) ;
76
90
77
91
for ( const flag of props . flags ) {
78
92
const endpointAccessible = await client . getBooleanValue ( flag . flagKey , flag . defaultValue ?? false ) ;
0 commit comments