Add components API

Signed-off-by: soupette <cyril.lpz@gmail.com>
This commit is contained in:
soupette 2020-04-09 14:13:23 +02:00
parent a38c6738e1
commit d512043b12
2 changed files with 39 additions and 0 deletions

View File

@ -0,0 +1,36 @@
import { cloneDeep } from 'lodash';
import invariant from 'invariant';
class ComponentApi {
components = {};
getComponent = name => {
invariant(name, 'A name must be provided');
return cloneDeep(this.components[name]) || null;
};
getComponents = () => {
return cloneDeep(this.components);
};
registerComponent = component => {
const { name, Component } = component;
invariant(Component, 'A Component must be provided');
invariant(name, 'A name must be provided');
invariant(this.components[name] === undefined, 'A similar field already exists');
this.components[name] = Component;
};
removeComponent = name => {
invariant(name, 'A name must be provided in order to remove a field');
delete this.components[name];
};
}
export default () => {
return new ComponentApi();
};

View File

@ -1,6 +1,9 @@
import ComponentApi from './ComponentApi';
import FieldApi from './FieldApi';
class Strapi {
componentApi = ComponentApi();
fieldApi = FieldApi();
}