-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathopen-feature.module.ts
161 lines (148 loc) · 4.8 KB
/
open-feature.module.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import type {
DynamicModule,
FactoryProvider as NestFactoryProvider,
ValueProvider,
ClassProvider,
Provider as NestProvider} from '@nestjs/common';
import {
Module,
ExecutionContext,
} from '@nestjs/common';
import type {
Client,
Hook,
Provider,
EvaluationContext,
ServerProviderEvents,
EventHandler,
Logger} from '@openfeature/server-sdk';
import {
OpenFeature,
AsyncLocalStorageTransactionContextPropagator,
} from '@openfeature/server-sdk';
import type { ContextFactory} from './context-factory';
import { ContextFactoryToken } from './context-factory';
import { APP_INTERCEPTOR } from '@nestjs/core';
import { EvaluationContextInterceptor } from './evaluation-context-interceptor';
import { ShutdownService } from './shutdown.service';
/**
* OpenFeatureModule is a NestJS wrapper for OpenFeature Server-SDK.
*/
@Module({})
export class OpenFeatureModule {
static forRoot({ useGlobalInterceptor = true, ...options }: OpenFeatureModuleOptions): DynamicModule {
OpenFeature.setTransactionContextPropagator(new AsyncLocalStorageTransactionContextPropagator());
if (options.logger) {
OpenFeature.setLogger(options.logger);
}
if (options.hooks) {
OpenFeature.addHooks(...options.hooks);
}
options.handlers?.forEach(([event, handler]) => {
OpenFeature.addHandler(event, handler);
});
const clientValueProviders: NestFactoryProvider<Client>[] = [
{
provide: getOpenFeatureClientToken(),
useFactory: () => OpenFeature.getClient(),
},
];
if (options?.defaultProvider) {
OpenFeature.setProvider(options.defaultProvider);
}
if (options?.providers) {
Object.entries(options.providers).forEach(([domain, provider]) => {
OpenFeature.setProvider(domain, provider);
clientValueProviders.push({
provide: getOpenFeatureClientToken(domain),
useFactory: () => OpenFeature.getClient(domain),
});
});
}
const nestProviders: NestProvider[] = [ShutdownService];
nestProviders.push(...clientValueProviders);
const contextFactoryProvider: ValueProvider = {
provide: ContextFactoryToken,
useValue: options?.contextFactory,
};
nestProviders.push(contextFactoryProvider);
if (useGlobalInterceptor) {
const interceptorProvider: ClassProvider = {
provide: APP_INTERCEPTOR,
useClass: EvaluationContextInterceptor,
};
nestProviders.push(interceptorProvider);
}
return {
global: true,
module: OpenFeatureModule,
providers: nestProviders,
exports: [...clientValueProviders, ContextFactoryToken],
};
}
}
/**
* Options for the {@link OpenFeatureModule}.
*/
export interface OpenFeatureModuleOptions {
/**
* The provider to be set as OpenFeature default provider.
* @see {@link OpenFeature#setProvider}
*/
defaultProvider?: Provider;
/**
* Domain scoped providers to set to OpenFeature.
* @see {@link OpenFeature#setProvider}
*/
providers?: {
[domain: string]: Provider;
};
/**
* Global {@link Logger} for OpenFeature.
* @see {@link OpenFeature#setLogger}
*/
logger?: Logger;
/**
* Global {@link EvaluationContext} for OpenFeature.
* @see {@link OpenFeature#setContext}
*/
context?: EvaluationContext;
/**
* Global {@link Hook Hooks} for OpenFeature.
* @see {@link OpenFeature#addHooks}
*/
hooks?: Hook[];
/**
* Global {@link EventHandler EventHandlers} for OpenFeature.
* @see {@link OpenFeature#addHandler}
*/
handlers?: [ServerProviderEvents, EventHandler][];
/**
* The {@link ContextFactory} for creating an {@link EvaluationContext} from Nest {@link ExecutionContext} information.
* This could be header values of a request or something similar.
* The context is automatically used for all feature flag evaluations during this request.
* @see {@link AsyncLocalStorageTransactionContextPropagator}
*/
contextFactory?: ContextFactory;
/**
* If set to false, the global {@link EvaluationContextInterceptor} is disabled.
* This means that automatic propagation of the {@link EvaluationContext} created by the {@link this#contextFactory} is not working.
*
* To enable it again for specific routes, the interceptor can be added for specific controllers or request handlers like seen below:
* ```typescript
* @Controller()
* @UseInterceptors(EvaluationContextInterceptor)
* export class Controller {}
* ```
* @default true
*/
useGlobalInterceptor?: boolean;
}
/**
* Returns an injection token for a (domain scoped) OpenFeature client.
* @param {string} domain The domain of the OpenFeature client.
* @returns {Client} The injection token.
*/
export function getOpenFeatureClientToken(domain?: string): string {
return domain ? `OpenFeatureClient_${domain}` : 'OpenFeatureClient_default';
}