Skip to content

Commit 5e8d539

Browse files
authored
refactor: Replaced explicit typings with auto generated ones (#745)
## Summary This SDK was originally javascript only. We had to add explicit typings in `lib/index.d.ts` to support typescript users. Now that it has been converted to typescript, we can safely remove `lib/index.d.ts` and autogenerate typings. This PR does exactly that. We also encountered some discrepancies with existing typings so we fixed those too. ## Test plan - Manually tested thoroughly with typescript projects - All existing tests pass.
1 parent 5d526bd commit 5e8d539

File tree

16 files changed

+249
-313
lines changed

16 files changed

+249
-313
lines changed

packages/optimizely-sdk/CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
99

1010
### Changed
1111
- Add package.json script for running Karma tests locally using Chrome ([#651](https://github.com/optimizely/javascript-sdk/pull/651)).
12+
- Replaced explicit typescript typings with auto generated ones ([#745](https://github.com/optimizely/javascript-sdk/pull/745))
1213

1314
## [4.9.1] - January 18, 2022
1415

@@ -18,13 +19,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1819
## [4.9.0] - January 14, 2022
1920

2021
### New Features
21-
* Add a set of new APIs for overriding and managing user-level flag, experiment and delivery rule decisions. These methods can be used for QA and automated testing purposes. They are an extension of the OptimizelyUserContext interface ([#705](https://github.com/optimizely/javascript-sdk/pull/705), [#727](https://github.com/optimizely/javascript-sdk/pull/727), [#729](https://github.com/optimizely/javascript-sdk/pull/729), [#730](https://github.com/optimizely/javascript-sdk/pull/730)):
22+
- Add a set of new APIs for overriding and managing user-level flag, experiment and delivery rule decisions. These methods can be used for QA and automated testing purposes. They are an extension of the OptimizelyUserContext interface ([#705](https://github.com/optimizely/javascript-sdk/pull/705), [#727](https://github.com/optimizely/javascript-sdk/pull/727), [#729](https://github.com/optimizely/javascript-sdk/pull/729), [#730](https://github.com/optimizely/javascript-sdk/pull/730)):
2223
- setForcedDecision
2324
- getForcedDecision
2425
- removeForcedDecision
2526
- removeAllForcedDecisions
2627

27-
* For details, refer to our documentation pages: [OptimizelyUserContext](https://docs.developers.optimizely.com/full-stack/v4.0/docs/optimizelyusercontext-javascript-node) and [Forced Decision methods](https://docs.developers.optimizely.com/full-stack/v4.0/docs/forced-decision-methods-javascript-node).
28+
- For details, refer to our documentation pages: [OptimizelyUserContext](https://docs.developers.optimizely.com/full-stack/v4.0/docs/optimizelyusercontext-javascript-node) and [Forced Decision methods](https://docs.developers.optimizely.com/full-stack/v4.0/docs/forced-decision-methods-javascript-node).
2829

2930
## [4.8.0] - November 29, 2021
3031

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2021, Optimizely
2+
* Copyright 2016-2022, Optimizely
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -45,7 +45,9 @@ import {
4545
} from '../../shared_types';
4646

4747
interface TryCreatingProjectConfigConfig {
48-
datafile: string;
48+
// TODO[OASIS-6649]: Don't use object type
49+
// eslint-disable-next-line @typescript-eslint/ban-types
50+
datafile: string | object;
4951
jsonSchemaValidator?: {
5052
validate(jsonObject: unknown): boolean,
5153
};

packages/optimizely-sdk/lib/core/project_config/project_config_manager.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2019-2021, Optimizely
2+
* Copyright 2019-2022, Optimizely
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -29,7 +29,9 @@ const logger = getLogger();
2929
const MODULE_NAME = 'PROJECT_CONFIG_MANAGER';
3030

3131
interface ProjectConfigManagerConfig {
32-
datafile?: string,
32+
// TODO[OASIS-6649]: Don't use object type
33+
// eslint-disable-next-line @typescript-eslint/ban-types
34+
datafile?: string | object,
3335
jsonSchemaValidator?: {
3436
validate(jsonObject: unknown): boolean,
3537
};
@@ -169,10 +171,12 @@ export class ProjectConfigManager {
169171
* the new config object's revision is newer than the current one, sets/updates the project config
170172
* and optimizely config object instance variables and returns null for the error. If unsuccessful,
171173
* the project config and optimizely config objects will not be updated, and the error is returned.
172-
* @param {string} newDatafile
174+
* @param {string | object} newDatafile
173175
* @returns {Error|null} error or null
174176
*/
175-
private handleNewDatafile(newDatafile: string): Error | null {
177+
// TODO[OASIS-6649]: Don't use object type
178+
// eslint-disable-next-line @typescript-eslint/ban-types
179+
private handleNewDatafile(newDatafile: string | object): Error | null {
176180
const { configObj, error } = tryCreatingProjectConfig({
177181
datafile: newDatafile,
178182
jsonSchemaValidator: this.jsonSchemaValidator,
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Copyright 2022, 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+
export {
17+
UserAttributes,
18+
OptimizelyConfig,
19+
OptimizelyVariable,
20+
OptimizelyVariation,
21+
OptimizelyExperiment,
22+
OptimizelyFeature,
23+
OptimizelyDecisionContext,
24+
OptimizelyForcedDecision,
25+
EventTags,
26+
Event,
27+
EventDispatcher,
28+
DatafileOptions,
29+
OptimizelyOptions,
30+
UserProfileService,
31+
UserProfile,
32+
ListenerPayload,
33+
OptimizelyDecision,
34+
OptimizelyUserContext,
35+
NotificationListener,
36+
Config,
37+
Client,
38+
ActivateListenerPayload,
39+
TrackListenerPayload,
40+
NotificationCenter
41+
} from './shared_types'

packages/optimizely-sdk/lib/index.browser.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2017, 2019-2020 Optimizely
2+
* Copyright 2016-2017, 2019-2022 Optimizely
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -31,7 +31,7 @@ import Optimizely from './optimizely';
3131
import eventProcessorConfigValidator from './utils/event_processor_config_validator';
3232
import { createNotificationCenter } from './core/notification_center';
3333
import { default as eventProcessor } from './plugins/event_processor';
34-
import { SDKOptions, OptimizelyDecideOption } from './shared_types';
34+
import { OptimizelyDecideOption, Client, Config } from './shared_types';
3535
import { createHttpPollingDatafileManager } from './plugins/datafile_manager/http_polling_datafile_manager';
3636

3737
const logger = getLogger();
@@ -47,13 +47,15 @@ let hasRetriedEvents = false;
4747

4848
/**
4949
* Creates an instance of the Optimizely class
50-
* @param {SDKOptions} config
51-
* @return {Optimizely|null} the Optimizely object
50+
* @param {Config} config
51+
* @return {Client|null} the Optimizely client object
5252
* null on error
5353
*/
54-
const createInstance = function(config: SDKOptions): Optimizely | null {
54+
const createInstance = function(config: Config): Client | null {
5555
try {
5656
// TODO warn about setting per instance errorHandler / logger / logLevel
57+
let isValidInstance = false
58+
5759
if (config.errorHandler) {
5860
setErrorHandler(config.errorHandler);
5961
}
@@ -68,10 +70,9 @@ const createInstance = function(config: SDKOptions): Optimizely | null {
6870

6971
try {
7072
configValidator.validate(config);
71-
config.isValidInstance = true;
73+
isValidInstance = true;
7274
} catch (ex) {
7375
logger.error(ex);
74-
config.isValidInstance = false;
7576
}
7677

7778
let eventDispatcher;
@@ -125,6 +126,7 @@ const createInstance = function(config: SDKOptions): Optimizely | null {
125126
errorHandler,
126127
datafileManager: config.sdkKey ? createHttpPollingDatafileManager(config.sdkKey, logger, config.datafile, config.datafileOptions) : undefined,
127128
notificationCenter,
129+
isValidInstance: isValidInstance
128130
};
129131

130132
const optimizely = new Optimizely(optimizelyOptions);
@@ -181,3 +183,5 @@ export default {
181183
__internalResetRetryState,
182184
OptimizelyDecideOption,
183185
};
186+
187+
export * from './export_types'

0 commit comments

Comments
 (0)