Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: update sdks, absorb changes #1119

Merged
merged 1 commit into from
Dec 23, 2024
Merged
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
2 changes: 1 addition & 1 deletion libs/providers/multi-provider-web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
"current-version": "echo $npm_package_version"
},
"peerDependencies": {
"@openfeature/web-sdk": "^1.2.1"
"@openfeature/web-sdk": "^1.4.0"
}
}
9 changes: 7 additions & 2 deletions libs/providers/multi-provider-web/src/lib/hook-executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,16 @@ export class HookExecutor {
}
}

finallyHooks(hooks: Hook[] | undefined, hookContext: HookContext, hints: HookHints) {
finallyHooks(
hooks: Hook[] | undefined,
hookContext: HookContext,
evaluationDetails: EvaluationDetails<FlagValue>,
hints: HookHints,
) {
// run "finally" hooks sequentially
for (const hook of hooks ?? []) {
try {
hook?.finally?.(hookContext, hints);
hook?.finally?.(hookContext, evaluationDetails, hints);
} catch (err) {
this.logger.error(`Unhandled error during 'finally' hook: ${err}`);
if (err instanceof Error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@
flagKey,
flagValueType: flagType,
defaultValue,
clientMetadata: {} as any,

Check warning on line 59 in libs/providers/multi-provider-web/src/lib/multi-provider-web.spec.ts

View workflow job for this annotation

GitHub Actions / lint-test-build (18.x)

Unexpected any. Specify a different type

Check warning on line 59 in libs/providers/multi-provider-web/src/lib/multi-provider-web.spec.ts

View workflow job for this annotation

GitHub Actions / lint-test-build (20.x)

Unexpected any. Specify a different type
providerMetadata: {} as any,

Check warning on line 60 in libs/providers/multi-provider-web/src/lib/multi-provider-web.spec.ts

View workflow job for this annotation

GitHub Actions / lint-test-build (18.x)

Unexpected any. Specify a different type

Check warning on line 60 in libs/providers/multi-provider-web/src/lib/multi-provider-web.spec.ts

View workflow job for this annotation

GitHub Actions / lint-test-build (20.x)

Unexpected any. Specify a different type
logger: logger,
};
multi.hooks[0].before?.(hookContext);
Expand Down Expand Up @@ -261,10 +261,10 @@
const hookContext = {
context: context,
flagKey: 'flag',
flagValueType: 'boolean' as any,

Check warning on line 264 in libs/providers/multi-provider-web/src/lib/multi-provider-web.spec.ts

View workflow job for this annotation

GitHub Actions / lint-test-build (18.x)

Unexpected any. Specify a different type

Check warning on line 264 in libs/providers/multi-provider-web/src/lib/multi-provider-web.spec.ts

View workflow job for this annotation

GitHub Actions / lint-test-build (20.x)

Unexpected any. Specify a different type
defaultValue: false,
clientMetadata: {} as any,

Check warning on line 266 in libs/providers/multi-provider-web/src/lib/multi-provider-web.spec.ts

View workflow job for this annotation

GitHub Actions / lint-test-build (18.x)

Unexpected any. Specify a different type

Check warning on line 266 in libs/providers/multi-provider-web/src/lib/multi-provider-web.spec.ts

View workflow job for this annotation

GitHub Actions / lint-test-build (20.x)

Unexpected any. Specify a different type
providerMetadata: {} as any,

Check warning on line 267 in libs/providers/multi-provider-web/src/lib/multi-provider-web.spec.ts

View workflow job for this annotation

GitHub Actions / lint-test-build (18.x)

Unexpected any. Specify a different type

Check warning on line 267 in libs/providers/multi-provider-web/src/lib/multi-provider-web.spec.ts

View workflow job for this annotation

GitHub Actions / lint-test-build (20.x)

Unexpected any. Specify a different type
logger: logger,
};

Expand Down Expand Up @@ -340,10 +340,10 @@
const hookContext = {
context: context,
flagKey: 'flag',
flagValueType: 'boolean' as any,

Check warning on line 343 in libs/providers/multi-provider-web/src/lib/multi-provider-web.spec.ts

View workflow job for this annotation

GitHub Actions / lint-test-build (18.x)

Unexpected any. Specify a different type

Check warning on line 343 in libs/providers/multi-provider-web/src/lib/multi-provider-web.spec.ts

View workflow job for this annotation

GitHub Actions / lint-test-build (20.x)

Unexpected any. Specify a different type
defaultValue: false,
clientMetadata: {} as any,

Check warning on line 345 in libs/providers/multi-provider-web/src/lib/multi-provider-web.spec.ts

View workflow job for this annotation

GitHub Actions / lint-test-build (18.x)

Unexpected any. Specify a different type

Check warning on line 345 in libs/providers/multi-provider-web/src/lib/multi-provider-web.spec.ts

View workflow job for this annotation

GitHub Actions / lint-test-build (20.x)

Unexpected any. Specify a different type
providerMetadata: {} as any,

Check warning on line 346 in libs/providers/multi-provider-web/src/lib/multi-provider-web.spec.ts

View workflow job for this annotation

GitHub Actions / lint-test-build (18.x)

Unexpected any. Specify a different type

Check warning on line 346 in libs/providers/multi-provider-web/src/lib/multi-provider-web.spec.ts

View workflow job for this annotation

GitHub Actions / lint-test-build (20.x)

Unexpected any. Specify a different type
logger: logger,
};

Expand Down Expand Up @@ -390,7 +390,11 @@
]);
const context = {};
callBeforeHook(multiProvider, context, 'flag', 'string', 'default');
expect(multiProvider.resolveStringEvaluation('flag', 'default', context)).toEqual({ value: 'value' });
expect(multiProvider.resolveStringEvaluation('flag', 'default', context)).toEqual({
value: 'value',
flagKey: 'flag',
flagMetadata: {},
});
});

