Skip to content

Commit 90c5fdb

Browse files
authored
feat: Added project_config_manager interface (#572)
* Added project_config_manager interface * Add comments * Rebase and fix spacing * Improcporate comments and clean up imports
1 parent 7fdb06a commit 90c5fdb

File tree

3 files changed

+135
-50
lines changed

3 files changed

+135
-50
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/**
2+
* Copyright 2020, 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+
17+
declare module '@optimizely/optimizely-sdk/lib/core/projet_config_manager' {
18+
import { ProjectConfig } from '@optimizely/optimizely-sdk/lib/core/project_config';
19+
20+
/**
21+
* ProjectConfigManager provides project config objects via its methods
22+
* getConfig and onUpdate. It uses a DatafileManager to fetch datafiles. It is
23+
* responsible for parsing and validating datafiles, and converting datafile
24+
* JSON objects into project config objects.
25+
* @param {ProjectConfig} config
26+
* @param {Object|string} config.datafile
27+
* @param {Object} config.datafileOptions
28+
* @param {Object} config.jsonSchemaValidator
29+
* @param {string} config.sdkKey
30+
*/
31+
export function ProjectConfigManager(config: ProjectConfig): ProjectConfigManager;
32+
33+
interface ProjectConfigManager {
34+
35+
/**
36+
* Returns the current project config object, or null if no project config object
37+
* is available
38+
* @return {ProjectConfig|null}
39+
*/
40+
getConfig(): ProjectConfig | null;
41+
42+
/**
43+
* Add a listener for project config updates. The listener will be called
44+
* whenever this instance has a new project config object available.
45+
* Returns a dispose function that removes the subscription
46+
* @param {Function} listener
47+
* @return {Function}
48+
*/
49+
onUpdate(): (listener: (config: ProjectConfig) => void) => () => void;
50+
51+
/**
52+
* Returns a Promise that fulfills when this ProjectConfigManager is ready to
53+
* use (meaning it has a valid project config object), or has failed to become
54+
* ready.
55+
*
56+
* Failure can be caused by the following:
57+
* - At least one of sdkKey or datafile is not provided in the constructor argument
58+
* - The provided datafile was invalid
59+
* - The datafile provided by the datafile manager was invalid
60+
* - The datafile manager failed to fetch a datafile
61+
*
62+
* The returned Promise is fulfilled with a result object containing these
63+
* properties:
64+
* - success (boolean): True if this instance is ready to use with a valid
65+
* project config object, or false if it failed to
66+
* become ready
67+
* - reason (string=): If success is false, this is a string property with
68+
* an explanatory message.
69+
* @return {Promise}
70+
*/
71+
onReady(): Promise<{ success: boolean; reason?: string }>;
72+
73+
/**
74+
* Returns the optimizely config object
75+
* @return {OptimizelyConfig}
76+
*/
77+
getOptimizelyConfig(): import('../../shared_types').OptimizelyConfig;
78+
79+
/**
80+
* Stop the internal datafile manager and remove all update listeners
81+
* @return {void}
82+
*/
83+
stop(): void;
84+
}
85+
}

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

+1-50
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ declare module '@optimizely/optimizely-sdk' {
131131
userId: string,
132132
attributes?: import('./shared_types').UserAttributes
133133
): { [variableKey: string]: unknown };
134-
getOptimizelyConfig(): OptimizelyConfig | null;
134+
getOptimizelyConfig(): import('./shared_types').OptimizelyConfig | null;
135135
onReady(options?: { timeout?: number }): Promise<{ success: boolean; reason?: string }>;
136136
close(): Promise<{ success: boolean; reason?: string }>;
137137
}
@@ -193,55 +193,6 @@ declare module '@optimizely/optimizely-sdk' {
193193
eventTags: EventTags;
194194
logEvent: Event;
195195
}
196-
197-
/**
198-
* Optimizely Config Entities
199-
*/
200-
export interface OptimizelyVariable {
201-
id: string;
202-
key: string;
203-
type: string;
204-
value: string;
205-
}
206-
207-
export interface OptimizelyVariation {
208-
id: string;
209-
key: string;
210-
featureEnabled?: boolean;
211-
variablesMap: {
212-
[variableKey: string]: OptimizelyVariable;
213-
};
214-
}
215-
216-
export interface OptimizelyExperiment {
217-
id: string;
218-
key: string;
219-
variationsMap: {
220-
[variationKey: string]: OptimizelyVariation;
221-
};
222-
}
223-
224-
export interface OptimizelyFeature {
225-
id: string;
226-
key: string;
227-
experimentsMap: {
228-
[experimentKey: string]: OptimizelyExperiment;
229-
};
230-
variablesMap: {
231-
[variableKey: string]: OptimizelyVariable;
232-
};
233-
}
234-
235-
export interface OptimizelyConfig {
236-
experimentsMap: {
237-
[experimentKey: string]: OptimizelyExperiment;
238-
};
239-
featuresMap: {
240-
[featureKey: string]: OptimizelyFeature;
241-
};
242-
revision: string;
243-
getDatafile(): string;
244-
}
245196
}
246197

247198
declare module '@optimizely/optimizely-sdk/lib/utils/enums' {

packages/optimizely-sdk/lib/shared_types.ts

+49
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,52 @@ export interface UserProfileService {
3939
lookup(userId: string): UserProfile;
4040
save(profile: UserProfile): void;
4141
}
42+
43+
export interface OptimizelyExperiment {
44+
id: string;
45+
key: string;
46+
variationsMap: {
47+
[variationKey: string]: OptimizelyVariation;
48+
};
49+
}
50+
51+
/**
52+
* Optimizely Config Entities
53+
*/
54+
export interface OptimizelyVariable {
55+
id: string;
56+
key: string;
57+
type: string;
58+
value: string;
59+
}
60+
61+
export interface OptimizelyFeature {
62+
id: string;
63+
key: string;
64+
experimentsMap: {
65+
[experimentKey: string]: OptimizelyExperiment;
66+
};
67+
variablesMap: {
68+
[variableKey: string]: OptimizelyVariable;
69+
};
70+
}
71+
72+
export interface OptimizelyVariation {
73+
id: string;
74+
key: string;
75+
featureEnabled?: boolean;
76+
variablesMap: {
77+
[variableKey: string]: OptimizelyVariable;
78+
};
79+
}
80+
81+
export interface OptimizelyConfig {
82+
experimentsMap: {
83+
[experimentKey: string]: OptimizelyExperiment;
84+
};
85+
featuresMap: {
86+
[featureKey: string]: OptimizelyFeature;
87+
};
88+
revision: string;
89+
getDatafile(): string;
90+
}

0 commit comments

Comments
 (0)