Skip to content

Commit ce2dab4

Browse files
committed
feat: Convert event_tags_validator module to TS (#528)
Summary: Convert event_tags_validator module from JS to TS Revise user_profile_service_validator module Test plan: Existing unit tests
1 parent 63d8648 commit ce2dab4

File tree

3 files changed

+16
-16
lines changed

3 files changed

+16
-16
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import decisionService from '../core/decision_service';
2222
import enums from '../utils/enums';
2323
import { getImpressionEvent, getConversionEvent } from '../core/event_builder/index.js';
2424
import { buildConversionEvent, buildImpressionEvent } from '../core/event_builder/event_helpers';
25-
import eventTagsValidator from '../utils/event_tags_validator';
25+
import * as eventTagsValidator from '../utils/event_tags_validator';
2626
import notificationCenter from '../core/notification_center';
2727
import projectConfig from '../core/project_config';
2828
import * as userProfileServiceValidator from '../utils/user_profile_service_validator';

packages/optimizely-sdk/lib/utils/event_tags_validator/index.js renamed to packages/optimizely-sdk/lib/utils/event_tags_validator/index.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,18 @@ import { sprintf } from '@optimizely/js-sdk-utils';
2121

2222
import { ERROR_MESSAGES } from '../enums';
2323

24-
var MODULE_NAME = 'EVENT_TAGS_VALIDATOR';
24+
const MODULE_NAME = 'EVENT_TAGS_VALIDATOR';
2525

2626
/**
2727
* Validates user's provided event tags
28-
* @param {Object} event tags
29-
* @return {boolean} True if event tags are valid
28+
* @param {unknown} eventTags
29+
* @return {boolean} true if event tags are valid
3030
* @throws If event tags are not valid
3131
*/
32-
export var validate = function(eventTags) {
32+
export function validate(eventTags: unknown): boolean {
3333
if (typeof eventTags === 'object' && !Array.isArray(eventTags) && eventTags !== null) {
3434
return true;
3535
} else {
3636
throw new Error(sprintf(ERROR_MESSAGES.INVALID_EVENT_TAGS, MODULE_NAME));
3737
}
3838
}
39-
40-
export default {
41-
validate: validate,
42-
}

packages/optimizely-sdk/lib/utils/user_profile_service_validator/index.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,19 @@ const MODULE_NAME = 'USER_PROFILE_SERVICE_VALIDATOR';
2626

2727
/**
2828
* Validates user's provided user profile service instance
29-
* @param {object} userProfileServiceInstance
29+
* @param {unknown} userProfileServiceInstance
3030
* @return {boolean} true if the instance is valid
3131
* @throws If the instance is not valid
3232
*/
33-
export function validate(userProfileServiceInstance: Record<string, unknown>): boolean {
34-
if (typeof userProfileServiceInstance.lookup !== 'function') {
35-
throw new Error(sprintf(ERROR_MESSAGES.INVALID_USER_PROFILE_SERVICE, MODULE_NAME, "Missing function 'lookup'"));
36-
} else if (typeof userProfileServiceInstance.save !== 'function') {
37-
throw new Error(sprintf(ERROR_MESSAGES.INVALID_USER_PROFILE_SERVICE, MODULE_NAME, "Missing function 'save'"));
33+
34+
export function validate(userProfileServiceInstance: unknown): boolean {
35+
if (typeof userProfileServiceInstance === 'object' && userProfileServiceInstance !== null) {
36+
if (typeof userProfileServiceInstance['lookup'] !== 'function') {
37+
throw new Error(sprintf(ERROR_MESSAGES.INVALID_USER_PROFILE_SERVICE, MODULE_NAME, "Missing function 'lookup'"));
38+
} else if (typeof userProfileServiceInstance['save'] !== 'function') {
39+
throw new Error(sprintf(ERROR_MESSAGES.INVALID_USER_PROFILE_SERVICE, MODULE_NAME, "Missing function 'save'"));
40+
}
41+
return true;
3842
}
39-
return true;
43+
throw new Error(sprintf(ERROR_MESSAGES.INVALID_USER_PROFILE_SERVICE, MODULE_NAME));
4044
}

0 commit comments

Comments
 (0)