Skip to content

Commit

Permalink
Merge pull request #11 from statsig-io/expose-sync-to-top-level
Browse files Browse the repository at this point in the history
Add Sync APIs to Statsig Top Level
  • Loading branch information
daniel-statsig authored Oct 16, 2023
2 parents 4e2eef0 + 5520231 commit 8ce0d54
Showing 1 changed file with 150 additions and 40 deletions.
190 changes: 150 additions & 40 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,16 @@ export const Statsig = {
*
* @param {StatsigUser} user - the user to check this gate value for
* @param {string} gateName - the name of the gate to check
* @returns {Promise<boolean>} - The value of the gate for the user. Gates are off (return false) by default
* @returns {boolean} - The value of the gate for the user. Gates are off (return false) by default
* @throws Error if initialize() was not called first
* @throws Error if the gateName is not provided or not a non-empty string
*/
checkGate(user: StatsigUser, gateName: string): Promise<boolean> {
return this._enforceServer().checkGate(user, gateName);
checkGateSync(user: StatsigUser, gateName: string): boolean {
return this._enforceServer().checkGateSync(user, gateName);
},

getFeatureGate(user: StatsigUser, gateName: string): Promise<FeatureGate> {
return this._enforceServer().getFeatureGate(user, gateName);
getFeatureGateSync(user: StatsigUser, gateName: string): FeatureGate {
return this._enforceServer().getFeatureGateSync(user, gateName);
},

/**
Expand All @@ -84,25 +84,25 @@ export const Statsig = {
*
* @param {StatsigUser} user - the user to check this gate value for
* @param {string} gateName - the name of the gate to check
* @returns {Promise<boolean>} - The value of the gate for the user. Gates are off (return false) by default
* @returns {boolean} - The value of the gate for the user. Gates are off (return false) by default
* @throws Error if initialize() was not called first
* @throws Error if the gateName is not provided or not a non-empty string
*/
checkGateWithExposureLoggingDisabled(
checkGateWithExposureLoggingDisabledSync(
user: StatsigUser,
gateName: string,
): Promise<boolean> {
return this._enforceServer().checkGateWithExposureLoggingDisabled(
): boolean {
return this._enforceServer().checkGateWithExposureLoggingDisabledSync(
user,
gateName,
);
},

getFeatureGateWithExposureLoggingDisabled(
getFeatureGateWithExposureLoggingDisabledSync(
user: StatsigUser,
gateName: string,
): Promise<FeatureGate> {
return this._enforceServer().getFeatureGateWithExposureLoggingDisabled(
): FeatureGate {
return this._enforceServer().getFeatureGateWithExposureLoggingDisabledSync(
user,
gateName,
);
Expand All @@ -124,12 +124,12 @@ export const Statsig = {
*
* @param {StatsigUser} user - the user to evaluate for the dyamic config
* @param {string} configName - the name of the dynamic config to get
* @returns {Promise<DynamicConfig>} - the config for the user
* @returns {DynamicConfig} - the config for the user
* @throws Error if initialize() was not called first
* @throws Error if the configName is not provided or not a non-empty string
*/
getConfig(user: StatsigUser, configName: string): Promise<DynamicConfig> {
return this._enforceServer().getConfig(user, configName);
getConfigSync(user: StatsigUser, configName: string): DynamicConfig {
return this._enforceServer().getConfigSync(user, configName);
},

/**
Expand All @@ -138,15 +138,14 @@ export const Statsig = {
*
* @param {StatsigUser} user - the user to evaluate for the dyamic config
* @param {string} configName - the name of the dynamic config to get
* @returns {Promise<DynamicConfig>} - the config for the user
* @returns {DynamicConfig} - the config for the user
* @throws Error if initialize() was not called first
* @throws Error if the configName is not provided or not a non-empty string
*/
getConfigWithExposureLoggingDisabled(
getConfigWithExposureLoggingDisabledSync(
user: StatsigUser,
configName: string,
): Promise<DynamicConfig> {
return this._enforceServer().getConfigWithExposureLoggingDisabled(
): DynamicConfig {
return this._enforceServer().getConfigWithExposureLoggingDisabledSync(
user,
configName,
);
Expand All @@ -168,15 +167,11 @@ export const Statsig = {
*
* @param {StatsigUser} user - the user to evaluate for the experiment
* @param {string} experimentName - the name of the experiment to get
* @returns {Promise<DynamicConfig>} - the experiment for the user, represented by a Dynamic Config object
* @returns {DynamicConfig} - the experiment for the user, represented by a Dynamic Config object
* @throws Error if initialize() was not called first
* @throws Error if the experimentName is not provided or not a non-empty string
*/
getExperiment(
user: StatsigUser,
experimentName: string,
): Promise<DynamicConfig> {
return this._enforceServer().getExperiment(user, experimentName);
getExperimentSync(user: StatsigUser, experimentName: string): DynamicConfig {
return this._enforceServer().getExperimentSync(user, experimentName);
},

/**
Expand All @@ -185,15 +180,14 @@ export const Statsig = {
*
* @param {StatsigUser} user - the user to evaluate for the experiment
* @param {string} experimentName - the name of the experiment to get
* @returns {Promise<DynamicConfig>} - the experiment for the user, represented by a Dynamic Config object
* @returns {DynamicConfig} - the experiment for the user, represented by a Dynamic Config object
* @throws Error if initialize() was not called first
* @throws Error if the experimentName is not provided or not a non-empty string
*/
getExperimentWithExposureLoggingDisabled(
getExperimentWithExposureLoggingDisabledSync(
user: StatsigUser,
experimentName: string,
): Promise<DynamicConfig> {
return this._enforceServer().getExperimentWithExposureLoggingDisabled(
): DynamicConfig {
return this._enforceServer().getExperimentWithExposureLoggingDisabledSync(
user,
experimentName,
);
Expand All @@ -215,12 +209,11 @@ export const Statsig = {
*
* @param {StatsigUser} user - the user to evaluate for the layer
* @param {string} layerName - the name of the layer to get
* @returns {Promise<Layer>} - the layer for the user, represented by a Layer
* @returns {Layer} - the layer for the user, represented by a Layer
* @throws Error if initialize() was not called first
* @throws Error if the layerName is not provided or not a non-empty string
*/
getLayer(user: StatsigUser, layerName: string): Promise<Layer> {
return this._enforceServer().getLayer(user, layerName);
getLayerSync(user: StatsigUser, layerName: string): Layer {
return this._enforceServer().getLayerSync(user, layerName);
},

/**
Expand All @@ -229,15 +222,15 @@ export const Statsig = {
*
* @param {StatsigUser} user - the user to evaluate for the layer
* @param {string} layerName - the name of the layer to get
* @returns {Promise<Layer>} - the layer for the user, represented by a Layer
* @returns {Layer} - the layer for the user, represented by a Layer
* @throws Error if initialize() was not called first
* @throws Error if the layerName is not provided or not a non-empty string
*/
getLayerWithExposureLoggingDisabled(
getLayerWithExposureLoggingDisabledSync(
user: StatsigUser,
layerName: string,
): Promise<Layer> {
return this._enforceServer().getLayerWithExposureLoggingDisabled(
): Layer {
return this._enforceServer().getLayerWithExposureLoggingDisabledSync(
user,
layerName,
);
Expand Down Expand Up @@ -381,6 +374,123 @@ export const Statsig = {
return this._enforceServer().syncStoreIdLists();
},

//#region Deprecated Async Methods

/**
* @deprecated Please use checkGateSync instead.
* @see https://docs.statsig.com/server/deprecation-notices
*/
checkGate(user: StatsigUser, gateName: string): Promise<boolean> {
return this._enforceServer().checkGate(user, gateName);
},

/**
* @deprecated Please use getFeatureGateSync instead.
* @see https://docs.statsig.com/server/deprecation-notices
*/
getFeatureGate(user: StatsigUser, gateName: string): Promise<FeatureGate> {
return this._enforceServer().getFeatureGate(user, gateName);
},

/**
* @deprecated Please use checkGateWithExposureLoggingDisabledSync instead.
* @see https://docs.statsig.com/server/deprecation-notices
*/
async checkGateWithExposureLoggingDisabled(
user: StatsigUser,
gateName: string,
): Promise<boolean> {
return this._enforceServer().checkGateWithExposureLoggingDisabled(
user,
gateName,
);
},

/**
* @deprecated Please use getFeatureGateWithExposureLoggingDisabledSync instead.
* @see https://docs.statsig.com/server/deprecation-notices
*/
getFeatureGateWithExposureLoggingDisabled(
user: StatsigUser,
gateName: string,
): Promise<FeatureGate> {
return this._enforceServer().getFeatureGateWithExposureLoggingDisabled(
user,
gateName,
);
},

/**
* @deprecated Please use getConfigSync instead.
* @see https://docs.statsig.com/server/deprecation-notices
*/
getConfig(user: StatsigUser, configName: string): Promise<DynamicConfig> {
return this._enforceServer().getConfig(user, configName);
},

/**
* @deprecated Please use getConfigWithExposureLoggingDisabledSync instead.
* @see https://docs.statsig.com/server/deprecation-notices
*/
getConfigWithExposureLoggingDisabled(
user: StatsigUser,
configName: string,
): Promise<DynamicConfig> {
return this._enforceServer().getConfigWithExposureLoggingDisabled(
user,
configName,
);
},

/**
* @deprecated Please use getExperimentSync instead.
* @see https://docs.statsig.com/server/deprecation-notices
*/
getExperiment(
user: StatsigUser,
experimentName: string,
): Promise<DynamicConfig> {
return this._enforceServer().getConfig(user, experimentName);
},

/**
* @deprecated Please use getExperimentWithExposureLoggingDisabledSync instead.
* @see https://docs.statsig.com/server/deprecation-notices
*/
getExperimentWithExposureLoggingDisabled(
user: StatsigUser,
experimentName: string,
): Promise<DynamicConfig> {
return this._enforceServer().getConfigWithExposureLoggingDisabled(
user,
experimentName,
);
},

/**
* @deprecated Please use getLayerSync instead.
* @see https://docs.statsig.com/server/deprecation-notices
*/
getLayer(user: StatsigUser, layerName: string): Promise<Layer> {
return this._enforceServer().getLayer(user, layerName);
},

/**
* @deprecated Please use getLayerWithExposureLoggingDisabledSync instead.
* @see https://docs.statsig.com/server/deprecation-notices
*/
getLayerWithExposureLoggingDisabled(
user: StatsigUser,
layerName: string,
): Promise<Layer> {
return this._enforceServer().getLayerWithExposureLoggingDisabled(
user,
layerName,
);
},

//#endregion

_enforceServer(): StatsigServer {
let instance = StatsigInstanceUtils.getInstance();
if (instance == null) {
Expand Down

0 comments on commit 8ce0d54

Please sign in to comment.