2021-10-22 14:52:54 +08:00
|
|
|
import { AnyExtension, RemoveThis, MaybeThisParameterType } from '../types'
|
2021-04-16 03:14:33 +08:00
|
|
|
|
|
|
|
export default function getExtensionField<T = any>(
|
|
|
|
extension: AnyExtension,
|
|
|
|
field: string,
|
2021-10-22 14:52:54 +08:00
|
|
|
context?: Omit<MaybeThisParameterType<T>, 'parent'>,
|
2021-04-16 03:14:33 +08:00
|
|
|
): RemoveThis<T> {
|
|
|
|
|
|
|
|
if (extension.config[field] === undefined && extension.parent) {
|
|
|
|
return getExtensionField(extension.parent, field, context)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof extension.config[field] === 'function') {
|
|
|
|
const value = extension.config[field].bind({
|
|
|
|
...context,
|
|
|
|
parent: extension.parent
|
|
|
|
? getExtensionField(extension.parent, field, context)
|
|
|
|
: null,
|
|
|
|
})
|
|
|
|
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
|
|
|
return extension.config[field]
|
|
|
|
}
|