fix api service bugs and remove workarounds

rebasing onto develop
This commit is contained in:
Denys Vuika
2024-09-13 10:04:09 -04:00
committed by Anton Ramanovich
parent f67a7d0721
commit c4f7fdf093
5 changed files with 80 additions and 23 deletions

View File

@@ -15,16 +15,15 @@
* limitations under the License.
*/
import { AdfHttpClient } from '@alfresco/adf-core/api';
import { StorageService, AppConfigService } from '@alfresco/adf-core';
import { AlfrescoApi, AlfrescoApiConfig } from '@alfresco/js-api';
import { Injectable } from '@angular/core';
import { AlfrescoApiService } from '../services/alfresco-api.service';
/** @deprecated use `AlfrescoApiService` instead */
@Injectable()
export class AlfrescoApiNoAuthService extends AlfrescoApiService {
constructor(storage: StorageService, appConfig: AppConfigService, private readonly adfHttpClient: AdfHttpClient) {
super(appConfig, storage);
constructor() {
super();
}
override createInstance(config: AlfrescoApiConfig) {

View File

@@ -46,8 +46,6 @@ import { AlfrescoViewerComponent } from './viewer';
import { ContentTypeDialogComponent } from './content-type';
import { MaterialModule } from './material.module';
import { AlfrescoIconComponent } from './alfresco-icon/alfresco-icon.component';
import { AlfrescoApiService } from './services/alfresco-api.service';
import { AlfrescoApiNoAuthService } from './api-factories/alfresco-api-no-auth.service';
import { AlfrescoApiLoaderService, createAlfrescoApiInstance } from './api-factories/alfresco-api-v2-loader.service';
@NgModule({
@@ -114,7 +112,6 @@ export class ContentModule {
providers: [
provideTranslations('adf-content-services', 'assets/adf-content-services'),
ContentAuthLoaderService,
{ provide: AlfrescoApiService, useClass: AlfrescoApiNoAuthService },
{
provide: APP_INITIALIZER,
useFactory: versionCompatibilityFactory,

View File

@@ -17,12 +17,12 @@
import { Injectable } from '@angular/core';
import { AlfrescoApiService } from '../services/alfresco-api.service';
import { AppConfigService, StorageService } from '@alfresco/adf-core';
@Injectable()
export class AlfrescoApiServiceMock extends AlfrescoApiService {
constructor(protected appConfig: AppConfigService, protected storageService: StorageService) {
super(appConfig, storageService);
constructor() {
super();
if (!this.alfrescoApi) {
this.initAlfrescoApi();
}

View File

@@ -0,0 +1,52 @@
/*!
* @license
* Copyright © 2005-2024 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 { TestBed } from '@angular/core/testing';
import { AlfrescoApiService } from './alfresco-api.service';
import { AppConfigService, StorageService } from '@alfresco/adf-core';
import { HttpClientTestingModule } from '@angular/common/http/testing';
describe('AlfrescoApiService', () => {
let service: AlfrescoApiService;
beforeEach(() => {
const appConfigSpy = jasmine.createSpyObj('AppConfigService', ['get']);
const storageSpy = jasmine.createSpyObj('StorageService', ['']);
TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
providers: [AlfrescoApiService, { provide: AppConfigService, useValue: appConfigSpy }, { provide: StorageService, useValue: storageSpy }]
});
service = TestBed.inject(AlfrescoApiService);
service['lastConfig'] = {
hostBpm: 'http://localhost:8080',
contextRootBpm: 'activiti-app'
} as any;
});
it('should return true for excluded error URL', () => {
const currentFullPath = 'http://localhost:8080/activiti-app/api/enterprise/system/properties';
expect(service.isExcludedErrorListener(currentFullPath)).toBeTrue();
});
it('should return false for non-excluded error URL', () => {
const currentFullPath = 'http://localhost:8080/activiti-app/api/enterprise/other';
expect(service.isExcludedErrorListener(currentFullPath)).toBeFalse();
});
});

View File

@@ -15,11 +15,12 @@
* limitations under the License.
*/
import { Inject, Injectable, InjectionToken, Optional } from '@angular/core';
import { inject, Injectable, InjectionToken } from '@angular/core';
import { AlfrescoApi, AlfrescoApiConfig } from '@alfresco/js-api';
import { ReplaySubject } from 'rxjs';
import { AlfrescoApiFactory } from './alfresco-api.interface';
import { AppConfigService, AppConfigValues, OauthConfigModel, OpenidConfiguration, StorageService } from '@alfresco/adf-core';
import { AdfHttpClient } from '@alfresco/adf-core/api';
export const ALFRESCO_API_FACTORY = new InjectionToken('ALFRESCO_API_FACTORY');
@@ -27,7 +28,12 @@ export const ALFRESCO_API_FACTORY = new InjectionToken('ALFRESCO_API_FACTORY');
providedIn: 'root'
})
export class AlfrescoApiService {
alfrescoApiInitialized = new ReplaySubject<boolean>(1);
protected appConfig = inject(AppConfigService);
protected storageService = inject(StorageService);
protected alfrescoApiFactory = inject<AlfrescoApiFactory>(ALFRESCO_API_FACTORY, { optional: true });
protected adfHttpClient = inject(AdfHttpClient);
alfrescoApiInitialized: ReplaySubject<boolean> = new ReplaySubject(1);
protected alfrescoApi: AlfrescoApi;
@@ -36,20 +42,12 @@ export class AlfrescoApiService {
idpConfig: OpenidConfiguration;
private excludedErrorUrl: string[] = ['api/enterprise/system/properties'];
private readonly excludedErrorUrl = ['/api/enterprise/system/properties'];
getInstance(): AlfrescoApi {
return this.alfrescoApi;
}
constructor(
protected appConfig: AppConfigService,
protected storageService: StorageService,
@Optional()
@Inject(ALFRESCO_API_FACTORY)
private alfrescoApiFactory?: AlfrescoApiFactory
) {}
async load(config: AlfrescoApiConfig): Promise<void> {
this.currentAppConfig = config;
@@ -122,15 +120,26 @@ export class AlfrescoApiService {
if (this.alfrescoApiFactory) {
return this.alfrescoApiFactory.createAlfrescoApi(config);
}
return new AlfrescoApi(config);
return new AlfrescoApi(
{
...config,
oauthInit: false
},
this.adfHttpClient
);
}
isDifferentConfig(lastConfig: AlfrescoApiConfig, newConfig: AlfrescoApiConfig) {
return JSON.stringify(lastConfig) !== JSON.stringify(newConfig);
}
private formatExcludedPath(path: string): string {
return path.replace(this.lastConfig.hostBpm + '/' + this.lastConfig.contextRootBpm, '');
}
isExcludedErrorListener(currentFullPath: string): boolean {
const formattedPath = currentFullPath.replace(this.lastConfig.hostBpm + '/' + this.lastConfig.contextRootBpm, '');
const formattedPath = this.formatExcludedPath(currentFullPath);
return this.excludedErrorUrl.includes(formattedPath);
}
}