Skip to content

Commit

Permalink
Export factory function for flag overrides (#57)
Browse files Browse the repository at this point in the history
* Export factory function for flag overrides

* Update index.ts
  • Loading branch information
z4kn4fein authored May 19, 2022
1 parent 83b60d1 commit a5ae664
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 11 deletions.
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "configcat-js",
"version": "5.7.4",
"version": "5.8.0",
"description": "ConfigCat is a configuration as a service that lets you manage your features and configurations without actually deploying new code.",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down Expand Up @@ -30,7 +30,7 @@
"license": "MIT",
"homepage": "https://configcat.com",
"dependencies": {
"configcat-common": "^5.0.2"
"configcat-common": "^5.1.0"
},
"devDependencies": {
"@types/chai": "^4.2.12",
Expand Down
24 changes: 24 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ export function createConsoleLogger(logLevel: LogLevel): configcatcommon.IConfig
return configcatcommon.createConsoleLogger(logLevel);
}

export function createFlagOverridesFromMap(map: { [name: string]: any }, behaviour: number): configcatcommon.FlagOverrides {
return new configcatcommon.FlagOverrides(new configcatcommon.MapOverrideDataSource(map), behaviour);
}

export type IJSAutoPollOptions = configcatcommon.IAutoPollOptions;

export type IJSLazyLoadingOptions = configcatcommon.ILazyLoadingOptions;
Expand All @@ -71,4 +75,24 @@ export const DataGovernance = {
EuOnly: configcatcommon.DataGovernance.EuOnly
};

export const OverrideBehaviour = {
/**
* When evaluating values, the SDK will not use feature flags and settings from the ConfigCat CDN, but it will use
* all feature flags and settings that are loaded from local-override sources.
*/
LocalOnly: configcatcommon.OverrideBehaviour.LocalOnly,
/**
* When evaluating values, the SDK will use all feature flags and settings that are downloaded from the ConfigCat CDN,
* plus all feature flags and settings that are loaded from local-override sources. If a feature flag or a setting is
* defined both in the fetched and the local-override source then the local-override version will take precedence.
*/
LocalOverRemote: configcatcommon.OverrideBehaviour.LocalOverRemote,
/**
* When evaluating values, the SDK will use all feature flags and settings that are downloaded from the ConfigCat CDN,
* plus all feature flags and settings that are loaded from local-override sources. If a feature flag or a setting is
* defined both in the fetched and the local-override source then the fetched version will take precedence.
*/
RemoteOverLocal: configcatcommon.OverrideBehaviour.RemoteOverLocal,
};

export default createClient;
7 changes: 7 additions & 0 deletions test/IndexTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { assert } from "chai";
import "mocha";
import * as configcatClient from "../src/index";
import { IConfigCatClient } from "configcat-common/lib/esm/ConfigCatClient";
import { FlagOverrides } from "configcat-common";

describe("ConfigCatClient index (main)", () => {
it("createClient ShouldCreateInstance", () => {
Expand Down Expand Up @@ -30,4 +31,10 @@ describe("ConfigCatClient index (main)", () => {

assert.isDefined(client);
});

it("createFlagOverridesFromMap ShouldCreateFlagOverrides", () => {
const overrides: FlagOverrides = configcatClient.createFlagOverridesFromMap({ test: true }, configcatClient.OverrideBehaviour.LocalOnly);

assert.isDefined(overrides);
});
});
11 changes: 11 additions & 0 deletions test/IntegrationTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ describe("Integration - ConfigCatClient", () => {

const clientLazyLoad: IConfigCatClient = configcatClient.createClientWithLazyLoad(sdkKey);

const clientOverride: IConfigCatClient = configcatClient.createClientWithAutoPoll(sdkKey, {
flagOverrides: configcatClient.createFlagOverridesFromMap({ stringDefaultCat: "NOT_CAT" }, configcatClient.OverrideBehaviour.LocalOnly)
});

it("Auto poll - getValue() with key: 'stringDefaultCat' should return 'Cat'", (done) => {
const defaultValue = "NOT_CAT";

Expand Down Expand Up @@ -457,4 +461,11 @@ describe("Integration - ConfigCatClient", () => {
assert.equal(settingKeys.double25Pi25E25Gr25Zero, -1);
assert.equal(settingKeys.keySampleText, "Cat");
});

it("Override - local only", async () => {
const defaultValue = "DEFAULT_CAT";

let actual: string = await clientOverride.getValueAsync("stringDefaultCat", defaultValue);
assert.strictEqual(actual, "NOT_CAT");
});
});

0 comments on commit a5ae664

Please sign in to comment.