2022-08-18 16:23:16 +02:00
|
|
|
import { GroupPolicyEnvironment } from "./types/group-policy-environment";
|
|
|
|
|
2022-08-16 14:05:03 +02:00
|
|
|
function getFlags<T>(envFlags: string | T): T {
|
|
|
|
if (typeof envFlags === "string") {
|
|
|
|
return JSON.parse(envFlags) as T;
|
|
|
|
} else {
|
|
|
|
return envFlags as T;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Placeholder for when we have a relevant feature flag
|
|
|
|
export type Flags = { test?: boolean };
|
|
|
|
export type FlagName = keyof Flags;
|
|
|
|
export function flagEnabled(flag: FlagName): boolean {
|
|
|
|
const flags = getFlags<Flags>(process.env.FLAGS);
|
|
|
|
return flags[flag] == null || flags[flag];
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* These flags are useful for development and testing.
|
|
|
|
* Dev Flags are always OFF in production.
|
|
|
|
*/
|
|
|
|
export type DevFlags = {
|
|
|
|
storeSessionDecrypted?: boolean;
|
2022-08-18 16:23:16 +02:00
|
|
|
managedEnvironment?: GroupPolicyEnvironment;
|
2022-08-16 14:05:03 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export type DevFlagName = keyof DevFlags;
|
|
|
|
|
|
|
|
/**
|
2022-08-18 16:23:16 +02:00
|
|
|
* Gets whether the given dev flag is truthy.
|
2022-08-16 14:05:03 +02:00
|
|
|
* Gets the value of a dev flag from environment.
|
|
|
|
* Will always return false unless in development.
|
|
|
|
* @param flag The name of the dev flag to check
|
|
|
|
* @returns The value of the flag
|
|
|
|
*/
|
|
|
|
export function devFlagEnabled(flag: DevFlagName): boolean {
|
|
|
|
if (process.env.ENV !== "development") {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const devFlags = getFlags<DevFlags>(process.env.DEV_FLAGS);
|
2022-08-18 16:23:16 +02:00
|
|
|
return devFlags[flag] == null || !!devFlags[flag];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the value of a dev flag from environment.
|
|
|
|
* Will always return false unless in development.
|
|
|
|
* @param flag The name of the dev flag to check
|
|
|
|
* @returns The value of the flag
|
|
|
|
* @throws Error if the flag is not enabled
|
|
|
|
*/
|
|
|
|
export function devFlagValue<K extends DevFlagName>(flag: K): DevFlags[K] {
|
|
|
|
if (!devFlagEnabled(flag)) {
|
|
|
|
throw new Error(`This method should not be called, it is protected by a disabled dev flag.`);
|
|
|
|
}
|
|
|
|
|
|
|
|
const devFlags = getFlags<DevFlags>(process.env.DEV_FLAGS);
|
|
|
|
return devFlags[flag];
|
2022-08-16 14:05:03 +02:00
|
|
|
}
|