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-

188 lines (187 loc) 7.95 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const index_1 = require("../index"); const Model_1 = __importDefault(require("../../Model")); const BasicSearchRestModel_1 = __importDefault(require("./BasicSearchRestModel")); const BasicIdRestModel_1 = __importDefault(require("./BasicIdRestModel")); const ComplexIdRestModel_1 = __importDefault(require("../ComplexIdRestModel")); const ComplexSearchRestModel_1 = __importDefault(require("../ComplexSearchRestModel")); const __1 = require("../../../.."); class BasicRestModel extends index_1.RestModel { constructor(...args) { super(new Model_1.default(args[0]), args[1], typeof args[2] === 'string' ? () => args[2] : args[2], Object.assign(Object.assign({}, args.length <= 4 ? args[3] : args.length === 6 && typeof args[5] === 'object' ? args[5] : args[6] ? args[6] : {}), { getItems: (args.length > 4 ? args[4] : undefined), getMetaData: (args.length > 4 && typeof args[5] === 'function' ? args[5] : undefined), itemStructure: (args.length > 4 ? args[3] : undefined) })); this.hiddeFields = (...fields) => { const schema = this.model.schema.deleteFields(...fields); const model = Object.assign(BasicRestModel, this); model.model.schema = schema; return model; }; this.basicIdRestModel = new BasicIdRestModel_1.default(this, args[1], this.getUrl); this.basicSearchRestModel = new BasicSearchRestModel_1.default(this.basicIdRestModel, this.getUrl); } /** @internal */ get _reducer() { return index_1.joinClassMethods(this.basicIdRestModel._reducer, this.basicSearchRestModel._reducer); } getSubModelWithKey(keyOrSchema, keyOrUrl, url) { if (typeof (keyOrSchema) === 'string') return new BasicIdRestModel_1.default(this, keyOrSchema, keyOrUrl ? () => keyOrUrl : this.getUrl); return new ComplexIdRestModel_1.default(this, keyOrUrl, keyOrSchema, url); } getSearchSubModel(optSchemaOrUrl, url) { if (typeof (optSchemaOrUrl) === 'string') { return new BasicSearchRestModel_1.default(this.basicIdRestModel, () => optSchemaOrUrl); } return new ComplexSearchRestModel_1.default(this.basicIdRestModel, optSchemaOrUrl, url); } /** @internal */ get _actions() { return index_1.joinClassMethods(this.basicIdRestModel._actions, this.basicSearchRestModel._actions); } /** * Hook used to fetch if needed to "/" path of model */ useFetchIfNeeded() { return this.basicSearchRestModel.useFetchIfNeeded.bind(this.basicSearchRestModel); } /** * Hook used to get data (without fetch if needed). Not optimal. Refresh on every change of model. */ useSelectorGet() { const state = __1.useSelector(state => this.basicSearchRestModel._reducer.getReducer(state)); return (queryString) => { return this.basicSearchRestModel._reducer.get(state, queryString); }; } /** * Hook used to fetch and fetch submodels with idOnly if needed to "/" path of model */ useFetchPopulatedIfNeeded() { return this.basicSearchRestModel.useFetchPopulatedIfNeeded.bind(this.basicSearchRestModel); } /** * Hook used to get populated data (without fetch if needed). Not optimal. Refresh on every change of model. */ useSelectorGetPopulated() { const state = __1.useSelector(state => this.basicSearchRestModel._reducer.getReducer(state)); return (queryString) => { return this.basicSearchRestModel._reducer.getPopulated(state, queryString); }; } /** * Hook used to get id data (without fetch if needed). Not optimal. Refresh on every change of model. */ useSelectorGetById() { const state = __1.useSelector(state => this.basicSearchRestModel._reducer.getReducer(state)); return (id) => { return this.basicIdRestModel._reducer.getByIdPopulated(state, id); }; } /** * Hook used to fetch if needed to "/:id" path of model */ useFetchByIdIfNeeded() { return this.basicIdRestModel.useFetchByIdIfNeeded.bind(this.basicIdRestModel); } /** * Hook used to get populated id data (without fetch if needed). Not optimal. Refresh on every change of model. */ useSelectorGetByIdPopulated() { const state = __1.useSelector(state => this.basicSearchRestModel._reducer.getReducer(state)); return (id) => { return this.basicIdRestModel._reducer.getByIdPopulated(state, id); }; } /** * Hook used to fetch and fetch submodels with idOnly if needed to "/:id" path of model */ useFetchByIdPopulatedIfNeeded() { return this.basicIdRestModel.useFetchByIdPopulatedIfNeeded.bind(this.basicIdRestModel); } /** * Hook used to get the result if there are from path "/". * Internally use the hook useFetchIfNeeded with the querystring provided. */ get useGet() { return this.basicSearchRestModel.useGet.bind(this.basicSearchRestModel); } /** * Uset to invalidate all requests */ get useInvalidateAll() { return this.basicIdRestModel.useInvalidateAll.bind(this.basicIdRestModel); } /** * Hook used to get the result populated (populating models with idOnly if there are) if there are from path "/". * Internally use the hook useFetchPopulatedIfNeeded with the querystring provided. */ get useGetPopulated() { return this.basicSearchRestModel.useGetPopulated.bind(this.basicSearchRestModel); } /** * Hook used to get the result if there are from path "/:id". * Internally use the hook useFetchByIdIfNeeded with the id provided. */ get useGetById() { return this.basicIdRestModel.useGetById.bind(this.basicIdRestModel); } /** * Hook used to get the result if there are from path "/". * Internally use the hook useFetchByIdPopulatedIfNeeded with the id provided. */ get useGetByIdPopulated() { return this.basicIdRestModel.useGetByIdPopulated.bind(this.basicIdRestModel); } /** * Used to change model: post, put, patch and delete */ useModificators() { const dispatch = __1.useDispatch(); const actionPost = this.basicIdRestModel._actions.post.bind(this.basicIdRestModel._actions); const actionPut = this.basicIdRestModel._actions.put.bind(this.basicIdRestModel._actions); const actionPatch = this.basicIdRestModel._actions.patch.bind(this.basicIdRestModel._actions); const actionDelete = this.basicIdRestModel._actions.delete.bind(this.basicIdRestModel._actions); const post = (item, callback) => dispatch(actionPost(item, callback)); const put = (id, item, callback) => dispatch(actionPut(id, item, callback)); const patch = (id, item, callback) => dispatch(actionPatch(id, item, callback)); const remove = (item, callback) => dispatch(actionDelete(item, callback)); return { post, put, patch, remove }; } /** * Used to post a entry model */ usePost() { const { post } = this.useModificators(); return post; } /** * Used to put a entry model */ usePut() { const { put } = this.useModificators(); return put; } /** * Used to patch a entry model */ usePatch() { const { patch } = this.useModificators(); return patch; } /** * Used to delete a entry model */ useDelete() { const { remove } = this.useModificators(); return remove; } } exports.default = BasicRestModel;