it('evaluates a number variable', () => {
Expand All @@ -406,7 +410,11 @@

callBeforeHook(multiProvider, context, 'flag', 'number', 0);

expect(multiProvider.resolveNumberEvaluation('flag', 0, context)).toEqual({ value: 1 });
expect(multiProvider.resolveNumberEvaluation('flag', 0, context)).toEqual({
value: 1,
flagKey: 'flag',
flagMetadata: {},
});
});

it('evaluates a boolean variable', () => {
Expand All @@ -420,7 +428,11 @@
]);
const context = {};
callBeforeHook(multiProvider, context, 'flag', 'boolean', false);
expect(multiProvider.resolveBooleanEvaluation('flag', false, context)).toEqual({ value: true });
expect(multiProvider.resolveBooleanEvaluation('flag', false, context)).toEqual({
value: true,
flagKey: 'flag',
flagMetadata: {},
});
});

it('evaluates an object variable', () => {
Expand All @@ -434,7 +446,11 @@
]);
const context = {};
callBeforeHook(multiProvider, context, 'flag', 'object', {});
expect(multiProvider.resolveObjectEvaluation('flag', {}, context)).toEqual({ value: { test: true } });
expect(multiProvider.resolveObjectEvaluation('flag', {}, context)).toEqual({
value: { test: true },
flagKey: 'flag',
flagMetadata: {},
});
});
});
describe('first match strategy', () => {
Expand Down Expand Up @@ -509,7 +525,7 @@
new FirstMatchStrategy(),
);
const result = callEvaluation(multiProvider, {});
expect(result).toEqual({ value: true });
expect(result).toEqual({ value: true, flagKey: 'flag', flagMetadata: {} });
expect(provider2.resolveBooleanEvaluation).toHaveBeenCalled();
expect(provider3.resolveBooleanEvaluation).not.toHaveBeenCalled();
});
Expand Down Expand Up @@ -539,7 +555,7 @@
new FirstMatchStrategy(),
);
const result = callEvaluation(multiProvider, {});
expect(result).toEqual({ value: true });
expect(result).toEqual({ value: true, flagKey: 'flag', flagMetadata: {} });
expect(provider2.resolveBooleanEvaluation).toHaveBeenCalled();
expect(provider3.resolveBooleanEvaluation).not.toHaveBeenCalled();
});
Expand Down Expand Up @@ -572,7 +588,7 @@
new FirstSuccessfulStrategy(),
);
const result = callEvaluation(multiProvider, {});
expect(result).toEqual({ value: true });
expect(result).toEqual({ value: true, flagKey: 'flag', flagMetadata: {} });
expect(provider2.resolveBooleanEvaluation).toHaveBeenCalled();
expect(provider3.resolveBooleanEvaluation).not.toHaveBeenCalled();
});
Expand Down Expand Up @@ -607,7 +623,7 @@
expect(provider2.resolveBooleanEvaluation).toHaveBeenCalled();
expect(provider3.resolveBooleanEvaluation).toHaveBeenCalled();

expect(result).toEqual({ value: true });
expect(result).toEqual({ value: true, flagKey: 'flag', flagMetadata: {} });
});

it('calls every provider and returns the fallback value if any disagree, and calls onMismatch', () => {
Expand Down Expand Up @@ -648,21 +664,21 @@
{
provider: provider1,
providerName: 'TestProvider-1',
details: { value: true },
details: { value: true, flagKey: 'flag', flagMetadata: {} },
},
{
provider: provider2,
providerName: 'TestProvider-2',
details: { value: false },
details: { value: false, flagKey: 'flag', flagMetadata: {} },
},
{
provider: provider3,
providerName: 'TestProvider-3',
details: { value: false },
details: { value: false, flagKey: 'flag', flagMetadata: {} },
},
]);

