Skip to content

Commit cfe4624

Browse files
authored
Adjust SDK to configcat-common v8.0 (#75)
* Remove deprecated code * Fix tests * Adjust to configcat-common v8.0
1 parent 6e86d5d commit cfe4624

File tree

11 files changed

+271
-705
lines changed

11 files changed

+271
-705
lines changed

package-lock.json

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"license": "MIT",
3434
"homepage": "https://configcat.com",
3535
"dependencies": {
36-
"configcat-common": "^7.0.1",
36+
"configcat-common": "^8.0.0",
3737
"tslib": "^2.4.1"
3838
},
3939
"devDependencies": {

samples/html/index.html

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
});
1717
// You can instantiate the client with different polling modes. See the Docs: https://configcat.com/docs/sdk-reference/js/#polling-modes
1818

19-
configCatClient.getValue("isAwesomeFeatureEnabled", false, function (value) {
19+
configCatClient.getValueAsync("isAwesomeFeatureEnabled", false).then(function (value) {
2020
console.log("isAwesomeFeatureEnabled: " + value);
2121
});
2222

@@ -25,11 +25,9 @@
2525
email: "configcat@example.com"
2626
};
2727
// Read more about the User Object: https://configcat.com/docs/sdk-reference/js/#user-object
28-
configCatClient.getValue("isPOCFeatureEnabled", false, function (value) {
29-
console.log("isPOCFeatureEnabled: " + value);
30-
},
31-
userObject
32-
);
28+
configCatClient.getValueAsync("isPOCFeatureEnabled", false, userObject).then(function (value) {
29+
console.log("isPOCFeatureEnabled: " + value);
30+
});
3331
</script>
3432
</head>
3533

src/Cache.ts

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,24 @@
1-
import type { ICache } from "configcat-common";
2-
import { ProjectConfig } from "configcat-common";
3-
4-
export class LocalStorageCache implements ICache {
5-
cache: { [key: string]: ProjectConfig } = {};
6-
7-
set(key: string, config: ProjectConfig): void {
8-
this.cache[key] = config;
1+
import type { IConfigCatCache } from "configcat-common";
92

3+
export class LocalStorageCache implements IConfigCatCache {
4+
set(key: string, value: string): void {
105
try {
11-
localStorage.setItem(key, btoa(JSON.stringify(config)));
6+
localStorage.setItem(key, btoa(value));
127
}
138
catch (ex) {
149
// local storage is unavailable
1510
}
1611
}
1712

18-
get(key: string): ProjectConfig | null {
19-
const config: ProjectConfig = this.cache[key];
20-
if (config) {
21-
return config;
22-
}
23-
13+
get(key: string): string | undefined {
2414
try {
25-
const configString: string | null = localStorage.getItem(key);
15+
const configString = localStorage.getItem(key);
2616
if (configString) {
27-
const config: ProjectConfig = JSON.parse(atob(configString));
28-
// JSON.parse creates a plain object instance, so we need to manually restore the prototype
29-
// (so we don't run into "... is not a function" errors).
30-
(Object.setPrototypeOf || ((o, proto) => o["__proto__"] = proto))(config, ProjectConfig.prototype);
31-
32-
if (config) {
33-
this.cache[key] = config;
34-
return config;
35-
}
17+
return atob(configString);
3618
}
3719
}
3820
catch (ex) {
3921
// local storage is unavailable or invalid cache value in localstorage
4022
}
41-
42-
return null;
4323
}
4424
}

src/index.ts

Lines changed: 16 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import type { IAutoPollOptions, IConfigCatClient, IConfigCatLogger, ILazyLoadingOptions, IManualPollOptions, LogLevel } from "configcat-common";
1+
import type { IAutoPollOptions, IConfigCatClient, IConfigCatLogger, ILazyLoadingOptions, IManualPollOptions, LogLevel, OverrideBehaviour, SettingValue } from "configcat-common";
2+
import { ExternalConfigCache, FlagOverrides, MapOverrideDataSource, PollingMode } from "configcat-common";
23
import * as configcatcommon from "configcat-common";
3-
import { FlagOverrides, MapOverrideDataSource, PollingMode } from "configcat-common";
44
import { LocalStorageCache } from "./Cache";
55
import { HttpConfigFetcher } from "./ConfigFetcher";
66
import CONFIGCAT_SDK_VERSION from "./Version";
@@ -19,9 +19,9 @@ export function getClient<TMode extends PollingMode | undefined>(sdkKey: string,
1919
return configcatcommon.getClient(sdkKey, pollingMode ?? PollingMode.AutoPoll, options,
2020
{
2121
configFetcher: new HttpConfigFetcher(),
22-
cache: new LocalStorageCache(),
2322
sdkType: "ConfigCat-JS",
24-
sdkVersion: CONFIGCAT_SDK_VERSION
23+
sdkVersion: CONFIGCAT_SDK_VERSION,
24+
defaultCacheFactory: options => new ExternalConfigCache(new LocalStorageCache(), options.logger)
2525
});
2626
}
2727

@@ -32,75 +32,11 @@ export function disposeAllClients(): void {
3232
configcatcommon.disposeAllClients();
3333
}
3434

35-
/**
36-
* Create an instance of ConfigCatClient and setup Auto polling with default options.
37-
* @param {string} sdkkey - SDK Key to access your configuration.
38-
* @param options - Options for Auto polling
39-
* @deprecated This function is obsolete and will be removed from the public API in a future major version. To obtain a ConfigCatClient instance with auto polling for a specific SDK Key, please use the 'getClient(sdkKey, PollingMode.AutoPoll, options, ...)' format.
40-
*/
41-
export function createClient(sdkkey: string, options?: IJSAutoPollOptions): IConfigCatClient {
42-
return createClientWithAutoPoll(sdkkey, options);
43-
}
44-
45-
/**
46-
* Create an instance of ConfigCatClient and setup Auto polling.
47-
* @param {string} sdkkey - SDK Key to access your configuration.
48-
* @param options - Options for Auto polling
49-
* @deprecated This function is obsolete and will be removed from the public API in a future major version. To obtain a ConfigCatClient instance with auto polling for a specific SDK Key, please use the 'getClient(sdkKey, PollingMode.AutoPoll, options, ...)' format.
50-
*/
51-
export function createClientWithAutoPoll(sdkKey: string, options?: IJSAutoPollOptions): IConfigCatClient {
52-
return configcatcommon.createClientWithAutoPoll(
53-
sdkKey,
54-
{
55-
configFetcher: new HttpConfigFetcher(),
56-
cache: new LocalStorageCache(),
57-
sdkType: "ConfigCat-JS",
58-
sdkVersion: CONFIGCAT_SDK_VERSION
59-
},
60-
options);
61-
}
62-
63-
/**
64-
* Create an instance of ConfigCatClient and setup Manual polling.
65-
* @param {string} sdkKey - SDK Key to access your configuration.
66-
* @param options - Options for Manual polling
67-
* @deprecated This function is obsolete and will be removed from the public API in a future major version. To obtain a ConfigCatClient instance with manual polling for a specific SDK Key, please use the 'getClient(sdkKey, PollingMode.ManualPoll, options, ...)' format.
68-
*/
69-
export function createClientWithManualPoll(sdkKey: string, options?: IJSManualPollOptions): IConfigCatClient {
70-
return configcatcommon.createClientWithManualPoll(
71-
sdkKey,
72-
{
73-
configFetcher: new HttpConfigFetcher(),
74-
cache: new LocalStorageCache(),
75-
sdkType: "ConfigCat-JS",
76-
sdkVersion: CONFIGCAT_SDK_VERSION
77-
},
78-
options);
79-
}
80-
81-
/**
82-
* Create an instance of ConfigCatClient and setup Lazy loading.
83-
* @param {string} sdkKey - SDK Key to access your configuration.
84-
* @param options - Options for Lazy loading
85-
* @deprecated This function is obsolete and will be removed from the public API in a future major version. To obtain a ConfigCatClient instance with lazy loading for a specific SDK Key, please use the 'getClient(sdkKey, PollingMode.LazyLoad, options, ...)' format.
86-
*/
87-
export function createClientWithLazyLoad(sdkKey: string, options?: IJSLazyLoadingOptions): IConfigCatClient {
88-
return configcatcommon.createClientWithLazyLoad(
89-
sdkKey,
90-
{
91-
configFetcher: new HttpConfigFetcher(),
92-
cache: new LocalStorageCache(),
93-
sdkType: "ConfigCat-JS",
94-
sdkVersion: CONFIGCAT_SDK_VERSION
95-
},
96-
options);
97-
}
98-
9935
export function createConsoleLogger(logLevel: LogLevel): IConfigCatLogger {
10036
return configcatcommon.createConsoleLogger(logLevel);
10137
}
10238

103-
export function createFlagOverridesFromMap(map: { [name: string]: any }, behaviour: number): FlagOverrides {
39+
export function createFlagOverridesFromMap(map: { [name: string]: NonNullable<SettingValue> }, behaviour: OverrideBehaviour): FlagOverrides {
10440
return new FlagOverrides(new MapOverrideDataSource(map), behaviour);
10541
}
10642

@@ -134,23 +70,29 @@ export { DataGovernance } from "configcat-common";
13470

13571
export type { IConfigCatLogger } from "configcat-common";
13672

73+
export type { LogEventId, LogMessage } from "configcat-common";
74+
13775
export { LogLevel } from "configcat-common";
13876

139-
export type { ICache } from "configcat-common";
77+
export { FormattableLogMessage } from "configcat-common";
78+
79+
export type { IConfigCatCache } from "configcat-common";
80+
81+
export type { IConfig, ISetting, ITargetingRule, IPercentageOption, SettingValue, VariationIdValue } from "configcat-common";
14082

141-
export { ProjectConfig, RolloutRule, RolloutPercentageItem, Setting } from "configcat-common";
83+
export { SettingType, Comparator } from "configcat-common";
14284

14385
export type { IConfigCatClient } from "configcat-common";
14486

14587
export { SettingKeyValue } from "configcat-common";
14688

147-
export type { IEvaluationDetails, SettingTypeOf, SettingValue, VariationIdTypeOf, VariationIdValue } from "configcat-common";
89+
export type { IEvaluationDetails, SettingTypeOf } from "configcat-common";
14890

14991
export { User } from "configcat-common";
15092

151-
export type { IOverrideDataSource } from "configcat-common";
93+
export type { FlagOverrides } from "configcat-common";
15294

153-
export { FlagOverrides, MapOverrideDataSource, OverrideBehaviour } from "configcat-common";
95+
export { OverrideBehaviour } from "configcat-common";
15496

15597
export { RefreshResult } from "configcat-common";
15698

test/HttpTests.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { assert } from "chai";
22
import * as mockxmlhttprequest from "mock-xmlhttprequest";
3-
import * as configcatClient from "../src/index";
43
import { LogLevel } from "../src/index";
54
import { FakeLogger } from "./helpers/fakes";
5+
import * as utils from "./helpers/utils";
66

77
describe("HTTP tests", () => {
88
const sdkKey = "PKDVCLf-Hq-h-kCzMp-L7Q/psuH7BGHoUmdONrzzUOY7A";
@@ -20,7 +20,7 @@ describe("HTTP tests", () => {
2020

2121
const logger = new FakeLogger();
2222

23-
const client = configcatClient.createClientWithManualPoll(sdkKey, {
23+
const client = utils.createClientWithManualPoll(sdkKey, {
2424
requestTimeoutMs,
2525
baseUrl,
2626
logger
@@ -33,7 +33,7 @@ describe("HTTP tests", () => {
3333
const defaultValue = "NOT_CAT";
3434
assert.strictEqual(defaultValue, await client.getValueAsync("stringDefaultCat", defaultValue));
3535

36-
assert.isDefined(logger.messages.find(([level, msg]) => level === LogLevel.Error && msg.startsWith("Request timed out.")));
36+
assert.isDefined(logger.messages.find(([level, msg]) => level === LogLevel.Error && msg.startsWith("Request timed out while trying to fetch config JSON.")));
3737
}
3838
finally {
3939
server.remove();
@@ -50,7 +50,7 @@ describe("HTTP tests", () => {
5050

5151
const logger = new FakeLogger();
5252

53-
const client = configcatClient.createClientWithManualPoll(sdkKey, {
53+
const client = utils.createClientWithManualPoll(sdkKey, {
5454
requestTimeoutMs: 1000,
5555
baseUrl,
5656
logger
@@ -61,7 +61,7 @@ describe("HTTP tests", () => {
6161
const defaultValue = "NOT_CAT";
6262
assert.strictEqual(defaultValue, await client.getValueAsync("stringDefaultCat", defaultValue));
6363

64-
assert.isDefined(logger.messages.find(([level, msg]) => level === LogLevel.Error && msg.startsWith("Double-check your SDK Key")));
64+
assert.isDefined(logger.messages.find(([level, msg]) => level === LogLevel.Error && msg.startsWith("Your SDK Key seems to be wrong.")));
6565
}
6666
finally {
6767
server.remove();
@@ -78,7 +78,7 @@ describe("HTTP tests", () => {
7878

7979
const logger = new FakeLogger();
8080

81-
const client = configcatClient.createClientWithManualPoll(sdkKey, {
81+
const client = utils.createClientWithManualPoll(sdkKey, {
8282
requestTimeoutMs: 1000,
8383
baseUrl,
8484
logger
@@ -89,7 +89,7 @@ describe("HTTP tests", () => {
8989
const defaultValue = "NOT_CAT";
9090
assert.strictEqual(defaultValue, await client.getValueAsync("stringDefaultCat", defaultValue));
9191

92-
assert.isDefined(logger.messages.find(([level, msg]) => level === LogLevel.Error && msg.startsWith("Unexpected HTTP response was received:")));
92+
assert.isDefined(logger.messages.find(([level, msg]) => level === LogLevel.Error && msg.startsWith("Unexpected HTTP response was received while trying to fetch config JSON:")));
9393
}
9494
finally {
9595
server.remove();
@@ -106,7 +106,7 @@ describe("HTTP tests", () => {
106106

107107
const logger = new FakeLogger();
108108

109-
const client = configcatClient.createClientWithManualPoll(sdkKey, {
109+
const client = utils.createClientWithManualPoll(sdkKey, {
110110
requestTimeoutMs: 1000,
111111
baseUrl,
112112
logger
@@ -119,7 +119,7 @@ describe("HTTP tests", () => {
119119

120120
console.log(logger.messages);
121121

122-
assert.isDefined(logger.messages.find(([level, msg]) => level === LogLevel.Error && msg.startsWith("Request failed due to a network or protocol error.")));
122+
assert.isDefined(logger.messages.find(([level, msg]) => level === LogLevel.Error && msg.startsWith("Unexpected error occurred while trying to fetch config JSON.")));
123123
}
124124
finally {
125125
server.remove();

test/IndexTests.ts

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,6 @@ describe("ConfigCatClient index (main)", () => {
1414
});
1515
}
1616

17-
it("createClient ShouldCreateInstance", () => {
18-
const client: IConfigCatClient = configcatClient.createClient("sdkKey");
19-
20-
assert.isDefined(client);
21-
});
22-
23-
it("createClientWithAutoPoll ShouldCreateInstance", () => {
24-
const client: IConfigCatClient = configcatClient.createClientWithAutoPoll("SDKKEY", {
25-
pollIntervalSeconds: 15,
26-
});
27-
assert.isDefined(client);
28-
});
29-
30-
it("createClientWithLazyLoad ShouldCreateInstance", () => {
31-
const client: IConfigCatClient = configcatClient.createClientWithLazyLoad("SDKKEY", {
32-
cacheTimeToLiveSeconds: 15,
33-
});
34-
35-
assert.isDefined(client);
36-
});
37-
38-
it("createClientWithManualPoll ShouldCreateInstance", () => {
39-
const client: IConfigCatClient = configcatClient.createClientWithManualPoll("SDKKEY");
40-
41-
assert.isDefined(client);
42-
});
43-
4417
it("createFlagOverridesFromMap ShouldCreateFlagOverrides", () => {
4518
const overrides: FlagOverrides = configcatClient.createFlagOverridesFromMap({ test: true }, configcatClient.OverrideBehaviour.LocalOnly);
4619

0 commit comments

Comments
 (0)