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

Commit

Permalink
Sjekk enablet for feature
Browse files Browse the repository at this point in the history
  • Loading branch information
terjeofnorway committed Oct 8, 2023
1 parent bcdc1a9 commit c12df1f
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions src/server/api-handlers/features.ts
Original file line number Diff line number Diff line change
@@ -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;
}

Expand All @@ -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,
Expand All @@ -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);
};

0 comments on commit c12df1f

Please sign in to comment.