Closed dwaite closed 6 years ago
Note: I do not know the copyright policy for the project (files are copyright Google, not OIDF) so I added myself as the copyright holder in the header for the new files.
👍
Closing this PR given that this is no longer something we want to do.
It would be very useful to have an alternative Requestor implementation that does not require jquery to be available globally. A fetch-based Requestor would be ideal.
Yes. It’s not that hard to build such an implementation. We realized that the typings for fetch
s assumed a lot of stuff was global which makes it untenable as a interface whole implementation can be provided.
I think using jquery..xhr
’s typings are much better suited. It should be easy to provide an implementation of the Requestor
that uses fetch
.
Yes, I did this in my app by instantiating the BaseTokenRequestHandler like so:
new AppAuth.BaseTokenRequestHandler({
// fetch-based alternative to built-in jquery implementation
xhr: function (settings) {
return new Promise(function (resolve, reject) {
fetch(settings.url, {
method: settings.method,
body: settings.data,
mode: 'cors',
cache: 'no-cache',
headers: settings.headers
}).then(function (response) {
if (response.ok) {
response.json().then(resolve);
} else {
reject(response.statusText);
}
}, reject);
});
}
})
With this change, I was able to drop jQuery as a dependency. It would be great if this was an OOTB option.
Yes, this is exactly what I was talking about. I could provide another Requestor
out of the box. Would that help?
Yes, that would be very helpful. An even more neat trick would be to detect the presence / absence of $.ajax and use that to decide on what would be the best default Requestor
.
Yes. That would be amazing.
Agree. jquery is not suitable for react-based apps
I wrote this fetch-based version as part of a sample FWIW.
https://github.com/pingidentity/angular-spa-sample/blob/master/src/app/html5_requestor.ts
@dwaite Its easy to write something specific for your app / use-case. However there are more things one needs to consider when its a library which could be potentially used by a lot of diverse clients.
fetcher
for the environmenttestFetchWithResult
andtestFetchWithError
; these are fetcher implementations that return either the given response body (by stringifying the supplied object) or the given promise rejection.