From 6e5944c0591b511b617e4bfafa908fb1c50722ba Mon Sep 17 00:00:00 2001 From: Peter Marton Date: Wed, 9 Jan 2019 14:22:46 -0800 Subject: [PATCH] feat(plugins): context, req.get() returns the whole context --- docs/_api/plugins.md | 25 +++++++++++++++++++++++++ docs/_api/server.md | 10 ++++++++++ lib/plugins/pre/context.js | 32 +++++++++++++++++++++++++++----- test/plugins/context.test.js | 9 +++++++++ test/plugins/plugins.test.js | 2 +- 5 files changed, 72 insertions(+), 6 deletions(-) diff --git a/docs/_api/plugins.md b/docs/_api/plugins.md index 211d829a7..1d4422796 100644 --- a/docs/_api/plugins.md +++ b/docs/_api/plugins.md @@ -37,6 +37,8 @@ permalink: /docs/plugins-api/ - [metrics](#metrics) - [Types](#types) - [metrics~callback](#metricscallback) +- [req.set](#reqset) +- [req.get](#reqget) ## Usage @@ -1164,3 +1166,26 @@ Type: [Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Sta - `req` **[Request](https://developer.mozilla.org/Add-ons/SDK/High-Level_APIs/request)** the request obj - `res` **[Response](https://developer.mozilla.org/docs/Web/Guide/HTML/HTML5)** the response obj - `route` **Route** the route obj that serviced the request + +## req.set + +Set context value by key +Requires the context plugin. + +**Parameters** + +- `key` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** key +- `value` **any** value + +Returns **[undefined](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/undefined)** no return value + +## req.get + +Get context value by key. +Requires the context plugin. + +**Parameters** + +- `key` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)?** key + +Returns **any** value stored in context diff --git a/docs/_api/server.md b/docs/_api/server.md index 6da81c48f..135cb38d6 100644 --- a/docs/_api/server.md +++ b/docs/_api/server.md @@ -77,6 +77,11 @@ routes and handlers for incoming requests. `res.writeContinue()` in `server.on('checkContinue')` when proxing (optional, default `false`) - `options.ignoreTrailingSlash` **[Boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** ignore trailing slash on paths (optional, default `false`) + - `options.strictFormatters` **[Boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** enables strict formatters + behavior: a formatter matching the response's content-type is required. If + not found, the response's content-type is automatically set to + 'application/octet-stream'. If a formatter for that content-type is not + found, sending the response errors. (optional, default `true`) **Examples** @@ -137,6 +142,11 @@ Creates a new Server. `res.writeContinue()` in `server.on('checkContinue')` when proxing (optional, default `false`) - `options.ignoreTrailingSlash` **[Boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** ignore trailing slash on paths (optional, default `false`) + - `options.strictFormatters` **[Boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** enables strict formatters + behavior: a formatter matching the response's content-type is required. If + not found, the response's content-type is automatically set to + 'application/octet-stream'. If a formatter for that content-type is not + found, sending the response errors. (optional, default `true`) **Examples** diff --git a/lib/plugins/pre/context.js b/lib/plugins/pre/context.js index 28796a96f..9fe42ed95 100644 --- a/lib/plugins/pre/context.js +++ b/lib/plugins/pre/context.js @@ -30,6 +30,18 @@ function ctx() { return function context(req, res, next) { var data = {}; + /** + * Set context value by key + * Requires the context plugin. + * + * @public + * @memberof Request + * @instance + * @function req.set + * @param {String} key - key + * @param {*} value - value + * @returns {undefined} no return value + */ req.set = function set(key, value) { assert.string(key, 'key must be string'); @@ -39,7 +51,22 @@ function ctx() { data[key] = value; }; + /** + * Get context value by key. + * Requires the context plugin. + * + * @public + * @memberof Request + * @instance + * @function req.get + * @param {String} [key] - key + * @returns {*} value stored in context + */ req.get = function get(key) { + if (key === undefined) { + return data; + } + assert.string(key, 'key must be string'); if (key === '') { @@ -48,11 +75,6 @@ function ctx() { return data[key]; }; - // private method which returns the entire context object - req._getAllContext = function _getAllContext() { - return data; - }; - return next(); }; } diff --git a/test/plugins/context.test.js b/test/plugins/context.test.js index 850f446a7..fe56fd0a3 100644 --- a/test/plugins/context.test.js +++ b/test/plugins/context.test.js @@ -62,6 +62,15 @@ describe('accept parser', function() { b: 2 }); assert.deepEqual(req.get('bar'), [1]); + + assert.deepEqual(req.get(), { + foo: { + a: 1, + b: 2 + }, + bar: [1] + }); + res.send(); return next(); } diff --git a/test/plugins/plugins.test.js b/test/plugins/plugins.test.js index 60214da11..f87d4506b 100644 --- a/test/plugins/plugins.test.js +++ b/test/plugins/plugins.test.js @@ -135,7 +135,7 @@ describe('all other plugins', function() { }, function(req, res, next) { assert.equal('floyd', req.get('pink')); - assert.deepEqual(expectedData, req._getAllContext()); + assert.deepEqual(expectedData, req.get()); asserted = true; res.send(200); return next();