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
refactor(core/strapi): move components to registry #16273
Changes from all commits
5f354dd
23bbfb9
ecfd60a
31a6fcb
310c688
430b1f6
7229657
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 |
---|---|---|
@@ -0,0 +1,97 @@ | ||
'use strict'; | ||
|
||
const { pickBy, has } = require('lodash/fp'); | ||
const { createComponent } = require('../domain/component'); | ||
const { hasNamespace, addNamespace } = require('../utils'); | ||
|
||
const validateKeySameToSingularName = (components) => { | ||
for (const cName of Object.keys(components)) { | ||
const component = components[cName]; | ||
|
||
if (cName !== component.schema.info.singularName) { | ||
throw new Error( | ||
`The key of the component should be the same as its singularName. Found ${cName} and ${component.schema.info.singularName}.` | ||
); | ||
} | ||
} | ||
}; | ||
|
||
const componentsRegistry = () => { | ||
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 registry was copied from content-types and adjusted comments and variables. |
||
const components = {}; | ||
|
||
return { | ||
/** | ||
* Returns this list of registered components uids | ||
* @returns {string[]} | ||
*/ | ||
keys() { | ||
return Object.keys(components); | ||
}, | ||
|
||
/** | ||
* Returns the instance of a component. Instantiate the component if not already done | ||
* @param {string} uid | ||
* @returns | ||
*/ | ||
get(uid) { | ||
return components[uid]; | ||
}, | ||
|
||
/** | ||
* Returns a map with all the components in a namespace | ||
* @param {string} namespace | ||
*/ | ||
getAll(namespace) { | ||
return pickBy((_, uid) => hasNamespace(uid, namespace))(components); | ||
}, | ||
|
||
/** | ||
* Registers a component | ||
* @param {string} uid | ||
* @param {Object} component | ||
*/ | ||
set(uid, component) { | ||
components[uid] = component; | ||
return this; | ||
}, | ||
|
||
/** | ||
* Registers a map of components for a specific namespace | ||
* @param {string} namespace | ||
* @param {{ [key: string]: Object }} newComponents | ||
*/ | ||
add(namespace, newComponents) { | ||
validateKeySameToSingularName(newComponents); | ||
|
||
for (const rawComponentName of Object.keys(newComponents)) { | ||
const uid = addNamespace(rawComponentName, namespace); | ||
|
||
if (has(uid, components)) { | ||
throw new Error(`Component ${uid} has already been registered.`); | ||
} | ||
|
||
components[uid] = createComponent(uid, newComponents[rawComponentName]); | ||
} | ||
}, | ||
|
||
/** | ||
* Wraps a component to extend it | ||
* @param {string} uid | ||
* @param {(component: Object) => Object} extendFn | ||
*/ | ||
extend(cUID, extendFn) { | ||
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. Does the registry need to implement any interface? Can these unused methods get deleted? |
||
const currentComponent = this.get(cUID); | ||
|
||
if (!currentComponent) { | ||
throw new Error(`Component ${cUID} doesn't exist`); | ||
} | ||
|
||
const newComponent = extendFn(currentComponent); | ||
components[cUID] = newComponent; | ||
|
||
return this; | ||
}, | ||
}; | ||
}; | ||
|
||
module.exports = componentsRegistry; |
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.
This method was dead code previously