-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ResponseOps] Package for feature flags (#185000)
## Summary This PR creates a package to expose tools for managing feature flags in the ResponseOps codebase. Usage: ``` const featureFlagService = createFeatureFlagService(['test.myFeature', 'test.myFeature.subFeature']); if (featureFlagService.isFeatureFlagSet('test.myFeature')) { // my feature code } ``` The code is typed so if you do `featureFlagService.isFeatureFlagSet('foo')` and `foo` is not part of the feature flags set TS will report an error. ### Checklist Delete any items that are not applicable to this PR. - [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios ### For maintainers - [x] This was checked for breaking API changes and was [labeled appropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process) --------- Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
- Loading branch information
1 parent
75f3af5
commit fe59dd4
Showing
12 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
Validating CODEOWNERS rules …
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# @kbn/response-ops-feature-flags | ||
|
||
This packages exposes a feature flag service that is used in the ResponseOps plugins and packages to handle feature flags. | ||
|
||
## Usage | ||
|
||
### Create feature flag service | ||
|
||
``` | ||
const featureFlagService = createFeatureFlagService(['test.myFeature', 'test.myFeature.subFeature']); // TS will infer the types automatically | ||
``` | ||
|
||
or | ||
|
||
``` | ||
type FeatureFlagValues = 'test.myFeature' | 'test.myOtherFeature' | ||
const featureFlagService = createFeatureFlagService<FeatureFlagValues>(['test.myFeature']); | ||
``` | ||
|
||
### Checking the existence of a feature flag | ||
``` | ||
const featureFlagService = createFeatureFlagService(['test.myFeature', 'test.myFeature.subFeature']); | ||
if (featureFlagService.isFeatureFlagSet('test.myFeature')) { | ||
// my feature code | ||
} | ||
``` |
29 changes: 29 additions & 0 deletions
29
packages/response-ops/feature_flag_service/feature_flag_service.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { createFeatureFlagService } from './feature_flag_service'; | ||
|
||
type FeatureFlagValues = 'test.myFeature' | 'test.myOtherFeature'; | ||
|
||
describe('FeatureFlagService', () => { | ||
it('returns true if the feature exists', () => { | ||
const featureFlagService = createFeatureFlagService(['test.myFeature']); | ||
expect(featureFlagService.isFeatureFlagSet('test.myFeature')).toBe(true); | ||
}); | ||
|
||
it('returns false if the feature does not exist', () => { | ||
const featureFlagService = createFeatureFlagService(['test.myFeature']); | ||
// @ts-expect-error: foo is not part of the valid feature flags | ||
expect(featureFlagService.isFeatureFlagSet('foo')).toBe(false); | ||
}); | ||
|
||
it('returns true if the feature exists (as typed)', () => { | ||
const featureFlagService = createFeatureFlagService<FeatureFlagValues>(['test.myFeature']); | ||
expect(featureFlagService.isFeatureFlagSet('test.myFeature')).toBe(true); | ||
}); | ||
}); |
25 changes: 25 additions & 0 deletions
25
packages/response-ops/feature_flag_service/feature_flag_service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
class FeatureFlag<T extends string = string> { | ||
private readonly featureFlags = new Set<T>(); | ||
|
||
constructor(featureFlags: T[]) { | ||
featureFlags.forEach((featureFlag) => this.featureFlags.add(featureFlag)); | ||
} | ||
|
||
public isFeatureFlagSet(featureFlag: T): boolean { | ||
return this.featureFlags.has(featureFlag); | ||
} | ||
} | ||
|
||
export const createFeatureFlagService = <T extends string>(featureFlags: T[]) => { | ||
return new FeatureFlag<typeof featureFlags[number]>(featureFlags); | ||
}; | ||
|
||
export type FeatureFlagService<T extends string = string> = InstanceType<typeof FeatureFlag<T>>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
export { createFeatureFlagService } from './feature_flag_service'; | ||
export type { FeatureFlagService } from './feature_flag_service'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
module.exports = { | ||
preset: '@kbn/test/jest_node', | ||
rootDir: '../../..', | ||
roots: ['<rootDir>/packages/response-ops/feature_flag_service'], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"type": "shared-common", | ||
"id": "@kbn/response-ops-feature-flag-service", | ||
"owner": "@elastic/response-ops" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"name": "@kbn/response-ops-feature-flag-service", | ||
"private": true, | ||
"version": "1.0.0", | ||
"license": "SSPL-1.0 OR Elastic License 2.0" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"extends": "../../../tsconfig.base.json", | ||
"compilerOptions": { | ||
"outDir": "target/types", | ||
"types": [ | ||
"jest", | ||
"node" | ||
] | ||
}, | ||
"include": [ | ||
"**/*.ts", | ||
], | ||
"exclude": [ | ||
"target/**/*" | ||
], | ||
"kbn_references": [] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters