So, I am working on an MVVM-based core SDK for use any time I am developing some Google Apps Script based software, called OpenSourceSDK. It contain core business logic, including base classes to extend. For example, the file Models/BaseModel.gs in it is defined to be:
class BaseModel {
static FromObject(obj) {
let model = new this()
return model.unmarshal(obj)
}
/**
* Creates a deep-copy of this, as plain Object
*/
marshal() {
return JSON.parse(JSON.stringify(this));
}
/**
* Writes the target object into this and returns this
* @param {*} obj
*/
unmarshal(obj) {
return Object.assign(this, obj);
}
}
I follow the instructions to create this library, and make sure to import it into the project that's using it. When I attempt to use it, things go sideways: I am able to access the functions, but not the classes nor the constants!! I start typing in OpenSourceSDK.BaseModel, and IntelliSense is like "idk what to do". I sanity test via Logger.log(typeof OpenSourceSDK.BaseModel), only to get undefined.
This is unacceptable.
What is the most efficient way to make sure these classes (and constants) are shared from that library?
NOTE: I do not accept the following "solutions":
- factory method per class. Why not? Because:
- maintenance nightmare. Imagine making any change to the constructor signature of the class. You'd have to update the factory function on every change!
- extra boilerplate that is wrapper around something that is so simple.
- what happen if we want to extend a base class coming from the library?
- going all function-based. That defeats the whole purpose of this project. There's a reason I am doing this MVVM, and hence OOP, style.
