Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/client-http/src/client/Configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export namespace Configuration {
Match = 'RESOLVE_REASON_MATCH',
NoSegmentMatch = 'RESOLVE_REASON_NO_SEGMENT_MATCH',
NoTreatmentMatch = 'RESOLVE_REASON_NO_TREATMENT_MATCH',
TargetingKeyError = 'RESOLVE_REASON_TARGETING_KEY_ERROR',
Archived = 'RESOLVE_REASON_FLAG_ARCHIVED',
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { ErrorCode, Logger, ProviderStatus } from '@openfeature/web-sdk';
import {
FlagNotFoundError,
Logger,
ParseError,
ProviderStatus,
TypeMismatchError,
InvalidContextError,
} from '@openfeature/js-sdk';
import { ConfidenceClient, Configuration } from '@spotify-confidence/client-http';
import { ConfidenceServerProvider } from './ConfidenceServerProvider';

Expand Down Expand Up @@ -65,6 +72,13 @@ const dummyConfiguration: Configuration = {
value: undefined,
schema: 'undefined',
},
['targeting-error-flag']: {
name: 'targeting-error-flag',
variant: '',
reason: Configuration.ResolveReason.TargetingKeyError,
value: { enabled: true },
schema: { enabled: 'boolean' },
},
},
resolveToken: 'before-each',
context: {},
Expand Down Expand Up @@ -94,6 +108,12 @@ describe('ConfidenceServerProvider', () => {
expect(resolveMock).toHaveBeenCalledTimes(2);
});

it('should throw invalid context error when the reason from confidence is targeting key error', async () => {
expect(() =>
instanceUnderTest.resolveBooleanEvaluation('targeting-error-flag.enabled', false, {}, dummyConsole),
).rejects.toThrow(InvalidContextError);
});

