Skip to content

Add hook to transform toggles before updating #249

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

besLisbeth
Copy link

I would like to propose a new configuration option for the unleash-proxy-client-js library that allows clients to asynchronously transform the list of feature toggles before the internal state is updated. This would provide a clean, flexible way to manage which toggles are updated, without overriding core logic such as the fetch method.

About the changes

In the current implementation, when the Unleash proxy client fetches toggles, it updates the internal state with the entire list of toggles returned by the proxy. This behavior works well for most scenarios, but introduces limitations for more complex use cases where only a subset of toggles should be processed. In our project, we want to update the list of feature flags with not very complex logic to not break the client and not to over-complicate the logic that could result in bad UX.

To work around this, I’m currently overriding the fetch method entirely, just to inject toggle transformation logic. However, this approach has significant downsides:

  • Hard to maintain: I must reimplement logic that the library already handles correctly.
  • Error-prone: Custom fetch logic risks breaking behavior like headers, caching, or error handling.
  • Tightly coupled: If internal fetch logic changes in future versions, the override could break silently.

My actual goal is not to replace fetch, but simply to intercept or shape the list of toggles before they’re applied to the client.

Adding my PR to your existing codebase will provide the possibility to the users to dynamically modify or filter the list of toggles after they are fetched from the Unleash proxy or the Unleash Edge — but before they’re applied to the client.

This is especially useful for cases where toggle processing depends on external data, such as user roles, remote config, or custom logic that requires asynchronous operations.

const client = initialize({ 
    url: 'https://app.unleash-hosted.com/demo/proxy', 
    clientKey: 'proxy-client-key', 
    appName: 'my-app', 
    transformToggles: async (toggles) => {
        const allowedToggleNames = await fetchUserTogglePermissions();
        return toggles.filter(toggle => allowedToggleNames.includes(toggle.name));
    },
});

When it could be to used:

  • You need to filter toggles based on async context (e.g., user permissions);
  • You want to enrich toggle data from another service before applying;
  • You want to limit the updates to only relevant features.

Open issues

This PR Closes #248

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Todo
Development

Successfully merging this pull request may close these issues.

Add hook to transform toggles before updating
1 participant