I have posted a question before on how to configure Aurelia's fetch client and use the configured client in all of my viewModels(Aurelia: configure fetch client) I configure my fetch client in my main.js file. Now i want to use this same configured fetch client in main.js itself. I cant inject an instance of the client because it will be defined after the injection.
import {HttpClient} from 'aurelia-fetch-client';
export function configure(aurelia) {
this.jsonobj = {
'operation':'verify'
};
aurelia.use
.standardConfiguration()
.developmentLogging();
let container = aurelia.container;
let http = new HttpClient();
http.configure(config => {
config
.useStandardConfiguration()
.withInterceptor({
request(request) {
request.headers.set('token', window.localStorage.getItem('token'));
return request;
}
});
});
container.registerInstance(HttpClient, http);
aurelia.start().then(
a => {
this.http.fetch('dist/components/api.php', {
credentials: 'include',
method: 'post',
body: JSON.stringify(this.jsonobj)
})
.then(response => response.json())
.then(data => {
if (data.error==='true') {
a.setRoot('login');
}else {
a.setRoot('app');
}
});
});
}