-
Notifications
You must be signed in to change notification settings - Fork 208
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
(feat): Add implicit conditions to configuration schema #475
Changes from 6 commits
6adb9a3
73bb80b
2656723
ba7a7e4
c76e6b2
4652e30
780c120
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/** @module @category Config */ | ||
import { clone, reduce, mergeDeepRight, equals } from "ramda"; | ||
import { clone, reduce, mergeDeepRight, equals, omit } from "ramda"; | ||
import { | ||
Config, | ||
ConfigObject, | ||
|
@@ -27,7 +27,6 @@ import { | |
implementerToolsConfigStore, | ||
temporaryConfigStore, | ||
getExtensionSlotsConfigStore, | ||
ExtensionsConfigStore, | ||
} from "./state"; | ||
import type {} from "@openmrs/esm-globals"; | ||
import { TemporaryConfigStore } from ".."; | ||
|
@@ -192,9 +191,14 @@ function computeExtensionConfigs( | |
*/ | ||
export function defineConfigSchema(moduleName: string, schema: ConfigSchema) { | ||
validateConfigSchema(moduleName, schema); | ||
const enhancedSchema = mergeDeepRight( | ||
schema, | ||
displayConditionsSchema | ||
) as ConfigSchema; | ||
|
||
const state = configInternalStore.getState(); | ||
configInternalStore.setState({ | ||
schemas: { ...state.schemas, [moduleName]: schema }, | ||
schemas: { ...state.schemas, [moduleName]: enhancedSchema }, | ||
}); | ||
} | ||
|
||
|
@@ -220,14 +224,20 @@ export function defineExtensionConfigSchema( | |
schema: ConfigSchema | ||
) { | ||
validateConfigSchema(extensionName, schema); | ||
const enhancedSchema = mergeDeepRight( | ||
schema, | ||
displayConditionsSchema | ||
) as ConfigSchema; | ||
|
||
const state = configInternalStore.getState(); | ||
if (state.schemas[extensionName]) { | ||
console.warn( | ||
`Config schema for extension ${extensionName} already exists. If there are multiple extensions with this same name, one will probably crash.` | ||
); | ||
} | ||
|
||
configInternalStore.setState({ | ||
schemas: { ...state.schemas, [extensionName]: schema }, | ||
schemas: { ...state.schemas, [extensionName]: enhancedSchema }, | ||
}); | ||
} | ||
|
||
|
@@ -256,7 +266,8 @@ export function getConfig(moduleName: string): Promise<Config> { | |
const store = getConfigStore(moduleName); | ||
function update(state: ConfigStore) { | ||
if (state.loaded && state.config) { | ||
resolve(state.config); | ||
const config = omit(["Display conditions"], state.config); | ||
resolve(config); | ||
unsubscribe && unsubscribe(); | ||
} | ||
} | ||
|
@@ -362,7 +373,7 @@ function getSchemaWithValuesAndSources(schema) { | |
return obj; | ||
}, {}); | ||
} else { | ||
// Schema is bad; error will have been logged during schema validation | ||
// at this point, the schema is bad and an error will have been logged during schema validation | ||
return {}; | ||
} | ||
} | ||
|
@@ -492,6 +503,12 @@ function validateConfigSchema( | |
const thisKeyPath = keyPath + (keyPath && ".") + key; | ||
const schemaPart = schema[key] as ConfigSchema; | ||
|
||
if (thisKeyPath === "Display conditions") { | ||
console.error( | ||
`${moduleName} declares a configuration option called "Display conditions"; the "Display conditions" option is a reserved name. ${updateMessage}` | ||
); | ||
} | ||
|
||
if (!isOrdinaryObject(schemaPart)) { | ||
console.error( | ||
`${moduleName} has bad config schema definition for key '${thisKeyPath}'. ${updateMessage}` | ||
|
@@ -863,3 +880,16 @@ function getExtensionNameFromId(extensionId: string) { | |
const [extensionName] = extensionId.split("#"); | ||
return extensionName; | ||
} | ||
|
||
/** | ||
* The displayConditionsSchema is implicitly included in every configuration schema | ||
*/ | ||
const displayConditionsSchema: ConfigSchema = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is quite different from how we handle the extension config. But it is a cool idea, and might actually be a better approach than what we currently have implemented. The only tricky part, at first glance, is that IMO these special keys ( |
||
"Display conditions": { | ||
privileges: { | ||
_description: "The privilege(s) the user must have to use this extension", | ||
_type: Type.Array, | ||
_default: [], | ||
}, | ||
}, | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I expect it not to appear here, either