Browse Source

remove config state

pull/10010/head
mehmet-erim 5 years ago
parent
commit
f9f229a413
  1. 65
      npm/ng-packs/packages/core/src/lib/models/config.ts
  2. 1
      npm/ng-packs/packages/core/src/lib/models/index.ts
  3. 264
      npm/ng-packs/packages/core/src/lib/states/config.state.ts
  4. 1
      npm/ng-packs/packages/core/src/lib/states/index.ts
  5. 8
      npm/ng-packs/packages/core/src/lib/tests/config-state.service.spec.ts
  6. 8
      npm/ng-packs/packages/core/src/lib/tests/date-utils.spec.ts
  7. 18
      npm/ng-packs/packages/core/src/lib/tests/environment.service.spec.ts
  8. 1653
      npm/ng-packs/yarn.lock

65
npm/ng-packs/packages/core/src/lib/models/config.ts

@ -1,65 +0,0 @@
import { ApplicationConfiguration } from './application-configuration';
import { ABP } from './common';
import { Environment as IEnvironment } from './environment';
import {
LocalizationParam as ILocalizationParam,
LocalizationWithDefault as ILocalizationWithDefault,
} from './localization';
export namespace Config {
/**
* @deprecated Use ApplicationConfiguration.Response instead. To be deleted in v5.0.
*/
export type State = ApplicationConfiguration.Response & ABP.Root & { environment: IEnvironment };
export type Environment = IEnvironment;
/**
* @deprecated Use ApplicationInfo interface instead. To be deleted in v5.0.
*/
export interface Application {
name: string;
baseUrl?: string;
logoUrl?: string;
}
/**
* @deprecated Use ApiConfig interface instead. To be deleted in v5.0.
*/
export type ApiConfig = {
[key: string]: string;
url: string;
} & Partial<{
rootNamespace: string;
}>;
/**
* @deprecated Use Apis interface instead. To be deleted in v5.0.
*/
export interface Apis {
[key: string]: ApiConfig;
default: ApiConfig;
}
export type LocalizationWithDefault = ILocalizationWithDefault;
export type LocalizationParam = ILocalizationParam;
/**
* @deprecated Use customMergeFn type instead. To be deleted in v5.0.
*/
export type customMergeFn = (
localEnv: Partial<Config.Environment>,
remoteEnv: any,
) => Config.Environment;
/**
* @deprecated Use RemoteEnv interface instead. To be deleted in v5.0.
*/
export interface RemoteEnv {
url: string;
mergeStrategy: 'deepmerge' | 'overwrite' | customMergeFn;
method?: string;
headers?: ABP.Dictionary<string>;
}
}

1
npm/ng-packs/packages/core/src/lib/models/index.ts

@ -1,7 +1,6 @@
export * from './application-configuration'; export * from './application-configuration';
export * from './auth'; export * from './auth';
export * from './common'; export * from './common';
export * from './config';
export * from './dtos'; export * from './dtos';
export * from './environment'; export * from './environment';
export * from './localization'; export * from './localization';

264
npm/ng-packs/packages/core/src/lib/states/config.state.ts

