express-http-context
Advanced tools
Comparing version
40
index.js
@@ -7,15 +7,35 @@ 'use strict'; | ||
module.exports = { | ||
middleware: function (req, res, next) { | ||
const ns = cls.getNamespace(nsid) || cls.createNamespace(nsid); | ||
ns.run(() => next()); | ||
}, | ||
get: function (key) { | ||
const ns = cls.getNamespace(nsid); | ||
/** Express.js middleware that is responsible for initializing the context for each request. */ | ||
function middleware(req, res, next) { | ||
const ns = cls.getNamespace(nsid) || cls.createNamespace(nsid); | ||
ns.run(() => next()); | ||
} | ||
/** | ||
* Gets a value from the context by key. Will return undefined if the context has not yet been initialized for this request or if a value is not found for the specified key. | ||
* @param {string} key | ||
*/ | ||
function get(key) { | ||
const ns = cls.getNamespace(nsid); | ||
if (ns.active) { | ||
return ns.get(key); | ||
}, | ||
set: function (key, value) { | ||
const ns = cls.getNamespace(nsid); | ||
} | ||
} | ||
/** | ||
* Adds a value to the context by key. If the key already exists, its value will be overwritten. No value will persist if the context has not yet been initialized. | ||
* @param {string} key | ||
* @param {*} value | ||
*/ | ||
function set(key, value) { | ||
const ns = cls.getNamespace(nsid); | ||
if (ns.active) { | ||
return ns.set(key, value); | ||
} | ||
} | ||
module.exports = { | ||
middleware, | ||
get: get, | ||
set: set | ||
} |
{ | ||
"name": "express-http-context", | ||
"version": "0.3.1", | ||
"version": "0.3.2", | ||
"description": "Get and set request-scoped context anywhere", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"cover": "istanbul cover ./node_modules/mocha/bin/_mocha index.js -R ./tests --coverage && istanbul check-coverage --statement 100 --branch 100 --function 100 --line 100", | ||
"test": "mocha ./tests" | ||
}, | ||
@@ -27,3 +28,10 @@ "repository": { | ||
"continuation-local-storage": "^3.2.0" | ||
}, | ||
"devDependencies": { | ||
"chai": "^3.5.0", | ||
"express": "^4.15.2", | ||
"istanbul": "^0.4.5", | ||
"mocha": "^3.2.0", | ||
"supertest": "^3.0.0" | ||
} | ||
} |
@@ -0,1 +1,5 @@ | ||
[](https://travis-ci.org/skonves/express-http-context) | ||
[](https://www.npmjs.com/package/express-http-context) | ||
[](https://www.npmjs.com/package/express-http-context) | ||
# Express HTTP Context | ||
@@ -2,0 +6,0 @@ Get and set request-scoped context anywhere. This is just an unopinionated, idiomatic ExpressJS implementation of [continuation-local-storage](https://www.npmjs.com/package/continuation-local-storage). It's a great place to store user state, claims from a JWT, request/correlation IDs, and any other request-scoped data. |
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
7436
77.98%7
40%99
482.35%1
-50%51
8.51%5
Infinity%