Problem this micro library focuses to solve:
Better organization of the server returned data, excluding from model UI related data and some helper functions.
Assuming you have a list of items returned from an API end point and you have to add UI only related properties (here called session props). When you need to save the modified data to the server we ususally do the following:
// get an item
let item = items[0];
const cleanItem = _.omit(item, [
'selected',
'visible',
'used',
"whatever property you don't want to go to the server",
]);
apiService.send(cleanItem);
Now you can do the following
interface propsDefinition {
itemName: string;
thisGoesToserver: []
}
interface sessionPropsDefinition {
selected: boolean;
visible: boolean;
whatEverYouDontwantToGoToServer: boolean;
}
class MyItem extends Model implements propsDefinition, sessionPropsDefinition {
itemName: string;
thisGoesToserver: [];
whatEverYouDontwantToGoToServer: boolean;
constructor(data: IRawData<propsDefinition, sessionPropsDefinition>) {
super(data);
}
}
const newItem = new MyItem({
props: {...},
session: {...}
});
// And you get the usual autocompleation, type checking, type guards, etc.
newItem.itemName = 'new name';
newItem.thisGoesToserver = ['yep'];
newItem.visible = true;
newItem.selected = false;
const sendToServer = newItem.toJSON();
-
- Done. On instance create structure of Props and SessionProps.
-
- Done. Create dynamic getters and setters.
-
- Done. Developper must be able to use MyObject.sessionPropName or MyObject.propName without referencing Props or SessionProps.
-
- Done. For extraProps: false. Show error not found on get and set property.
-
- Done. If extraProps: false -> Seal prototype.
-
- Done. If extraProps: true -> Add property from setter method.
-
- Done. Create method to export props as JSON excluding any sessionProperties.
-
- Done. Ability to cache values (getters) of properties. Example: Math.PI * 256 - expected calculation to happen only once
-
- -> Do not let duplicated keys to be used in Props and SessionProps.
-
- Ability to clear on demand cached values
-
- Add collection entity
If you like where this is going please star this repo :)
- Documentation and more examples
- fix typos