Skip to content
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

feat: enable use of project API key for default deployments #39

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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: 10 additions & 6 deletions src/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,25 @@ const instances = {};
* Initializes a singleton {@link ExperimentClient} identified by the configured
* instance name.
*
* @param apiKey The deployment API Key
* @param apiKey The Amplitude Project API Key used in the client. If a deployment key is provided
* in the config, it will be used instead.
* @param config See {@link ExperimentConfig} for config options
*/
const initialize = (
apiKey: string,
config?: ExperimentConfig,
): ExperimentClient => {
const usedKey = config?.deploymentKey || apiKey;
// Store instances by appending the instance name and api key. Allows for
// initializing multiple default instances for different api keys.
const instanceName = config?.instanceName || Defaults.instanceName;
const instanceKey = `${instanceName}.${apiKey}`;
const instanceKey = `${instanceName}.${usedKey}`;
if (!instances[instanceKey]) {
config = {
userProvider: new DefaultUserProvider(),
...config,
};
instances[instanceKey] = new ExperimentClient(apiKey, config);
instances[instanceKey] = new ExperimentClient(usedKey, config);
}
return instances[instanceKey];
};
Expand All @@ -43,17 +45,19 @@ const initialize = (
* You must be using amplitude-js SDK version 8.17.0+ for this integration to
* work.
*
* @param apiKey The deployment API Key
* @param apiKey The Amplitude Project API Key used in the client. If a deployment key is provided
* in the config, it will be used instead.
* @param config See {@link ExperimentConfig} for config options
*/
const initializeWithAmplitudeAnalytics = (
apiKey: string,
config?: ExperimentConfig,
): ExperimentClient => {
const usedKey = config?.deploymentKey || apiKey;
// Store instances by appending the instance name and api key. Allows for
// initializing multiple default instances for different api keys.
const instanceName = config?.instanceName || Defaults.instanceName;
const instanceKey = `${instanceName}.${apiKey}`;
const instanceKey = `${instanceName}.${usedKey}`;
const connector = AnalyticsConnector.getInstance(instanceName);
if (!instances[instanceKey]) {
config = {
Expand All @@ -63,7 +67,7 @@ const initializeWithAmplitudeAnalytics = (
),
...config,
};
instances[instanceKey] = new ExperimentClient(apiKey, config);
instances[instanceKey] = new ExperimentClient(usedKey, config);
if (config.automaticFetchOnAmplitudeIdentityChange) {
connector.identityStore.addIdentityListener(() => {
instances[instanceKey].fetch();
Expand Down
7 changes: 7 additions & 0 deletions src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ export interface ExperimentConfig {
* (Advanced) Use your own http client.
*/
httpClient?: HttpClient;

/**
* The Experiment deployment key. If provided, it will be used instead of the project API key
*/
deploymentKey?: string;
}

/**
Expand All @@ -149,6 +154,7 @@ export interface ExperimentConfig {
| **userProvider** | `null` |
| **analyticsProvider** | `null` |
| **exposureTrackingProvider** | `null` |
| **deploymentKey** | `undefined` |

*
* @category Configuration
Expand All @@ -171,4 +177,5 @@ export const Defaults: ExperimentConfig = {
userProvider: null,
exposureTrackingProvider: null,
httpClient: FetchHttpClient,
deploymentKey: undefined,
};
Loading