UNPKG

@rest-api/react-models

Version:

[![npm version](https://img.shields.io/npm/v/@rest-api/react-models)](https://www.npmjs.com/package/@rest-api/react-models) [![codecov](https://codecov.io/gh/hector7/rest-api-react-models/branch/master/graph/badge.svg)](https://codecov.io/gh/hector7/rest-

55 lines (54 loc) 2.62 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const utils_1 = require("./utils"); class BasicIdReducer { constructor(model) { this.restModel = model; } getReducer(state) { const res = Object.assign({ searchs: {}, derived: {}, ids: {} }, state[this.restModel.model.name]); return Object.assign({}, res, { ids: Object.assign({}, res.ids, { [this.restModel._id]: Object.assign({}, res.ids[this.restModel._id]) }) }); } getById(state, id) { if (typeof id !== 'string' && typeof id !== 'number') throw (new Error(`id ${id} must be a string or number. Type is ${typeof id}`)); return utils_1.getResult(this.getReducer(state).ids[this.restModel._id][id]); } getByIdPopulated(state, id) { if (typeof id !== 'string' && typeof id !== 'number') throw (new Error(`id ${id} must be a string or number. Type is ${typeof id}`)); const res = this.getById(state, id); return res !== null ? this.restModel.model.schema._convertToPopulated(state, res) : null; } getIdError(state, id) { if (typeof id !== 'string' && typeof id !== 'number') throw (new Error(`id ${id} must be a string or number. Type is ${typeof id}`)); return utils_1.getError(this.getReducer(state).ids[this.restModel._id][id]); } isIdInitialized(state, id) { if (typeof id !== 'string' && typeof id !== 'number') throw (new Error(`id ${id} must be a string or number. Type is ${typeof id}`)); return utils_1.isInitialized(this.getReducer(state).ids[this.restModel._id][id]); } isIdPopulated(state, id) { if (typeof id !== 'string' && typeof id !== 'number') throw (new Error(`id ${id} must be a string or number. Type is ${typeof id}`)); const res = this.getById(state, id); return res !== null ? this.restModel.model.schema._isPopulated(state, res) : false; } isIdFetching(state, id) { if (typeof id !== 'string' && typeof id !== 'number') throw (new Error(`id ${id} must be a string or number. Type is ${typeof id}`)); return utils_1.isFetching(this.getReducer(state).ids[this.restModel._id][id]); } isIdInvalidated(state, id) { if (typeof id !== 'string' && typeof id !== 'number') throw (new Error(`id ${id} must be a string or number. Type is ${typeof id}`)); return utils_1.isInvalidated(this.getReducer(state).ids[this.restModel._id][id]); } } exports.default = BasicIdReducer;