Skip to content

Commit 765ad87

Browse files
authored
feat: Convert decision service to TS (#659)
* WIP WIP Initial conversion with all unit tests pass WIP WIP Define more intefaces Define the rest of interfaces Clean up WIP WIP Finish defining remaining interfaces Clean up * Incorporate comments * Sort imports alphabetically * Fix comment
1 parent adf18af commit 765ad87

File tree

10 files changed

+1208
-1178
lines changed

10 files changed

+1208
-1178
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Copyright 2021, Optimizely
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
import { UserAttributes, Audience } from '../../shared_types';
17+
18+
export interface AudienceEvaluator {
19+
evaluate(
20+
audienceConditions: Array<string | string[]>,
21+
audiencesById: { [id: string]: Audience },
22+
userAttributes?: UserAttributes
23+
): boolean;
24+
}
25+
26+
export function createAudienceEvaluator(config: unknown): AudienceEvaluator;

packages/optimizely-sdk/lib/core/audience_evaluator/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,7 @@ AudienceEvaluator.prototype.evaluateConditionWithUserAttributes = function(userA
111111
};
112112

113113
export default AudienceEvaluator;
114+
115+
export var createAudienceEvaluator = function(UNSTABLE_conditionEvaluators) {
116+
return new AudienceEvaluator(UNSTABLE_conditionEvaluators);
117+
};

packages/optimizely-sdk/lib/core/audience_evaluator/index.tests.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import sinon from 'sinon';
1717
import { assert } from 'chai';
1818
import { getLogger } from '@optimizely/js-sdk-logging';
1919

20-
import AudienceEvaluator from './index';
20+
import { createAudienceEvaluator } from './index';
2121
import * as conditionTreeEvaluator from '../condition_tree_evaluator';
2222
import * as customAttributeConditionEvaluator from '../custom_attribute_condition_evaluator';
2323

@@ -85,7 +85,7 @@ describe('lib/core/audience_evaluator', function() {
8585
describe('APIs', function() {
8686
context('with default condition evaluator', function() {
8787
beforeEach(function() {
88-
audienceEvaluator = new AudienceEvaluator();
88+
audienceEvaluator = createAudienceEvaluator();
8989
});
9090
describe('evaluate', function() {
9191
it('should return true if there are no audiences', function() {
@@ -296,7 +296,7 @@ describe('lib/core/audience_evaluator', function() {
296296
const mockEnvironment = {
297297
special: true,
298298
};
299-
audienceEvaluator = new AudienceEvaluator({
299+
audienceEvaluator = createAudienceEvaluator({
300300
special_condition_type: {
301301
evaluate: function(condition, userAttributes, logger) {
302302
const result = mockEnvironment[condition.value] && userAttributes[condition.match] > 0;
@@ -322,7 +322,7 @@ describe('lib/core/audience_evaluator', function() {
322322

323323
describe('when passing an invalid additional evaluator', function() {
324324
beforeEach(function() {
325-
audienceEvaluator = new AudienceEvaluator({
325+
audienceEvaluator = createAudienceEvaluator({
326326
custom_attribute: {
327327
evaluate: function() {
328328
return false;

packages/optimizely-sdk/lib/core/bucketer/index.ts

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ import murmurhash from 'murmurhash';
2222
import { LogHandler } from '@optimizely/js-sdk-logging';
2323
import {
2424
DecisionResponse,
25-
Experiment,
26-
Variation,
25+
BucketerParams,
2726
TrafficAllocation,
2827
Group,
2928
} from '../../shared_types';
@@ -40,19 +39,6 @@ const MAX_TRAFFIC_VALUE = 10000;
4039
const MODULE_NAME = 'BUCKETER';
4140
const RANDOM_POLICY = 'random';
4241

43-
interface BucketerParams {
44-
experimentId: string;
45-
experimentKey: string;
46-
userId: string;
47-
trafficAllocationConfig: TrafficAllocation[];
48-
experimentKeyMap: { [key: string]: Experiment };
49-
groupIdMap: { [key: string]: Group };
50-
variationIdMap: { [id: string]: Variation } ;
51-
varationIdMapKey: string;
52-
logger: LogHandler;
53-
bucketingId: string;
54-
}
55-
5642
/**
5743
* Determines ID of variation to be shown for the given input params
5844
* @param {Object} bucketerParams

packages/optimizely-sdk/lib/core/decision_service/index.d.ts

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

0 commit comments

Comments
 (0)