describe('resolveBooleanEvaluation', () => {
it('should resolve a boolean', async () => {
expect(await instanceUnderTest.resolveBooleanEvaluation('testFlag.bool', false, {}, dummyConsole)).toEqual({
Expand Down Expand Up @@ -125,33 +145,21 @@ describe('ConfidenceServerProvider', () => {
});

it('should return default if the flag is not found', async () => {
const actual = await instanceUnderTest.resolveBooleanEvaluation('notARealFlag.bool', false, {}, dummyConsole);

expect(actual).toEqual({
value: false,
errorCode: ErrorCode.FLAG_NOT_FOUND,
reason: 'ERROR',
});
expect(() =>
instanceUnderTest.resolveBooleanEvaluation('notARealFlag.bool', false, {}, dummyConsole),
).rejects.toThrow(new FlagNotFoundError('Flag "notARealFlag" was not found'));
});

it('should return default if the flag requested is the wrong type', async () => {
const actual = await instanceUnderTest.resolveBooleanEvaluation('testFlag.str', false, {}, dummyConsole);

expect(actual).toEqual({
value: false,
errorCode: ErrorCode.TYPE_MISMATCH,
reason: 'ERROR',
});
expect(() => instanceUnderTest.resolveBooleanEvaluation('testFlag.str', false, {}, dummyConsole)).rejects.toThrow(
TypeMismatchError,
);
});

it('should return default if the value requested is not in the flag schema', async () => {
const actual = await instanceUnderTest.resolveBooleanEvaluation('testFlag.404', false, {}, dummyConsole);

expect(actual).toEqual({
value: false,
errorCode: ErrorCode.PARSE_ERROR,
reason: 'ERROR',
});
expect(() => instanceUnderTest.resolveBooleanEvaluation('testFlag.404', false, {}, dummyConsole)).rejects.toThrow(
ParseError,
);
});
});

Expand Down Expand Up @@ -201,33 +209,21 @@ describe('ConfidenceServerProvider', () => {
});

it('should return default if the flag is not found', async () => {
const actual = await instanceUnderTest.resolveNumberEvaluation('notARealFlag.int', 1, {}, dummyConsole);

expect(actual).toEqual({
value: 1,
errorCode: ErrorCode.FLAG_NOT_FOUND,
reason: 'ERROR',
});
expect(() => instanceUnderTest.resolveNumberEvaluation('notARealFlag.int', 1, {}, dummyConsole)).rejects.toThrow(
FlagNotFoundError,
);
});

it('should return default if the flag requested is the wrong type', async () => {
const actual = await instanceUnderTest.resolveNumberEvaluation('testFlag.str', 1, {}, dummyConsole);

expect(actual).toEqual({
value: 1,
errorCode: ErrorCode.TYPE_MISMATCH,
reason: 'ERROR',
});
expect(() => instanceUnderTest.resolveNumberEvaluation('testFlag.str', 1, {}, dummyConsole)).rejects.toThrow(
TypeMismatchError,
);
});

it('should return default if the value requested is not in the flag schema', async () => {
const actual = await instanceUnderTest.resolveNumberEvaluation('testFlag.404', 1, {}, dummyConsole);

expect(actual).toEqual({
value: 1,
errorCode: ErrorCode.PARSE_ERROR,
reason: 'ERROR',
});
expect(() => instanceUnderTest.resolveNumberEvaluation('testFlag.404', 1, {}, dummyConsole)).rejects.toThrow(
ParseError,
);
});
});

Expand Down Expand Up @@ -255,43 +251,27 @@ describe('ConfidenceServerProvider', () => {
});

it('should return default if the flag is not found', async () => {
const actual = await instanceUnderTest.resolveStringEvaluation('notARealFlag.str', 'default', {}, dummyConsole);

expect(actual).toEqual({
value: 'default',
errorCode: ErrorCode.FLAG_NOT_FOUND,
reason: 'ERROR',
});
expect(() =>
instanceUnderTest.resolveStringEvaluation('notARealFlag.str', 'default', {}, dummyConsole),
).rejects.toThrow(new FlagNotFoundError('Flag "notARealFlag" was not found'));
});

it('should return default if the flag requested is the wrong type', async () => {
const actual = await instanceUnderTest.resolveStringEvaluation('testFlag.int', 'default', {}, dummyConsole);

expect(actual).toEqual({
value: 'default',
errorCode: ErrorCode.TYPE_MISMATCH,
reason: 'ERROR',
});
expect(() =>
instanceUnderTest.resolveStringEvaluation('testFlag.int', 'default', {}, dummyConsole),
).rejects.toThrow(TypeMismatchError);
});

it('should return default if the flag requested is the wrong type from nested obj', async () => {
const actual = await instanceUnderTest.resolveStringEvaluation('testFlag.obj.int', 'default', {}, dummyConsole);

expect(actual).toEqual({
value: 'default',
errorCode: ErrorCode.TYPE_MISMATCH,
reason: 'ERROR',
});
expect(() =>
instanceUnderTest.resolveStringEvaluation('testFlag.obj.int', 'default', {}, dummyConsole),
).rejects.toThrow(TypeMismatchError);
});

it('should return default if the value requested is not in the flag schema', async () => {
const actual = await instanceUnderTest.resolveStringEvaluation('testFlag.404', 'default', {}, dummyConsole);

expect(actual).toEqual({
value: 'default',
errorCode: ErrorCode.PARSE_ERROR,
reason: 'ERROR',
});
expect(() =>
instanceUnderTest.resolveStringEvaluation('testFlag.404', 'default', {}, dummyConsole),
).rejects.toThrow(ParseError);
});
});

Expand Down Expand Up @@ -338,52 +318,34 @@ describe('ConfidenceServerProvider', () => {
});

