Skip to content

Commit

Permalink
feat(core): add fallbackvalues to flags
Browse files Browse the repository at this point in the history
  • Loading branch information
cstrnt committed Nov 2, 2023
1 parent f2378cc commit a7ec87d
Show file tree
Hide file tree
Showing 16 changed files with 150 additions and 13 deletions.
7 changes: 7 additions & 0 deletions apps/angular-example/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# angular-example

## 0.0.12

### Patch Changes

- @tryabby/angular@2.0.2
- @tryabby/devtools@5.0.0

## 0.0.11

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion apps/angular-example/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "angular-example",
"version": "0.0.11",
"version": "0.0.12",
"private": true,
"scripts": {
"ng": "ng",
Expand Down
9 changes: 9 additions & 0 deletions apps/web/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# web

## 0.2.30

### Patch Changes

- Updated dependencies
- @tryabby/core@5.1.0
- @tryabby/devtools@5.0.0
- @tryabby/next@5.0.2

## 0.2.29

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion apps/web/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "web",
"version": "0.2.29",
"version": "0.2.30",
"private": true,
"scripts": {
"build": "next build",
Expand Down
7 changes: 7 additions & 0 deletions packages/angular/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @tryabby/angular

## 2.0.2

### Patch Changes

- Updated dependencies
- @tryabby/core@5.1.0

## 2.0.1

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/angular/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tryabby/angular",
"version": "2.0.1",
"version": "2.0.2",
"scripts": {
"ng": "ng",
"start": "ng serve",
Expand Down
6 changes: 6 additions & 0 deletions packages/core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @tryabby/core

## 5.1.0

### Minor Changes

- add fallbackvalues to flags

## 5.0.1

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tryabby/core",
"version": "5.0.1",
"version": "5.1.0",
"description": "",
"main": "dist/index.js",
"files": [
Expand Down
43 changes: 37 additions & 6 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,20 @@ type Settings<
RemoteConfigName,
RemoteConfigValueString
>,
EnvName extends string = string,
> = {
flags?: {
defaultValue?: boolean;
devOverrides?: {
[K in FlagName]?: boolean;
};
fallbackValues?: {
[K in FlagName]?:
| boolean
| {
[K in EnvName]?: boolean;
};
};
};
remoteConfig?: {
defaultValues?: {
Expand Down Expand Up @@ -88,7 +96,12 @@ export type AbbyConfig<
tests?: Tests;
flags?: FlagName[];
remoteConfig?: RemoteConfig;
settings?: Settings<F.NoInfer<FlagName>, F.NoInfer<RemoteConfigName>, F.NoInfer<RemoteConfig>>;
settings?: Settings<
F.NoInfer<FlagName>,
F.NoInfer<RemoteConfigName>,
F.NoInfer<RemoteConfig>,
Environments[number]
>;
debug?: boolean;
};

Expand Down Expand Up @@ -301,13 +314,31 @@ export class Abby<

const defaultValue = this._cfg.settings?.flags?.defaultValue;

// return the defaultValue if exists
if (storedValue === undefined && defaultValue != null) {
return defaultValue;
if (storedValue !== undefined) {
this.log(`getFeatureFlag() => storedValue:`, storedValue);
return storedValue;
}
// before we return the default value we check if there is a fallback value set
const hasFallbackValue = key in (this._cfg.settings?.flags?.fallbackValues ?? {});

if (hasFallbackValue) {
const fallbackValue = this._cfg.settings?.flags?.fallbackValues?.[key as FlagName];
if (fallbackValue !== undefined) {
if (typeof fallbackValue === "boolean") {
this.log(`getFeatureFlag() => fallbackValue:`, fallbackValue);
return fallbackValue;
} else {
const envFallbackValue = fallbackValue[this._cfg.currentEnvironment as string];
if (envFallbackValue !== undefined) {
this.log(`getFeatureFlag() => envFallbackValue:`, envFallbackValue);
return envFallbackValue;
}
}
}
}

this.log(`getFeatureFlag() => storedValue:`, storedValue);
return storedValue;
this.log(`getFeatureFlag() => defaultValue:`, defaultValue ?? false);
return defaultValue ?? false;
}

/**
Expand Down
57 changes: 57 additions & 0 deletions packages/core/tests/base.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,14 @@ describe("Abby", () => {
config1: "String",
config2: "String",
},
flags: ["flag1", "flag2"],
settings: {
flags: {
fallbackValues: {
flag1: false,
flag2: true,
},
},
remoteConfig: {
fallbackValues: {
config1: "fallback1",
Expand All @@ -132,6 +139,56 @@ describe("Abby", () => {
expect(abby.getRemoteConfig("config1")).toBe("fallback1");

expect(abby.getRemoteConfig("config2")).toBe("default");

expect(abby.getFeatureFlag("flag1")).toBe(false);

expect(abby.getFeatureFlag("flag2")).toBe(true);
});

it("uses env specific fallbacks", () => {
let currentEnv = "development";

const abby = new Abby({
environments: ["development", "test"],
projectId: "",
currentEnvironment: currentEnv as any,
remoteConfig: {
config1: "String",
config2: "String",
},
flags: ["flag1", "flag2"],
settings: {
flags: {
fallbackValues: {
flag1: false,
flag2: {
test: true,
},
},
},
remoteConfig: {
fallbackValues: {
config1: "fallback1",
},
defaultValues: {
String: "default",
},
},
},
});

abby.init({ flags: [], tests: [], remoteConfig: [] });

expect(abby.getRemoteConfig("config1")).toBe("fallback1");

expect(abby.getRemoteConfig("config2")).toBe("default");

expect(abby.getFeatureFlag("flag1")).toBe(false);

expect(abby.getFeatureFlag("flag2")).toBe(false);

currentEnv = "test";
expect(abby.getFeatureFlag("flag2")).toBe(true);
});

it("updates a local variant in dev mode", () => {
Expand Down
6 changes: 6 additions & 0 deletions packages/next/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @tryabby/next

## 5.0.2

### Patch Changes

- @tryabby/react@5.0.2

## 5.0.1

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/next/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tryabby/next",
"version": "5.0.1",
"version": "5.0.2",
"description": "",
"main": "dist/index.js",
"files": [
Expand Down
7 changes: 7 additions & 0 deletions packages/react/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @tryabby/react

## 5.0.2

### Patch Changes

- Updated dependencies
- @tryabby/core@5.1.0

## 5.0.1

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/react/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tryabby/react",
"version": "5.0.1",
"version": "5.0.2",
"description": "",
"main": "dist/index.js",
"files": [
Expand Down
7 changes: 7 additions & 0 deletions packages/svelte/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @tryabby/svelte

## 2.0.2

### Patch Changes

- Updated dependencies
- @tryabby/core@5.1.0

## 2.0.1

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/svelte/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tryabby/svelte",
"version": "2.0.1",
"version": "2.0.2",
"main": "dist/index.umd.cjs",
"homepage": "https://docs.tryabby.dev",
"type": "module",
Expand Down

0 comments on commit a7ec87d

Please sign in to comment.