@speakr/speakr-module-services
Version:
SPEAKR Shared Service Module
226 lines (221 loc) • 6.41 kB
JavaScript
;
const _ = require('lodash');
/*
* Create an ID for the logging function
* */
const generateId = (size) => {
/*
* Generic id is empty to begin with
* */
let id = '';
/*
* Pshhhh make it random as hell so you can use all standard characters
* */
let set = '23456789ABCDEFGHJKMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz';
/*
* Voodoo
* */
for (let i = 0; i < size; i++) {
/*
* Build the logger id and let the Math hit the Floor
* */
id += set.charAt(Math.floor(Math.random() * set.length));
}
/*
* Return the log ID
* */
return id;
};
/*
* Create the logger
* */
const logger = (req) => {
/*
* Get the headers id for the log
* */
req.headers._id = generateId(8);
/*
* Set the log to the request headers
* */
const id = req.headers._id + ' : ';
/*
* Return the log message and data
* */
return {
/*
* Info returns the msg & data in the request in the info param
* */
info: (msg, data) => {
/*
* If a log exists then load it ro the request log as info
* */
if (req.log) {
/*
* Attach the info ID with the ID, Message and Data
* */
req.log.info(id + msg, data);
}
},
/*
* Debug will apply the message and data to a debug param
* */
debug: (msg, data) => {
/*
* If a log exists then create the debugger
* */
if (req.log) {
/*
* Attach the debug with the ID, Message and Data
* */
req.log.debug(id + msg, data);
}
},
/*
* Error sets the error log data to the error param
* */
error: (msg, data) => {
/*
* If a log exists for error then apply it
* */
if (req.log) {
/*
* Set the error with the ID, Message and Data
* */
req.log.error(id + msg, data);
}
}
};
};
/*
* Control passes arguments from the request to the controller.
* */
module.exports = function control(...args) {
/*
* Returns an object of from, with, using
* */
return {
/*
* From is the whole request object req that comes from express.
* It handles all the body, params and query then builds the options variable
* that is used by controllers as options var.
* */
from: (req) => {
/*
* Parse requests parameters types into the options object or set them as blank objects
* */
const body = args.length && _.pick(req.body, args) || {};
const params = args.length && _.pick(req.params, args) || {};
const query = args.length && _.pick(req.query, args) || {};
const file = args.length && _.pick(req.file, args) || {};
/*
* Put all the body, params and query options into the options variable as
* req.params
* req.body
* req.query
* */
const options = {
/*
* Set options.params
* */
params: args.length && Object.assign(body, params, query, file) || Object.assign(req.params, req.body, req.query, req.file),
/*
* Set the options.req so you have a copy of the original request available in
* the options var that is passed to the controllers
* */
req: req
};
/*
* Return the handler and specify the with and using params that are passed from the routes
* */
return {
/*
* With is the handler method that specifies
* */
with: (handler) => {
/*
* Return the requests and fire off the response
* */
return {
/*
* Using passes the response and also creates the logger for the request
* that appears in the console
* */
using: (res) => {
/*
* Log the request
* */
const log = logger(req);
/*
* Set options.res
* */
options.res = res;
/*
* Handle the response that comes back from the controller with the options variable
* then fire off the results in the express response method
* */
return handler(options).then((result) => {
/*
* Better make that response a JSON string haters!
* */
res.send(JSON.stringify(result));
/*
* Return the result of the controller
* */
return result;
/*
* You must have screwed something up so let's fire off an error
* */
}).catch((err) => {
/*
* Better add the error to the log
* */
log.error('error', {
error: err
});
/*
* Sometimes we do not know what the hell happened so we need to return a generic 500
* */
if (!err) err = 'An unexpected error has occurred';
/*
* Make sure it's a 500 because we have no clue what failed
* */
res.statusCode = err.code || 500;
/*
* Fuck it & ship the generic error if there is a code for it
* */
if (err.code) {
res.send(err);
}
/*
* If no error code is specified then we know what the hell happened and the system will fail
* */
else {
/*
* You fails at life so give up and send some failzzz
* */
res.send({
error: {
/*
* Every error should have a status code
* */
code: res.statusCode,
/*
* If the error message is not a string then make it one hater!
* */
message: err.message || err.toString(),
purpose: err.purpose || ''
}
});
}
/*
* You fucked up!
* */
return err;
});
}
};
}
};
}
};
};