-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathopenfeature.make.ts
More file actions
210 lines (188 loc) · 5.58 KB
/
openfeature.make.ts
File metadata and controls
210 lines (188 loc) · 5.58 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/**
* OpenFeature provider for Next.js App Router
*
* There is also openfeature.default.ts which targets default runtimes.
* If you update this file, please update openfeature.default.ts as well.
*
* This file should stay equivalent to openfeature.default.ts, except that it
* imports from index.next-js to get cached functions.
*/
import {
ErrorCode,
type EvaluationContext,
type JsonValue,
type Provider,
type ProviderMetadata,
type ResolutionDetails,
type ResolutionReason,
StandardResolutionReasons,
} from '@openfeature/server-sdk';
import { type FlagsClient, ResolutionReason as Reason } from './types';
function mapReason(reason: Reason): ResolutionReason {
switch (reason) {
case Reason.ERROR:
return StandardResolutionReasons.ERROR;
case Reason.PAUSED:
return StandardResolutionReasons.STATIC;
case Reason.FALLTHROUGH:
return StandardResolutionReasons.DEFAULT;
case Reason.TARGET_MATCH:
case Reason.RULE_MATCH:
return StandardResolutionReasons.TARGETING_MATCH;
default:
return StandardResolutionReasons.UNKNOWN;
}
}
export function make(
createClient: (sdkKeyOrConnectionString: string) => FlagsClient,
defaultFlagsClient: FlagsClient,
) {
return class VercelProvider implements Provider {
readonly metadata: ProviderMetadata = {
name: 'vercel-nodejs-provider',
} as const;
readonly runsOn = 'server';
readonly client: FlagsClient;
constructor();
constructor(client: FlagsClient);
constructor(connectionString: string);
constructor(
clientOrConnectionString: FlagsClient | string = defaultFlagsClient,
) {
if (typeof clientOrConnectionString === 'string') {
this.client = createClient(clientOrConnectionString);
} else {
this.client = clientOrConnectionString;
}
}
async initialize(context?: EvaluationContext): Promise<void> {
await this.client.initialize();
}
async onClose() {
await this.client.shutdown();
}
async resolveBooleanEvaluation(
flagKey: string,
defaultValue: boolean,
context: EvaluationContext,
): Promise<ResolutionDetails<boolean>> {
const result = await this.client.evaluate<boolean>(
flagKey,
defaultValue,
context,
);
if (result.reason === Reason.ERROR) {
return {
value: defaultValue,
reason: StandardResolutionReasons.ERROR,
errorCode: ErrorCode.GENERAL,
errorMessage: result.errorMessage,
};
}
if (typeof result.value !== 'boolean') {
return {
value: defaultValue,
reason: StandardResolutionReasons.ERROR,
errorCode: ErrorCode.TYPE_MISMATCH,
errorMessage: `Expected boolean value for flag "${flagKey}"`,
};
}
return {
value: result.value,
reason: mapReason(result.reason),
variant: result.variantId,
};
}
async resolveStringEvaluation(
flagKey: string,
defaultValue: string,
context: EvaluationContext,
): Promise<ResolutionDetails<string>> {
const result = await this.client.evaluate<string>(
flagKey,
defaultValue,
context,
);
if (result.reason === Reason.ERROR) {
return {
value: defaultValue,
reason: StandardResolutionReasons.ERROR,
errorCode: ErrorCode.GENERAL,
errorMessage: result.errorMessage,
};
}
if (typeof result.value !== 'string') {
return {
value: defaultValue,
reason: StandardResolutionReasons.ERROR,
errorCode: ErrorCode.TYPE_MISMATCH,
errorMessage: `Expected string value for flag "${flagKey}"`,
};
}
return {
value: result.value,
reason: mapReason(result.reason),
variant: result.variantId,
errorMessage: result.errorMessage,
};
}
async resolveNumberEvaluation(
flagKey: string,
defaultValue: number,
context: EvaluationContext,
): Promise<ResolutionDetails<number>> {
const result = await this.client.evaluate<number>(
flagKey,
defaultValue,
context,
);
if (result.reason === Reason.ERROR) {
return {
value: defaultValue,
reason: StandardResolutionReasons.ERROR,
errorCode: ErrorCode.GENERAL,
errorMessage: result.errorMessage,
};
}
if (typeof result.value !== 'number') {
return {
value: defaultValue,
reason: StandardResolutionReasons.ERROR,
errorCode: ErrorCode.TYPE_MISMATCH,
errorMessage: `Expected number value for flag "${flagKey}"`,
};
}
return {
value: result.value,
reason: mapReason(result.reason),
variant: result.variantId,
errorMessage: result.errorMessage,
};
}
async resolveObjectEvaluation<T extends JsonValue>(
flagKey: string,
defaultValue: T,
context: EvaluationContext,
): Promise<ResolutionDetails<T>> {
const result = await this.client.evaluate<T>(
flagKey,
defaultValue,
context,
);
if (result.reason === Reason.ERROR) {
return {
value: defaultValue,
reason: StandardResolutionReasons.ERROR,
errorCode: ErrorCode.GENERAL,
errorMessage: result.errorMessage,
};
}
return {
value: result.value,
reason: mapReason(result.reason),
variant: result.variantId,
errorMessage: result.errorMessage,
};
}
};
}