Using the Feature Flag API
Introduction
This how-to describes how to query Studio Pro feature flags from an extension. Feature flags are arguments that are passed to Studio Pro in the command line.
Prerequisites
Before starting this how-to, make sure you have completed the following prerequisites:
- This how-to uses the results of Get Started with the Web Extensibility API. Complete that how-to before starting this one.
Set Up the Extension Structure
Replace the contents of your src/main/index.ts file with the following:
import { ComponentContext, IComponent, getStudioProApi } from "@mendix/extensions-api";
const TEST_FLAG = "test-flag";
export const component: IComponent = {
async loaded(componentContext: ComponentContext) {
const studioPro = getStudioProApi(componentContext);
// Add a menu item to the Extensions menu
await studioPro.ui.extensionsMenu.add({
caption: "Check Feature Flag",
menuId: "check-feature-flag-menu",
action: async () => {
const isProvided = await studioPro.ui.featureFlags.isCommandLineFlagProvided(TEST_FLAG);
await studioPro.ui.messageBoxes.show("info", `Feature flag '${TEST_FLAG}': ${isProvided ? "Yes" : "No"}`);
}
});
}
}The code uses the following:
studioPro.ui.featureFlagsto query whether a flag was provided on the command linestudioPro.ui.extensionsMenuto add a menu item that runs the check on demandstudioPro.ui.messageBoxesto show the result to the user
The function is async so you can use await when querying the flag. If you follow the code above, you can add the menu item from the Extensions menu. Click it to see whether the flag was provided. Start Studio Pro with the flag on the command line to see the result change from No to Yes.
The Feature Flag API
The Feature Flag API is available in:
studioPro.ui.featureFlags
It exposes a single method:
isCommandLineFlagProvided(featureFlagName)returns aPromise<boolean>that resolves totrueif the named flag was passed to Studio Pro on the command line, andfalseotherwise.
featureFlagName is the exact name of the flag as it is provided on the command line, without the -- in front of the name. The comparison is by name, so make sure it matches the flag you expect Studio Pro to be started with.
Extensibility Feedback
If you would like to provide additional feedback, you can complete a short survey.
Any feedback is appreciated.