periodicjs
Version:
Periodic is a rapid enterprise application framework for data driven web and mobile applications.
158 lines (130 loc) • 5.64 kB
HTML
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: lib/utilities/routing.js</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-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: lib/utilities/routing.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>'use strict';
/**
* returns a string that's used in an express router that's always prefixed with a preceding '/'
*
* @param {String} adminPath
* @returns {String} route used for express router, that's always prefixed with a "/"
*/
function _route_prefix(adminPath) {
return (adminPath === '')
? '/'
: (adminPath && adminPath.charAt(0) === '/')
? adminPath
: '/' + adminPath;
}
/**
* returns a route string without the precending '/'
*
* @param {String} adminPath
* @returns {String}
*/
function _admin_prefix(adminPath) {
return _route_prefix(adminPath).substr(1);
}
/**
* returns a route string that always has a preceding '/' and a suffixed '/', this is typically used for specifiying links to paths as absolute urls
*
* @param {String} adminPath
* @returns {String}
*/
function _manifest_prefix(adminPath) {
var admin_prefix = _admin_prefix(adminPath);
return (admin_prefix.length > 0)
? '/'+admin_prefix+'/'
: '/';
}
/**
* short hand function to return all prefix types
*
* @param {String} adminPath
* @returns {String}
*/
function all_prefixes(adminPath){
return {
route_prefix : _route_prefix(adminPath),
admin_prefix : _admin_prefix(adminPath),
manifest_prefix : _manifest_prefix(adminPath),
};
}
function splitModelNameReducer(result, model_name) {
let split = model_name.split('_');
let parent = split.shift();
let child = split.join('_');
result[parent] = result[parent] || [];
result[parent].push(child);
return result;
}
function regexModelNameReducer(result, model_name) {
let [parent, child, ] = model_name.replace(/^([^\s_]+)_{1}(.+)$/, '$1 $2').split(' ');
result[parent] = result[parent] || [];
result[parent].push(child);
return result;
}
/**
* Enforces the shape of an api response, by allow for three properties (result,status and data) all other properties are on data
*
* @param {string} options.result result of request (usually sucess or error)
* @param {number} options.status http resonse code equivalent
* @param {object} options.data data for response
* @returns {object} with the shape {result,status,data}
*/
function formatResponse(options = {}) {
const formattedOptions = Object.keys(options).reduce((result, key) => {
if (key === 'result') {
result[ key ] = options[ key ].toString();
} else if (key === 'status') {
result[ key ] = parseInt(options[ key ], 10);
} else if(key==='data'){
result.data = Object.assign({},result.data,options[ key ]);
} else {
result.data[ key ] = options[ key ];
}
return result;
}, { data: {}, });
return Object.assign({}, {
result: 'success',
status: 200,
data:{},
}, formattedOptions);
}
module.exports = {
splitModelNameReducer,
regexModelNameReducer,
all_prefixes,
formatResponse,
route_prefix : _route_prefix,
admin_prefix : _admin_prefix,
manifest_prefix : _manifest_prefix,
};</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-periodic.html">periodic</a></li></ul><h3>Classes</h3><ul><li><a href="Periodic.html">Periodic</a></li><li><a href="Periodic.Periodic.html">Periodic</a></li></ul><h3>Global</h3><ul><li><a href="global.html#_admin_prefix">_admin_prefix</a></li><li><a href="global.html#_manifest_prefix">_manifest_prefix</a></li><li><a href="global.html#_route_prefix">_route_prefix</a></li><li><a href="global.html#all_prefixes">all_prefixes</a></li><li><a href="global.html#configRuntimeEnvironment">configRuntimeEnvironment</a></li><li><a href="global.html#configureLogger">configureLogger</a></li><li><a href="global.html#configureLowkie">configureLowkie</a></li><li><a href="global.html#configureMongoose">configureMongoose</a></li><li><a href="global.html#configureSequelize">configureSequelize</a></li><li><a href="global.html#configureViews">configureViews</a></li><li><a href="global.html#endTimer">endTimer</a></li><li><a href="global.html#formatResponse">formatResponse</a></li><li><a href="global.html#getEnv">getEnv</a></li><li><a href="global.html#handler">handler</a></li><li><a href="global.html#initializeExpress">initializeExpress</a></li><li><a href="global.html#loadConfiguration">loadConfiguration</a></li><li><a href="global.html#setAppRunningEnv">setAppRunningEnv</a></li><li><a href="global.html#setUpFolderStructure">setUpFolderStructure</a></li><li><a href="global.html#startTimer">startTimer</a></li></ul>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Wed Jul 18 2018 23:08:12 GMT-0400 (Eastern Daylight Time)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>