Skip to content

feat: Convert fns module to TS #547

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

Closed
wants to merge 25 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
e755af9
WIP: fallback client
Jun 5, 2020
9ba4356
Undo fallback client changes
Jun 10, 2020
ed7472f
Remove test investigation list
Jun 10, 2020
91c70de
Restore unrelated rollup config changes
Jun 10, 2020
d3e9741
Remove unrelated eslint change
Jun 10, 2020
d2f0830
WIP: unit test support for typescript
Jun 10, 2020
5028195
Use version of mocha types compatible with our version of mocha
Jun 11, 2020
927dc10
Consolidate to a single test command, remove esm, set allowJs: true i…
Jun 11, 2020
10a075c
Sync exclude/include between rollup and tsconfig. Disable declaration…
Jun 11, 2020
1465212
Enable sourcemaps in tsconfig
Jun 11, 2020
aeb1a3d
Foo test example for TS
Jun 11, 2020
9230ea2
Update lint implementation for TypeScript
Jun 11, 2020
43335bb
Undo unrelated
Jun 11, 2020
589809d
Remove duplicate entries in exclude arrays
Jun 16, 2020
bd9367a
Mention Jira number in index.d.ts TODOs
Jun 16, 2020
ee8eb91
Convert string_value_validator module to TS
Jun 16, 2020
afa540b
Add ts-loader to karma + webpack configuration, enabling karma tests …
Jun 17, 2020
63d8648
Convert user_profile_service_validator module to TS (#526)
yavorona Jul 20, 2020
ce2dab4
feat: Convert event_tags_validator module to TS (#528)
yavorona Jul 29, 2020
a489529
feat: Convert project_config_schema and json_schema_validator modules…
yavorona Jul 30, 2020
8de8c80
feat: Convert attributes_validator to TS (#530)
yavorona Jul 30, 2020
71b5f35
fix(json schema validator): Replace empty object type assertion with …
mjc1283 Jul 30, 2020
0b9fd11
Fix configured entry point for json schema validator bundle
Jul 30, 2020
2a0523b
feat: Convert event_tag_utils module to TS (#544)
yavorona Jul 30, 2020
13701c9
Convert fns to TS
yavorona Aug 1, 2020
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
16 changes: 15 additions & 1 deletion packages/optimizely-sdk/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ module.exports = {
commonjs: true,
node: true,
},
extends: 'eslint:recommended',
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
],
globals: {
Atomics: 'readonly',
SharedArrayBuffer: 'readonly',
Expand All @@ -14,7 +17,18 @@ module.exports = {
ecmaVersion: 6,
sourceType: 'module',
},
overrides: [
{
'files': ['*.ts'],
'rules': {
'@typescript-eslint/explicit-module-boundary-types': ['error']
}
}
],
rules: {
'no-prototype-builtins': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-empty-function': 'off',
'no-shadow': 'error',
},
};
12 changes: 12 additions & 0 deletions packages/optimizely-sdk/karma.base.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ module.exports = {

webpack: {
mode: 'production',
module: {
rules: [
{
test: /\.[tj]s$/,
use: 'ts-loader',
exclude: /node_modules/,
},
],
},
resolve: {
extensions: ['.ts', '.js'],
},
},

//browserStack setup
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import { sprintf } from '@optimizely/js-sdk-utils';
import { getLogger } from '@optimizely/js-sdk-logging';

import fns from '../../utils/fns';
import * as fns from '../../utils/fns';
import {
LOG_LEVEL,
LOG_MESSAGES,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
***************************************************************************/
import { sprintf } from '@optimizely/js-sdk-utils';

import fns from '../../utils/fns';
import * as fns from '../../utils/fns';
import {
LOG_LEVEL,
LOG_MESSAGES,
Expand Down
4 changes: 2 additions & 2 deletions packages/optimizely-sdk/lib/core/decision_service/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import { sprintf } from'@optimizely/js-sdk-utils';

import bucketer from '../bucketer';
import enums from '../../utils/enums';
import fns from '../../utils/fns';
import * as fns from '../../utils/fns';
import projectConfig from '../project_config';
import AudienceEvaluator from '../audience_evaluator';
import stringValidator from '../../utils/string_value_validator';
import * as stringValidator from '../../utils/string_value_validator';

var MODULE_NAME = 'DECISION_SERVICE';
var ERROR_MESSAGES = enums.ERROR_MESSAGES;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import AudienceEvaluator from '../audience_evaluator';
import errorHandler from '../../plugins/error_handler';
import eventBuilder from '../../core/event_builder/index.js';
import eventDispatcher from '../../plugins/event_dispatcher/index.node';
import jsonSchemaValidator from '../../utils/json_schema_validator';
import * as jsonSchemaValidator from '../../utils/json_schema_validator';
import {
getTestProjectConfig,
getTestProjectConfigWithFeatures,
Expand Down
18 changes: 13 additions & 5 deletions packages/optimizely-sdk/lib/core/event_builder/event_helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
*/
import { getLogger } from '@optimizely/js-sdk-logging';

import fns from '../../utils/fns';
import * as fns from '../../utils/fns';
import projectConfig from '../project_config';
import eventTagUtils from '../../utils/event_tag_utils';
import attributesValidator from'../../utils/attributes_validator';
import * as eventTagUtils from '../../utils/event_tag_utils';
import * as attributesValidator from'../../utils/attributes_validator';

var logger = getLogger('EVENT_BUILDER');

Expand Down Expand Up @@ -106,6 +106,14 @@ export var buildConversionEvent = function(config) {
var eventTags = config.eventTags;
var eventId = projectConfig.getEventId(configObj, eventKey);

let revenue = null;
let eventValue = null;

if (eventTags) {
revenue = eventTagUtils.getRevenueValue(eventTags, logger);
eventValue = eventTagUtils.getEventValue(eventTags, logger);
}

return {
type: 'conversion',
timestamp: fns.currentTimestamp(),
Expand All @@ -131,8 +139,8 @@ export var buildConversionEvent = function(config) {
key: eventKey,
},

revenue: eventTagUtils.getRevenueValue(eventTags, logger),
value: eventTagUtils.getEventValue(eventTags, logger),
revenue: revenue,
value: eventValue,
tags: eventTags,
};
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import sinon from 'sinon';
import { assert } from 'chai';

import fns from '../../utils/fns';
import * as fns from '../../utils/fns';
import projectConfig from '../project_config';
import { buildImpressionEvent, buildConversionEvent } from './event_helpers';

Expand Down
6 changes: 3 additions & 3 deletions packages/optimizely-sdk/lib/core/event_builder/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import fns from '../../utils/fns';
import * as fns from '../../utils/fns';
import enums from '../../utils/enums';
import projectConfig from '../project_config';
import eventTagUtils from '../../utils/event_tag_utils';
import attributeValidator from '../../utils/attributes_validator';
import * as eventTagUtils from '../../utils/event_tag_utils';
import * as attributeValidator from '../../utils/attributes_validator';

var ACTIVATE_EVENT_KEY = 'campaign_activated';
var CUSTOM_ATTRIBUTE_FEATURE_TYPE = 'custom';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import sinon from 'sinon';
import { assert } from 'chai';

import fns from '../../utils/fns';
import * as fns from '../../utils/fns';
import testData from '../../tests/test_data';
import projectConfig from '../project_config';
import packageJSON from '../../../package.json';
Expand Down
2 changes: 1 addition & 1 deletion packages/optimizely-sdk/lib/core/project_config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
import { sprintf, objectValues } from '@optimizely/js-sdk-utils';

import fns from '../../utils/fns';
import * as fns from '../../utils/fns';
import {
ERROR_MESSAGES,
LOG_MESSAGES,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { forEach, cloneDeep } from 'lodash';
import { getLogger } from '@optimizely/js-sdk-logging';
import { sprintf } from '@optimizely/js-sdk-utils';

import fns from '../../utils/fns';
import * as fns from '../../utils/fns';
import projectConfig from './';
import {
ERROR_MESSAGES,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { sprintf } from '@optimizely/js-sdk-utils';
import { getLogger } from '@optimizely/js-sdk-logging';
import { HttpPollingDatafileManager } from '@optimizely/js-sdk-datafile-manager';

import fns from '../../utils/fns';
import * as fns from '../../utils/fns';
import { ERROR_MESSAGES } from '../../utils/enums';
import projectConfig from '../../core/project_config';
import { getOptimizelyConfig } from '../optimizely_config';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { ERROR_MESSAGES, LOG_MESSAGES } from '../../utils/enums';
import testData from '../../tests/test_data';
import * as optimizelyConfig from '../optimizely_config/index';
import projectConfigManager from './project_config_manager';
import jsonSchemaValidator from '../../utils/json_schema_validator';
import * as jsonSchemaValidator from '../../utils/json_schema_validator';

describe('lib/core/project_config/project_config_manager', function() {
var globalStubErrorHandler;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
/**
* Project Config JSON Schema file used to validate the project json datafile
*/
export var schema = {
import { JSONSchema4 } from 'json-schema';

var schemaDefinition = {
$schema: 'http://json-schema.org/draft-04/schema#',
type: 'object',
properties: {
Expand Down Expand Up @@ -276,4 +278,6 @@ export var schema = {
},
};

export default schema;
const schema = schemaDefinition as JSONSchema4

export default schema
2 changes: 1 addition & 1 deletion packages/optimizely-sdk/lib/index.browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
} from '@optimizely/js-sdk-logging';
import { LocalStoragePendingEventsDispatcher } from '@optimizely/js-sdk-event-processor';

import fns from './utils/fns';
import * as fns from './utils/fns';
import configValidator from './utils/config_validator';
import defaultErrorHandler from './plugins/error_handler';
import defaultEventDispatcher from './plugins/event_dispatcher/index.browser';
Expand Down
10 changes: 10 additions & 0 deletions packages/optimizely-sdk/lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ declare module '@optimizely/optimizely-sdk' {

// The options object given to Optimizely.createInstance.
export interface Config {
// TODO[OASIS-6649]: Don't use object type
// eslint-disable-next-line @typescript-eslint/ban-types
datafile?: object | string;
datafileOptions?: DatafileOptions;
errorHandler?: ErrorHandler;
Expand All @@ -50,6 +52,8 @@ declare module '@optimizely/optimizely-sdk' {
| enums.LOG_LEVEL.INFO
| enums.LOG_LEVEL.NOTSET
| enums.LOG_LEVEL.WARNING;
// TODO[OASIS-6649]: Don't use object type
// eslint-disable-next-line @typescript-eslint/ban-types
jsonSchemaValidator?: object;
userProfileService?: UserProfileService | null;
eventBatchSize?: number;
Expand Down Expand Up @@ -115,6 +119,8 @@ declare module '@optimizely/optimizely-sdk' {
// HTTP method with which to send the event.
httpVerb: 'POST';
// Value to send in the request body, JSON-serialized.
// TODO[OASIS-6649]: Don't use any type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
params: any;
}

Expand Down Expand Up @@ -159,6 +165,8 @@ declare module '@optimizely/optimizely-sdk' {
}

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

Expand All @@ -183,6 +191,8 @@ declare module '@optimizely/optimizely-sdk' {
endOfRange: number;
}>;
audienceIds: string[];
// TODO[OASIS-6649]: Don't use object type
// eslint-disable-next-line @typescript-eslint/ban-types
forcedVariations: object;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/optimizely-sdk/lib/index.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
LogLevel,
} from '@optimizely/js-sdk-logging';

import fns from './utils/fns';
import * as fns from './utils/fns';
import Optimizely from './optimizely';
import enums from './utils/enums';
import loggerPlugin from './plugins/logger';
Expand Down
2 changes: 1 addition & 1 deletion packages/optimizely-sdk/lib/index.react_native.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
LogLevel,
} from '@optimizely/js-sdk-logging';

import fns from './utils/fns';
import * as fns from './utils/fns';
import enums from './utils/enums';
import Optimizely from './optimizely';
import configValidator from './utils/config_validator';
Expand Down
40 changes: 20 additions & 20 deletions packages/optimizely-sdk/lib/optimizely/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@
import { sprintf, objectValues } from '@optimizely/js-sdk-utils';
import * as eventProcessor from '@optimizely/js-sdk-event-processor';

import fns from '../utils/fns'
import * as fns from '../utils/fns'
import { validate } from '../utils/attributes_validator';
import decisionService from '../core/decision_service';
import enums from '../utils/enums';
import { getImpressionEvent, getConversionEvent } from '../core/event_builder/index.js';
import { buildConversionEvent, buildImpressionEvent } from '../core/event_builder/event_helpers';
import eventTagsValidator from '../utils/event_tags_validator';
import * as eventTagsValidator from '../utils/event_tags_validator';
import notificationCenter from '../core/notification_center';
import projectConfig from '../core/project_config';
import userProfileServiceValidator from '../utils/user_profile_service_validator';
import stringValidator from '../utils/string_value_validator';
import * as userProfileServiceValidator from '../utils/user_profile_service_validator';
import * as stringValidator from '../utils/string_value_validator';
import projectConfigManager from '../core/project_config/project_config_manager';

var ERROR_MESSAGES = enums.ERROR_MESSAGES;
Expand Down Expand Up @@ -754,8 +754,8 @@ Optimizely.prototype._getFeatureVariableForType = function(featureKey, variableK
);
return null;
}

var decision = this.decisionService.getVariationForFeature(configObj, featureFlag, userId, attributes);
var decision = this.decisionService.getVariationForFeature(configObj, featureFlag, userId, attributes);
var featureEnabled = decision.variation !== null ? decision.variation.featureEnabled : false;
var variableValue = this._getFeatureVariableValueFromVariation(featureKey, featureEnabled, decision.variation, variable, userId);

Expand All @@ -766,7 +766,7 @@ Optimizely.prototype._getFeatureVariableForType = function(featureKey, variableK
variationKey: decision.variation.key,
};
}

this.notificationCenter.sendNotifications(NOTIFICATION_TYPES.DECISION, {
type: DECISION_NOTIFICATION_TYPES.FEATURE_VARIABLE,
userId: userId,
Expand All @@ -785,10 +785,10 @@ Optimizely.prototype._getFeatureVariableForType = function(featureKey, variableK
};

/**
* Helper method to get the non type-casted value for a variable attached to a
* feature flag. Returns appropriate variable value depending on whether there
* was a matching variation, feature was enabled or not or varible was part of the
* available variation or not. Also logs the appropriate message explaining how it
* Helper method to get the non type-casted value for a variable attached to a
* feature flag. Returns appropriate variable value depending on whether there
* was a matching variation, feature was enabled or not or varible was part of the
* available variation or not. Also logs the appropriate message explaining how it
* evaluated the value of the variable.
*
* @param {string} featureKey Key of the feature whose variable's value is
Expand Down Expand Up @@ -857,7 +857,7 @@ Optimizely.prototype._getFeatureVariableValueFromVariation = function(featureKey
)
);
}

return projectConfig.getTypeCastValue(variableValue, variable.type, this.logger);
}

Expand Down Expand Up @@ -1021,21 +1021,21 @@ Optimizely.prototype.getAllFeatureVariables = function(featureKey, userId, attri
if (!this.__validateInputs({ feature_key: featureKey, user_id: userId }, attributes)) {
return null;
}

var configObj = this.projectConfigManager.getConfig();
if (!configObj) {
return null;
}

var featureFlag = projectConfig.getFeatureFromKey(configObj, featureKey, this.logger);
if (!featureFlag) {
return null;
}

var decision = this.decisionService.getVariationForFeature(configObj, featureFlag, userId, attributes);
var featureEnabled = decision.variation !== null ? decision.variation.featureEnabled : false;
var decision = this.decisionService.getVariationForFeature(configObj, featureFlag, userId, attributes);
var featureEnabled = decision.variation !== null ? decision.variation.featureEnabled : false;
var allVariables = {};

featureFlag.variables.forEach(function (variable) {
allVariables[variable.key] = this._getFeatureVariableValueFromVariation(featureKey, featureEnabled, decision.variation, variable, userId);
}.bind(this));
Expand All @@ -1054,8 +1054,8 @@ Optimizely.prototype.getAllFeatureVariables = function(featureKey, userId, attri
decisionInfo: {
featureKey: featureKey,
featureEnabled: featureEnabled,
source: decision.decisionSource,
variableValues: allVariables,
source: decision.decisionSource,
variableValues: allVariables,
sourceInfo: sourceInfo,
},
});
Expand Down
Loading