From 6769c96942dc5d1edff937c66b9b702935fe23be Mon Sep 17 00:00:00 2001 From: "Paul \"LeoNerd\" Evans" Date: Wed, 2 Nov 2016 17:55:23 +0000 Subject: [PATCH 1/3] Add a method for querying the js-sdk's current 'request' function in case people want to wrap it --- lib/matrix.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/matrix.js b/lib/matrix.js index 1a4e8ee4e..4edc6c326 100644 --- a/lib/matrix.js +++ b/lib/matrix.js @@ -82,6 +82,13 @@ module.exports.request = function(r) { request = r; }; +/** + * Return the currently-set request function. + */ +module.exports.getRequest = function() { + return request; +}; + /** * Construct a Matrix Client. Similar to {@link module:client~MatrixClient} * except that the 'request', 'store' and 'scheduler' dependencies are satisfied. From 4529578cd6e7e4b350d7aa16c24475886370b535 Mon Sep 17 00:00:00 2001 From: "Paul \"LeoNerd\" Evans" Date: Wed, 2 Nov 2016 18:02:02 +0000 Subject: [PATCH 2/3] Make a handy shortcut for SDK users to provide request wrapping functions in a neat stack --- lib/matrix.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/lib/matrix.js b/lib/matrix.js index 4edc6c326..56d38d520 100644 --- a/lib/matrix.js +++ b/lib/matrix.js @@ -89,6 +89,19 @@ module.exports.getRequest = function() { return request; }; +/** + * Apply wrapping code around the request function. The wrapper function is + * installed as the new request handler, and when invoked it is passed the + * previous value, along with the options and callback arguments. + * @param {requestWrapperFunction} wrapper The wrapping function. + */ +module.exports.wrapRequest = function(wrapper) { + var origRequest = request; + request = function(options, callback) { + return wrapper(origRequest, options, callback); + }; +}; + /** * Construct a Matrix Client. Similar to {@link module:client~MatrixClient} * except that the 'request', 'store' and 'scheduler' dependencies are satisfied. @@ -136,6 +149,16 @@ module.exports.createClient = function(opts) { * @param {requestCallback} callback The request callback. */ +/** + * A wrapper for the request function interface. + * @callback requestWrapperFunction + * @param {requestFunction} origRequest The underlying request function being + * wrapped + * @param {Object} opts The options for this HTTP request, given in the same + * form as {@link requestFunction}. + * @param {requestCallback} callback The request callback. + */ + /** * The request callback interface for performing HTTP requests. This matches the * API for the {@link https://github.com/request/request#requestoptions-callback| From 65f1b3c9760caa1753f4d6bf78aec0f250217c76 Mon Sep 17 00:00:00 2001 From: "Paul \"LeoNerd\" Evans" Date: Wed, 2 Nov 2016 18:04:00 +0000 Subject: [PATCH 3/3] Document the return type of getRequest() --- lib/matrix.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/matrix.js b/lib/matrix.js index 56d38d520..260f1d23f 100644 --- a/lib/matrix.js +++ b/lib/matrix.js @@ -84,6 +84,7 @@ module.exports.request = function(r) { /** * Return the currently-set request function. + * @return {requestFunction} The current request function. */ module.exports.getRequest = function() { return request;