UNPKG

fluro

Version:

Promise based HTTP Fluro client for the browser and node.js

904 lines (690 loc) 33.6 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>fluro.types.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"> <script src="scripts/nav.js" defer></script> <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="access.html">access</a><ul class='methods'><li data-type='method'><a href="access.html#.addEventListener">addEventListener</a></li><li data-type='method'><a href="access.html#.can">can</a></li><li data-type='method'><a href="access.html#.canDeleteItem">canDeleteItem</a></li><li data-type='method'><a href="access.html#.canEditItem">canEditItem</a></li><li data-type='method'><a href="access.html#.canKnowOf">canKnowOf</a></li><li data-type='method'><a href="access.html#.canViewItem">canViewItem</a></li><li data-type='method'><a href="access.html#.has">has</a></li><li data-type='method'><a href="access.html#.isAuthor">isAuthor</a></li><li data-type='method'><a href="access.html#.removeAllListeners">removeAllListeners</a></li><li data-type='method'><a href="access.html#.removeEventListener">removeEventListener</a></li><li data-type='method'><a href="access.html#.retrieveActionableRealms">retrieveActionableRealms</a></li><li data-type='method'><a href="access.html#.retrieveCurrentSession">retrieveCurrentSession</a></li><li data-type='method'><a href="access.html#.setDefaultApplication">setDefaultApplication</a></li></ul></li><li><a href="api.html">api</a><ul class='methods'><li data-type='method'><a href="api.html#.delete">delete</a></li><li data-type='method'><a href="api.html#.generateEndpointURL">generateEndpointURL</a></li><li data-type='method'><a href="api.html#.get">get</a></li><li data-type='method'><a href="api.html#.post">post</a></li><li data-type='method'><a href="api.html#.put">put</a></li></ul></li><li><a href="app.device.html">device</a></li><li><a href="asset.html">asset</a><ul class='methods'><li data-type='method'><a href="asset.html#.avatarUrl">avatarUrl</a></li><li data-type='method'><a href="asset.html#.downloadUrl">downloadUrl</a></li><li data-type='method'><a href="asset.html#.filesize">filesize</a></li><li data-type='method'><a href="asset.html#.getUrl">getUrl</a></li><li data-type='method'><a href="asset.html#.imageUrl">imageUrl</a></li><li data-type='method'><a href="asset.html#.playerUrl">playerUrl</a></li><li data-type='method'><a href="asset.html#.posterUrl">posterUrl</a></li><li data-type='method'><a href="asset.html#.typeFromMime">typeFromMime</a></li></ul></li><li><a href="auth.html">auth</a><ul class='methods'><li data-type='method'><a href="auth.html#.addEventListener">addEventListener</a></li><li data-type='method'><a href="auth.html#.changeAccount">changeAccount</a></li><li data-type='method'><a href="auth.html#.getCurrentToken">getCurrentToken</a></li><li data-type='method'><a href="auth.html#.getCurrentUser">getCurrentUser</a></li><li data-type='method'><a href="auth.html#.impersonate">impersonate</a></li><li data-type='method'><a href="auth.html#.login">login</a></li><li data-type='method'><a href="auth.html#.logout">logout</a></li><li data-type='method'><a href="auth.html#.refreshAccessToken">refreshAccessToken</a></li><li data-type='method'><a href="auth.html#.removeAllListeners">removeAllListeners</a></li><li data-type='method'><a href="auth.html#.removeEventListener">removeEventListener</a></li><li data-type='method'><a href="auth.html#.retrieveUserFromResetToken">retrieveUserFromResetToken</a></li><li data-type='method'><a href="auth.html#.sendResetPasswordRequest">sendResetPasswordRequest</a></li><li data-type='method'><a href="auth.html#.set">set</a></li><li data-type='method'><a href="auth.html#.signup">signup</a></li><li data-type='method'><a href="auth.html#.updateUserWithToken">updateUserWithToken</a></li></ul></li><li><a href="cache.html">cache</a><ul class='methods'><li data-type='method'><a href="cache.html#.get">get</a></li><li data-type='method'><a href="cache.html#.reset">reset</a></li></ul></li><li><a href="components.html">components</a><ul class='methods'><li data-type='method'><a href="components.html#.hydrateModel">hydrateModel</a></li></ul></li><li><a href="content.html">content</a><ul class='methods'><li data-type='method'><a href="content.html#.duplicate">duplicate</a></li><li data-type='method'><a href="content.html#.external">external</a></li><li data-type='method'><a href="content.html#.filter">filter</a></li><li data-type='method'><a href="content.html#.form">form</a></li><li data-type='method'><a href="content.html#.get">get</a></li><li data-type='method'><a href="content.html#.getMultiple">getMultiple</a></li><li data-type='method'><a href="content.html#.keys">keys</a></li><li data-type='method'><a href="content.html#.list">list</a></li><li data-type='method'><a href="content.html#.query">query</a></li><li data-type='method'><a href="content.html#.related">related</a></li><li data-type='method'><a href="content.html#.retrieve">retrieve</a></li><li data-type='method'><a href="content.html#.slug">slug</a></li><li data-type='method'><a href="content.html#.submitInteraction">submitInteraction</a></li><li data-type='method'><a href="content.html#.submitPost">submitPost</a></li><li data-type='method'><a href="content.html#.thread">thread</a></li><li data-type='method'><a href="content.html#.values">values</a></li></ul></li><li><a href="date.html">date</a><ul class='methods'><li data-type='method'><a href="date.html#.countdown">countdown</a></li><li data-type='method'><a href="date.html#.dateFromID">dateFromID</a></li><li data-type='method'><a href="date.html#.formatDate">formatDate</a></li><li data-type='method'><a href="date.html#.getAge">getAge</a></li><li data-type='method'><a href="date.html#.groupEventByDate">groupEventByDate</a></li><li data-type='method'><a href="date.html#.isDifferentTimezoneThanUser">isDifferentTimezoneThanUser</a></li><li data-type='method'><a href="date.html#.isMultiDayEvent">isMultiDayEvent</a></li><li data-type='method'><a href="date.html#.localDate">localDate</a></li><li data-type='method'><a href="date.html#.militaryTimestamp">militaryTimestamp</a></li><li data-type='method'><a href="date.html#.readableEventDate">readableEventDate</a></li><li data-type='method'><a href="date.html#.readableEventTime">readableEventTime</a></li><li data-type='method'><a href="date.html#.timeago">timeago</a></li><li data-type='method'><a href="date.html#.timeline">timeline</a></li><li data-type='method'><a href="date.html#.timestampToAmPm">timestampToAmPm</a></li><li data-type='method'><a href="date.html#.timezones">timezones</a></li></ul></li><li><a href="fluro.html">fluro</a></li><li><a href="types.html">types</a><ul class='methods'><li data-type='method'><a href="types.html#.all">all</a></li><li data-type='method'><a href="types.html#.basicTypes">basicTypes</a></li><li data-type='method'><a href="types.html#.get">get</a></li><li data-type='method'><a href="types.html#.mapDefinitionItems">mapDefinitionItems</a></li><li data-type='method'><a href="types.html#.parentType">parentType</a></li><li data-type='method'><a href="types.html#.postableTypes">postableTypes</a></li><li data-type='method'><a href="types.html#.readable">readable</a></li><li data-type='method'><a href="types.html#.reloadTerminology">reloadTerminology</a></li><li data-type='method'><a href="types.html#.retrieve">retrieve</a></li><li data-type='method'><a href="types.html#.subTypes">subTypes</a></li><li data-type='method'><a href="types.html#.term">term</a></li></ul></li><li><a href="utils.html">utils</a><ul class='methods'><li data-type='method'><a href="utils.html#.arrayIDs">arrayIDs</a></li><li data-type='method'><a href="utils.html#.comma">comma</a></li><li data-type='method'><a href="utils.html#.currencySymbol">currencySymbol</a></li><li data-type='method'><a href="utils.html#.errorMessage">errorMessage</a></li><li data-type='method'><a href="utils.html#.extractFromArray">extractFromArray</a></li><li data-type='method'><a href="utils.html#.formatCurrency">formatCurrency</a></li><li data-type='method'><a href="utils.html#.getDefaultValueForField">getDefaultValueForField</a></li><li data-type='method'><a href="utils.html#.getFlattenedFields">getFlattenedFields</a></li><li data-type='method'><a href="utils.html#.getStringID">getStringID</a></li><li data-type='method'><a href="utils.html#.guid">guid</a></li><li data-type='method'><a href="utils.html#.hash">hash</a></li><li data-type='method'><a href="utils.html#.injectScript">injectScript</a></li><li data-type='method'><a href="utils.html#.machineName">machineName</a></li><li data-type='method'><a href="utils.html#.mapParameters">mapParameters</a></li><li data-type='method'><a href="utils.html#.matchInArray">matchInArray</a></li><li data-type='method'><a href="utils.html#.processCardPrioritySort">processCardPrioritySort</a></li></ul></li><li><a href="video.html">video</a><ul class='methods'><li data-type='method'><a href="video.html#.hhmmss">hhmmss</a></li><li data-type='method'><a href="video.html#.readableMilliseconds">readableMilliseconds</a></li><li data-type='method'><a href="video.html#.readableSeconds">readableSeconds</a></li></ul></li></ul> </nav> <div id="main"> <h1 class="page-title">fluro.types.js</h1> <section> <article> <pre class="prettyprint source linenums"><code>import _ from 'lodash'; /////////////////////////////////////////////////////////////////////////////// /** * Creates a new FluroTypes service * This module provides a number of helpful functions for retrieving, translating and understanding types, schemas and definitions * that are defined within Fluro * @alias types * @constructor * @hideconstructor * @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 FluroTypes = function(FluroCore) { var service = { glossary: {}, }; ////////////////////////////////// service.icon = function(type, library) { if (!library) { library = 'far'; } var icon; switch (type) { case 'academic': icon = 'school'; break; case 'statsheet': icon = 'calculator-alt'; break; case 'simpleemail': icon = 'envelope'; break; case 'smscorrespondence': icon = 'mobile-alt'; break; case 'deployment': icon = 'cloud-upload'; break; case 'roster': icon = 'clipboard-user'; break; case 'package': icon = 'box-open'; break; case 'method': icon = 'credit-card-front'; break; case 'resultset': icon = 'poll-people'; break; case 'timetrigger': icon = 'clock'; break; case 'user': icon = 'user'; break; case 'policy': icon = 'id-card'; break; case 'account': icon = 'browser'; break; case 'application': icon = 'layer-group'; break; case 'article': icon = 'file-alt'; break; case 'asset': icon = 'file-archive'; break; case 'audio': icon = 'file-audio'; break; case 'checkin': icon = 'sign-in'; break; case 'capability': icon = 'star'; break; case 'code': icon = 'code'; break; case 'collection': // icon = 'box-full'; icon = 'folder'; break; case 'component': icon = 'tachometer-alt'; break; case 'log': icon = 'history'; break; case 'contact': icon = 'child'; break; case 'definition': icon = 'books-medical'; break; case 'contactdetail': icon = 'file-invoice'; break; case 'eventtrack': icon = 'random'; break; case 'event': icon = 'calendar-star'; break; case 'family': icon = 'home'; break; case 'team': icon = 'users'; break; case 'attendance': // icon = 'calendar-check'; icon = 'calculator'; break; case 'image': icon = 'image'; break; case 'conversation': icon = 'comments-alt'; break; case 'integration': icon = 'plug'; break; case 'interaction': icon = 'compress'; break; case 'location': icon = 'map-marked-alt'; break; case 'mailout': icon = 'paper-plane'; break; case 'plan': icon = 'clipboard-list'; break; case 'post': icon = 'comment-alt-lines'; break; case 'process': icon = 'exchange'; break; case 'product': icon = 'shopping-cart'; break; case 'purchase': icon = 'file-invoice-dollar'; break; case 'query': icon = 'terminal'; break; case 'reaction': icon = 'bolt'; break; case 'realm': icon = 'bullseye'; break; case 'role': icon = 'user-lock'; break; case 'site': case 'sitemodel': icon = 'sitemap'; break; case 'tag': icon = 'tag'; break; case 'ticket': icon = 'ticket-alt'; break; case 'transaction': icon = 'usd-square'; break; case 'persona': icon = 'user'; break; case 'assignment': icon = 'user-clock'; break; case 'video': icon = 'video'; break; case 'form': icon ='file-signature'; break; } if (icon) { return [library, icon]; } } ////////////////////////////////// /** * Retrieves a specified definition or primitive type object * @alias types.get * @param {string} definedName The definition or type name you want to retrieve * @param {object} options extra options for the request * @return {promise} An promise that will resolve to the type definition from Fluro */ service.get = function(definedName, options) { if (!options) { options = { // flat:true } } /////////////////////////// return new Promise(function(resolve, reject) { FluroCore.api.get(`/defined/type/${definedName}`, options) .then(function(res) { resolve(res.data); }, reject); }); } /////////////////////////////////////////////////////////////////////////////// /** * A helpful function for mapping an array of items into a grouped array broken up by definition * @alias types.mapDefinitionItems * @param {Array} array An array of content items * @param {String} baseType The default base type to map, eg. 'tag', 'contact', 'event' * @return {Array} A mapped array broken up by definition * @example * //Returns {something:[{title:'Demographic', plural:'Demographics', key:'demographic', entries:[{...},{...}]}]} * fluro.types.mapDefinitionItems([{title:'test', definition:'demographic'}], 'tag'); * */ service.mapDefinitionItems = function(array, backup) { var self = this; //////////////////////////// if (!array || !array.length) { return []; } //////////////////////////// return _.chain(array) // .orderBy(function(item) { // return String(item.title).toLowerCase() // }) .reduce(function(set, entry) { var key = entry.definition || backup; var existing = set[key]; if (!existing) { existing = set[key] = { title: service.readable(key, false, backup), plural: service.readable(key, true, backup), key, entries: [], } } existing.entries.push(entry); return set; }, {}) .values() .orderBy(function(type) { return type.key == backup }) .value(); } ////////////////////////////////// /** * Retrieves all definitions available in the current account. Useful for making one request and caching * @alias types.all * @param {object} options extra options for the request * @return {promise} An promise that will resolve to the array of definitions */ service.all = function(options) { if (!options) { options = { // flat:true } } /////////////////////////// return new Promise(function(resolve, reject) { return FluroCore.api.get(`/defined`, options) .then(function(res) { resolve(res.data); }, reject); }); } ////////////////////////////////// /** * Retrieves all definitions available in the current account. Useful for making one request and caching * @alias types.terms * @param {object} options extra options for the request * @return {promise} An promise that will resolve to the array of definitions and their names */ var inflightTermsRequest; ////////////////////////////////// service.terms = function(options) { if(!options) { options = {} } // console.log('LETS LOAD TERMS', options, FluroCore.auth.getCurrentUser()); /////////////////////////// if (inflightTermsRequest &amp;&amp; !options.forceRefresh) { return inflightTermsRequest; } /////////////////////////// inflightTermsRequest = new Promise(function(resolve, reject) { if (!options) { options = { cache:false, // flat:true } } /////////////////////////// options.cache = false; /////////////////////////// service.glossary = {}; return FluroCore.api.get(`/defined/terms`, options).then(function(res) { _.each(res.data, function(entry, key) { entry.definitionName = key; }); service.glossary = res.data; return resolve(res.data); }, reject); }); /////////////////////////// return inflightTermsRequest; } ////////////////////////////////// /** * Retrieves a glossary of glossary for readable definition titles and plurals * @alias types.reloadTerminology * @return {promise} An promise that will resolve to the matching basic types or reject with the responding error */ service.reloadTerminology = function(options) { if (!options) { options = { forceRefresh:true, } } // console.log('load terms reloadTerminology') return service.terms(options); } ////////////////////////////////// var basicTypes = [ 'asset', 'checkin', 'image', 'audio', 'video', 'account', 'persona', 'application', 'deployment', 'article', 'assignment', 'post', 'resultset', 'timetrigger', 'onboard', 'code', 'component', 'collection', 'family', 'contact', 'method', 'contactdetail', 'personadetail', 'task', 'definition', 'endpoint', 'event', 'view', 'process', 'eventtrack', 'log', 'integration', 'interaction', 'location', 'package', 'product', 'purchase', 'query', 'realm', 'role', 'site', 'tag', 'team', 'roster', 'capability', 'plan', 'transaction', 'reaction', 'user', 'policy', 'mailout', 'ticket', 'academic', 'attendance', ] service.isBasicType = function(typeName) { return _.includes(basicTypes, typeName); } ////////////////////////////////// /** * Input a definition name or basic type and receive the human readable version of that type * @alias types.readable * @param {String} definitionName The definition or _type * @param {Boolean} plural Whether to return it's plural version * @return {String} Eg. 'Audio', 'Detail Sheet', or 'Events'... */ service.readable = function(definitionName, plural) { if(definitionName == 'node') { return plural ? 'Items' : 'Item'; } ////////////////////////////////////////// var readable = definitionName; var match = service.glossary ? service.glossary[readable] : null; if (match) { readable = plural ? match.plural : match.title; } else { readable = plural ? _.startCase(readable) + 's' : _.startCase(readable); } return readable; } ////////////////////////////////// /** * Input a definition name or basic type and receive the basic details about that definition * @alias types.term * @param {String} definitionName The definition or _type * @return {Object} The details about this definition as defined in the glossary */ service.term = function(definitionName) { return service.glossary ? service.glossary[definitionName] : null; } ////////////////////////////////// /** * Input a definition name or basic type and receive the most basic _type of that definition * @alias types.parentType * @param {String} definitionName The definition or _type * @return {String} Eg. 'photo', 'service', or 'song'... */ service.parentType = function(definitionName) { var match = service.glossary ? service.glossary[definitionName] : null; if (match) { definitionName = match.parentType || definitionName; } return definitionName; } ////////////////////////////////// /** * Retrieve an array of all basic types * @alias types.basicTypes * @return {Array} eg. 'service', 'concert', 'conference' */ service.basicTypes = function() { var values = _.map(basicTypes, function(typeName) { return service.glossary[typeName]; }) return Promise.resolve(values); } ////////////////////////////////// /** * Input a definition name or basic type and receive the most basic _type of that definition * @alias types.subTypes * @param {String} definitionName The basic _type * @param {Boolean} includeBasicType Whether to include the basic type definition in the results * @return {Array} eg. 'service', 'concert', 'conference' */ service.subTypes = function(typeName, includeBasicType) { var definitions = _.chain(service.glossary) .reduce(function(set, term, key) { if(term.status == 'archived') { return set; } term.definitionName = key; if (term.parentType == typeName) { set.push(term); } return set; }, []) .orderBy(function(definition) { return definition.title; }) .value(); /////////////////////////////////////// if(includeBasicType) { var basicTypeMatch = service.glossary[typeName]; if(basicTypeMatch) { definitions.unshift(basicTypeMatch) } } /////////////////////////////////////// return Promise.resolve(definitions); // var match = service.glossary ? service.glossary[definitionName] : null; // if (match) { // definitionName = match.parentType || definitionName; // } // return definitionName; } ////////////////////////////////// /** * Input a definition name or basic type and receive the most basic _type of that definition * @alias types.postableTypes * @param {String} definitionName The definition or _type * @param {Object} options Extra options * @return {Array} an array of definitions that can be posted * /** service.postableTypes = function(typeName, options) { if(!options) { options = { list: true, strict: true, } } return new Promise(function(resolve, reject) { FluroCore.api.post('/defined', options) .then(function(res) { // console.log('GOT ALL THE TYPES', res.data); resolve(res.data); }, reject); }); // FluroContent.endpoint('post/types/' + type, true, true) // .query(options) // .$promise.then(function(res) { // var filtered = _.filter(res, function(definition) { // var definitionName = definition.definitionName; // var canView = FluroAccess.can('view', definitionName, 'post'); // var canCreate = FluroAccess.can('create', definitionName, 'post'); // var canSubmit = FluroAccess.can('submit', definitionName, 'post'); // // console.log('CAN?', $rootScope.user, type, canCreate, canSubmit); // return (canCreate || canSubmit); // }); // return deferred.resolve(filtered); // }, deferred.reject); // var match = service.glossary ? service.glossary[definitionName] : null; // if (match) { // definitionName = match.parentType || definitionName; // } // return definitionName; } /**/ ////////////////////////////////// /** * Input a definition name or basic type and receive the most basic _type of that definition * @alias types.postableTypes * @param {String} definitionName The definition or _type * @param {Object} options Extra options * @return {Array} an array of definitions that can be posted * */ service.processTypes = function(typeName, options) { if (!options) { options = { list: true, strict: false, } } return new Promise(function(resolve, reject) { console.log('GET THE PROCESS TYPES') // return resolve([]); FluroCore.api.get(`/process/types/${typeName}`, { params: options }) .then(function(res) { // var filtered = _.filter(res, function(definition) { // var definitionName = definition.definitionName; // var canView = FluroAccess.can('view', definitionName, 'process'); // var canCreate = FluroAccess.can('create', definitionName, 'process'); // return (canView || canCreate); // }); var ordered = _.orderBy(res.data, function(definition) { definition.title; }) // console.log('GOT ALL THE TYPES', res.data); resolve(ordered); }, reject); }); } ////////////////////////////////// // /** // * Input definition names or basic types and receive a list of all // * posts that can be attached to that type of content // * @alias types.postTypes // * @param {Array} definitionNames The definitions or _types to check // * @param {Object} options Extra options // * @return {Array} an array of definitions that can be posted // * // */ // service.postTypes = function(typeName, options) { // if (!options) { // options = { // list: true, // strict: true, // } // } // return new Promise(function(resolve, reject) { // FluroCore.api.get(`/post/types/${typeName}`, { // params: options // }) // .then(function(res) { // resolve(res.data); // }, reject); // }); // } ////////////////////////////////// /** * Retrieves a list of specified types and their respective definitions * @alias types.retrieve * @param {array} types The names of the basic types you want to retrieve * @return {promise} An promise that will resolve to the matching basic types or reject with the responding error */ service.retrieve = function(types, options) { if (!options) { options = { // flat:true } } options.types = types; /////////////////////////// return new Promise(function(resolve, reject) { FluroCore.api.post('/defined', options) .then(function(res) { // console.log('GOT ALL THE TYPES', res.data); resolve(res.data); }, reject); }); } // ////////////////////////////////// // //Get all sub definitions for an array of primitive types // service.subDefinitions = function(primitiveTypes, options) { // if (!options) { // options = { // // flat:true // } // } // var definitionCache = fluro.cache.get('subDefinitions'); // //////////////////////////////////////////////////////// // var promises = _.map(primitiveTypes, function(type) { // if(definitionCache[type]) { // return Promise.resolve(definitionCache[type]); // } // /////////////////////////////////////////////////// // return new Promise(function(resolve, reject) { // FluroCore.api.get(`/defined/types/${type}`) // .then(function(res) { // definitionCache[type] = res.data; // resolve(definitionCache[type]); // }, reject); // }); // }) // return Promise.all(promises); // } ////////////////////////////////// return service; } /////////////////////////////////////////////////////////////////////////////// export default FluroTypes;</code></pre> </article> </section> </div> <br class="clear"> <footer> Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.3</a> on Tue Jun 29 2021 08:38:17 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>