UNPKG

mir-client

Version:
72 lines (66 loc) 2.41 kB
import * as R from 'ramda' /** * @function CreateGetOneFactory - Creates GetOneFactory for a certain Mir API. * @param {Object} axios - Initialized Axios instance. * @param {string} resource - Resource name. * @returns {GetOneFactory} - A factory function that accepts a Mir API resource name. */ function CreateGetOneFactory(axios, resource) { /** * @typedef GetOneFactory - A Query Builder object for getting a single record from a collection. * @type {function} * @param {string} id - The id of the desired record. * @returns {QueryBuilder} - Object that acts as a query builder for Mir requests. */ const GetOneFactory = (id) => { /** * A Query Builder object * @typedef QueryBuilder * @type {Object} * @property {Object} params - Stores parameters for the request. * @property {Object} headers - Stores headers for the request. * @property {function} projection - Updates params object with a projection configuration. * @property {function} send - Sends a request with the current stored headers and params configurations. */ const QueryBuilder = { params: {}, headers: {}, /** * @function projection * @param {string} attribute - Name of the resource attribute to apply projection. * @param {boolean} bool - Indicates whether the attribute should appear in the results. * @this QueryBuilder */ projection(attribute, bool) { if(typeof bool != 'boolean') { var err = new Error() err.message = 'Bool param only accepts values of true or false.' err.code = 400 throw err } const value = bool ? 1 : 0 this.params.projection = R.merge(this.params, R.objOf(attribute, value)) return this }, /** * @function send * @param {function} hook - TODO: not implemented, will contain function to alter returned results. * @this QueryBuilder * @returns {Promise} - Axios promise */ send(hook) { const params = this.params const headers = this.headers return axios.get(`/${resource}/${id}`, { params, headers }).then((result) => { return result }) } } return QueryBuilder } return GetOneFactory } export default CreateGetOneFactory