Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions docs/_api/plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
10 changes: 10 additions & 0 deletions docs/_api/server.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**

Expand Down Expand Up @@ -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**

Expand Down
36 changes: 34 additions & 2 deletions lib/plugins/pre/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -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');

Expand All @@ -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;
};

Expand Down
9 changes: 9 additions & 0 deletions test/plugins/context.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
2 changes: 1 addition & 1 deletion test/plugins/plugins.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down