Skip to content
This repository has been archived by the owner on Sep 18, 2024. It is now read-only.

Commit

Permalink
Forbedrer default setting av feature toggle ved utilgjengelig Unleash
Browse files Browse the repository at this point in the history
  • Loading branch information
terjeofnorway committed Oct 10, 2023
1 parent 3e363d2 commit 87400b4
Showing 1 changed file with 18 additions and 19 deletions.
37 changes: 18 additions & 19 deletions src/server/api-handlers/features.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ let unleashInstance: Unleash;

type Features = { [key: string]: boolean };

// If Unleash instance goes down, we don't want screen sharing and chatbot to be
// feature disabled. So we default to true if the instance can not initialize.
// NOTE: Other filters may have other strategies, by setting to false.
const defaultToggles: Features = {
'dekoratoren.screensharing': true,
'dekoratoren.chatbot': true,
};

const initializeUnleash = async () => {
const { UNLEASH_SERVER_API_TOKEN, UNLEASH_SERVER_API_URL } = process.env;
if (!UNLEASH_SERVER_API_TOKEN || !UNLEASH_SERVER_API_URL) {
Expand All @@ -26,7 +34,12 @@ const initializeUnleash = async () => {
return true;
};

initializeUnleash();
const resolveRequestedFeatures = (requestedFeatures: string[]) => {
return requestedFeatures.reduce((acc: Features, feature: string) => {
acc[feature] = unleashInstance ? unleashInstance.isEnabled(feature) : defaultToggles[feature];
return acc;
}, {});
};

export const getFeaturesHandler: RequestHandler = async (req, res) => {
// If for some reason the unleash instance is not initialized,
Expand All @@ -40,23 +53,9 @@ export const getFeaturesHandler: RequestHandler = async (req, res) => {
res.status(400).json({ error: 'Missing feature query parameters' });
return;
}
const requestedFeatures: string[] = forceArray(query.feature).filter((feature) => typeof feature === 'string');

// Cant easily fetch feature toggles when running locally
// so just mock this.
if (process.env.NODE_ENV === 'development') {
const mockFeatures = requestedFeatures.reduce((acc: Features, feature: string) => {
acc[feature] = true;
return acc;
}, {});
res.json(mockFeatures);
return;
}

const features = requestedFeatures.reduce((acc: Features, feature: string) => {
acc[feature] = unleashInstance.isEnabled(feature);
return acc;
}, {});

res.json(features);
const requestedFeatures: string[] = forceArray(query.feature).filter((feature) => typeof feature === 'string');
res.json(resolveRequestedFeatures(requestedFeatures));
};

initializeUnleash();

0 comments on commit 87400b4

Please sign in to comment.