diff --git a/lib/core/src/lib/app-config/app-config-storage-prefix.factory.spec.ts b/lib/core/src/lib/app-config/app-config-storage-prefix.factory.spec.ts new file mode 100644 index 0000000000..ccf837d9b1 --- /dev/null +++ b/lib/core/src/lib/app-config/app-config-storage-prefix.factory.spec.ts @@ -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; + +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); + }); + }); +}); diff --git a/lib/core/src/lib/app-config/app-config-storage-prefix.factory.ts b/lib/core/src/lib/app-config/app-config-storage-prefix.factory.ts new file mode 100644 index 0000000000..b856c42e2e --- /dev/null +++ b/lib/core/src/lib/app-config/app-config-storage-prefix.factory.ts @@ -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; +} + +export const STORAGE_PREFIX_FACTORY_SERVICE = new InjectionToken('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 { + 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); + }) + ); + } +} diff --git a/lib/core/src/lib/app-config/app-config.loader.ts b/lib/core/src/lib/app-config/app-config.loader.ts index 11dde868f7..f93fdec933 100644 --- a/lib/core/src/lib/app-config/app-config.loader.ts +++ b/lib/core/src/lib/app-config/app-config.loader.ts @@ -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(AppConfigValues.DISABLECSRF, true); storageService.prefix = appConfigService.get(AppConfigValues.STORAGE_PREFIX, ''); - appConfigService.select(AppConfigValues.STORAGE_PREFIX).subscribe((property) => { + storagePrefixFactory.getPrefix().subscribe((property) => { storageService.prefix = property; }); }; diff --git a/lib/core/src/lib/app-config/public-api.ts b/lib/core/src/lib/app-config/public-api.ts index 76d9e2d9f0..59a6f021e1 100644 --- a/lib/core/src/lib/app-config/public-api.ts +++ b/lib/core/src/lib/app-config/public-api.ts @@ -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'; diff --git a/lib/core/src/lib/core.module.ts b/lib/core/src/lib/core.module.ts index 60fc0a87ce..3f7a2cf9c5 100644 --- a/lib/core/src/lib/core.module.ts +++ b/lib/core/src/lib/core.module.ts @@ -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,