Skip to content

Commit

Permalink
[AAE-19928] Add storage prefix factory (#9303)
Browse files Browse the repository at this point in the history
* [AAE-19928] Add storage prefix factory

* fix names

* CR

* CR
  • Loading branch information
BSekula authored Feb 5, 2024
1 parent 311be13 commit 47d1165
Show file tree
Hide file tree
Showing 5 changed files with 178 additions and 3 deletions.
107 changes: 107 additions & 0 deletions lib/core/src/lib/app-config/app-config-storage-prefix.factory.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
});
52 changes: 52 additions & 0 deletions lib/core/src/lib/app-config/app-config-storage-prefix.factory.ts
Original file line number Diff line number Diff line change
@@ -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);
})
);
}
}
11 changes: 9 additions & 2 deletions lib/core/src/lib/app-config/app-config.loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,29 @@
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
*
* @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;
});
};
Expand Down
1 change: 1 addition & 0 deletions lib/core/src/lib/app-config/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
10 changes: 9 additions & 1 deletion lib/core/src/lib/core.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 47d1165

Please sign in to comment.