Skip to content

Commit f04a3f0

Browse files
committed
fixup: review feedback sdk update
Signed-off-by: Todd Baert <[email protected]>
1 parent b9f7bc9 commit f04a3f0

File tree

8 files changed

+51
-47
lines changed

8 files changed

+51
-47
lines changed

libs/providers/ofrep-web/src/lib/ofrep-web-provider.spec.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import TestLogger from '../../test/test-logger';
44
import { server } from '../../../../shared/ofrep-core/src/test/mock-service-worker';
55
import { ClientProviderEvents, ClientProviderStatus, OpenFeature } from '@openfeature/web-sdk';
66
// eslint-disable-next-line @nx/enforce-module-boundaries
7-
import { TEST_FLAG_METADATA, TEST_FLAG_SET_METADATA } from 'libs/shared/ofrep-core/src/test/test-constants';
7+
import { TEST_FLAG_METADATA, TEST_FLAG_SET_METADATA } from '../../../../shared/ofrep-core/src/test/test-constants';
88

99
describe('OFREPWebProvider', () => {
1010
beforeAll(() => server.listen());
@@ -190,9 +190,7 @@ describe('OFREPWebProvider', () => {
190190
flagKey,
191191
value: false,
192192
errorCode: 'PARSE_ERROR',
193-
// TODO: there's a bug in the web SDK which calls `instantiateErrorByErrorCode` without a message, so our message is lost :(
194-
// errorMessage: 'Flag or flag configuration could not be parsed',
195-
errorMessage: '',
193+
errorMessage: 'Flag or flag configuration could not be parsed',
196194
reason: 'ERROR',
197195
flagMetadata: {},
198196
});

libs/providers/ofrep-web/src/lib/ofrep-web-provider.ts

+13-12
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ import { FlagCache, MetadataCache } from './model/in-memory-cache';
2929
import { OFREPWebProviderOptions } from './model/ofrep-web-provider-options';
3030
import { isResolutionError } from './model/resolution-error';
3131

32-
const ERROR_TO_MESSAGE: { [key in ErrorCode]: string } = {
32+
const ErrorMessageMap: { [key in ErrorCode]: string } = {
3333
[ErrorCode.FLAG_NOT_FOUND]: 'Flag was not found',
3434
[ErrorCode.GENERAL]: 'General error',
3535
[ErrorCode.INVALID_CONTEXT]: 'Context is invalid or could be parsed',
3636
[ErrorCode.PARSE_ERROR]: 'Flag or flag configuration could not be parsed',
3737
[ErrorCode.PROVIDER_FATAL]: 'Provider is in a fatal error state',
3838
[ErrorCode.PROVIDER_NOT_READY]: 'Provider is not yet ready',
3939
[ErrorCode.TARGETING_KEY_MISSING]: 'Targeting key is missing',
40-
[ErrorCode.TYPE_MISMATCH]: 'Flag is not of expect type',
40+
[ErrorCode.TYPE_MISMATCH]: 'Flag is not of expected type',
4141
};
4242

4343
export class OFREPWebProvider implements Provider {
@@ -203,22 +203,23 @@ export class OFREPWebProvider implements Provider {
203203
const bulkSuccessResp = response.value;
204204
const newCache: FlagCache = {};
205205

206-
if ('flags' in bulkSuccessResp && typeof bulkSuccessResp.flags === 'object') {
207-
bulkSuccessResp.flags?.forEach((evalResp: EvaluationResponse) => {
206+
if ('flags' in bulkSuccessResp && Array.isArray(bulkSuccessResp.flags)) {
207+
bulkSuccessResp.flags.forEach((evalResp: EvaluationResponse) => {
208208
if (isEvaluationFailureResponse(evalResp)) {
209209
newCache[evalResp.key] = {
210+
reason: StandardResolutionReasons.ERROR,
211+
flagMetadata: evalResp.metadata,
210212
errorCode: evalResp.errorCode,
211213
errorDetails: evalResp.errorDetails,
212-
reason: StandardResolutionReasons.ERROR,
213214
};
214215
}
215216

216217
if (isEvaluationSuccessResponse(evalResp) && evalResp.key) {
217218
newCache[evalResp.key] = {
218219
value: evalResp.value,
219-
flagMetadata: evalResp.metadata,
220-
reason: evalResp.reason,
221220
variant: evalResp.variant,
221+
reason: evalResp.reason,
222+
flagMetadata: evalResp.metadata,
222223
};
223224
}
224225
});
@@ -281,27 +282,27 @@ export class OFREPWebProvider implements Provider {
281282
return {
282283
value: defaultValue,
283284
flagMetadata: this._flagSetMetadataCache,
284-
reason: 'ERROR',
285+
reason: StandardResolutionReasons.ERROR,
285286
errorCode: ErrorCode.FLAG_NOT_FOUND,
286-
errorMessage: ERROR_TO_MESSAGE[ErrorCode.FLAG_NOT_FOUND],
287+
errorMessage: ErrorMessageMap[ErrorCode.FLAG_NOT_FOUND],
287288
};
288289
}
289290

290291
if (isResolutionError(resolved)) {
291292
return {
292293
...resolved,
293294
value: defaultValue,
294-
errorMessage: ERROR_TO_MESSAGE[resolved.errorCode],
295+
errorMessage: ErrorMessageMap[resolved.errorCode],
295296
};
296297
}
297298

298299
if (typeof resolved.value !== type) {
299300
return {
300301
value: defaultValue,
301302
flagMetadata: this._flagSetMetadataCache,
302-
reason: 'ERROR',
303+
reason: StandardResolutionReasons.ERROR,
303304
errorCode: ErrorCode.TYPE_MISMATCH,
304-
errorMessage: ERROR_TO_MESSAGE[ErrorCode.TYPE_MISMATCH],
305+
errorMessage: ErrorMessageMap[ErrorCode.TYPE_MISMATCH],
305306
};
306307
}
307308

libs/providers/ofrep/src/lib/ofrep-provider.spec.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
} from '@openfeature/ofrep-core';
88
import { ErrorCode, GeneralError, TypeMismatchError } from '@openfeature/server-sdk';
99
// eslint-disable-next-line @nx/enforce-module-boundaries
10-
import { TEST_FLAG_METADATA } from 'libs/shared/ofrep-core/src/test/test-constants';
10+
import { TEST_FLAG_METADATA } from '../../../../shared/ofrep-core/src/test/test-constants';
1111
// eslint-disable-next-line @nx/enforce-module-boundaries
1212
import { server } from '../../../../shared/ofrep-core/src/test/mock-service-worker';
1313

@@ -130,8 +130,10 @@ describe('OFREPProvider should', () => {
130130
expect(flag.flagMetadata).toEqual(TEST_FLAG_METADATA);
131131
});
132132

133-
it('throw TypeMismatchError if response type is different rom requested one', async () => {
134-
await expect(provider.resolveNumberEvaluation('my-flag', 42, {})).rejects.toThrow(TypeMismatchError);
133+
it('return TypeMismatchError if response type is different rom requested one', async () => {
134+
const flag = await provider.resolveNumberEvaluation('my-flag', 42, {});
135+
expect(flag.errorCode).toEqual(ErrorCode.TYPE_MISMATCH);
136+
expect(flag.flagMetadata).toEqual(TEST_FLAG_METADATA);
135137
});
136138

137139
it('send auth header from headerFactory', async () => {

libs/providers/ofrep/src/lib/ofrep-provider.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ import {
88
toResolutionDetails,
99
} from '@openfeature/ofrep-core';
1010
import {
11+
ErrorCode,
1112
EvaluationContext,
1213
GeneralError,
1314
JsonValue,
1415
Provider,
1516
ResolutionDetails,
16-
TypeMismatchError,
17+
StandardResolutionReasons,
1718
} from '@openfeature/server-sdk';
1819

1920
export type OFREPProviderOptions = OFREPProviderBaseOptions;
@@ -103,7 +104,13 @@ export class OFREPProvider implements Provider {
103104
}
104105

105106
if (typeof result.value.value !== typeof defaultValue) {
106-
throw new TypeMismatchError(`Expected flag type ${typeof defaultValue} but got ${typeof result.value.value}`);
107+
return {
108+
value: defaultValue,
109+
reason: StandardResolutionReasons.ERROR,
110+
flagMetadata: result.value.metadata,
111+
errorCode: ErrorCode.TYPE_MISMATCH,
112+
errorMessage: 'Flag is not of expected type',
113+
};
107114
}
108115

109116
return toResolutionDetails(result.value);

libs/shared/ofrep-core/src/lib/api/ofrep-api.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { FlagMetadata, ResolutionDetails } from '@openfeature/core';
1+
import { ErrorCode, FlagMetadata, ResolutionDetails, StandardResolutionReasons } from '@openfeature/core';
22
import {
33
EvaluationFlagValue,
44
EvaluationRequest,
@@ -163,12 +163,13 @@ export function handleEvaluationError<T>(
163163
callback?.(resultOrError);
164164

165165
if ('value' in resultOrError) {
166-
const code = resultOrError.value.errorCode;
166+
const code = resultOrError.value.errorCode || ErrorCode.GENERAL;
167167
const message = resultOrError.value.errorCode;
168168
const metadata = resultOrError.value.metadata;
169169

170170
const resolution: ResolutionDetails<T> = {
171171
value: defaultValue,
172+
reason: StandardResolutionReasons.ERROR,
172173
flagMetadata: metadata,
173174
errorCode: code,
174175
errorMessage: message,

libs/shared/ofrep-core/src/lib/model/evaluation.ts

-8
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,6 @@ export interface EvaluationRequest {
77
context?: EvaluationContext;
88
}
99

10-
// export enum EvaluationSuccessReason {
11-
// Static = 'STATIC',
12-
// TargetingMatch = 'TARGETING_MATCH',
13-
// Split = 'SPLIT',
14-
// Disabled = 'DISABLED',
15-
// Unknown = 'UNKNOWN',
16-
// }
17-
1810
export type EvaluationFlagValue = FlagValue;
1911

2012
export interface MetadataResponse {

package-lock.json

+17-14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@
5353
"@nx/web": "20.3.1",
5454
"@nx/workspace": "20.3.1",
5555
"@openfeature/core": "^1.6.0",
56-
"@openfeature/server-sdk": "^1.17.0",
57-
"@openfeature/web-sdk": "^1.4.0",
56+
"@openfeature/server-sdk": "^1.17.1",
57+
"@openfeature/web-sdk": "^1.4.1",
5858
"@opentelemetry/sdk-metrics": "^1.15.0",
5959
"@swc-node/register": "~1.10.0",
6060
"@swc/cli": "~0.6.0",

0 commit comments

Comments
 (0)