help-me-respond
Advanced tools
Comparing version 1.2.0 to 1.2.1
62
index.js
@@ -1,9 +0,9 @@ | ||
var config = require("config") | ||
var tools = require('./tools') | ||
var i18n = require('i18n-nodejs')(config.lang, config.langFile); | ||
var messages = require('./messages') | ||
const CONFIG = require("config") | ||
const TOOLS = require('./tools') | ||
const I18N = require('i18n-nodejs')(CONFIG.lang, CONFIG.langFile); | ||
const MESSAGES = require('./messages') | ||
function http400(res, msg, headers) { | ||
if (!msg || msg == undefined) { | ||
msg = messages.BAD_REQUEST; | ||
msg = MESSAGES.BAD_REQUEST; | ||
} | ||
@@ -15,3 +15,3 @@ return rCode(400, res, msg, headers); | ||
if (!msg || msg == undefined) { | ||
msg = messages.NOT_FOUND; | ||
msg = MESSAGES.NOT_FOUND; | ||
} | ||
@@ -23,3 +23,3 @@ return rCode(404, res, msg, headers); | ||
if (!msg || msg == undefined) { | ||
msg = messages.FORBIDDEN; | ||
msg = MESSAGES.FORBIDDEN; | ||
} | ||
@@ -31,3 +31,3 @@ return rCode(403, res, msg, headers); | ||
if (!msg || msg == undefined) { | ||
msg = messages.UNAUTHENTICATED; | ||
msg = MESSAGES.UNAUTHENTICATED; | ||
} | ||
@@ -38,4 +38,4 @@ return rCode(401, res, msg, headers); | ||
function http502(res, resp, body) { | ||
var code = 502; | ||
var headers = ''; | ||
let code = 502; | ||
let headers = ''; | ||
if (resp !== undefined && resp !== null) { | ||
@@ -64,3 +64,4 @@ code = resp.statusCode; | ||
function rCode(code, res, msg, headers) { | ||
var args = null | ||
let args = null | ||
let stack = null; | ||
@@ -73,14 +74,20 @@ // if there is no code in the message, its probably error | ||
res = res.status(code); | ||
// if user set some headers | ||
if (headers && headers !== undefined) { | ||
headers['content-type'] = 'application/json'; | ||
if (!CONFIG.disableJsonHeader) { | ||
headers['content-type'] = 'application/json'; | ||
} | ||
res = res.header(headers); | ||
} else { | ||
var headers = { | ||
'content-type': 'application/json' | ||
}; | ||
res = res.header(headers); | ||
if (!CONFIG.disableJsonHeader) { | ||
let headers = {} | ||
headers['content-type'] = 'application/json'; | ||
res = res.header(headers); | ||
} | ||
} | ||
if (code >= 400 && msg instanceof Error) { | ||
msg = msg.message.toString() | ||
stack = msg.stack.split('\n'); | ||
stack.splice(0, 1); | ||
msg = msg.message.toString(); | ||
} | ||
@@ -92,4 +99,4 @@ | ||
if (tools.isJsonString(msg)) { | ||
var jsonObj = JSON.parse(msg) | ||
if (TOOLS.isJsonString(msg)) { | ||
let jsonObj = JSON.parse(msg) | ||
if (jsonObj.msg) { | ||
@@ -106,6 +113,6 @@ msg = jsonObj.msg | ||
msg = { | ||
friendlyMessage: i18n.__(msg, args) | ||
friendlyMessage: I18N.__(msg, args) | ||
} | ||
} else if (msg && msg instanceof Object) { | ||
if (config && !config.prefixNone) { | ||
if (CONFIG && !CONFIG.prefixNone) { | ||
msg = { | ||
@@ -117,10 +124,11 @@ data: msg | ||
if (checkFriendly(msg.message)) { | ||
msg.friendlyMessage = i18n.__(msg.message, args) | ||
msg.friendlyMessage = I18N.__(msg.message, args) | ||
delete msg.message | ||
} else { | ||
msg.message = i18n.__(msg.message, args) | ||
msg.message = I18N.__(msg.message, args) | ||
} | ||
} else { | ||
msg = { | ||
message: i18n.__(msg, args) | ||
message: I18N.__(msg, args), | ||
stack: stack | ||
} | ||
@@ -142,3 +150,3 @@ } | ||
} else { | ||
return res.json(msg); | ||
return CONFIG.disableJsonHeader ? res.send(msg) : res.json(msg); | ||
} | ||
@@ -148,4 +156,4 @@ } | ||
function checkFriendly(msg) { | ||
if (config && config.friendlyMessages) { | ||
return config.friendlyMessages.indexOf(msg) !== -1 | ||
if (CONFIG && CONFIG.friendlyMessages) { | ||
return CONFIG.friendlyMessages.indexOf(msg) !== -1 | ||
} | ||
@@ -152,0 +160,0 @@ return false |
{ | ||
"name": "help-me-respond", | ||
"version": "1.2.0", | ||
"version": "1.2.1", | ||
"description": "Simple nodejs response helper", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -26,5 +26,7 @@ ## Help me respond | ||
"lang": "en", | ||
"langFile": "../../locales/locales.json" | ||
"langFile": "../../config/locales.json" | ||
} | ||
*langFile* path is used in the library therefore, the path is relative to the *index.js* file from *help-me-respond* folder | ||
* Create the specified above locales folder and locales file and add some messages there. | ||
@@ -48,4 +50,3 @@ | ||
{ | ||
"friendlyMessages": [ | ||
] | ||
"friendlyMessages": [] | ||
} | ||
@@ -59,5 +60,11 @@ | ||
### Turn off data prefix | ||
When you server returns any data, a prefix **data** is added to response. You can turn it off by setting **prefixNone** to **true** in your config file. For more info about config see [Config setup](#config-setup) above. | ||
When you server returns any data, a prefix **data** is added to response. | ||
You can turn it off by setting **prefixNone** to **true** in your config file. For more info about config see [Config setup](#config-setup) above. | ||
### Turn off default 'application/json' header | ||
Remove the default *application/json* header by setting **disableJsonHeader** to **true** in your config file. For more info about config see [Config setup](#config-setup) above. | ||
## API | ||
@@ -64,0 +71,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8521
7
161
149