@ot06/be-utils
Advanced tools
Comparing version 0.0.14 to 0.0.15
@@ -14,10 +14,19 @@ 'use strict'; | ||
var _constants = require("../constants"); | ||
var _expressHttpContext = _interopRequireDefault(require("express-http-context")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var { | ||
HEADER_PROPS | ||
} = _constants.HEADERS; | ||
var expressMiddlerwares = { | ||
RequestHandler: _requestHandler.default, | ||
RouteHandler: _RouteHandler.default, | ||
ResponseHandler: _ResponseHandler.default | ||
ResponseHandler: _ResponseHandler.default, | ||
HEADER_PROPS, | ||
httpContext: _expressHttpContext.default | ||
}; | ||
var _default = expressMiddlerwares; | ||
exports.default = _default; |
@@ -8,4 +8,10 @@ 'use strict'; | ||
var _ResponseBody = _interopRequireDefault(require("../classes/ResponseBody")); | ||
var _expressHttpContext = _interopRequireDefault(require("express-http-context")); | ||
var _nanoid = require("nanoid"); | ||
var _classes = require("../classes"); | ||
var _constants = require("../constants"); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -20,2 +26,5 @@ | ||
var DEFAULT_CONFIG = {}; | ||
var { | ||
EXTRACT_HEADERS | ||
} = _constants.HEADERS; | ||
@@ -27,2 +36,3 @@ class RequestHandler { | ||
this.asyncWrapper = this.asyncWrapper.bind(this); | ||
this.extractHeaders = this.extractHeaders.bind(this); | ||
} | ||
@@ -47,3 +57,3 @@ | ||
if (error.constructor.name === _ResponseBody.default.name) { | ||
if (error.constructor.name === _classes.ResponseBody.name) { | ||
responseBody = error; | ||
@@ -62,4 +72,40 @@ } else if (error.getResponseBody && typeof error.getResponseBody === 'function') { | ||
extractHeaders(request, response, next) { | ||
var { | ||
headers = {}, | ||
query = {} | ||
} = request; | ||
EXTRACT_HEADERS.forEach(HEADER => { | ||
var { | ||
HEADER_KEY, | ||
QUERY_KEY, | ||
PROP, | ||
GENERATE_RANDOM | ||
} = HEADER; | ||
var value = _extractFromHeader(headers, HEADER_KEY); | ||
if (QUERY_KEY) { | ||
value = value || query[QUERY_KEY]; | ||
} | ||
if (!value && GENERATE_RANDOM) { | ||
value = (0, _nanoid.nanoid)(); | ||
} | ||
_expressHttpContext.default.set(PROP, value); | ||
}); | ||
return process.nextTick(next); | ||
} | ||
} | ||
exports.default = RequestHandler; | ||
exports.default = RequestHandler; | ||
function _extractFromHeader() { | ||
var headers = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
var key = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ''; | ||
var value = headers[key] || ''; | ||
value = value || headers[key.toLowerCase()] || ''; | ||
return value; | ||
} |
@@ -8,4 +8,8 @@ 'use strict'; | ||
var _ResponseBody = _interopRequireDefault(require("../classes/ResponseBody")); | ||
var _expressHttpContext = _interopRequireDefault(require("express-http-context")); | ||
var _classes = require("../classes"); | ||
var _constants = require("../constants"); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -20,2 +24,6 @@ | ||
var DEFAULT_CONFIG = {}; | ||
var { | ||
SET_HEADERS, | ||
EXPOSE_HEADERS | ||
} = _constants.HEADERS; | ||
@@ -28,2 +36,3 @@ class ResponseHandler { | ||
this.handleError = this.handleError.bind(this); | ||
this.setHeaders = this.setHeaders.bind(this); | ||
} | ||
@@ -44,3 +53,3 @@ | ||
var _error = new _ResponseBody.default(404, message); | ||
var _error = new _classes.ResponseBody(404, message); | ||
@@ -56,3 +65,3 @@ response.body = _error; | ||
if (error && error.constructor.name === _ResponseBody.default.name) { | ||
if (error && error.constructor.name === _classes.ResponseBody.name) { | ||
response.body = error; | ||
@@ -76,3 +85,3 @@ return _sendResponse(request, response, next); | ||
var responseBody = error.constructor.name === _ResponseBody.default.name ? error : new _ResponseBody.default(500, undefined, undefined, error); | ||
var responseBody = error.constructor.name === _classes.ResponseBody.name ? error : new _classes.ResponseBody(500, undefined, undefined, error); | ||
response.body = responseBody; | ||
@@ -82,2 +91,10 @@ return this.handleResponse(request, response, next); | ||
setHeaders(request, response, next) { | ||
_setHeaders(response); | ||
_exposeHeaders(response); | ||
return process.nextTick(next); | ||
} | ||
} | ||
@@ -94,3 +111,3 @@ | ||
if (!resBody || !statusCode) { | ||
resBody = new _ResponseBody.default(500, 'Response Data Not Found!'); | ||
resBody = new _classes.ResponseBody(500, 'Response Data Not Found!'); | ||
} | ||
@@ -108,2 +125,21 @@ | ||
response.status(statusCode).redirect(data); | ||
} | ||
function _exposeHeaders(response) { | ||
var { | ||
HEADER_KEY = '', | ||
HEADER_VALUE = '' | ||
} = EXPOSE_HEADERS || {}; | ||
response.set(HEADER_KEY, HEADER_VALUE); | ||
} | ||
function _setHeaders(request, response) { | ||
SET_HEADERS.forEach(HEADER => { | ||
var { | ||
HEADER_KEY, | ||
PROP | ||
} = HEADER; | ||
var value = _expressHttpContext.default.get(PROP) || ''; | ||
response.set(HEADER_KEY, value); | ||
}); | ||
} |
{ | ||
"name": "@ot06/be-utils", | ||
"version": "0.0.14", | ||
"version": "0.0.15", | ||
"description": "Backend Utilities Module", | ||
@@ -10,5 +10,6 @@ "main": "dist/index.js", | ||
"dependencies": { | ||
"express-http-context": "^1.2.4", | ||
"lodash": "4.17.21", | ||
"moment": "2.29.1", | ||
"nanoid": "^3.1.22", | ||
"nanoid": "^3.1.25", | ||
"qs": "6.9.6", | ||
@@ -15,0 +16,0 @@ "uuid": "^8.3.2" |
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
19421
13
428
6
+ Addedexpress-http-context@^1.2.4
+ Added@types/body-parser@1.19.5(transitive)
+ Added@types/cls-hooked@4.3.9(transitive)
+ Added@types/connect@3.4.38(transitive)
+ Added@types/express@4.17.21(transitive)
+ Added@types/express-serve-static-core@4.19.6(transitive)
+ Added@types/http-errors@2.0.4(transitive)
+ Added@types/mime@1.3.5(transitive)
+ Added@types/node@22.10.1(transitive)
+ Added@types/qs@6.9.17(transitive)
+ Added@types/range-parser@1.2.7(transitive)
+ Added@types/send@0.17.4(transitive)
+ Added@types/serve-static@1.15.7(transitive)
+ Addedasync-hook-jl@1.7.6(transitive)
+ Addedcls-hooked@4.2.2(transitive)
+ Addedemitter-listener@1.1.2(transitive)
+ Addedexpress-http-context@1.2.4(transitive)
+ Addedsemver@5.7.2(transitive)
+ Addedshimmer@1.2.1(transitive)
+ Addedstack-chain@1.3.7(transitive)
+ Addedundici-types@6.20.0(transitive)
Updatednanoid@^3.1.25