Skip to content
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

POC: migrate provider helper #660

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions examples/migration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const { initialize, migrateFlag } = require('../lib');

const client = initialize({
appName: 'migration-application',
url: 'https://sandbox.getunleash.io/enterprise/api/',
refreshInterval: 3000,
metricsInterval: 3000,
environment: 'production',
customHeaders: {
Authorization: 'migration:development.924579bdb2542b96b8c00d3a22f2345c95b521e6b3717ad57ff8d43b',
},
});
client.on('error', console.error);
client.on('warn', console.log);

const homebrewFlagProvider = (_flag) => true || _flag; // mock

setInterval(async () => {
const context = {
userId: `${Math.random() * 100}`,
sessionId: Math.round(Math.random() * 1000),
remoteAddress: '127.0.0.1',
};
const toggleName = 'initiative';
const value = await migrateFlag(toggleName, homebrewFlagProvider, context);
console.log(`${toggleName} enabled: ${value}`);
}, 1000);
21 changes: 21 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,24 @@ export async function flushMetrics(): Promise<void> {
export async function destroyWithFlush(): Promise<void> {
return instance && instance.destroyWithFlush();
}

export async function migrateFlag(
flag: string,
evaluateLegacy: (flagName: string) => boolean | Promise<boolean>,
context?: Context,
) {
const value = evaluateLegacy('flag');

if (!instance) return value;

if (
instance.isEnabled('isSafeMode') ||
!instance.isEnabled('isRolledOut', { flag, ...context })
) {
(async () => instance.count(flag, await value))(); // only get metrics

return value;
}

return instance.isEnabled(flag);
}
Loading