[core/apis] moved away from ts decorators
This commit is contained in:
@@ -17,15 +17,13 @@
|
||||
import React, { ComponentType, createContext, FC } from 'react';
|
||||
import { FeatureFlagName } from '../plugin/types';
|
||||
import { FeatureFlagsApi } from '../apis/definitions/featureFlags';
|
||||
import { staticImplements } from '../../testUtils';
|
||||
|
||||
// TODO: figure out where to put implementations of APIs, both inside apps
|
||||
// but also in core/separate package.
|
||||
@staticImplements<FeatureFlagsApi>()
|
||||
export class FeatureFlags {
|
||||
private static readonly localStorageKey = 'featureFlags';
|
||||
class FeatureFlagsImpl implements FeatureFlagsApi {
|
||||
private readonly localStorageKey = 'featureFlags';
|
||||
|
||||
private static getUserEnabledFeatureFlags(): Set<FeatureFlagName> {
|
||||
private getUserEnabledFeatureFlags(): Set<FeatureFlagName> {
|
||||
if (!('localStorage' in window)) {
|
||||
throw new Error(
|
||||
'Feature Flags are not supported on browsers without the Local Storage API',
|
||||
@@ -42,9 +40,7 @@ export class FeatureFlags {
|
||||
}
|
||||
}
|
||||
|
||||
private static saveUserEnabledFeatureFlags(
|
||||
flags: Set<FeatureFlagName>,
|
||||
): void {
|
||||
private saveUserEnabledFeatureFlags(flags: Set<FeatureFlagName>): void {
|
||||
if (!('localStorage' in window)) {
|
||||
throw new Error(
|
||||
'Feature Flags are not supported on browsers without the Local Storage API',
|
||||
@@ -59,32 +55,34 @@ export class FeatureFlags {
|
||||
);
|
||||
}
|
||||
|
||||
static getItem(name: FeatureFlagName): boolean {
|
||||
getItem(name: FeatureFlagName): boolean {
|
||||
return this.getUserEnabledFeatureFlags().has(name);
|
||||
}
|
||||
|
||||
static enable(name: FeatureFlagName): void {
|
||||
enable(name: FeatureFlagName): void {
|
||||
const flags = this.getUserEnabledFeatureFlags();
|
||||
flags.add(name);
|
||||
this.saveUserEnabledFeatureFlags(flags);
|
||||
}
|
||||
|
||||
static disable(name: FeatureFlagName): void {
|
||||
disable(name: FeatureFlagName): void {
|
||||
const flags = this.getUserEnabledFeatureFlags();
|
||||
flags.delete(name);
|
||||
this.saveUserEnabledFeatureFlags(flags);
|
||||
}
|
||||
|
||||
static toggle(name: FeatureFlagName): void {
|
||||
toggle(name: FeatureFlagName): void {
|
||||
(this.getItem(name) ? this.disable : this.enable).bind(this)(name);
|
||||
}
|
||||
}
|
||||
|
||||
export const FeatureFlags = new FeatureFlagsImpl();
|
||||
|
||||
/**
|
||||
* Create a shared React context for Feature Flags.
|
||||
*
|
||||
* This will be used to propagate all available feature flags to
|
||||
* Backstage components. This enables viewing all of the components.
|
||||
* Backstage components. This enables viewing all of the available flags.
|
||||
*/
|
||||
export interface FeatureFlagsEntry {
|
||||
name: FeatureFlagName;
|
||||
|
||||
@@ -1,45 +0,0 @@
|
||||
/*
|
||||
* Copyright 2020 Spotify AB
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/**
|
||||
* This utilizes an experimental TypeScript feature called decorators.
|
||||
* This was originally added to allow us to expose a FeatureFlags API
|
||||
* with static methods (for backwards compatibility primarily). It takes
|
||||
* an existing interface and applies the types to static methods in classes.
|
||||
*
|
||||
* @see https://www.typescriptlang.org/docs/handbook/decorators.html
|
||||
* @example
|
||||
* interface StaticProps {
|
||||
* append(name: string, extra: string): string;
|
||||
* reverse(name: string): string;
|
||||
* }
|
||||
*
|
||||
* @staticImplements<StaticProps>()
|
||||
* class StaticPropsClass {
|
||||
* static append(name, extra) {
|
||||
* return `${name}${extra}`;
|
||||
* }
|
||||
*
|
||||
* static reverse(name) {
|
||||
* return name.reverse();
|
||||
* }
|
||||
* }
|
||||
*/
|
||||
export function staticImplements<T>() {
|
||||
return <U extends T>(constructor: U) => {
|
||||
constructor;
|
||||
};
|
||||
}
|
||||
@@ -2,7 +2,6 @@
|
||||
"extends": "../../tsconfig.json",
|
||||
"include": ["src"],
|
||||
"compilerOptions": {
|
||||
"noImplicitAny": false,
|
||||
"experimentalDecorators": true
|
||||
"noImplicitAny": false
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user