-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(feat) O3-3266: Optional deps can set feature flags (#1028)
* (feat) O3-3266: Optional deps can set feature flags * Core review suggestion --------- Co-authored-by: Dennis Kigen <[email protected]>
- Loading branch information
1 parent
5e39216
commit fb2bcf8
Showing
4 changed files
with
89 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { | ||
type FeatureFlagDefinition, | ||
openmrsFetch, | ||
registerFeatureFlag, | ||
setFeatureFlag, | ||
getCurrentUser, | ||
restBaseUrl, | ||
} from '@openmrs/esm-framework/src/internal'; | ||
import { satisfies } from 'semver'; | ||
|
||
export function registerOptionalDependencyHandler() { | ||
const subscription = getCurrentUser().subscribe((session) => { | ||
if (session.authenticated) { | ||
subscription.unsubscribe(); | ||
setupOptionalDependencies(); | ||
} | ||
}); | ||
|
||
return Promise.resolve(); | ||
} | ||
|
||
function setupOptionalDependencies() { | ||
const optionalDependencyFlags = window.installedModules.reduce< | ||
Map<string, { version: string; feature: FeatureFlagDefinition }> | ||
>((curr, module) => { | ||
if (module[1]?.optionalBackendDependencies) { | ||
Object.entries(module[1].optionalBackendDependencies).forEach((optionalDependency) => { | ||
const optionalDependencyDescriptor = optionalDependency[1]; | ||
if (typeof optionalDependencyDescriptor !== 'string') { | ||
if ( | ||
typeof optionalDependencyDescriptor.feature === 'object' && | ||
optionalDependencyDescriptor.feature && | ||
optionalDependencyDescriptor.feature.flagName?.length > 0 && | ||
optionalDependencyDescriptor.feature.label?.length > 0 && | ||
optionalDependencyDescriptor.feature.description?.length > 0 | ||
) { | ||
curr.set(optionalDependency[0], { | ||
version: optionalDependencyDescriptor.version, | ||
feature: optionalDependencyDescriptor.feature, | ||
}); | ||
} else { | ||
console.warn( | ||
`Feature flag descriptor for ${module[0]} does not match expected type. Feature flags must define a 'flagName', 'label', and 'description'`, | ||
); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
return curr; | ||
}, new Map()); | ||
|
||
if (optionalDependencyFlags.size > 0) { | ||
openmrsFetch<{ results: { uuid: string; version: string }[] }>(`${restBaseUrl}/module?v=custom:(uuid,version)`) | ||
.then((response) => { | ||
(response.data.results ?? []).forEach((backendModule) => { | ||
if (optionalDependencyFlags.has(backendModule.uuid)) { | ||
const optionalDependency = optionalDependencyFlags.get(backendModule.uuid); | ||
if (optionalDependency && satisfies(backendModule.version, optionalDependency.version)) { | ||
registerFeatureFlag( | ||
optionalDependency.feature.flagName, | ||
optionalDependency.feature.label, | ||
optionalDependency.feature.description, | ||
); | ||
|
||
setFeatureFlag(optionalDependency.feature.flagName, true); | ||
} | ||
} | ||
}); | ||
}) | ||
.catch(() => {}); // swallow any issues fetching | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters