You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently there is an HttpInterceptor in the Angular implementation, which allow the access token to be automatically added to any request header. This feature is made possible thanks to '@angular/common/http' but could easily be rewritten in pure javascript to be made available directly in the core library.
Some snippets examples:
Fetch
const{fetch: origFetch}=window;window.fetch=async(...args)=>{console.log("fetch called with args:",args);constresponse=awaitorigFetch(...args);/* work with the cloned response in a separate promise chain -- could use the same chain with `await`. */response.clone().json().then(body=>console.log("intercepted response:",body)).catch(err=>console.error(err));/* the original response can be resolved unmodified: *///return response;/* or mock the response: */return{ok: true,status: 200,json: async()=>({userId: 1,id: 1,title: "Mocked!!",completed: false})};};// test it out with a typical fetch callfetch("https://jsonplaceholder.typicode.com/todos/1").then(response=>response.json()).then(json=>console.log("original caller received:",json)).catch(err=>console.error(err));
XMLHttpRequest
(function(open){XMLHttpRequest.prototype.open=function(XMLHttpRequest){varself=this;this.addEventListener("readystatechange",function(){if(this.responseText.length>0&&this.readyState==4&&this.responseURL.indexOf('www.google.com')>=0){Object.defineProperty(self,'response',{get: function(){returnbValue;},set: function(newValue){bValue=newValue;},enumerable: true,configurable: true});self.response='updated value'// Intercepted Value }},false);open.apply(this,arguments);};})(XMLHttpRequest.prototype.open);
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Currently there is an
HttpInterceptor
in the Angular implementation, which allow theaccess token
to be automatically added to any request header. This feature is made possible thanks to'@angular/common/http'
but could easily be rewritten in pure javascript to be made available directly in the core library.Some snippets examples:
Fetch
XMLHttpRequest
Beta Was this translation helpful? Give feedback.
All reactions