@shoutem/fetch-token-intercept
Advanced tools
Comparing version 0.2.0 to 0.2.2
@@ -1,2 +0,2 @@ | ||
'use strict'; | ||
"use strict"; | ||
@@ -11,4 +11,2 @@ Object.defineProperty(exports, "__esModule", { | ||
var _http = require('./services/http'); | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
@@ -55,7 +53,6 @@ | ||
_createClass(AccessTokenProvider, [{ | ||
key: 'renew', | ||
key: "renew", | ||
value: function renew() { | ||
// if token resolver is not authorized it should just resolve | ||
if (!this.isAuthorized()) { | ||
console.warn('Please authorize provider before renewing or check shouldIntercept config.'); | ||
return Promise.resolve(); | ||
@@ -80,3 +77,3 @@ } | ||
}, { | ||
key: 'authorize', | ||
key: "authorize", | ||
value: function authorize(refreshToken, accessToken) { | ||
@@ -92,3 +89,3 @@ this.tokens = _extends({}, this.tokens, { refreshToken: refreshToken, accessToken: accessToken }); | ||
}, { | ||
key: 'getAuthorization', | ||
key: "getAuthorization", | ||
value: function getAuthorization() { | ||
@@ -103,3 +100,3 @@ return this.tokens; | ||
}, { | ||
key: 'clear', | ||
key: "clear", | ||
value: function clear() { | ||
@@ -110,3 +107,3 @@ this.tokens.accessToken = null; | ||
}, { | ||
key: 'isAuthorized', | ||
key: "isAuthorized", | ||
value: function isAuthorized() { | ||
@@ -116,3 +113,3 @@ return this.tokens.refreshToken !== null; | ||
}, { | ||
key: 'fetchAccessToken', | ||
key: "fetchAccessToken", | ||
value: function fetchAccessToken(tokenRequest) { | ||
@@ -124,7 +121,7 @@ var fetch = this.fetch; | ||
}, { | ||
key: 'handleFetchAccessTokenResponse', | ||
key: "handleFetchAccessTokenResponse", | ||
value: function handleFetchAccessTokenResponse(response) { | ||
this.renewAccessTokenPromise = null; | ||
if ((0, _http.isResponseUnauthorized)(response)) { | ||
if (this.config.isResponseUnauthorized(response)) { | ||
this.clear(); | ||
@@ -137,3 +134,3 @@ return null; | ||
}, { | ||
key: 'handleAccessToken', | ||
key: "handleAccessToken", | ||
value: function handleAccessToken(accessToken, resolve) { | ||
@@ -149,3 +146,3 @@ this.tokens = _extends({}, this.tokens, { accessToken: accessToken }); | ||
}, { | ||
key: 'handleError', | ||
key: "handleError", | ||
value: function handleError(error, reject) { | ||
@@ -158,3 +155,3 @@ this.renewAccessTokenPromise = null; | ||
}, { | ||
key: 'resolveAccessToken', | ||
key: "resolveAccessToken", | ||
value: function resolveAccessToken(resolve, reject) { | ||
@@ -161,0 +158,0 @@ var _this = this; |
@@ -17,2 +17,4 @@ 'use strict'; | ||
var http = _interopRequireWildcard(_http); | ||
var _TokenExpiredException = require('./services/TokenExpiredException'); | ||
@@ -32,2 +34,4 @@ | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } | ||
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } | ||
@@ -57,2 +61,3 @@ | ||
}, | ||
isResponseUnauthorized: http.isResponseUnauthorized, | ||
parseAccessToken: null, | ||
@@ -100,4 +105,9 @@ authorizeRequest: null, | ||
* (Required) Adds authorization for intercepted requests | ||
* authorizeRequest: (request) => authorizedRequest, | ||
* authorizeRequest: (request, accessToken) => authorizedRequest, | ||
* | ||
* Checks if response should be considered unauthorized (by default only 401 responses are | ||
* considered unauthorized. Override this method if you need to trigger token renewal for | ||
* other response statuses. | ||
* isResponseUnauthorized: (response) => boolean, | ||
* | ||
* Number of retries after initial request was unauthorized | ||
@@ -277,6 +287,5 @@ * fetchRetryCount: 1, | ||
var request = requestContext.request; | ||
var shouldIntercept = this.config.shouldIntercept; | ||
return Promise.resolve(shouldIntercept(request)).then(function (shouldIntercept) { | ||
return Promise.resolve(this.config.shouldIntercept(request)).then(function (shouldIntercept) { | ||
return _extends({}, requestContext, { shouldIntercept: shouldIntercept }); | ||
@@ -304,4 +313,4 @@ }); | ||
if (request && accessToken) { | ||
return Promise.resolve(authorizeRequest(request, accessToken)).then(function (request) { | ||
return _extends({}, requestContext, { accessToken: accessToken, request: request }); | ||
return Promise.resolve(authorizeRequest(request, accessToken)).then(function (authorizedRequest) { | ||
return _extends({}, requestContext, { accessToken: accessToken, request: authorizedRequest }); | ||
}); | ||
@@ -316,11 +325,10 @@ } | ||
var request = requestContext.request; | ||
var shouldFetch = this.config.shouldFetch; | ||
// verifies all outside conditions from config are met | ||
if (!shouldFetch) { | ||
if (!this.config.shouldFetch) { | ||
return requestContext; | ||
} | ||
return Promise.resolve(shouldFetch(request)).then(function (shouldFetch) { | ||
return Promise.resolve(this.config.shouldFetch(request)).then(function (shouldFetch) { | ||
return _extends({}, requestContext, { shouldFetch: shouldFetch }); | ||
@@ -362,3 +370,2 @@ }); | ||
var shouldIntercept = requestContext.shouldIntercept; | ||
var shouldInvalidateAccessToken = this.config.shouldInvalidateAccessToken; | ||
@@ -373,3 +380,3 @@ | ||
return Promise.resolve(shouldInvalidateAccessToken(response)).then(function (shouldInvalidateAccessToken) { | ||
return Promise.resolve(this.config.shouldInvalidateAccessToken(response)).then(function (shouldInvalidateAccessToken) { | ||
return _extends({}, requestContext, { shouldInvalidateAccessToken: shouldInvalidateAccessToken }); | ||
@@ -406,2 +413,3 @@ }); | ||
fetchReject = requestContext.fetchReject; | ||
var isResponseUnauthorized = this.config.isResponseUnauthorized; | ||
@@ -411,7 +419,6 @@ // can only be empty on network errors | ||
if (!response) { | ||
fetchReject(); | ||
return; | ||
return fetchReject(); | ||
} | ||
if (shouldIntercept && (0, _http.isResponseUnauthorized)(response)) { | ||
if (shouldIntercept && isResponseUnauthorized(response)) { | ||
throw new _TokenExpiredException2.default(_extends({}, requestContext)); | ||
@@ -418,0 +425,0 @@ } |
@@ -6,2 +6,3 @@ 'use strict'; | ||
}); | ||
exports.isResponseUnauthorized = undefined; | ||
exports.attach = attach; | ||
@@ -15,2 +16,4 @@ exports.configure = configure; | ||
var _http = require('./services/http'); | ||
var _FetchInterceptor = require('./FetchInterceptor'); | ||
@@ -24,16 +27,2 @@ | ||
function init() { | ||
if ((0, _environment.isReactNative)()) { | ||
attach(global); | ||
} else if ((0, _environment.isWorker)()) { | ||
attach(self); | ||
} else if ((0, _environment.isWeb)()) { | ||
attach(window); | ||
} else if ((0, _environment.isNode)()) { | ||
attach(global); | ||
} else { | ||
throw new Error('Unsupported environment for fetch-token-intercept'); | ||
} | ||
} | ||
function attach(env) { | ||
@@ -52,2 +41,3 @@ if (!env.fetch) { | ||
// monkey patch fetch | ||
// eslint-disable-next-line no-unused-vars | ||
var fetchWrapper = function fetchWrapper(fetch) { | ||
@@ -60,5 +50,20 @@ return function () { | ||
}; | ||
// eslint-disable-next-line no-param-reassign | ||
env.fetch = fetchWrapper(env.fetch); | ||
} | ||
function init() { | ||
if ((0, _environment.isReactNative)()) { | ||
attach(global); | ||
} else if ((0, _environment.isWorker)()) { | ||
attach(self); | ||
} else if ((0, _environment.isWeb)()) { | ||
attach(window); | ||
} else if ((0, _environment.isNode)()) { | ||
attach(global); | ||
} else { | ||
throw new Error('Unsupported environment for fetch-token-intercept'); | ||
} | ||
} | ||
function configure(config) { | ||
@@ -82,2 +87,5 @@ interceptor.configure(config); | ||
exports.isResponseUnauthorized = _http.isResponseUnauthorized; | ||
init(); |
@@ -1,2 +0,2 @@ | ||
'use strict'; | ||
"use strict"; | ||
@@ -16,3 +16,3 @@ Object.defineProperty(exports, "__esModule", { | ||
return response['status'] === status; | ||
return response.status === status; | ||
} | ||
@@ -19,0 +19,0 @@ |
{ | ||
"name": "@shoutem/fetch-token-intercept", | ||
"version": "0.2.0", | ||
"version": "0.2.2", | ||
"description": "Fetch interceptor for managing refresh token flow.", | ||
@@ -37,2 +37,3 @@ "main": "lib/index.js", | ||
"babel-plugin-transform-builtin-extend": "^1.1.2", | ||
"babel-plugin-transform-object-rest-spread": "^6.23.0", | ||
"babel-preset-es2015": "^6.9.0", | ||
@@ -39,0 +40,0 @@ "babel-preset-stage-0": "^6.3.13", |
@@ -0,1 +1,5 @@ | ||
[![CircleCI](https://img.shields.io/circleci/project/github/shoutem/fetch-token-intercept.svg)](https://github.com/shoutem/fetch-token-intercept) | ||
[![Code Climate](https://img.shields.io/codeclimate/github/shoutem/fetch-token-intercept.svg)]() | ||
[![GitHub license](https://img.shields.io/badge/license-New%20BSD-blue.svg)](https://raw.githubusercontent.com/shoutem/fetch-token-intercept/master/LICENSE) | ||
# fetch-token-intercept | ||
@@ -9,2 +13,3 @@ Library for easy renewing of access tokens in OAuth's refresh token flow. This library will monkey | ||
- [Auth0 blog - Refresh Tokens: When to Use Them and How They Interact with JWTs](https://auth0.com/blog/refresh-tokens-what-are-they-and-when-to-use-them/) | ||
- [Shoutem blog - Keeping your tokens fresh](https://medium.com/shoutem/keeping-your-api-tokens-fresh-72059a7b0586) | ||
@@ -50,3 +55,3 @@ >Note: | ||
// considered unauthorized). Override this method if you need to trigger token renewal for | ||
// other response statuses. | ||
// other response statuses. Check API reference for helper method which defines default behaviour | ||
isResponseUnauthorized: (response) => boolean, | ||
@@ -102,3 +107,3 @@ | ||
## API reference | ||
## API reference <a name="api-reference"></a> | ||
@@ -118,2 +123,10 @@ ### Exports | ||
`isResponseUnauthorized(response)` | ||
Utility method which determines if given response should be considered unauthorized. | ||
By default, responses with status code `401` are considered unauthorized. | ||
You can use this method in `isResponseUnauthorized` of `config` object | ||
when you want to extend default behaviour. | ||
## Tests | ||
@@ -120,0 +133,0 @@ |
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
40177
728
137
26