expect(result).toEqual({ value: true });
expect(result).toEqual({ value: true, flagKey: 'flag', flagMetadata: {} });
});

it('returns an error if any provider returns an error', () => {
Expand Down
44 changes: 34 additions & 10 deletions libs/providers/multi-provider-web/src/lib/multi-provider-web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ import {
ProviderMetadata,
BeforeHookContext,
ResolutionDetails,
FlagMetadata,
ErrorCode,
EvaluationDetails,
FlagValue,
OpenFeatureError,
StandardResolutionReasons,
} from '@openfeature/web-sdk';
import { HookExecutor } from './hook-executor';
import { constructAggregateError, throwAggregateErrorFromPromiseResults } from './errors';
Expand Down Expand Up @@ -232,32 +238,31 @@ export class WebMultiProvider implements Provider {
hookContext: HookContext,
hookHints: HookHints,
) {
let evaluationResult: ResolutionDetails<T>;
let evaluationDetails: EvaluationDetails<T>;

try {
this.hookExecutor.beforeHooks(provider.hooks, hookContext, hookHints);

evaluationResult = this.callProviderResolve(
const resolutionDetails = this.callProviderResolve<T>(
provider,
flagKey,
defaultValue,
hookContext.context,
) as ResolutionDetails<T>;

const afterHookEvalDetails = {
...evaluationResult,
flagMetadata: Object.freeze(evaluationResult.flagMetadata ?? {}),
evaluationDetails = {
...resolutionDetails,
flagMetadata: Object.freeze(resolutionDetails.flagMetadata ?? {}),
flagKey,
};

this.hookExecutor.afterHooks(provider.hooks, hookContext, afterHookEvalDetails, hookHints);
return evaluationResult;
this.hookExecutor.afterHooks(provider.hooks, hookContext, evaluationDetails, hookHints);
} catch (error: unknown) {
this.hookExecutor.errorHooks(provider.hooks, hookContext, error, hookHints);
throw error;
} finally {
this.hookExecutor.finallyHooks(provider.hooks, hookContext, hookHints);
evaluationDetails = this.getErrorEvaluationDetails(flagKey, defaultValue, error);
}
this.hookExecutor.finallyHooks(provider.hooks, hookContext, evaluationDetails, hookHints);
return evaluationDetails;
toddbaert marked this conversation as resolved.
Show resolved Hide resolved
}

private callProviderResolve<T extends boolean | string | number | JsonValue>(
Expand Down Expand Up @@ -291,4 +296,23 @@ export class WebMultiProvider implements Provider {
},
];
}

private getErrorEvaluationDetails<T extends FlagValue>(
flagKey: string,
defaultValue: T,
err: unknown,
flagMetadata: FlagMetadata = {},
): EvaluationDetails<T> {
const errorMessage: string = (err as Error)?.message;
const errorCode: ErrorCode = (err as OpenFeatureError)?.code || ErrorCode.GENERAL;

return {
errorCode,
errorMessage,
value: defaultValue,
reason: StandardResolutionReasons.ERROR,
flagMetadata: Object.freeze(flagMetadata),
flagKey,
};
}
}
2 changes: 1 addition & 1 deletion libs/providers/multi-provider/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
"current-version": "echo $npm_package_version"
},
"peerDependencies": {
"@openfeature/server-sdk": "^1.6.0"
"@openfeature/server-sdk": "^1.17.0"
}
}
9 changes: 7 additions & 2 deletions libs/providers/multi-provider/src/lib/hook-executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,16 @@ export class HookExecutor {
}
}

