mir-client
Version:
A Mir API Client
51 lines (46 loc) • 1.57 kB
JavaScript
import * as R from 'ramda'
/**
* @function CreatePutFactory - Creates PutFactory instance for a certain Mir API.
* @param {Object} axios - Initialized Axios instance.
* @param {string} resource - The target collection.
* @returns {PutFactory} - A factory function that accepts a Mir API resource name.
*/
function CreatePutFactory(axios, resource) {
/**
* @typedef PutFactory
* @type {function}
* @param {string} id - The id of the object to be replaced.
* @param {Object} document - The document to put.
* @returns {QueryBuilder} - Object that acts as a query builder for Mir requests.
* A note on QueryBuilder--PUT currently allows no queries but retains the QueryBuilder structure for future development.
*/
const PutFactory = (id, 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) {
const headers = this.headers
return axios.put(`/${resource}/${id}`, document, {
headers
}).then((result) => {
return result
})
}
}
return QueryBuilder
}
return PutFactory
}
export default CreatePutFactory