This repository has been archived by the owner on Oct 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.ts
53 lines (47 loc) · 1.91 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { RemoteConfigParams } from '../../common/types/remote-config.types';
import { EnvironmentTypes } from '../../common/types/sdk-options.types';
import { doRequest } from '../../common/utils';
import { FeatureFlags, RemoteConfig } from './types';
export default class RemoteConfigService {
static REMOTE_CONFIG_BASE_URL: string = 'https://remote-config.superviz.com';
/**
* @function getRemoteConfig
* @description Retrieves the remote configuration for the specified environment.
* @param environment
The environment to retrieve the remote configuration for. Defaults to EnvironmentTypes.PROD.
* @returns A Promise that resolves with the remote configuration object.
*/
static async getRemoteConfig(
environment: EnvironmentTypes = EnvironmentTypes.PROD,
): Promise<RemoteConfig> {
const { version } = await import('../../../.version.js');
if (environment === EnvironmentTypes.LOCAL) {
const all = await import('../../../.remote-config.js');
return all.default.remoteConfig;
}
const remoteConfigParams: RemoteConfigParams = {
version,
environment,
};
const url = this.createUrl(remoteConfigParams);
return doRequest(url, 'GET', null) as Promise<RemoteConfig>;
}
static async getFeatures(apiKey: string): Promise<FeatureFlags> {
return doRequest(
`${this.REMOTE_CONFIG_BASE_URL}/features/${apiKey}`,
'GET',
undefined,
) as Promise<FeatureFlags>;
}
/**
* @function createUrl
* @description
Creates a URL for fetching remote configuration
data based on the provided version and environment.
* @param {RemoteConfigParams} params - The parameters for creating the URL.
* @returns {string} The URL for fetching remote configuration data.
*/
static createUrl({ version, environment }: RemoteConfigParams): string {
return `${this.REMOTE_CONFIG_BASE_URL}/sdk/${version}?env=${environment}`;
}
}