Skip to content

Commit 5ca998c

Browse files
committed
Updated docs
Fixed some lining errors Added missing test dependency
1 parent 689ee2b commit 5ca998c

File tree

6 files changed

+49
-36
lines changed

6 files changed

+49
-36
lines changed

.babelrc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
2-
presets: ["es2015"],
3-
plugins: ["transform-object-rest-spread",
2+
"presets": ["es2015"],
3+
"plugins": ["transform-object-rest-spread",
44
["babel-plugin-transform-builtin-extend", {
5-
globals: ["Error", "Array"]
5+
"globals": ["Error", "Array"]
66
}]],
7-
sourceMaps: true
7+
"sourceMaps": true
88
}

README.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ by renewing the current access token and then retrying an initial fetch operatio
66
If you are not familiar with refresh token flow you should check some of the following resources:
77
- [RFC standards track regarding refresh token flow](https://tools.ietf.org/html/rfc6749#page-10)
88
- [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/)
9+
- [Shoutem blog - Keeping your tokens fresh](https://medium.com/shoutem/keeping-your-api-tokens-fresh-72059a7b0586)
910

1011
>Note:
1112
This library expects that fetch and promise api's are available at target environment. You should
@@ -46,7 +47,7 @@ config: {
4647
4748
// Checks if response should be considered unauthorized (by default only 401 responses are
4849
// considered unauthorized). Override this method if you need to trigger token renewal for
49-
// other response statuses.
50+
// other response statuses. Check API reference for helper method which defines default behaviour
5051
isResponseUnauthorized: (response) => boolean,
5152
5253
// (Required) Adds authorization for intercepted requests
@@ -97,7 +98,7 @@ to stop fetch interception.
9798
...
9899
```
99100

100-
## API reference
101+
## API reference <a name="api-reference"></a>
101102

102103
### Exports
103104
`configure(configuration)`
@@ -112,6 +113,14 @@ to stop fetch interception.
112113

113114
Clears all tokens from interceptor.
114115

116+
`isResponseUnauthorized(response)`
117+
118+
Utility method which determines if given response should be considered unauthorized.
119+
By default, responses with status code `401` are considered unauthorized.
120+
You can use this method in `isResponseUnauthorized` of `config` object
121+
when you want to extend default behaviour.
122+
123+
115124
## Tests
116125

117126
```

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"babel-core": "^6.9.1",
3535
"babel-eslint": "^6.0.0",
3636
"babel-plugin-transform-builtin-extend": "^1.1.2",
37+
"babel-plugin-transform-object-rest-spread": "^6.23.0",
3738
"babel-preset-es2015": "^6.9.0",
3839
"babel-preset-stage-0": "^6.3.13",
3940
"babel-register": "^6.9.0",

src/FetchInterceptor.js

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ export default class FetchInterceptor {
195195
fetchArgs,
196196
fetchResolve,
197197
fetchReject,
198-
}
198+
};
199199
}
200200

201201
createRequest(requestContext) {
@@ -205,14 +205,13 @@ export default class FetchInterceptor {
205205
return {
206206
...requestContext,
207207
request,
208-
}
208+
};
209209
}
210210

211211
shouldIntercept(requestContext) {
212212
const { request } = requestContext;
213-
const { shouldIntercept } = this.config;
214213

215-
return Promise.resolve(shouldIntercept(request))
214+
return Promise.resolve(this.config.shouldIntercept(request))
216215
.then(shouldIntercept =>
217216
({ ...requestContext, shouldIntercept })
218217
);
@@ -229,10 +228,10 @@ export default class FetchInterceptor {
229228
const { accessToken } = this.accessTokenProvider.getAuthorization();
230229
const { authorizeRequest } = this.config;
231230

232-
if (request && accessToken){
231+
if (request && accessToken) {
233232
return Promise.resolve(authorizeRequest(request, accessToken))
234-
.then(request =>
235-
({ ...requestContext, accessToken, request })
233+
.then(authorizedRequest =>
234+
({ ...requestContext, accessToken, request: authorizedRequest })
236235
);
237236
}
238237

@@ -241,14 +240,13 @@ export default class FetchInterceptor {
241240

242241
shouldFetch(requestContext) {
243242
const { request } = requestContext;
244-
const { shouldFetch } = this.config;
245243

246244
// verifies all outside conditions from config are met
247-
if (!shouldFetch) {
245+
if (!this.config.shouldFetch) {
248246
return requestContext;
249247
}
250248

251-
return Promise.resolve(shouldFetch(request))
249+
return Promise.resolve(this.config.shouldFetch(request))
252250
.then(shouldFetch =>
253251
({ ...requestContext, shouldFetch })
254252
);
@@ -282,15 +280,14 @@ export default class FetchInterceptor {
282280

283281
shouldInvalidateAccessToken(requestContext) {
284282
const { shouldIntercept } = requestContext;
285-
const { shouldInvalidateAccessToken } = this.config;
286283

287284
if (!shouldIntercept) {
288285
return requestContext;
289286
}
290287

291288
const { response } = requestContext;
292289
// check if response invalidates access token
293-
return Promise.resolve(shouldInvalidateAccessToken(response))
290+
return Promise.resolve(this.config.shouldInvalidateAccessToken(response))
294291
.then(shouldInvalidateAccessToken =>
295292
({ ...requestContext, shouldInvalidateAccessToken })
296293
);
@@ -319,12 +316,11 @@ export default class FetchInterceptor {
319316

320317
// can only be empty on network errors
321318
if (!response) {
322-
fetchReject();
323-
return;
319+
return fetchReject();
324320
}
325321

326322
if (shouldIntercept && isResponseUnauthorized(response)) {
327-
throw new TokenExpiredException({ ...requestContext })
323+
throw new TokenExpiredException({ ...requestContext });
328324
}
329325

330326
if (this.config.onResponse) {

src/index.js

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,11 @@ import {
44
isWeb,
55
isNode,
66
} from './services/environment';
7+
import { isResponseUnauthorized } from './services/http';
78
import FetchInterceptor from './FetchInterceptor';
89

910
let interceptor = null;
1011

11-
function init() {
12-
if (isReactNative()) {
13-
attach(global);
14-
} else if (isWorker()) {
15-
attach(self);
16-
} else if (isWeb()) {
17-
attach(window);
18-
} else if (isNode()) {
19-
attach(global);
20-
} else {
21-
throw new Error('Unsupported environment for fetch-token-intercept');
22-
}
23-
}
24-
2512
export function attach(env) {
2613
if (!env.fetch) {
2714
throw Error('No fetch available. Unable to register fetch-token-intercept');
@@ -35,10 +22,26 @@ export function attach(env) {
3522
interceptor = new FetchInterceptor(env.fetch);
3623

3724
// monkey patch fetch
25+
// eslint-disable-next-line no-unused-vars
3826
const fetchWrapper = fetch => (...args) => interceptor.intercept(...args);
27+
// eslint-disable-next-line no-param-reassign
3928
env.fetch = fetchWrapper(env.fetch);
4029
}
4130

31+
function init() {
32+
if (isReactNative()) {
33+
attach(global);
34+
} else if (isWorker()) {
35+
attach(self);
36+
} else if (isWeb()) {
37+
attach(window);
38+
} else if (isNode()) {
39+
attach(global);
40+
} else {
41+
throw new Error('Unsupported environment for fetch-token-intercept');
42+
}
43+
}
44+
4245
export function configure(config) {
4346
interceptor.configure(config);
4447
}
@@ -55,4 +58,8 @@ export function clear() {
5558
return interceptor.clear();
5659
}
5760

61+
export {
62+
isResponseUnauthorized,
63+
};
64+
5865
init();

src/services/http.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ function isResponseStatus(response, status) {
66
return false;
77
}
88

9-
return response['status'] === status;
9+
return response.status === status;
1010
}
1111

1212
export function isResponseOk(response) {

0 commit comments

Comments
 (0)