From 666dcd6657ef29f920f34ccc63545524a34f82a5 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 | 33 +++++++++++++++++++++++++++++++++ docs/_api/server.md | 10 ++++++++++ lib/plugins/pre/context.js | 36 ++++++++++++++++++++++++++++++++++-- test/plugins/context.test.js | 9 +++++++++ test/plugins/plugins.test.js | 2 +- 5 files changed, 87 insertions(+), 3 deletions(-) diff --git a/docs/_api/plugins.md b/docs/_api/plugins.md index 211d829a7..7777cdb03 100644 --- a/docs/_api/plugins.md +++ b/docs/_api/plugins.md @@ -37,6 +37,9 @@ permalink: /docs/plugins-api/ - [metrics](#metrics) - [Types](#types) - [metrics~callback](#metricscallback) +- [req.set](#reqset) +- [req.get](#reqget) +- [req.getAll](#reqgetall) ## Usage @@ -1164,3 +1167,33 @@ 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 + +## req.getAll + +Get all context +Requires the context plugin. + +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..37bc06124 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,6 +51,17 @@ 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) { assert.string(key, 'key must be string'); @@ -48,8 +71,17 @@ function ctx() { return data[key]; }; - // private method which returns the entire context object - req._getAllContext = function _getAllContext() { + /** + * Get all context + * Requires the context plugin. + * + * @public + * @memberof Request + * @instance + * @function req.getAll + * @returns {*} value stored in context + */ + req.getAll = function getAll() { return data; }; diff --git a/test/plugins/context.test.js b/test/plugins/context.test.js index 850f446a7..03bc97ba6 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.getAll(), { + 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..fa5ae698e 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.getAll()); asserted = true; res.send(200); return next();