mappersmith
Advanced tools
Comparing version 2.19.0 to 2.20.0
@@ -38,2 +38,3 @@ 'use strict'; | ||
this.GatewayClassFactory = GatewayClassFactory; | ||
this.maxMiddlewareStackExecutionAllowed = configs.maxMiddlewareStackExecutionAllowed; | ||
} | ||
@@ -69,2 +70,3 @@ | ||
var gatewayConfigs = this.manifest.gatewayConfigs; | ||
var chainRequestPhase = function chainRequestPhase(requestPromise, middleware) { | ||
@@ -77,10 +79,18 @@ return requestPromise.then(function (request) { | ||
}; | ||
var chainResponsePhase = function chainResponsePhase(next, middleware) { | ||
return function () { | ||
return middleware.response(next); | ||
}; | ||
}; | ||
return new this.Promise(function (resolve, reject) { | ||
var executions = 0; | ||
var executeMiddlewareStack = function executeMiddlewareStack() { | ||
return middleware.reduce(chainRequestPhase, _this3.Promise.resolve(initialRequest)).then(function (finalRequest) { | ||
executions++; | ||
if (executions > _this3.maxMiddlewareStackExecutionAllowed) { | ||
throw new Error('[Mappersmith] infinite loop detected (middleware stack invoked ' + executions + ' times). Check the use of "renew" in one of the middleware.'); | ||
} | ||
var chainResponsePhase = function chainResponsePhase(next, middleware) { | ||
return function () { | ||
return middleware.response(next, executeMiddlewareStack); | ||
}; | ||
}; | ||
var callGateway = function callGateway() { | ||
@@ -90,6 +100,9 @@ return new GatewayClass(finalRequest, gatewayConfigs).call(); | ||
var execute = middleware.reduce(chainResponsePhase, callGateway); | ||
return execute(); | ||
}); | ||
}; | ||
return execute().then(function (response) { | ||
return resolve(response); | ||
}); | ||
return new this.Promise(function (resolve, reject) { | ||
executeMiddlewareStack().then(function (response) { | ||
return resolve(response); | ||
}).catch(reject); | ||
@@ -96,0 +109,0 @@ }); |
@@ -18,3 +18,3 @@ 'use strict'; | ||
/* global VERSION */ | ||
var version = exports.version = '2.19.0'; | ||
var version = exports.version = '2.20.0'; | ||
@@ -28,2 +28,12 @@ var configs = exports.configs = { | ||
/** | ||
* The maximum amount of executions allowed before it is considered an infinite loop. | ||
* In the response phase of middleware, it's possible to execute a function called "renew", | ||
* which can be used to rerun the middleware stack. This feature is useful in some scenarios, | ||
* for example, re-fetching an invalid access token. | ||
* This configuration is used to detect infinite loops, don't increase this value too much | ||
* @default 2 | ||
*/ | ||
maxMiddlewareStackExecutionAllowed: 2, | ||
/** | ||
* Gateway implementation, it defaults to "lib/gateway/xhr" for browsers and | ||
@@ -30,0 +40,0 @@ * "lib/gateway/http" for node |
@@ -85,3 +85,3 @@ 'use strict'; | ||
next().then(function (response) { | ||
resolve(enhancedResponse(response, retryConfigs.headerRetryCount, retryCount, retryConfigs.headerRetryTime, retryTime)); | ||
shouldRetry && retryConfigs.validateRetry(response) ? scheduleRequest() : resolve(enhancedResponse(response, retryConfigs.headerRetryCount, retryCount, retryConfigs.headerRetryTime, retryTime)); | ||
}).catch(function (response) { | ||
@@ -88,0 +88,0 @@ shouldRetry && retryConfigs.validateRetry(response) ? scheduleRequest() : reject(enhancedResponse(response, retryConfigs.headerRetryCount, retryCount, retryConfigs.headerRetryTime, retryTime)); |
{ | ||
"name": "mappersmith", | ||
"version": "2.19.0", | ||
"version": "2.20.0", | ||
"description": "It is a lightweight rest client for node.js and the browser", | ||
@@ -5,0 +5,0 @@ "author": "Tulio Ornelas <ornelas.tulio@gmail.com>", |
@@ -395,2 +395,60 @@ [![npm version](https://badge.fury.io/js/mappersmith.svg)](http://badge.fury.io/js/mappersmith) | ||
The response phase can optionally receive a function called "renew". This function can be used to rerun the | ||
middleware stack. This feature is useful in some scenarios, for example, automatically refreshing an expired access token. | ||
Example: | ||
```javascript | ||
const AccessTokenMiddleware = () => { | ||
// maybe this is stored elsewhere, here for simplicity | ||
let accessToken = null | ||
return () => ({ | ||
request(request) { | ||
return Promise | ||
.resolve(accessToken) | ||
.then((token) => token || fetchAccessToken()) | ||
.then((token) => { | ||
accessToken = token | ||
return request.enhance({ | ||
headers: { 'Authorization': `Token ${token}` } | ||
}) | ||
}) | ||
}, | ||
response(next, renew) { | ||
return next().catch(response => { | ||
if (response.status() === 401) { // token expired | ||
accessToken = null | ||
return renew() | ||
} | ||
return next() | ||
}) | ||
} | ||
}) | ||
} | ||
``` | ||
Then: | ||
```javascript | ||
const AccessToken = AccessTokenMiddleware() | ||
const client = forge({ | ||
// ... | ||
middleware: [ AccessToken ], | ||
// ... | ||
}) | ||
``` | ||
"renew" can only be invoked sometimes before it's considered an infinite loop, make sure your middleware | ||
can distinguish an error from a "renew". By default, mappersmith will allow 2 calls to "renew". This can | ||
be configured with `configs.maxMiddlewareStackExecutionAllowed`. It's advised to keep this number low. | ||
Example: | ||
```javascript | ||
import { configs } from 'mappersmith' | ||
configs.maxMiddlewareStackExecutionAllowed = 3 | ||
``` | ||
If an infinite loop is detected, mappersmith will throw an error. | ||
### <a name="global-middleware"></a> Global middleware | ||
@@ -397,0 +455,0 @@ |
@@ -127,2 +127,14 @@ 'use strict'; | ||
}, | ||
/** | ||
* Utility method to get a header value by name | ||
* | ||
* @param {String} name | ||
* | ||
* @return {String|Undefined} | ||
*/ | ||
header: function header(name) { | ||
return this.headers()[name.toLowerCase()]; | ||
}, | ||
body: function body() { | ||
@@ -129,0 +141,0 @@ return this.requestParams[this.methodDescriptor.bodyAttr]; |
@@ -103,3 +103,3 @@ 'use strict'; | ||
if (index > 0) { | ||
var key = headerPair.substring(0, index).toLowerCase(); | ||
var key = headerPair.substring(0, index).toLowerCase().trim(); | ||
var val = headerPair.substring(index + 2).trim(); | ||
@@ -106,0 +106,0 @@ headers[key] = val; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
107943
2099
1075