UNPKG

mir-client

Version:
58 lines (52 loc) 1.79 kB
import * as R from 'ramda' /** * @function CreatePostFactory - Creates PostFactory for a certain Mir API. * @param {Object} axios - Initialized Axios instance. * @param {string} resource - The target collection. * @returns {PostFactory} - A factory function that accepts a Mir API resource name. */ function CreatePostFactory(axios, resource) { /** * @typedef PostFactory * @type {function} * @param {Object} document - The document to post. * @returns {QueryBuilder} - Object that acts as a query builder for Mir requests. * A note on QueryBuilder--POST currently allows no queries but retains the QueryBuilder structure for future development. */ const PostFactory = (document) => { /** * A Query Builder object * @typedef QueryBuilder * @type {Object} * @property {Object} headers - Stores headers for the request. * @property {function} send - Sends a request with the current stored headers. */ const QueryBuilder = { headers: {}, /** * @function send * @param {function} hook - TODO: not implemented * @this QueryBuilder * @returns {Promise} - Axios promise */ send(hook) { // this error check will not work if we start posting lists for bulk inserts later on if(typeof document != 'object') { var err = new Error() err.message = 'Document to POST must be a JSON object.' err.code = 400 throw err } const headers = this.headers return axios.post(`/${resource}`, document, { headers }).then((result) => { return result }) } } return QueryBuilder } return PostFactory } export default CreatePostFactory