async finallyHooks(hooks: Hook[] | undefined, hookContext: HookContext, hints: HookHints) {
async finallyHooks(
hooks: Hook[] | undefined,
hookContext: HookContext,
evaluationDetails: EvaluationDetails<FlagValue>,
hints: HookHints,
) {
// run "finally" hooks sequentially
for (const hook of hooks ?? []) {
try {
await hook?.finally?.(hookContext, hints);
await hook?.finally?.(hookContext, evaluationDetails, hints);
} catch (err) {
this.logger.error(`Unhandled error during 'finally' hook: ${err}`);
if (err instanceof Error) {
Expand Down
40 changes: 28 additions & 12 deletions libs/providers/multi-provider/src/lib/multi-provider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,11 @@ describe('MultiProvider', () => {
]);
const context = {};
await callBeforeHook(multiProvider, context, 'flag', 'string', 'default');
expect(await multiProvider.resolveStringEvaluation('flag', 'default', context)).toEqual({ value: 'value' });
expect(await multiProvider.resolveStringEvaluation('flag', 'default', context)).toEqual({
value: 'value',
flagKey: 'flag',
flagMetadata: {},
});
});

it('evaluates a number variable', async () => {
Expand All @@ -453,7 +457,11 @@ describe('MultiProvider', () => {

await callBeforeHook(multiProvider, context, 'flag', 'number', 0);

expect(await multiProvider.resolveNumberEvaluation('flag', 0, context)).toEqual({ value: 1 });
expect(await multiProvider.resolveNumberEvaluation('flag', 0, context)).toEqual({
value: 1,
flagKey: 'flag',
flagMetadata: {},
});
});

it('evaluates a boolean variable', async () => {
Expand All @@ -467,7 +475,11 @@ describe('MultiProvider', () => {
]);
const context = {};
await callBeforeHook(multiProvider, context, 'flag', 'boolean', false);
expect(await multiProvider.resolveBooleanEvaluation('flag', false, context)).toEqual({ value: true });
expect(await multiProvider.resolveBooleanEvaluation('flag', false, context)).toEqual({
value: true,
flagKey: 'flag',
flagMetadata: {},
});
});

it('evaluates an object variable', async () => {
Expand All @@ -481,7 +493,11 @@ describe('MultiProvider', () => {
]);
const context = {};
await callBeforeHook(multiProvider, context, 'flag', 'object', {});
expect(await multiProvider.resolveObjectEvaluation('flag', {}, context)).toEqual({ value: { test: true } });
expect(await multiProvider.resolveObjectEvaluation('flag', {}, context)).toEqual({
flagKey: 'flag',
flagMetadata: {},
value: { test: true },
});
});
});
describe('first match strategy', () => {
Expand Down Expand Up @@ -554,7 +570,7 @@ describe('MultiProvider', () => {
new FirstMatchStrategy(),
);
const result = await callEvaluation(multiProvider, {}, logger);
expect(result).toEqual({ value: true });
expect(result).toEqual({ value: true, flagKey: 'flag', flagMetadata: {} });
expect(provider2.resolveBooleanEvaluation).toHaveBeenCalled();
expect(provider3.resolveBooleanEvaluation).not.toHaveBeenCalled();
});
Expand Down Expand Up @@ -582,7 +598,7 @@ describe('MultiProvider', () => {
new FirstMatchStrategy(),
);
const result = await callEvaluation(multiProvider, {}, logger);
expect(result).toEqual({ value: true });
expect(result).toEqual({ value: true, flagKey: 'flag', flagMetadata: {} });
expect(provider2.resolveBooleanEvaluation).toHaveBeenCalled();
expect(provider3.resolveBooleanEvaluation).not.toHaveBeenCalled();
});
Expand Down Expand Up @@ -615,7 +631,7 @@ describe('MultiProvider', () => {
new FirstSuccessfulStrategy(),
);
const result = await callEvaluation(multiProvider, {}, logger);
expect(result).toEqual({ value: true });
expect(result).toEqual({ value: true, flagKey: 'flag', flagMetadata: {} });
expect(provider2.resolveBooleanEvaluation).toHaveBeenCalled();
expect(provider3.resolveBooleanEvaluation).not.toHaveBeenCalled();
});
Expand Down Expand Up @@ -659,7 +675,7 @@ describe('MultiProvider', () => {
expect(provider2.resolveBooleanEvaluation).toHaveBeenCalled();
expect(provider3.resolveBooleanEvaluation).toHaveBeenCalled();

expect(await resultPromise).toEqual({ value: true });
expect(await resultPromise).toEqual({ value: true, flagKey: 'flag', flagMetadata: {} });
});

it('calls every provider and returns the fallback value if any disagree, and calls onMismatch', async () => {
Expand Down Expand Up @@ -700,21 +716,21 @@ describe('MultiProvider', () => {
{
provider: provider1,
providerName: 'TestProvider-1',
details: { value: true },
details: { value: true, flagKey: 'flag', flagMetadata: {} },
},
{
provider: provider2,
providerName: 'TestProvider-2',
details: { value: false },
details: { value: false, flagKey: 'flag', flagMetadata: {} },
},
{
provider: provider3,
providerName: 'TestProvider-3',
details: { value: false },
details: { value: false, flagKey: 'flag', flagMetadata: {} },
},
]);

expect(result).toEqual({ value: true });
expect(result).toEqual({ value: true, flagKey: 'flag', flagMetadata: {} });
});

it('returns an error if any provider returns an error', async () => {
Expand Down
Loading
Loading