fluro
Version:
Promise based HTTP Fluro client for the browser and node.js
381 lines (267 loc) • 15.1 kB
HTML
<html lang="en">
<head>
<meta charset="utf-8">
<title>api/fluro.content.js - Documentation</title>
<script src="scripts/prettify/prettify.js"></script>
<script src="scripts/prettify/lang-css.js"></script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc.css">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<input type="checkbox" id="nav-trigger" class="nav-trigger" />
<label for="nav-trigger" class="navicon-button x">
<div class="navicon"></div>
</label>
<label for="nav-trigger" class="overlay"></label>
<nav >
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="FluroAccess.html">FluroAccess</a><ul class='methods'><li data-type='method'><a href="FluroAccess.html#.retrieveCurrentSession">retrieveCurrentSession</a></li><li data-type='method'><a href="FluroAccess.html#.setDefaultApplication">setDefaultApplication</a></li></ul></li><li><a href="FluroAPI.html">FluroAPI</a></li><li><a href="FluroAsset.html">FluroAsset</a><ul class='methods'><li data-type='method'><a href="FluroAsset.html#.avatarUrl">avatarUrl</a></li><li data-type='method'><a href="FluroAsset.html#.downloadUrl">downloadUrl</a></li><li data-type='method'><a href="FluroAsset.html#.filesize">filesize</a></li><li data-type='method'><a href="FluroAsset.html#.getUrl">getUrl</a></li><li data-type='method'><a href="FluroAsset.html#.imageUrl">imageUrl</a></li><li data-type='method'><a href="FluroAsset.html#.posterUrl">posterUrl</a></li></ul></li><li><a href="FluroAuth.html">FluroAuth</a><ul class='methods'><li data-type='method'><a href="FluroAuth.html#.addEventListener">addEventListener</a></li><li data-type='method'><a href="FluroAuth.html#.changeAccount">changeAccount</a></li><li data-type='method'><a href="FluroAuth.html#.getCurrentToken">getCurrentToken</a></li><li data-type='method'><a href="FluroAuth.html#.getCurrentUser">getCurrentUser</a></li><li data-type='method'><a href="FluroAuth.html#.login">login</a></li><li data-type='method'><a href="FluroAuth.html#.logout">logout</a></li><li data-type='method'><a href="FluroAuth.html#.refreshAccessToken">refreshAccessToken</a></li><li data-type='method'><a href="FluroAuth.html#.removeAllListeners">removeAllListeners</a></li><li data-type='method'><a href="FluroAuth.html#.removeEventListener">removeEventListener</a></li><li data-type='method'><a href="FluroAuth.html#.sendResetPasswordRequest">sendResetPasswordRequest</a></li><li data-type='method'><a href="FluroAuth.html#.set">set</a></li><li data-type='method'><a href="FluroAuth.html#.signup">signup</a></li></ul></li><li><a href="FluroCache.html">FluroCache</a><ul class='methods'><li data-type='method'><a href="FluroCache.html#.get">get</a></li><li data-type='method'><a href="FluroCache.html#.reset">reset</a></li></ul></li><li><a href="FluroContent.html">FluroContent</a><ul class='methods'><li data-type='method'><a href="FluroContent.html#.external">external</a></li><li data-type='method'><a href="FluroContent.html#.get">get</a></li><li data-type='method'><a href="FluroContent.html#.query">query</a></li><li data-type='method'><a href="FluroContent.html#.retrieve">retrieve</a></li><li data-type='method'><a href="FluroContent.html#.search">search</a></li><li data-type='method'><a href="FluroContent.html#.slug">slug</a></li></ul></li><li><a href="FluroCore.html">FluroCore</a></li><li><a href="FluroDate.html">FluroDate</a><ul class='methods'><li data-type='method'><a href="FluroDate.html#.localDate">localDate</a></li><li data-type='method'><a href="FluroDate.html#.readableEventDate">readableEventDate</a></li></ul></li><li><a href="FluroTypes.html">FluroTypes</a><ul class='methods'><li data-type='method'><a href="FluroTypes.html#.retrieve">retrieve</a></li></ul></li><li><a href="FluroUtils.html">FluroUtils</a><ul class='methods'><li data-type='method'><a href="FluroUtils.html#.arrayIDs">arrayIDs</a></li><li data-type='method'><a href="FluroUtils.html#.comma">comma</a></li><li data-type='method'><a href="FluroUtils.html#.errorMessage">errorMessage</a></li><li data-type='method'><a href="FluroUtils.html#.getStringID">getStringID</a></li><li data-type='method'><a href="FluroUtils.html#.mapParameters">mapParameters</a></li></ul></li></ul><h3>Modules</h3><ul><li><a href="module-fluro.html">fluro</a></li></ul>
</nav>
<div id="main">
<h1 class="page-title">api/fluro.content.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>import _ from 'lodash';
///////////////////////////////////////////////////
/**
* Creates a new FluroContent instance.
* This module provides a number of helper functions for Creating, Reading, Updating and Deleting content via the Fluro API
*
* @constructor
* @param {FluroCore} fluro A reference to the parent instance of the FluroCore module. This module is usually created by a FluroCore instance that passes itself in as the first argument.
*/
var FluroContent = function(fluro) {
if (!fluro.api) {
throw new Error(`Can't Instantiate FluroContent before FluroAPI exists`);
}
//Keep track of any refresh requests
var inflightRefreshRequest;
///////////////////////////////////////////////////
var service = {}
///////////////////////////////////////////////////
/**
* Runs a search from the Fluro server and returns the results
* @alias FluroContent.search
* @param {String} terms The keywords to search for
* @param {Object} options Extra Configuration and options for how to search the database and how to render the results
* @param {Object} options.limit How many results should be returned. Defaults to 10
* @param {Array} options.types Specify types or definition names for which items should be searched for
* @param {Boolean} options.showQuery If true will return the query used to search instead of the search results themselves
* @return {Array} An array of content items that match the search, if options.types is specified will be a nested array of results for each type
*
* @example
* FluroContent.search('Wonder', {limit:5, types:['song', 'album', 'tag']}).then(function(results) {
* //Will return a nested array with up to 5 results for each type
* //[{_type:'Song', results:[{title:"Wonder"...}]}, {_type:'Album', results:[{title:"Wonder"...}]}]
* })
*
* FluroContent.search('Wonder', {limit:5}).then(function(results) {
* //Will return an array of up to 5 items the user has access to view that match the search terms
* //[{title:"Wonder", _type:'article', definition:'song'...}, {title:"Wonder", _type:'article', definition:'album'...}]
* })
*/
service.search = function(terms, options) {
if (!options) {
options = {};
}
if (!options.limit) {
options.limit = 10;
}
return new Promise(function(resolve, reject) {
if (!terms || !terms.length) {
return resolve([]);
}
var requestOptions = {
params: options,
}
/////////////////////////////////////////////
//Retrieve the query results
fluro.api.get(`/content/search/${terms}`, requestOptions).then(function(res) {
resolve(res.data);
}, reject);
});
}
///////////////////////////////////////////////////
/**
* A helper function for retrieving the results of a specified query
* @alias FluroContent.query
* @param {String} queryID The id of the query you want to run
* @param {Object} options The options for the query
* @param {Object} options.params The query string parameters for the query that will be mapped ?one=value&two=value
* @param {Object} options.variables Any query variables you wish to inject each key will be mapped ?variables[key]=value
* @return {Promise} A promise that will be resolved with the results or an error
*/
service.query = function(queryID, options) {
//Get as just a query
queryID = fluro.utils.getStringID(queryID);
if (!options) {
options = {}
}
return new Promise(function(resolve, reject) {
var requestOptions = {
params: {}
}
//If there are query string parameters
if (options.params) {
requestOptions.params = options.params;
}
/////////////////////////////////////////////
if (options.variables) {
var variableParams = _.reduce(options.variables, function(set, value, key) {
set[`variables[${key}]`] = value;
return set;
}, {})
//Add it to our parameters
_.assign(requestOptions.params, variableParams);
}
/////////////////////////////////////////////
//Retrieve the query results
fluro.api.get(`/content/_query/${queryID}`, requestOptions).then(function(res) {
resolve(res.data);
}, reject);
})
}
///////////////////////////////////////////////////
/**
* This function returns a single populated item by providing its _id
* @alias FluroContent.get
* @param {String} id The item's _id
* @param {Object} params Extra query string parameters for the request
* @return {Promise} A promise that will be resolved with the item or an error
* @example
*
* //Retrieve just the title for item '5be504eabf33991239599d63'
* FluroContent.get('5be504eabf33991239599d63', {select:'title'})
*/
service.get = function(id, params) {
if (!params) {
params = {}
}
return new Promise(function(resolve, reject) {
var requestOptions = {
params: {}
}
//If there are query string parameters
if (params) {
requestOptions.params = params;
}
/////////////////////////////////////////////
//Retrieve the query results
fluro.api.get(`/content/get/${id}`, requestOptions).then(function(res) {
resolve(res.data);
}, reject);
})
}
///////////////////////////////////////////////////
/**
* This function returns a single populated item by providing its _external id
* @alias FluroContent.external
* @param {String} externalID The item's _external id property
* @param {Object} params Extra query string parameters for the request
* @return {Promise} A promise that will be resolved with the item or an error
* @example
*
* //Retrieve just the title for item with external id that matches '5be504-eabf33991-239599-d63'
* FluroContent.external('5be504-eabf33991-239599-d63', {select:'title'})
*/
service.external = function(id, params) {
if (!params) {
params = {}
}
return new Promise(function(resolve, reject) {
var requestOptions = {
params: {}
}
//If there are query string parameters
if (params) {
requestOptions.params = params;
}
/////////////////////////////////////////////
//Retrieve the query results
fluro.api.get(`/content/external/${id}`, requestOptions).then(function(res) {
resolve(res.data);
}, reject);
})
}
///////////////////////////////////////////////////
/**
* This function returns a single populated item by providing its slug
* @alias FluroContent.slug
* @param {String} slug The item's slug value
* @param {Object} params Extra query string parameters for the request
* @return {Promise} A promise that will be resolved with the item or an error
* @example
*
* //Retrieve just the title for item with the slug 'my-article'
* FluroContent.slug('my-article', {select:'title'})
*/
service.slug = function(id, params) {
if (!params) {
params = {}
}
return new Promise(function(resolve, reject) {
var requestOptions = {
params: {}
}
//If there are query string parameters
if (params) {
requestOptions.params = params;
}
/////////////////////////////////////////////
//Retrieve the query results
fluro.api.get(`/content/external/${id}`, requestOptions).then(function(res) {
resolve(res.data);
}, reject);
})
}
///////////////////////////////////////////////////
/**
* A helper function for running a dynamic query
* @alias FluroContent.retrieve
* @param {Object} criteria The query criteria
* @param {Object} options Extra options and parameters
* @return {Promise} A promise that will be resolved with the results or an error
* @example
*
* //Find all events that have a status of active or archived where the endDate is greater than or equal to now and return the titles
* FluroContent.retrieve({_type:'event', status:{$in:['active', 'archived'], endDate:{$gte:"date('now')"}}}, {select:'title'})
*/
service.retrieve = function(criteria, options) {
if (!options) {
options = {}
}
return new Promise(function(resolve, reject) {
var requestOptions = {
params: {}
}
//If there are query string parameters
if (options) {
requestOptions.params = options;
}
/////////////////////////////////////////////
//Retrieve the query results
fluro.api.post(`/content/_query`, criteria, requestOptions).then(function(res) {
resolve(res.data);
}, reject);
})
}
///////////////////////////////////////////////////
return service;
}
export default FluroContent;</code></pre>
</article>
</section>
</div>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Wed Apr 24 2019 19:46:45 GMT+1000 (Australian Eastern Standard Time) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
</footer>
<script>prettyPrint();</script>
<script src="scripts/polyfill.js"></script>
<script src="scripts/linenumber.js"></script>
</body>
</html>