From 3e6d2ce730d2ef02e57adc0e11b57cf9b1f86f53 Mon Sep 17 00:00:00 2001 From: Bilawal Hameed Date: Thu, 26 Mar 2020 12:56:56 +0100 Subject: [PATCH] [core/api/app] add FeatureFlags API implementation --- packages/core/src/api/app/FeatureFlags.tsx | 73 ++++++++++++++++++++++ packages/core/src/api/index.ts | 1 + 2 files changed, 74 insertions(+) create mode 100644 packages/core/src/api/app/FeatureFlags.tsx diff --git a/packages/core/src/api/app/FeatureFlags.tsx b/packages/core/src/api/app/FeatureFlags.tsx new file mode 100644 index 0000000000..31a3c37341 --- /dev/null +++ b/packages/core/src/api/app/FeatureFlags.tsx @@ -0,0 +1,73 @@ +/* + * 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. + */ + +// import React, { createContext, useContext, useState, FC } from 'react'; +import { FeatureFlagName } from '../plugin/Plugin/types'; +import { FeatureFlagsApi } from '../apis/definitions/featureFlags'; + +// TODO: figure out where to put implementations of APIs, both inside apps +// but also in core/separate package. +export class FeatureFlags implements FeatureFlagsApi { + private readonly localStorageKey = 'featureFlags'; + + private getEnabledFeatureFlags(): Set { + if (!('localStorage' in window)) { + throw new Error( + 'Feature Flags are not supported on browsers without the Local Storage API', + ); + } + + try { + const featureFlagsJson = window.localStorage.getItem( + this.localStorageKey, + ); + return new Set<>(Object.keys(JSON.parse(featureFlagsJson!))); + } catch (err) { + return new Set<>(); + } + } + + private saveFeatureFlags(flags: Set): void { + if (!('localStorage' in window)) { + throw new Error( + 'Feature Flags are not supported on browsers without the Local Storage API', + ); + } + + window.localStorage.setItem( + this.localStorageKey, + JSON.stringify( + [...flags].reduce((list, flag) => ({ ...list, [flag]: true }), {}), + ), + ); + } + + getItem(name: FeatureFlagName): boolean { + return this.getFeatureFlags().has(name); + } + + enable(name: FeatureFlagName): void { + const flags = this.getFeatureFlags(); + flags.add(name); + this.saveFeatureFlags(flags); + } + + disable(name: FeatureFlagName): void { + const flags = this.getFeatureFlags(); + flags.delete(name); + this.saveFeatureFlags(flags); + } +} diff --git a/packages/core/src/api/index.ts b/packages/core/src/api/index.ts index dff75514ae..e60a7efa18 100644 --- a/packages/core/src/api/index.ts +++ b/packages/core/src/api/index.ts @@ -16,4 +16,5 @@ export * from './api'; export * from './apis'; +export { FeatureFlags } from './app/FeatureFlags'; export { useApp } from './app/AppContext';