@ -1,264 +0,0 @@
import { HttpErrorResponse } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Action, createSelector, Selector, State, StateContext, Store } from '@ngxs/store';
import compare from 'just-compare';
import { throwError } from 'rxjs';
import { catchError, distinctUntilChanged } from 'rxjs/operators';
import { GetAppConfiguration, PatchConfigState, SetEnvironment } from '../actions/config.actions';
import { RestOccurError } from '../actions/rest.actions';
import { Config } from '../models/config';
import { ConfigStateService } from '../services/config-state.service';
import { EnvironmentService } from '../services/environment.service';
import { interpolate } from '../utils/string-utils';
/**
* @deprecated Use ConfigStateService instead. To be deleted in v5.0.
*/
@State<Config.State>({
name: 'ConfigState',
defaults: {} as Config.State,
})
@Injectable()
export class ConfigState {
@Selector()
static getAll(state: Config.State) {
return state;
}
@Selector()
static getApplicationInfo(state: Config.State): Config.Application {
return state.environment.application || ({} as Config.Application);
}
@Selector()
static getEnvironment(state: Config.State): Config.Environment {
return state.environment;
}
static getOne(key: string) {
const selector = createSelector([ConfigState], (state: Config.State) => {
return state[key];
});
return selector;
}
static getDeep(keys: string[] | string) {
if (typeof keys === 'string') {
keys = keys.split('.');
}
if (!Array.isArray(keys)) {
throw new Error('The argument must be a dot string or an string array.');
}
const selector = createSelector([ConfigState], (state: Config.State) => {
return (keys as string[]).reduce((acc, val) => {
if (acc) {
return acc[val];
}
return undefined;
}, state);
});
return selector;
}
static getApiUrl(key?: string) {
const selector = createSelector([ConfigState], (state: Config.State): string => {
return (state.environment.apis[key || 'default'] || state.environment.apis.default).url;
});
return selector;
}
static getFeature(key: string) {
const selector = createSelector([ConfigState], (state: Config.State) => {
return state.features.values?.[key];
});
return selector;
}
static getSetting(key: string) {
const selector = createSelector([ConfigState], (state: Config.State) => {
return state.setting.values?.[key];
});
return selector;
}
static getSettings(keyword?: string) {
const selector = createSelector([ConfigState], (state: Config.State) => {
const settings = state.setting.values || {};
if (!keyword) return settings;
const keysFound = Object.keys(settings).filter(key => key.indexOf(keyword) > -1);
return keysFound.reduce((acc, key) => {
acc[key] = settings[key];
return acc;
}, {});
});
return selector;
}
/**
* @deprecated use PermissionService's getGrantedPolicyStream or getGrantedPolicy methods.
*/
static getGrantedPolicy(key: string) {
const selector = createSelector([ConfigState], (state: Config.State): boolean => {
if (!key) return true;
const getPolicy = (k: string) => state.auth.grantedPolicies[k] || false;
const orRegexp = /\|\|/g;
const andRegexp = /&&/g;
// TODO: Allow combination of ANDs & ORs
if (orRegexp.test(key)) {
const keys = key.split('||').filter(Boolean);
if (keys.length < 2) return false;
return keys.some(k => getPolicy(k.trim()));
} else if (andRegexp.test(key)) {
const keys = key.split('&&').filter(Boolean);
if (keys.length < 2) return false;
return keys.every(k => getPolicy(k.trim()));
}
return getPolicy(key);
});
return selector;
}
static getLocalizationResource(resourceName: string) {
const selector = createSelector(
[ConfigState],
(
state: Config.State,
): {
[key: string]: string;
} => {
return state.localization.values[resourceName];
},
);
return selector;
}
static getLocalization(
key: string | Config.LocalizationWithDefault,
...interpolateParams: string[]
) {
if (!key) key = '';
let defaultValue: string;
if (typeof key !== 'string') {
defaultValue = key.defaultValue;
key = key.key;
}
const keys = key.split('::') as string[];
const selector = createSelector([ConfigState], (state: Config.State): string => {
const warn = (message: string) => {
if (!state.environment.production) console.warn(message);
};
if (keys.length < 2) {
warn('The localization source separator (::) not found.');
return defaultValue || (key as string);
}
if (!state.localization) return defaultValue || keys[1];
const sourceName =
keys[0] ||
state.environment.localization?.defaultResourceName ||
state.localization.defaultResourceName;
const sourceKey = keys[1];
if (sourceName === '_') {
return defaultValue || sourceKey;
}
if (!sourceName) {
warn(
'Localization source name is not specified and the defaultResourceName was not defined!',
);
return defaultValue || sourceKey;
}
const source = state.localization.values[sourceName];
if (!source) {
warn('Could not find localization source: ' + sourceName);
return defaultValue || sourceKey;
}
let localization = source[sourceKey];
if (typeof localization === 'undefined') {
return defaultValue || sourceKey;
}
interpolateParams = interpolateParams.filter(params => params != null);
if (localization) localization = interpolate(localization, interpolateParams);
if (typeof localization !== 'string') localization = '';
return localization || defaultValue || (key as string);
});
return selector;
}
constructor(
private store: Store,
private environmentService: EnvironmentService,
private configState: ConfigStateService,
) {
this.syncConfigState();
this.syncEnvironment();
}
private syncConfigState() {
this.configState
.createOnUpdateStream(state => state)
.pipe(distinctUntilChanged(compare))
.subscribe(config => this.store.dispatch(new PatchConfigState(config as any)));
}
private syncEnvironment() {
this.environmentService
.createOnUpdateStream(state => state)
.pipe(distinctUntilChanged(compare))
.subscribe(env => this.store.dispatch(new PatchConfigState({ environment: env } as any)));
}
@Action(GetAppConfiguration)
addData({ patchState, dispatch }: StateContext<Config.State>) {
const apiName = 'default';
const api = this.store.selectSnapshot(ConfigState.getApiUrl(apiName));
return this.configState.refreshAppState().pipe(
catchError((err: HttpErrorResponse) => {
dispatch(new RestOccurError(err));
return throwError(err);
}),
);
}
@Action(SetEnvironment)
setEnvironment(_, { environment }: SetEnvironment) {
return this.environmentService.setState(environment);
}
@Action(PatchConfigState)
setConfig({ patchState, getState }: StateContext<Config.State>, { state }: PatchConfigState) {
patchState({ ...getState(), ...state });
}
}

1
npm/ng-packs/packages/core/src/lib/states/index.ts

@ -1,2 +1 @@
export * from './config.state';
export * from './profile.state'; export * from './profile.state';

8
npm/ng-packs/packages/core/src/lib/tests/config-state.service.spec.ts

