From c12df1f4a78eb21a4ad7a8dd5337e0e92c0ba129 Mon Sep 17 00:00:00 2001 From: Terje Karlsen Date: Sun, 8 Oct 2023 10:26:02 +0200 Subject: [PATCH] Sjekk enablet for feature --- src/server/api-handlers/features.ts | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/src/server/api-handlers/features.ts b/src/server/api-handlers/features.ts index a8a2bfffd..1c2708807 100644 --- a/src/server/api-handlers/features.ts +++ b/src/server/api-handlers/features.ts @@ -1,11 +1,16 @@ import { RequestHandler } from 'express'; -import { startUnleash, initialize, Unleash } from 'unleash-client'; +import { initialize, Unleash } from 'unleash-client'; let unleashInstance: Unleash; +const featurePrefix = 'dekoratoren'; +const expectedFeatures = ['skjermdeling', 'chatbotscript']; + +type Features = { [key: string]: boolean }; const initializeUnleash = async () => { const { UNLEASH_SERVER_API_TOKEN, UNLEASH_SERVER_API_URL } = process.env; if (!UNLEASH_SERVER_API_TOKEN || !UNLEASH_SERVER_API_URL) { + console.error('Missing UNLEASH_SERVER_API_TOKEN or UNLEASH_SERVER_API_URL'); return false; } @@ -22,17 +27,15 @@ const initializeUnleash = async () => { return true; }; -export const getFeaturesHandler: RequestHandler = async (req, res) => { - // Cant easily fetch feature toggles when running locally - // so just mock this. +initializeUnleash(); +export const getFeaturesHandler: RequestHandler = async (req, res) => { if (!unleashInstance) { await initializeUnleash(); } - console.log(unleashInstance.isSynchronized() ? 'Unleash is synchronized' : 'Unleash is not synchronized'); - - // res.status(500).send('Missing UNLEASH_SERVER_API_TOKEN or UNLEASH_SERVER_API_URL'); + // Cant easily fetch feature toggles when running locally + // so just mock this. if (process.env.NODE_ENV === 'development') { const features = { skjermdeling: true, @@ -42,8 +45,12 @@ export const getFeaturesHandler: RequestHandler = async (req, res) => { return; } - res.json({ - skjermdeling: true, - chatbotscript: true, - }); + console.log(unleashInstance.isEnabled('dekoratoren.skjermdeling')); + + const features = expectedFeatures.reduce((acc: Features, feature: string) => { + acc[feature] = unleashInstance.isEnabled(`${featurePrefix}.${feature}`); + return acc; + }, {}); + + res.json(features); };