diff --git a/npm/ng-packs/apps/dev-app/src/environments/environment.ts b/npm/ng-packs/apps/dev-app/src/environments/environment.ts index 87ca8d1447..771698e394 100644 --- a/npm/ng-packs/apps/dev-app/src/environments/environment.ts +++ b/npm/ng-packs/apps/dev-app/src/environments/environment.ts @@ -23,23 +23,18 @@ export const environment = { rootNamespace: 'MyCompanyName.MyProjectName', }, AbpFeatureManagement: { - url: 'https://localhost:44305', rootNamespace: 'Volo.Abp', }, AbpPermissionManagement: { - url: 'https://localhost:44305', rootNamespace: 'Volo.Abp.PermissionManagement', }, AbpTenantManagement: { - url: 'https://localhost:44305', rootNamespace: 'Volo.Abp.TenantManagement', }, AbpIdentity: { - url: 'https://localhost:44305', rootNamespace: 'Volo.Abp', }, AbpSettingManagement: { - url: 'https://localhost:44305', rootNamespace: 'Volo.Abp.SettingManagement', }, }, diff --git a/npm/ng-packs/packages/core/src/lib/models/environment.ts b/npm/ng-packs/packages/core/src/lib/models/environment.ts index b29f0d338d..46596685b0 100644 --- a/npm/ng-packs/packages/core/src/lib/models/environment.ts +++ b/npm/ng-packs/packages/core/src/lib/models/environment.ts @@ -18,15 +18,14 @@ export interface ApplicationInfo { logoUrl?: string; } -export type ApiConfig = { +export interface ApiConfig { [key: string]: string; url: string; -} & Partial<{ - rootNamespace: string; -}>; + rootNamespace?: string; +} export interface Apis { - [key: string]: ApiConfig; + [key: string]: Partial; default: ApiConfig; } diff --git a/npm/ng-packs/packages/core/src/lib/services/environment.service.ts b/npm/ng-packs/packages/core/src/lib/services/environment.service.ts index 2062294b7b..10d7664acb 100644 --- a/npm/ng-packs/packages/core/src/lib/services/environment.service.ts +++ b/npm/ng-packs/packages/core/src/lib/services/environment.service.ts @@ -4,6 +4,9 @@ import { map } from 'rxjs/operators'; import { Apis, Environment } from '../models/environment'; import { InternalStore } from '../utils/internal-store-utils'; +const mapToApiUrl = (key: string) => (apis: Apis) => + (apis[key] || apis.default).url || apis.default.url; + @Injectable({ providedIn: 'root' }) export class EnvironmentService { private readonly store = new InternalStore({} as Environment); @@ -21,13 +24,11 @@ export class EnvironmentService { } getApiUrl(key?: string) { - return (this.store.state.apis[key || 'default'] || this.store.state.apis.default).url; + return mapToApiUrl(key)(this.store.state.apis); } getApiUrl$(key?: string) { - return this.store - .sliceState(state => state.apis) - .pipe(map((apis: Apis) => (apis[key || 'default'] || apis.default).url)); + return this.store.sliceState(state => state.apis).pipe(map(mapToApiUrl(key))); } setState(environment: Environment) { diff --git a/npm/ng-packs/packages/core/src/lib/tests/environment.service.spec.ts b/npm/ng-packs/packages/core/src/lib/tests/environment.service.spec.ts index fe9f50a546..95c2b58a6c 100644 --- a/npm/ng-packs/packages/core/src/lib/tests/environment.service.spec.ts +++ b/npm/ng-packs/packages/core/src/lib/tests/environment.service.spec.ts @@ -1,3 +1,4 @@ +import { waitForAsync } from '@angular/core/testing'; import { createServiceFactory, SpectatorService } from '@ngneat/spectator/jest'; import { Environment } from '../models'; import { EnvironmentService } from '../services'; @@ -17,6 +18,7 @@ export const ENVIRONMENT_DATA = ({ other: { url: 'https://localhost:44306', }, + yetAnother: {}, }, localization: { defaultResourceName: 'MyProjectName', @@ -39,19 +41,28 @@ describe('ConfigState', () => { }); describe('#getEnvironment', () => { - it('should return ENVIRONMENT_DATA', () => { - expect(environment.getEnvironment()).toEqual(ENVIRONMENT_DATA); - environment.getEnvironment$().subscribe(data => expect(data).toEqual(ENVIRONMENT_DATA)); - }); + it( + 'should return ENVIRONMENT_DATA', + waitForAsync(() => { + expect(environment.getEnvironment()).toEqual(ENVIRONMENT_DATA); + environment.getEnvironment$().subscribe(data => expect(data).toEqual(ENVIRONMENT_DATA)); + }), + ); }); describe('#getApiUrl', () => { - it('should return api url', () => { - expect(environment.getApiUrl()).toEqual(ENVIRONMENT_DATA.apis.default.url); - environment - .getApiUrl$('other') - .subscribe(data => expect(data).toEqual(ENVIRONMENT_DATA.apis.other.url)); - }); + it( + 'should return api url', + waitForAsync(() => { + expect(environment.getApiUrl()).toEqual(ENVIRONMENT_DATA.apis.default.url); + environment + .getApiUrl$('other') + .subscribe(data => expect(data).toEqual(ENVIRONMENT_DATA.apis.other.url)); + environment + .getApiUrl$('yetAnother') + .subscribe(data => expect(data).toEqual(ENVIRONMENT_DATA.apis.default.url)); + }), + ); }); // TODO: create permission.service.spec.ts