@ -1,6 +1,5 @@
import { HttpClientTestingModule } from '@angular/common/http/testing'; import { HttpClientTestingModule } from '@angular/common/http/testing';
import { createServiceFactory, SpectatorService } from '@ngneat/spectator/jest'; import { createServiceFactory, SpectatorService } from '@ngneat/spectator/jest';
import { Store } from '@ngxs/store';
import { import {
ApplicationConfigurationDto, ApplicationConfigurationDto,
CurrentUserDto, CurrentUserDto,
@ -101,17 +100,14 @@ export const CONFIG_STATE_DATA = {
registerLocaleFn: () => Promise.resolve(), registerLocaleFn: () => Promise.resolve(),
} as any as ApplicationConfigurationDto; } as any as ApplicationConfigurationDto;
describe('ConfigState', () => { describe('ConfigStateService', () => {
let spectator: SpectatorService<ConfigStateService>; let spectator: SpectatorService<ConfigStateService>;
let configState: ConfigStateService; let configState: ConfigStateService;
const createService = createServiceFactory({ const createService = createServiceFactory({
service: ConfigStateService, service: ConfigStateService,
imports: [HttpClientTestingModule], imports: [HttpClientTestingModule],
providers: [ providers: [{ provide: CORE_OPTIONS, useValue: { skipGetAppConfiguration: true } }],
{ provide: CORE_OPTIONS, useValue: { skipGetAppConfiguration: true } },
{ provide: Store, useValue: {} },
],
}); });
beforeEach(() => { beforeEach(() => {

8
npm/ng-packs/packages/core/src/lib/tests/date-utils.spec.ts

@ -1,5 +1,5 @@
import { ConfigStateService } from '../services'; import { ConfigStateService } from '../services';
import { getShortDateFormat, getShortTimeFormat, getShortDateShortTimeFormat } from '../utils'; import { getShortDateFormat, getShortDateShortTimeFormat, getShortTimeFormat } from '../utils';
const dateTimeFormat = { const dateTimeFormat = {
calendarAlgorithmType: 'SolarCalendar', calendarAlgorithmType: 'SolarCalendar',
@ -19,7 +19,7 @@ describe('Date Utils', () => {
}); });
describe('#getShortDateFormat', () => { describe('#getShortDateFormat', () => {
test('should get the short date format from ConfigState and return it', () => { test('should get the short date format from ConfigStateService and return it', () => {
const getDeepSpy = jest.spyOn(config, 'getDeep'); const getDeepSpy = jest.spyOn(config, 'getDeep');
getDeepSpy.mockReturnValueOnce(dateTimeFormat); getDeepSpy.mockReturnValueOnce(dateTimeFormat);
@ -29,7 +29,7 @@ describe('Date Utils', () => {
}); });
describe('#getShortTimeFormat', () => { describe('#getShortTimeFormat', () => {
test('should get the short time format from ConfigState and return it', () => { test('should get the short time format from ConfigStateService and return it', () => {
const getDeepSpy = jest.spyOn(config, 'getDeep'); const getDeepSpy = jest.spyOn(config, 'getDeep');
getDeepSpy.mockReturnValueOnce(dateTimeFormat); getDeepSpy.mockReturnValueOnce(dateTimeFormat);
@ -39,7 +39,7 @@ describe('Date Utils', () => {
}); });
describe('#getShortDateShortTimeFormat', () => { describe('#getShortDateShortTimeFormat', () => {
test('should get the short date time format from ConfigState and return it', () => { test('should get the short date time format from ConfigStateService and return it', () => {
const getDeepSpy = jest.spyOn(config, 'getDeep'); const getDeepSpy = jest.spyOn(config, 'getDeep');
getDeepSpy.mockReturnValueOnce(dateTimeFormat); getDeepSpy.mockReturnValueOnce(dateTimeFormat);

18
npm/ng-packs/packages/core/src/lib/tests/environment.service.spec.ts

@ -25,7 +25,7 @@ export const ENVIRONMENT_DATA = {
}, },
} as any as Environment; } as any as Environment;
describe('ConfigState', () => { describe('Environment', () => {
let spectator: SpectatorService<EnvironmentService>; let spectator: SpectatorService<EnvironmentService>;
let environment: EnvironmentService; let environment: EnvironmentService;
@ -64,20 +64,4 @@ describe('ConfigState', () => {
}), }),
); );
}); });
// TODO: create permission.service.spec.ts
// describe('#getGrantedPolicy', () => {
// it('should return a granted policy', () => {
// expect(ConfigState.getGrantedPolicy('Abp.Identity')(CONFIG_STATE_DATA)).toBe(false);
// expect(ConfigState.getGrantedPolicy('Abp.Identity || Abp.Account')(CONFIG_STATE_DATA)).toBe(
// true,
// );
// expect(ConfigState.getGrantedPolicy('Abp.Account && Abp.Identity')(CONFIG_STATE_DATA)).toBe(
// false,
// );
// expect(ConfigState.getGrantedPolicy('Abp.Account &&')(CONFIG_STATE_DATA)).toBe(false);
// expect(ConfigState.getGrantedPolicy('|| Abp.Account')(CONFIG_STATE_DATA)).toBe(false);
// expect(ConfigState.getGrantedPolicy('')(CONFIG_STATE_DATA)).toBe(true);
// });
// });
}); });

1653
npm/ng-packs/yarn.lock

File diff suppressed because it is too large
Loading…
Cancel
Save