[AAE-19928] Add storage prefix factory (#9303)

* [AAE-19928] Add storage prefix factory

* fix names

* CR

* CR
This commit is contained in:
Bartosz Sekula
2024-02-05 13:01:33 +01:00
committed by GitHub
parent 311be13f95
commit 47d1165d5f
5 changed files with 178 additions and 3 deletions

View File

@@ -0,0 +1,107 @@
/*!
* @license
* Copyright © 2005-2023 Hyland Software, Inc. and its affiliates. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { of } from 'rxjs';
import { StoragePrefixFactory, StoragePrefixFactoryService } from './app-config-storage-prefix.factory';
import { AppConfigService } from './app-config.service';
type TestAppConfigService = Pick<AppConfigService, 'select'>;
describe('StoragePrefixFactory', () => {
it('should get prefix set in app.config.json', () => {
const appConfigPrefix = 'prefix-from-app-config-json';
const appConfigService: TestAppConfigService = {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
select(_property: string) {
return of(appConfigPrefix);
}
};
const prefixFactory = new StoragePrefixFactory(appConfigService as AppConfigService);
prefixFactory.getPrefix().subscribe((prefix) => {
expect(prefix).toBe(appConfigPrefix);
});
});
it('should work with NO prefix set in app.config.json', () => {
const appConfigPrefix = undefined;
const appConfigService: TestAppConfigService = {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
select(_property: string) {
return of(appConfigPrefix);
}
};
const prefixFactory = new StoragePrefixFactory(appConfigService as AppConfigService);
prefixFactory.getPrefix().subscribe((prefix) => {
expect(prefix).toBe(appConfigPrefix);
});
});
it('should return prefix from provided factory, when NO prefix is set in app.config.json', () => {
const appConfigPrefix = undefined;
const appConfigService: TestAppConfigService = {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
select(_property: string) {
return of(appConfigPrefix);
}
};
const externalPrefixFactory: StoragePrefixFactoryService = {
getPrefix() {
return of('prefix-from-factory');
}
};
const prefixFactory = new StoragePrefixFactory(
appConfigService as AppConfigService,
externalPrefixFactory
);
prefixFactory.getPrefix().subscribe((prefix) => {
expect(prefix).toBe('prefix-from-factory');
});
});
it('should return prefix from app.config.json even when factory is provided', () => {
const appConfigPrefix = 'prefix-from-app-config-json';
const appConfigService: TestAppConfigService = {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
select(_property: string) {
return of(appConfigPrefix);
}
};
const externalPrefixFactory: StoragePrefixFactoryService = {
getPrefix() {
return of('prefix-from-factory');
}
};
const prefixFactory = new StoragePrefixFactory(
appConfigService as AppConfigService,
externalPrefixFactory
);
prefixFactory.getPrefix().subscribe((prefix) => {
expect(prefix).toBe(appConfigPrefix);
});
});
});

View File

@@ -0,0 +1,52 @@
/*!
* @license
* Copyright © 2005-2023 Hyland Software, Inc. and its affiliates. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Inject, Injectable, InjectionToken, Optional } from '@angular/core';
import { AppConfigService, AppConfigValues } from './app-config.service';
import { Observable, of } from 'rxjs';
import { switchMap } from 'rxjs/operators';
export interface StoragePrefixFactoryService {
getPrefix(): Observable<string | undefined>;
}
export const STORAGE_PREFIX_FACTORY_SERVICE = new InjectionToken<StoragePrefixFactoryService>('STORAGE_PREFIX_FACTORY_SERVICE');
@Injectable({
providedIn: 'root'
})
export class StoragePrefixFactory {
constructor(
private appConfigService: AppConfigService,
@Optional()
@Inject(STORAGE_PREFIX_FACTORY_SERVICE) private storagePrefixFactory?: StoragePrefixFactoryService
) {}
getPrefix(): Observable<string | undefined> {
return this.appConfigService.select(AppConfigValues.STORAGE_PREFIX).pipe(
switchMap((prefix: string | undefined) => {
if (prefix) {
return of(prefix);
}
return this.storagePrefixFactory ?
this.storagePrefixFactory.getPrefix() :
of(prefix);
})
);
}
}

View File

@@ -18,6 +18,7 @@
import { AppConfigService, AppConfigValues } from './app-config.service';
import { StorageService } from '../common/services/storage.service';
import { AdfHttpClient } from '@alfresco/adf-core/api';
import { StoragePrefixFactory } from './app-config-storage-prefix.factory';
/**
* Create a factory to load app configuration
@@ -25,15 +26,21 @@ import { AdfHttpClient } from '@alfresco/adf-core/api';
* @param appConfigService app config service
* @param storageService storage service
* @param adfHttpClient http client
* @param storagePrefixFactory prefix factory
* @returns factory function
*/
export function loadAppConfig(appConfigService: AppConfigService, storageService: StorageService, adfHttpClient: AdfHttpClient) {
export function loadAppConfig(
appConfigService: AppConfigService,
storageService: StorageService,
adfHttpClient: AdfHttpClient,
storagePrefixFactory: StoragePrefixFactory
) {
const init = () => {
adfHttpClient.disableCsrf = appConfigService.get<boolean>(AppConfigValues.DISABLECSRF, true);
storageService.prefix = appConfigService.get<string>(AppConfigValues.STORAGE_PREFIX, '');
appConfigService.select(AppConfigValues.STORAGE_PREFIX).subscribe((property) => {
storagePrefixFactory.getPrefix().subscribe((property) => {
storageService.prefix = property;
});
};

View File

@@ -18,5 +18,6 @@
export * from './app-config.service';
export * from './debug-app-config.service';
export * from './app-config.pipe';
export * from './app-config-storage-prefix.factory';
export * from './app-config.module';

View File

@@ -66,6 +66,7 @@ import { AlfrescoApiLoaderService, createAlfrescoApiInstance } from './api-facto
import { AdfDateFnsAdapter } from './common/utils/date-fns-adapter';
import { MomentDateAdapter } from './common/utils/moment-date-adapter';
import { AdfDateTimeFnsAdapter } from './common/utils/datetime-fns-adapter';
import { StoragePrefixFactory } from './app-config';
@NgModule({
imports: [
@@ -151,10 +152,17 @@ export class CoreModule {
AdfDateFnsAdapter,
AdfDateTimeFnsAdapter,
MomentDateAdapter,
StoragePrefixFactory,
{
provide: APP_INITIALIZER,
useFactory: loadAppConfig,
deps: [ AppConfigService, StorageService, AdfHttpClient ], multi: true
deps: [
AppConfigService,
StorageService,
AdfHttpClient,
StoragePrefixFactory
],
multi: true
},
{
provide: APP_INITIALIZER,