Created
July 1, 2020 17:01
-
-
Save saiganov/58300e5341224dd5e57c6eb59d47cd0f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { singleton } from 'aurelia-dependency-injection' | |
import {HttpClient} from "aurelia-fetch-client"; | |
import * as environment from './../../../config/environment.json'; | |
import {LocalStorageService} from "../../common/local-storage.service"; | |
@singleton() | |
export class ConfiguredHttpClient { | |
private readonly _instance: HttpClient; | |
private readonly _localStorage: LocalStorageService; | |
constructor(http: HttpClient, localStorage: LocalStorageService | |
) { | |
this._instance = http; | |
this._localStorage = localStorage; | |
} | |
public withDefaultConfiguration() { | |
const ls = this._localStorage; | |
// @ts-ignore | |
const apiGateway = environment.apiGateway; | |
this._instance.configure(config => { | |
config | |
.useStandardConfiguration() | |
.withBaseUrl(apiGateway) | |
.withInterceptor({ | |
request(request) { | |
request.headers.append('Authorization', 'Bearer ' + ls.getUserAccessToken()) | |
return request | |
}, | |
responseError(error) { | |
if(error.status === 401) { | |
ls.clearUserData(); | |
// TODO: redirect to login page | |
} | |
return error; | |
} | |
}) | |
}) | |
return this; | |
} | |
public getInstance(): HttpClient { | |
return this._instance; | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Aurelia} from 'aurelia-framework'; | |
import * as environment from '../config/environment.json'; | |
import {PLATFORM} from 'aurelia-pal'; | |
import '@progress/kendo-ui/js/kendo.all' | |
import '@progress/kendo-ui/css/web/kendo.common.min.css'; | |
import '@progress/kendo-ui/css/web/kendo.bootstrap.min.css'; | |
import 'app.css' | |
import { HttpClient } from 'aurelia-fetch-client'; | |
import {ConfiguredHttpClient} from "./features/login/configured-http-client"; | |
export function configure(aurelia: Aurelia) { | |
aurelia.use | |
.standardConfiguration() | |
.feature(PLATFORM.moduleName('resources/index')) | |
.plugin(PLATFORM.moduleName('aurelia-kendoui-bridge')) | |
.plugin(PLATFORM.moduleName('aurelia-validation')); | |
aurelia.use.developmentLogging(environment.debug ? 'debug' : 'warn'); | |
if (environment.testing) { | |
aurelia.use.plugin(PLATFORM.moduleName('aurelia-testing')); | |
} | |
const container = aurelia.container; | |
const http = container.get(ConfiguredHttpClient).withDefaultConfiguration().getInstance(); | |
container.registerInstance(HttpClient, http) | |
aurelia.start().then(() => aurelia.setRoot(PLATFORM.moduleName('app'))); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment