Skip to content

Commit 4d07e74

Browse files
Add an alternative HTTP Fetcher that wraps jquery instead of using fetch()
1 parent 38c4b44 commit 4d07e74

File tree

4 files changed

+99
-7
lines changed

4 files changed

+99
-7
lines changed

README.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,30 @@ client.getToken({
188188
})
189189
```
190190

191-
## Sending passive OpenID Connect authentication requests using hidden iframes (Advanced)
191+
192+
The `FetcherJQuery` is an alternative interface that uses `jQuery.ajax()` instead of `fetch()`. Consider the `FetcherJQuery` *beta*.
193+
194+
Notice that this class takes the jQuery object as a second argument to the constructor. The fetch options argument is provided as options to `jQuery.ajax()`.
195+
196+
The `fetch()` function returns a Promise that resolves the response data.
197+
198+
```javascript
199+
200+
// Initialization
201+
let f = new FetcherJQuery(client, $)
202+
let url = 'https://some-api.httpjs.net/rest/me'
203+
f.fetch(url, {})
204+
.then((data) => {
205+
console.log("I got protected json data from the API", data)
206+
})
207+
.catch((err) => {
208+
console.error("Error from fetcher", err)
209+
})
210+
```
211+
212+
213+
214+
## Sending passive OpenID Connect authentication requests using hidden iFrames (Advanced)
192215

193216
If your OpenID Connect provider support passive requests and the enduser is already authenticated with single sign-on, you may obtain an authenticated state using a hidden iframe without redirecting the user at all.
194217

src/HTTP/Fetcher.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11

2-
class TokenExpiredException extends Error {
3-
4-
}
2+
import ExpiredTokenError from '../errors/ExpiredTokenError'
53

64
export default class Fetcher {
75
constructor(jso) {
@@ -12,7 +10,7 @@ export default class Fetcher {
1210
return fetch(url, opts)
1311
.then((response) => {
1412
if (response.status === 401) {
15-
throw new TokenExpiredException()
13+
throw new ExpiredTokenError()
1614
}
1715
return response
1816
})
@@ -48,7 +46,7 @@ export default class Fetcher {
4846
fetchOpts.headers.Authorization = 'Bearer ' + token.access_token
4947
return this._fetch(url, fetchOpts)
5048
.catch((err) => {
51-
if (err instanceof TokenExpiredException) {
49+
if (err instanceof ExpiredTokenError) {
5250
console.error("Token was expired. Deleting all tokens for this provider and get a new one", err)
5351
this.jso.wipeTokens()
5452
return this.fetch(url, opts, reccur+1)

src/HTTP/FetcherJQuery.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import ExpiredTokenError from '../errors/ExpiredTokenError'
2+
3+
export default class FetcherJQuery {
4+
constructor(jso, jquery) {
5+
this.jso = jso
6+
this.jquery = jquery
7+
}
8+
9+
_fetch(url, opts) {
10+
11+
return new Promise((resolve, reject) => {
12+
13+
opts.done = resolve
14+
opts.fail = (jqXHR, textStatus, errorThrown) => {
15+
let status = parseInt(textStatus, 10)
16+
if (status === 401) {
17+
this.jso.wipeTokens()
18+
return reject(new ExpiredTokenError())
19+
}
20+
return reject(errorThrown)
21+
}
22+
return this.jquery.ajax(url, opts)
23+
24+
})
25+
26+
}
27+
28+
fetch(url, opts, reccur) {
29+
reccur = reccur ? reccur : 0
30+
if (reccur > 2) {
31+
throw new Error("Reccursion error. Expired tokens deleted and tried again multiple times.")
32+
}
33+
let getTokenOpts = {}
34+
let fetchOpts = {
35+
'mode': 'cors'
36+
}
37+
if (opts) {
38+
fetchOpts = opts
39+
Object.assign(fetchOpts, opts)
40+
}
41+
if (opts && opts.jso) {
42+
Object.assign(getTokenOpts, opts.jso)
43+
}
44+
45+
return this.jso.getToken(getTokenOpts)
46+
.catch((err) => {
47+
console.error("Error fetching token to use ", err)
48+
})
49+
.then((token) => {
50+
// console.log("I got the token: ", token.access_token)
51+
52+
if (!fetchOpts.headers) {
53+
fetchOpts.headers = {}
54+
}
55+
fetchOpts.headers.Authorization = 'Bearer ' + token.access_token
56+
return this._fetch(url, fetchOpts)
57+
.catch((err) => {
58+
if (err instanceof ExpiredTokenError) {
59+
console.error("Token was expired. Deleting all tokens for this provider and get a new one", err)
60+
this.jso.wipeTokens()
61+
return this.fetch(url, opts, reccur+1)
62+
}
63+
})
64+
65+
})
66+
}
67+
68+
69+
70+
}

src/JSO.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import IFramePassive from './Loaders/IFramePassive'
2323
import Popup from './Loaders/Popup'
2424

2525
import Fetcher from './HTTP/Fetcher'
26+
import FetcherJQuery from './HTTP/FetcherJQuery'
2627

2728
// import ExpiredTokenError from './errors/ExpiredTokenError'
2829
// import HTTPError from './errors/HTTPError'
@@ -435,4 +436,4 @@ class JSO extends EventEmitter {
435436

436437
// Object.assign(JSO.prototype, new EventEmitter({}))
437438

438-
export {JSO, BasicLoader, HTTPRedirect, Popup, IFramePassive, Fetcher}
439+
export {JSO, BasicLoader, HTTPRedirect, Popup, IFramePassive, Fetcher, FetcherJQuery}

0 commit comments

Comments
 (0)