@rest-api/react-models
Version:
[](https://www.npmjs.com/package/@rest-api/react-models) [](https://codecov.io/gh/hector7/rest-
63 lines (62 loc) • 2.22 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.joinClassMethods = exports.RestModel = void 0;
const ReducerStorage_1 = __importDefault(require("../ReducerStorage"));
const State_1 = require("../State");
class ReducerImplementation extends Object {
constructor(modelName) {
super();
this.modelName = modelName;
}
addBasicIdReducer(idKey) {
ReducerStorage_1.default.addBasicIdReducer(this.modelName, idKey);
}
}
class RestModel extends ReducerImplementation {
constructor(model, id, url, opts = {}) {
super(model.name);
this._id = id;
this.getUrl = url;
this.model = model;
this.trailingSlash = opts.trailingSlash ? opts.trailingSlash : false;
this.headers = opts.headers ? opts.headers : {};
this.itemStructure = opts.itemStructure;
this.getItems = opts.getItems ? opts.getItems : items => items;
this.getMetaData = opts.getMetaData ? opts.getMetaData : () => null;
super.addBasicIdReducer(id);
this.state = new State_1.ModelState(this);
}
get validateItem() {
return this.model.schema.validate.bind(this.model.schema);
}
get validateFetch() {
if (this.itemStructure !== undefined)
return this.itemStructure.validate.bind(this.itemStructure);
return this.model.schema.validateArray.bind(this.model.schema);
}
/** @internal */
get itemType() {
throw new Error('Method only exist for typing reasons');
}
/** @internal */
get idType() {
return this.model.schema._schema[this._id].type;
}
}
exports.RestModel = RestModel;
function joinClassMethods(...args) {
const res = {};
args.forEach(cClass => {
const cMethods = Object.getOwnPropertyNames(Object.getPrototypeOf(cClass));
cMethods.forEach(key => {
if (key !== 'constructor') {
res[key] = cClass[key].bind(cClass);
}
});
});
return res;
}
exports.joinClassMethods = joinClassMethods;