mirror of https://github.com/abpframework/abp
parent
3d8c72727b
commit
602eafe169
@ -1,21 +1,24 @@
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import { Injector } from '@angular/core';
|
||||
import { Store } from '@ngxs/store';
|
||||
import { of } from 'rxjs';
|
||||
import { catchError, switchMap } from 'rxjs/operators';
|
||||
import { SetEnvironment } from '../actions/config.actions';
|
||||
import { Config } from '../models/config';
|
||||
|
||||
export async function getRemoteEnv(injector: Injector, environment: Config.Environment) {
|
||||
export function getRemoteEnv(injector: Injector, environment: Partial<Config.Environment>) {
|
||||
const { remoteEnv } = environment;
|
||||
if (!remoteEnv?.url) return Promise.resolve();
|
||||
const { headers = {}, method = 'GET', url } = remoteEnv || ({} as Config.RemoteEnv);
|
||||
if (!url) return Promise.resolve();
|
||||
|
||||
const http = injector.get(HttpClient);
|
||||
const store = injector.get(Store);
|
||||
|
||||
const env = await http
|
||||
.request<Config.Environment>(remoteEnv.method || 'GET', remoteEnv.url, {
|
||||
headers: remoteEnv.headers || {},
|
||||
})
|
||||
return http
|
||||
.request<Config.Environment>(method, url, { headers })
|
||||
.pipe(
|
||||
catchError(() => of({} as Config.Environment)), // TODO: Handle error
|
||||
switchMap(env => store.dispatch(new SetEnvironment({ ...environment, ...env }))),
|
||||
)
|
||||
.toPromise();
|
||||
|
||||
return store.dispatch(new SetEnvironment({ ...environment, ...env })).toPromise();
|
||||
}
|
||||
|
||||
Loading…
Reference in new issue