|
|
|
@ -5,6 +5,7 @@ import { catchError, switchMap } from 'rxjs/operators'; |
|
|
|
import { SetEnvironment } from '../actions/config.actions'; |
|
|
|
import { Config } from '../models/config'; |
|
|
|
import { RestOccurError } from '../actions/rest.actions'; |
|
|
|
import { deepMerge } from './object-utils'; |
|
|
|
|
|
|
|
export function getRemoteEnv(injector: Injector, environment: Partial<Config.Environment>) { |
|
|
|
const { remoteEnv } = environment; |
|
|
|
@ -18,7 +19,22 @@ export function getRemoteEnv(injector: Injector, environment: Partial<Config.Env |
|
|
|
.request<Config.Environment>(method, url, { headers }) |
|
|
|
.pipe( |
|
|
|
catchError(err => store.dispatch(new RestOccurError(err))), // TODO: Condiser get handle function from a provider
|
|
|
|
switchMap(env => store.dispatch(new SetEnvironment({ ...environment, ...env }))), |
|
|
|
switchMap(env => store.dispatch(mergeEnvironments(environment, env, remoteEnv))), |
|
|
|
) |
|
|
|
.toPromise(); |
|
|
|
} |
|
|
|
|
|
|
|
function mergeEnvironments( |
|
|
|
local: Partial<Config.Environment>, |
|
|
|
remote: any, |
|
|
|
config: Config.RemoteEnv, |
|
|
|
) { |
|
|
|
switch (config.mergeStrategy) { |
|
|
|
case 'deepmerge': |
|
|
|
return new SetEnvironment(deepMerge(local, remote)); |
|
|
|
case 'overwrite': |
|
|
|
return new SetEnvironment(remote); |
|
|
|
default: |
|
|
|
return new SetEnvironment(config.mergeStrategy(local, remote)); |
|
|
|
} |
|
|
|
} |
|
|
|
|