Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@
* limitations under the License. *
***************************************************************************/

import { ConditionTree } from '../project_config/entities';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't import.

const AND_CONDITION = 'and';
const OR_CONDITION = 'or';
const NOT_CONDITION = 'not';

const DEFAULT_OPERATOR_TYPES = [AND_CONDITION, OR_CONDITION, NOT_CONDITION];

type ConditionTree<Leaf> = Leaf | unknown[];

type LeafEvaluator<Leaf> = (leaf: Leaf) => boolean | null;

/**
Expand Down
102 changes: 102 additions & 0 deletions packages/optimizely-sdk/lib/core/project_config/entities.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/**
* Copyright 2020, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UserAttributes and EventTags from optimizely-sdk can be defined here and import using it's reference there.


export type UserAttributes = {
// TODO[OASIS-6649]: Don't use any type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[name: string]: any;
};

export type EventTags = {
[key: string]: string | number | boolean;
};

export type ConditionTree<Leaf> = Leaf | unknown[];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UserAttributes, EventTags, and ConditionTree are not related to project_config and should not be defined here.

UserAttributes is already defined in shared_types.ts.

ConditionTree is already defined in condition_tree.ts.


export interface Variation {
id: string;
key: string;
}

export type TrafficAllocation = {
entityId: string;
endOfRange: number;
};

export interface Experiment {
id: string;
key: string;
status: string;
layerId: string;
variations: Variation[];
trafficAllocation: TrafficAllocation[];
audienceIds: string[];
// TODO[OASIS-6649]: Don't use object type
// eslint-disable-next-line @typescript-eslint/ban-types
forcedVariations: object;
}

export interface Group {
experiments: Experiment[];
id: string;
policy: string;
trafficAllocation: TrafficAllocation[];
}

export type Condition = {
name: string;
type: string;
match?: string;
value: string | number | boolean | null;
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This duplicates the definition already existing in custom_attribute_condition_evaluator.ts. It is OK to relocate that definition to this file.


export interface Audience {
id: string;
conditions: ConditionTree<Condition>;
name: string;
}

export interface Event {
experimentIds: string[];
id: string;
key: string;
}

export interface FeatureVariable {
defaultValue: string;
id: string;
key: string;
subType?: string;
type: string;
}

export interface Rollout {
experiments: Experiment[];
id: string;
}

export interface FeatureFlag {
experimentIds: string[];
id: string;
key: string;
rolloutId: string;
variables: FeatureVariable[];
}

export interface Attribute {
id: string;
key: string;
}
19 changes: 19 additions & 0 deletions packages/optimizely-sdk/lib/core/project_config/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Copyright 2020, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

declare module '@optimizely/optimizely-sdk/lib/core/project_config' {
export interface ProjectConfig {}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add function definitions which will be called or used from lib/optimizely/index.js

88 changes: 41 additions & 47 deletions packages/optimizely-sdk/lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,48 +63,73 @@ declare module '@optimizely/optimizely-sdk' {

export interface Client {
notificationCenter: NotificationCenter;
activate(experimentKey: string, userId: string, attributes?: UserAttributes): string | null;
track(eventKey: string, userId: string, attributes?: UserAttributes, eventTags?: EventTags): void;
getVariation(experimentKey: string, userId: string, attributes?: UserAttributes): string | null;
activate(
experimentKey: string,
userId: string,
attributes?: import('../lib/core/project_config/entities').UserAttributes
): string | null;
track(
eventKey: string,
userId: string,
attributes?: import('../lib/core/project_config/entities').UserAttributes,
eventTags?: import('../lib/core/project_config/entities').EventTags
): void;
getVariation(
experimentKey: string,
userId: string,
attributes?: import('../lib/core/project_config/entities').UserAttributes
): string | null;
setForcedVariation(experimentKey: string, userId: string, variationKey: string | null): boolean;
getForcedVariation(experimentKey: string, userId: string): string | null;
isFeatureEnabled(featureKey: string, userId: string, attributes?: UserAttributes): boolean;
getEnabledFeatures(userId: string, attributes?: UserAttributes): string[];
getFeatureVariable(featureKey: string, variableKey: string, userId: string, attributes?: UserAttributes): unknown
isFeatureEnabled(
featureKey: string,
userId: string,
attributes?: import('../lib/core/project_config/entities').UserAttributes
): boolean;
getEnabledFeatures(
userId: string,
attributes?: import('../lib/core/project_config/entities').UserAttributes
): string[];
getFeatureVariable(
featureKey: string,
variableKey: string,
userId: string,
attributes?: import('../lib/core/project_config/entities').UserAttributes
): unknown;
getFeatureVariableBoolean(
featureKey: string,
variableKey: string,
userId: string,
attributes?: UserAttributes
attributes?: import('../lib/core/project_config/entities').UserAttributes
): boolean | null;
getFeatureVariableDouble(
featureKey: string,
variableKey: string,
userId: string,
attributes?: UserAttributes
attributes?: import('../lib/core/project_config/entities').UserAttributes
): number | null;
getFeatureVariableInteger(
featureKey: string,
variableKey: string,
userId: string,
attributes?: UserAttributes
attributes?: import('../lib/core/project_config/entities').UserAttributes
): number | null;
getFeatureVariableString(
featureKey: string,
variableKey: string,
userId: string,
attributes?: UserAttributes
attributes?: import('../lib/core/project_config/entities').UserAttributes
): string | null;
getFeatureVariableJSON(
featureKey: string,
variableKey: string,
userId: string,
attributes?: UserAttributes
attributes?: import('../lib/core/project_config/entities').UserAttributes
): unknown;
getAllFeatureVariables(
featureKey: string,
userId: string,
attributes?: UserAttributes
attributes?: import('../lib/core/project_config/entities').UserAttributes
): { [variableKey: string]: unknown };
getOptimizelyConfig(): OptimizelyConfig | null;
onReady(options?: { timeout?: number }): Promise<{ success: boolean; reason?: string }>;
Expand Down Expand Up @@ -155,52 +180,21 @@ declare module '@optimizely/optimizely-sdk' {

export interface ListenerPayload {
userId: string;
attributes: UserAttributes;
attributes: import('../lib/core/project_config/entities').UserAttributes;
}

export interface ActivateListenerPayload extends ListenerPayload {
experiment: Experiment;
variation: Variation;
experiment: import('./core/project_config/entities').Experiment;
variation: import('./core/project_config/entities').Variation;
logEvent: Event;
}

export type UserAttributes = {
// TODO[OASIS-6649]: Don't use any type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[name: string]: any;
};

export type EventTags = {
[key: string]: string | number | boolean;
};

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can't remove these from here, these are part of the public interface used by SDK users.

export interface TrackListenerPayload extends ListenerPayload {
eventKey: string;
eventTags: EventTags;
eventTags: import('../lib/core/project_config/entities').EventTags;
logEvent: Event;
}

interface Experiment {
id: string;
key: string;
status: string;
layerId: string;
variations: Variation[];
trafficAllocation: Array<{
entityId: string;
endOfRange: number;
}>;
audienceIds: string[];
// TODO[OASIS-6649]: Don't use object type
// eslint-disable-next-line @typescript-eslint/ban-types
forcedVariations: object;
}

interface Variation {
id: string;
key: string;
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can't remove this, this is part of the public interface used by SDK users.

// Information about past bucketing decisions for a user.
export interface UserProfile {
user_id: string;
Expand Down