Skip to content

Commit 64ab88d

Browse files
authored
feat: Correct client evaluation typings. (#554)
Some code was refactored to make server and client share evaluation results. This should not have been done because the results are not the same in client and server SDKs. Server SDKs can always produce a reason and client SDKs cannot. This meant that the typing said that reason was required in the client SDK, but it could be null. This is a 'feat' to ensure a minor version bump in case of minor incompatibilities.
1 parent 115bd82 commit 64ab88d

File tree

18 files changed

+109
-191
lines changed

18 files changed

+109
-191
lines changed

packages/shared/common/src/api/data/LDEvaluationDetail.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { LDEvaluationReason } from './LDEvaluationReason';
22
import { LDFlagValue } from './LDFlagValue';
33

4+
// TODO: On major version change "variationIndex" to only be optional and not nullable.
5+
46
/**
57
* An object that combines the result of a feature flag evaluation with information about
68
* how it was calculated.

packages/shared/common/src/internal/evaluation/evaluationDetail.ts

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
11
import ErrorKinds from './ErrorKinds';
2-
import { createErrorEvaluationDetail, createSuccessEvaluationDetail } from './evaluationDetail';
32
import EventFactoryBase, { EvalEventArgs } from './EventFactoryBase';
43

5-
export {
6-
createSuccessEvaluationDetail,
7-
createErrorEvaluationDetail,
8-
ErrorKinds,
9-
EvalEventArgs,
10-
EventFactoryBase,
11-
};
4+
export { ErrorKinds, EvalEventArgs, EventFactoryBase };

packages/shared/sdk-client/src/LDClientImpl.storage.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import {
55
setupMockStreamingProcessor,
66
} from '@launchdarkly/private-js-mocks';
77

8-
import LDEmitter from './api/LDEmitter';
98
import { toMulti } from './context/addAutoEnv';
109
import * as mockResponseJson from './evaluation/mockResponse.json';
1110
import LDClientImpl from './LDClientImpl';
11+
import LDEmitter from './LDEmitter';
1212
import { DeleteFlag, Flags, PatchFlag } from './types';
1313

1414
let mockPlatform: ReturnType<typeof createBasicPlatform>;

packages/shared/sdk-client/src/LDClientImpl.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import {
66
internal,
77
LDClientError,
88
LDContext,
9-
LDEvaluationDetail,
10-
LDEvaluationDetailTyped,
119
LDFlagSet,
1210
LDFlagValue,
1311
LDLogger,
@@ -20,21 +18,25 @@ import {
2018
import { LDStreamProcessor } from '@launchdarkly/js-sdk-common/dist/api/subsystem';
2119

2220
import { ConnectionMode, LDClient, type LDOptions } from './api';
23-
import LDEmitter, { EventName } from './api/LDEmitter';
21+
import { LDEvaluationDetail, LDEvaluationDetailTyped } from './api/LDEvaluationDetail';
2422
import { LDIdentifyOptions } from './api/LDIdentifyOptions';
2523
import Configuration from './configuration';
2624
import { addAutoEnv } from './context/addAutoEnv';
2725
import { ensureKey } from './context/ensureKey';
2826
import createDiagnosticsManager from './diagnostics/createDiagnosticsManager';
27+
import {
28+
createErrorEvaluationDetail,
29+
createSuccessEvaluationDetail,
30+
} from './evaluation/evaluationDetail';
2931
import createEventProcessor from './events/createEventProcessor';
3032
import EventFactory from './events/EventFactory';
3133
import FlagManager from './flag-manager/FlagManager';
3234
import { ItemDescriptor } from './flag-manager/ItemDescriptor';
35+
import LDEmitter, { EventName } from './LDEmitter';
3336
import PollingProcessor from './polling/PollingProcessor';
3437
import { DeleteFlag, Flags, PatchFlag } from './types';
3538

36-
const { createErrorEvaluationDetail, createSuccessEvaluationDetail, ClientMessages, ErrorKinds } =
37-
internal;
39+
const { ClientMessages, ErrorKinds } = internal;
3840

3941
export default class LDClientImpl implements LDClient {
4042
private readonly config: Configuration;
@@ -483,7 +485,7 @@ export default class LDClientImpl implements LDClient {
483485
defaultValue: any,
484486
eventFactory: EventFactory,
485487
typeChecker?: (value: any) => [boolean, string],
486-
): LDFlagValue {
488+
): LDEvaluationDetail {
487489
if (!this.uncheckedContext) {
488490
this.logger.debug(ClientMessages.missingContextKeyNoEvent);
489491
return createErrorEvaluationDetail(ErrorKinds.UserNotSpecified, defaultValue);

packages/shared/sdk-client/src/api/LDClient.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
1-
import {
2-
LDContext,
3-
LDEvaluationDetail,
4-
LDEvaluationDetailTyped,
5-
LDFlagSet,
6-
LDFlagValue,
7-
LDLogger,
8-
} from '@launchdarkly/js-sdk-common';
1+
import { LDContext, LDFlagSet, LDFlagValue, LDLogger } from '@launchdarkly/js-sdk-common';
92

103
import ConnectionMode from './ConnectionMode';
4+
import { LDEvaluationDetail, LDEvaluationDetailTyped } from './LDEvaluationDetail';
115
import { LDIdentifyOptions } from './LDIdentifyOptions';
126

137
/**
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import {
2+
LDEvaluationDetail as CommonDetail,
3+
LDEvaluationDetailTyped as CommonDetailTyped,
4+
LDEvaluationReason,
5+
} from '@launchdarkly/js-sdk-common';
6+
7+
// Implementation note: In client-side SDKs the reason is optional. The common type, which is also
8+
// used by server SDKs, has a required reason. This file contains a client specific
9+
// LDEvaluationDetail which has an optional reason.
10+
11+
// TODO: On major version change "reason" to be optional instead of nullable.
12+
13+
/**
14+
* An object that combines the result of a feature flag evaluation with information about
15+
* how it was calculated.
16+
*
17+
* This is the result of calling `LDClient.variationDetail`.
18+
*/
19+
export type LDEvaluationDetail = Omit<CommonDetail, 'reason'> & {
20+
/**
21+
* An optional object describing the main factor that influenced the flag evaluation value.
22+
*/
23+
reason: LDEvaluationReason | null;
24+
};
25+
26+
/**
27+
* An object that combines the result of a feature flag evaluation with information about
28+
* how it was calculated.
29+
*
30+
* This is the result of calling detailed variation methods.
31+
*/
32+
export type LDEvaluationDetailTyped<TFlag> = Omit<CommonDetailTyped<TFlag>, 'reason'> & {
33+
/**
34+
* An optional object describing the main factor that influenced the flag evaluation value.
35+
*/
36+
reason: LDEvaluationReason | null;
37+
};

packages/shared/sdk-client/src/api/LDInspection.ts

Lines changed: 0 additions & 105 deletions
This file was deleted.

0 commit comments

Comments
 (0)