periodicjs.core.responder
Advanced tools
Comparing version 0.3.2 to 0.3.3
@@ -7,2 +7,12 @@ 'use strict'; | ||
const JSON_Adapter = require(path.join(__dirname, './json_content')); | ||
let test_app_views_dir_path = path.join(__dirname, '../../../../../app/views'); | ||
let app_views_dir_path = path.join(__dirname, '../../../app/views'); | ||
let app_prefix_path; | ||
try { | ||
fs.statSync(app_views_dir_path); | ||
app_prefix_path = path.join(__dirname, '../../../'); | ||
} catch (e) { | ||
app_views_dir_path = test_app_views_dir_path; | ||
app_prefix_path = path.join(__dirname, '../../../../../'); | ||
} | ||
@@ -15,10 +25,10 @@ /** | ||
*/ | ||
var findValidViewFromPaths = function (_default, dirs = []) { | ||
var findValidViewFromPaths = function(_default, dirs = []) { | ||
if (!dirs.length) return Promisie.resolve(_default); | ||
dirs.reverse(); // use the specific routes first, fallback to the default route | ||
return Promisie.retry(() => { | ||
let filePath = dirs.shift(); | ||
return fs.statAsync(filePath) | ||
.then(() => filePath, e => Promise.reject(e)); | ||
}, { times: dirs.length, timeout: 0, }) | ||
let filePath = dirs.shift(); | ||
return fs.statAsync(filePath) | ||
.then(() => filePath, e => Promise.reject(e)); | ||
}, { times: dirs.length, timeout: 0, }) | ||
.then(fp => fp) | ||
@@ -41,3 +51,3 @@ .catch(() => _default); | ||
*/ | ||
const _RENDER = function (data, options) { | ||
const _RENDER = function(data, options) { | ||
// console.log('__render', { data, options },'this',this); | ||
@@ -56,11 +66,11 @@ try { | ||
else dirs.push(path.join(options.dirname, `${ viewname }${ (/^\./.test(fileext)) ? fileext : '.' + fileext }`)); | ||
} | ||
if (typeof themename === 'string' && typeof fileext === 'string') { | ||
dirs.push(path.join(__dirname, '../../../../../node_modules', themename, 'views', `${viewname}${(/^\./.test(fileext)) ? fileext : '.' + fileext}`)); | ||
dirs.push(path.join(__dirname, '../../../../../content/container', themename, 'views', `${viewname}${(/^\./.test(fileext)) ? fileext : '.' + fileext}`)); | ||
dirs.push(path.join(app_prefix_path, 'node_modules', themename, 'views', `${viewname}${(/^\./.test(fileext)) ? fileext : '.' + fileext}`)); | ||
dirs.push(path.join(app_prefix_path, 'content/container', themename, 'views', `${viewname}${(/^\./.test(fileext)) ? fileext : '.' + fileext}`)); | ||
} | ||
if (typeof extname === 'string' && typeof fileext === 'string') dirs.push(path.join(__dirname, '../../../../', extname, 'views', `${ viewname }${ (/^\./.test(fileext)) ? fileext : '.' + fileext }`)); | ||
dirs.push(path.join(__dirname, '../../../../../app/views', `${viewname}${(/^\./.test(fileext)) ? fileext : '.' + fileext}`)); | ||
if (typeof extname === 'string' && typeof fileext === 'string') dirs.push(path.join(app_prefix_path, 'node_modules', extname, 'views', `${ viewname }${ (/^\./.test(fileext)) ? fileext : '.' + fileext }`)); | ||
dirs.push(path.join(app_prefix_path, 'app/views', `${viewname}${(/^\./.test(fileext)) ? fileext : '.' + fileext}`)); | ||
if (options.resolve_filepath === true) { | ||
@@ -77,3 +87,3 @@ return findValidViewFromPaths(viewname, dirs); | ||
} | ||
} catch (e) { | ||
} catch (e) { | ||
return Promisie.reject(e); | ||
@@ -90,3 +100,3 @@ } | ||
*/ | ||
const _ERROR = function (err, options) { | ||
const _ERROR = function(err, options) { | ||
try { | ||
@@ -99,3 +109,3 @@ if (this.custom_error_path) options.viewname = (options.viewname) ? path.join(this.custom_error_path, options.viewname) : path.join(this.custom_error_path, 'home/error404'); | ||
}), options); | ||
} catch (e) { | ||
} catch (e) { | ||
return Promise.reject(e); | ||
@@ -124,66 +134,66 @@ } | ||
constructor(options = {}) { | ||
super(options); | ||
this.engine = (options.engine && typeof options.engine.render === 'function') ? options.engine : ejs; | ||
this.engine_configuration = options.engine_configuration; | ||
this.extname = options.extname; | ||
this.themename = options.themename || 'periodicjs.theme.default'; | ||
this.viewname = options.viewname; | ||
this.fileext = options.fileext || '.ejs'; | ||
this.locals = (options.locals && typeof options.locals === 'object') ? options.locals : {}; | ||
this.custom_error_path = options.custom_error_path; | ||
} | ||
/** | ||
* Renders HTML from provided data and template | ||
* @param {Object} data Data that is passed to render template | ||
* @param {Object} [options={}] Configurable options for rendering see _RENDER for full details | ||
* @param {Function} [options.formatRender=_RENDER] Custom rendering function. It is not recommended to override the default value of this property and may no longer work properly | ||
* @param {Object} [options.req] Express request object. If options.req and options.res are defined the express .render method will be used to render template | ||
* @param {Object} [options.res] Express response object. If options.res and options.req are defined the express .render method will be used to render template | ||
* @param {Boolean} [options.skip_response] If true function will resolve with the rendered template instead of sending a response | ||
* @param {Function} cb Optional callback function. If arugment is not passed function will | ||
* @return {Object} Returns a Promise if cb arguement is not provided | ||
*/ | ||
render (data, options = {}, cb = false) { | ||
if (typeof options === 'function') { | ||
cb = options; | ||
options = {}; | ||
super(options); | ||
this.engine = (options.engine && typeof options.engine.render === 'function') ? options.engine : ejs; | ||
this.engine_configuration = options.engine_configuration; | ||
this.extname = options.extname; | ||
this.themename = options.themename || 'periodicjs.theme.default'; | ||
this.viewname = options.viewname; | ||
this.fileext = options.fileext || '.ejs'; | ||
this.locals = (options.locals && typeof options.locals === 'object') ? options.locals : {}; | ||
this.custom_error_path = options.custom_error_path; | ||
} | ||
if (options.req && options.res) { | ||
data.flash_messages = (typeof options.req.flash === 'function') ? options.req.flash() : {}; | ||
return _RENDER.call(this, {}, Object.assign(options, { resolve_filepath: true, })) | ||
.then(filepath => Promisie.promisify(options.res.render, options.res)(filepath, data)) | ||
.then(rendered => { | ||
if (typeof cb === 'function') cb(null, rendered); | ||
else if (options.skip_response && typeof cb !== 'function') return Promisie.resolve(rendered); | ||
else { | ||
options.res.status(200).send(rendered); | ||
return Promisie.resolve(rendered); | ||
} | ||
}) | ||
.catch(err => this.error(err, options, cb)); | ||
} else { | ||
options.formatRender = (typeof options.formatRender === 'function') ? options.formatRender : _RENDER.bind(this); | ||
options.sync = true; | ||
return super.render(Object.assign({ flash_messages: {}, }, this.locals, data), options) | ||
.then(result => { | ||
if (typeof cb === 'function') cb(null, result); | ||
else return result; | ||
}, e => { | ||
if (typeof cb === 'function') cb(e); | ||
else return Promisie.reject(e); | ||
}); | ||
/** | ||
* Renders HTML from provided data and template | ||
* @param {Object} data Data that is passed to render template | ||
* @param {Object} [options={}] Configurable options for rendering see _RENDER for full details | ||
* @param {Function} [options.formatRender=_RENDER] Custom rendering function. It is not recommended to override the default value of this property and may no longer work properly | ||
* @param {Object} [options.req] Express request object. If options.req and options.res are defined the express .render method will be used to render template | ||
* @param {Object} [options.res] Express response object. If options.res and options.req are defined the express .render method will be used to render template | ||
* @param {Boolean} [options.skip_response] If true function will resolve with the rendered template instead of sending a response | ||
* @param {Function} cb Optional callback function. If arugment is not passed function will | ||
* @return {Object} Returns a Promise if cb arguement is not provided | ||
*/ | ||
render(data, options = {}, cb = false) { | ||
if (typeof options === 'function') { | ||
cb = options; | ||
options = {}; | ||
} | ||
if (options.req && options.res) { | ||
data.flash_messages = (typeof options.req.flash === 'function') ? options.req.flash() : {}; | ||
return _RENDER.call(this, {}, Object.assign(options, { resolve_filepath: true, })) | ||
.then(filepath => Promisie.promisify(options.res.render, options.res)(filepath, data)) | ||
.then(rendered => { | ||
if (typeof cb === 'function') cb(null, rendered); | ||
else if (options.skip_response && typeof cb !== 'function') return Promisie.resolve(rendered); | ||
else { | ||
options.res.status(200).send(rendered); | ||
return Promisie.resolve(rendered); | ||
} | ||
}) | ||
.catch(err => this.error(err, options, cb)); | ||
} else { | ||
options.formatRender = (typeof options.formatRender === 'function') ? options.formatRender : _RENDER.bind(this); | ||
options.sync = true; | ||
return super.render(Object.assign({ flash_messages: {}, }, this.locals, data), options) | ||
.then(result => { | ||
if (typeof cb === 'function') cb(null, result); | ||
else return result; | ||
}, e => { | ||
if (typeof cb === 'function') cb(e); | ||
else return Promisie.reject(e); | ||
}); | ||
} | ||
} | ||
} | ||
/** | ||
* Renders error view from template | ||
* @param {*} err Any error data that should be passed to template | ||
* @param {Object} [options={}] Configurable options for rendering see _ERROR for full details | ||
* @param {Function} [options.formatError=_RENDER] Custom rendering function. It is not recommended to override the default value of this property and may no longer work properly | ||
* @param {Object} [options.req] Express request object. If options.req and options.res are defined the express .render method will be used to render template | ||
* @param {Object} [options.res] Express response object. If options.res and options.req are defined the express .render method will be used to render template | ||
* @param {Boolean} [options.skip_response] If true function will resolve with the rendered | ||
* @param {Function} cb Optional callback function. If arugment is not passed function will | ||
* @return {Object} Returns a Promise if cb arguement is not provided | ||
*/ | ||
error (err, options = {}, cb = false) { | ||
/** | ||
* Renders error view from template | ||
* @param {*} err Any error data that should be passed to template | ||
* @param {Object} [options={}] Configurable options for rendering see _ERROR for full details | ||
* @param {Function} [options.formatError=_RENDER] Custom rendering function. It is not recommended to override the default value of this property and may no longer work properly | ||
* @param {Object} [options.req] Express request object. If options.req and options.res are defined the express .render method will be used to render template | ||
* @param {Object} [options.res] Express response object. If options.res and options.req are defined the express .render method will be used to render template | ||
* @param {Boolean} [options.skip_response] If true function will resolve with the rendered | ||
* @param {Function} cb Optional callback function. If arugment is not passed function will | ||
* @return {Object} Returns a Promise if cb arguement is not provided | ||
*/ | ||
error(err, options = {}, cb = false) { | ||
if (typeof options === 'function') { | ||
@@ -214,3 +224,3 @@ cb = options; | ||
}); | ||
} else { | ||
} else { | ||
options.formatError = (typeof options.formatError === 'function') ? options.formatError : _ERROR.bind(this); | ||
@@ -231,2 +241,2 @@ options.sync = true; | ||
module.exports = { HTML_ADAPTER, findValidViewFromPaths, }; | ||
module.exports = { HTML_ADAPTER, findValidViewFromPaths, }; |
@@ -9,4 +9,3 @@ { | ||
}, | ||
"contributors": [ | ||
{ | ||
"contributors": [{ | ||
"name": "Yaw Joseph Etse", | ||
@@ -69,4 +68,3 @@ "email": "yaw.etse@gmail.com" | ||
"main": "index.js", | ||
"maintainers": [ | ||
{ | ||
"maintainers": [{ | ||
"name": "ecasilla", | ||
@@ -93,4 +91,4 @@ "email": "ecasilla@icloud.com" | ||
}, | ||
"version": "0.3.2", | ||
"version": "0.3.3", | ||
"license": "MIT" | ||
} | ||
} |
1243832
1937