it('should resolve a full object with type mismatch default', async () => {
expect(
await instanceUnderTest.resolveObjectEvaluation(
expect(() =>
instanceUnderTest.resolveObjectEvaluation(
'testFlag.obj',
{
testBool: false,
},
{},
dummyConsole,
),
).toEqual({
errorCode: 'TYPE_MISMATCH',
reason: 'ERROR',
value: {
testBool: false,
},
});
).rejects.toThrow(TypeMismatchError);
});

it('should return default if the flag is not found', async () => {
const actual = await instanceUnderTest.resolveObjectEvaluation('notARealFlag.obj', {}, {}, dummyConsole);

expect(actual).toEqual({
value: {},
errorCode: ErrorCode.FLAG_NOT_FOUND,
reason: 'ERROR',
});
expect(() => instanceUnderTest.resolveObjectEvaluation('notARealFlag.obj', {}, {}, dummyConsole)).rejects.toThrow(
new FlagNotFoundError('Flag "notARealFlag" was not found'),
);
});

it('should return default if the flag requested is the wrong type', async () => {
const actual = await instanceUnderTest.resolveObjectEvaluation('testFlag.str', {}, {}, dummyConsole);

expect(actual).toEqual({
value: {},
errorCode: ErrorCode.TYPE_MISMATCH,
reason: 'ERROR',
});
expect(() => instanceUnderTest.resolveObjectEvaluation('testFlag.str', {}, {}, dummyConsole)).rejects.toThrow(
TypeMismatchError,
);
});

it('should return default if the value requested is not in the flag schema', async () => {
const actual = await instanceUnderTest.resolveObjectEvaluation('testFlag.404', {}, {}, dummyConsole);

expect(actual).toEqual({
value: {},
errorCode: ErrorCode.PARSE_ERROR,
reason: 'ERROR',
});
expect(() => instanceUnderTest.resolveObjectEvaluation('testFlag.404', {}, {}, dummyConsole)).rejects.toThrow(
ParseError,
);
});
});
});
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import {
ErrorCode,
EvaluationContext,
FlagNotFoundError,
InvalidContextError,
JsonValue,
Logger,
ParseError,
Provider,
ProviderMetadata,
ProviderStatus,
ResolutionDetails,
ResolutionReason,
TypeMismatchError,
} from '@openfeature/js-sdk';

import { ConfidenceClient, ResolveContext, Configuration } from '@spotify-confidence/client-http';
Expand Down Expand Up @@ -38,7 +42,7 @@ export class ConfidenceServerProvider implements Provider {
configuration: Configuration,
flagKey: string,
defaultValue: T,
_logger: Logger,
logger: Logger,
): ResolutionDetails<T> {
if (!configuration) {
return {
Expand All @@ -49,63 +53,47 @@ export class ConfidenceServerProvider implements Provider {
}

const [flagName, ...pathParts] = flagKey.split('.');
try {
const flag = configuration.flags[flagName];

if (!flag) {
return {
errorCode: ErrorCode.FLAG_NOT_FOUND,
value: defaultValue,
reason: 'ERROR',
};
}

if (Configuration.ResolveReason.NoSegmentMatch === flag.reason) {
return {
value: defaultValue,
reason: 'DEFAULT',
};
}

let flagValue: Configuration.FlagValue;
try {
flagValue = Configuration.FlagValue.traverse(flag, pathParts.join('.'));
} catch (e) {
return {
errorCode: 'PARSE_ERROR' as ErrorCode,
value: defaultValue,
reason: 'ERROR',
};
}
if (flagValue.value === null) {
return {
value: defaultValue,
reason: mapConfidenceReason(flag.reason),
};
}
if (!Configuration.FlagValue.matches(flagValue, defaultValue)) {
return {
errorCode: 'TYPE_MISMATCH' as ErrorCode,
value: defaultValue,
reason: 'ERROR',
};
}

const flag = configuration.flags[flagName];

if (!flag) {
logger.warn('Flag "%s" was not found', flagName);
throw new FlagNotFoundError(`Flag "${flagName}" was not found`);
}

if (flag.reason === Configuration.ResolveReason.TargetingKeyError) {
throw new InvalidContextError();
}

if (Configuration.ResolveReason.NoSegmentMatch === flag.reason) {
return {
value: flagValue.value as T,
reason: mapConfidenceReason(flag.reason),
variant: flag.variant,
flagMetadata: {
resolveToken: configuration.resolveToken || '',
},
};
} catch (e) {
return {
errorCode: ErrorCode.GENERAL,
value: defaultValue,
reason: 'ERROR',
reason: 'DEFAULT',
};
}

let flagValue: Configuration.FlagValue;
try {
flagValue = Configuration.FlagValue.traverse(flag, pathParts.join('.'));
} catch (e) {
logger.warn('Value with path "%s" was not found in flag "%s"', pathParts.join('.'), flagName);
throw new ParseError();
}

if (!Configuration.FlagValue.matches(flagValue, defaultValue)) {
logger.warn('Value for "%s" is of incorrect type', flagKey);
throw new TypeMismatchError();
}

logger.info('Value for "%s" successfully evaluated', flagKey);
return {
value: flagValue.value === null ? defaultValue : (flagValue.value as T),
reason: mapConfidenceReason(flag.reason),
variant: flag.variant,
flagMetadata: {
resolveToken: configuration.resolveToken || '',
},
};
}

private async fetchFlag<T>(
Expand Down
Loading