Skip to content

Commit

Permalink
Merge pull request #7913 from abpframework/fix/7912
Browse files Browse the repository at this point in the history
Made url and rootNamespace optional in api config
  • Loading branch information
bnymncoskuner committed Mar 2, 2021
2 parents c4b48bd + 2723611 commit 27a059f
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 24 deletions.
5 changes: 0 additions & 5 deletions npm/ng-packs/apps/dev-app/src/environments/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
},
Expand Down
9 changes: 4 additions & 5 deletions npm/ng-packs/packages/core/src/lib/models/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ApiConfig>;
default: ApiConfig;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -17,6 +18,7 @@ export const ENVIRONMENT_DATA = ({
other: {
url: 'https://localhost:44306',
},
yetAnother: {},
},
localization: {
defaultResourceName: 'MyProjectName',
Expand All @@ -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
Expand Down

0 comments on commit 27a059f

Please sign in to comment.