Skip to content
This repository has been archived by the owner on Feb 8, 2024. It is now read-only.

Commit

Permalink
[AAE-7657] Let js-api depend on generic http client (#1332)
Browse files Browse the repository at this point in the history
* [AAE-7657] Let js-api depend on generic http client

* Add another overrides

* Fix other typing issues

* Fix other typing issues II

* Fix cspell

* Fix unit tests

* Fix unit tests II.

* Workaround for old circular dependency issues

* Clean up workaround
  • Loading branch information
popovicsandras authored Mar 23, 2022
1 parent e69b01f commit 2730595
Show file tree
Hide file tree
Showing 56 changed files with 411 additions and 317 deletions.
36 changes: 5 additions & 31 deletions api-codegen/src/main/resources/api-code-gen/base.api.mustache
Original file line number Diff line number Diff line change
@@ -1,36 +1,10 @@
{{>licenseInfo}}

import { AlfrescoApi } from '../../../alfrescoApi';
import { AlfrescoApiClient, RequestOptions } from '../../../alfrescoApiClient';
import { ApiClient } from '../../../api-clients/api-client';
import { HttpClient } from '../../../api-clients/http-client.interface';

export class BaseApi {
apiClient: AlfrescoApiClient;
constructor(alfrescoApi?: AlfrescoApi) {
if (alfrescoApi) {
this.apiClient = alfrescoApi.contentClient;
}
}

post<T = any>(options: RequestOptions): Promise<T> {
return this.apiClient.post<T>(options);
}

put<T = any>(options: RequestOptions): Promise<T> {
return this.apiClient.put<T>(options);
}

get<T = any>(options: RequestOptions): Promise<T> {
return this.apiClient.get<T>(options);
}

delete<T = void>(options: RequestOptions): Promise<T> {
return this.apiClient.delete(options);
}

errorMessage(param: string, methodName: string) {
return `Missing param ${param} in ${methodName}`;
export abstract class BaseApi extends ApiClient {
get apiClient(): HttpClient {
return this.httpClient ?? this.alfrescoApi.contentClient;
}
}

5 changes: 5 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,8 @@ export * from './src/alfrescoApi';
export * from './src/alfrescoApiCompatibility';
export * from './src/alfrescoApiClient';
export * from './src/alfrescoApiConfig';

export * from './src/to-deprecate/alfresco-api-type';

export * from './src/api-clients/api-client';
export * from './src/api-clients/http-client.interface';
9 changes: 7 additions & 2 deletions src/alfrescoApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ import { ProcessClient } from './processClient';
import { Storage } from './storage';
import { AlfrescoApiConfig } from './alfrescoApiConfig';
import { Authentication } from './authentication/authentication';
import { LegacyAlfrescoApiHelper } from './to-deprecate/legacy-alfresco-api.helper';
import { AlfrescoApiType } from './to-deprecate/alfresco-api-type';

export class AlfrescoApi implements Emitter {
export class AlfrescoApi extends LegacyAlfrescoApiHelper implements Emitter, AlfrescoApiType {
storage: Storage;
config: AlfrescoApiConfig;
contentClient: ContentClient;
Expand All @@ -50,6 +52,7 @@ export class AlfrescoApi implements Emitter {
username: string;

constructor(config?: AlfrescoApiConfig) {
super();
ee(this);

if (config) {
Expand All @@ -59,7 +62,7 @@ export class AlfrescoApi implements Emitter {

setConfig(config: AlfrescoApiConfig) {
if (!config) {
config = {};
config = {} as AlfrescoApiConfig;
}

this.storage = new Storage();
Expand Down Expand Up @@ -166,6 +169,7 @@ export class AlfrescoApi implements Emitter {
}
}

/**@private? */
errorListeners() {

this.contentClient.off('error', () => {
Expand Down Expand Up @@ -218,6 +222,7 @@ export class AlfrescoApi implements Emitter {
});
}

/**@private? */
errorHandler(error: { status?: number }) {
if (error.status === 401) {
this.invalidateSession();
Expand Down
19 changes: 2 additions & 17 deletions src/alfrescoApiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,11 @@ import superagent, { Response, ProgressEvent } from 'superagent';
import { Authentication } from './authentication/authentication';
import { BasicAuth } from './authentication/basicAuth';
import { Oauth2 } from './authentication/oauth2';
import { HttpClient, RequestOptions } from './api-clients/http-client.interface';

declare const Buffer: any;
declare const Blob: any;

export interface RequestOptions {
path: string;
httpMethod?: string;
pathParams?: any;
queryParams?: any;
headerParams?: any;
formParams?: any;
bodyParam?: any;
contentTypes?: string[];
accepts?: string[];
returnType?: any;
contextRoot?: string;
responseType?: string;
url?: string;
}

/**
* Returns a string representation for an actual parameter.
* @param param The actual parameter.
Expand Down Expand Up @@ -85,7 +70,7 @@ export function buildCollectionParam(param: string[], collectionFormat: string):
}
}

export class AlfrescoApiClient implements ee.Emitter {
export class AlfrescoApiClient implements ee.Emitter, HttpClient {

on: ee.EmitterMethod;
off: ee.EmitterMethod;
Expand Down
2 changes: 1 addition & 1 deletion src/alfrescoApiConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class AlfrescoApiConfig {
hostOauth2?: string;
authType?: string = 'BASIC';
oauth2?: Oauth2Config;
contextRoot?: string = 'alfresco';
contextRoot = 'alfresco';
tenant?: string = '-default-';
contextRootBpm?: string = 'activiti-app';
domainPrefix?: string = '';
Expand Down
61 changes: 61 additions & 0 deletions src/api-clients/api-client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*!
* @license
* Copyright 2018 Alfresco Software, Ltd.
*
* 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 { AlfrescoApiType } from "../to-deprecate/alfresco-api-type";
import { LegacyAlfrescoApiHelper } from "../to-deprecate/legacy-alfresco-api.helper";
import { HttpClient, RequestOptions } from "./http-client.interface";

export abstract class ApiClient {

protected alfrescoApi: AlfrescoApiType;
protected httpClient: HttpClient;

get apiClient(): HttpClient {
return this.httpClient;
}

constructor(legacyApi?: AlfrescoApiType);
constructor(httpClient: HttpClient);
constructor(httpClient?: AlfrescoApiType | HttpClient) {
// TODO: remove legacyApi?: AlfrescoApi option and clean up this code. BREAKING CHANGE!
if (httpClient instanceof LegacyAlfrescoApiHelper) {
this.alfrescoApi = httpClient as AlfrescoApiType;
} else {
this.httpClient = httpClient as HttpClient;
}
}

post<T = any>(options: RequestOptions): Promise<T> {
return this.apiClient.post<T>(options);
}

put<T = any>(options: RequestOptions): Promise<T> {
return this.apiClient.put<T>(options);
}

get<T = any>(options: RequestOptions): Promise<T> {
return this.apiClient.get<T>(options);
}

delete<T = void>(options: RequestOptions): Promise<T> {
return this.apiClient.delete(options);
}

errorMessage(param: string, methodName: string) {
return `Missing param ${param} in ${methodName}`;
}
}
79 changes: 79 additions & 0 deletions src/api-clients/http-client.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*!
* @license
* Copyright 2019 Alfresco Software, Ltd.
*
* 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.
*/

export interface RequestOptions {
path: string;
httpMethod?: string;
pathParams?: any;
queryParams?: any;
headerParams?: any;
formParams?: any;
bodyParam?: any;
contentTypes?: string[];
accepts?: string[];
returnType?: any;
contextRoot?: string;
responseType?: string;
url?: string;
}

export interface HttpClientConfig {
contextRoot: string;
host?: string; // Should be mandatory but can't make it because of AlfrescoApiConfig incompatibility 😕
servicePath?: string; // Should be mandatory but can't make it because of AlfrescoApiConfig incompatibility 😕
}
export interface HttpClient {
basePath: string;
config: HttpClientConfig;

request<T = any>(options: RequestOptions): Promise<T>;
post<T = any>(options: RequestOptions): Promise<T>;
put<T = any>(options: RequestOptions): Promise<T>;
get<T = any>(options: RequestOptions): Promise<T>;
delete<T = void>(options: RequestOptions): Promise<T>;
/** @deprecated */
callApi(
path: string,
httpMethod: string,
pathParams?: any,
queryParams?: any,
headerParams?: any,
formParams?: any,
bodyParam?: any,
contentTypes?: string[],
accepts?: string[],
returnType?: any,
contextRoot?: string,
responseType?: string,
url?: string
): Promise<any>;
/** @deprecated */
callCustomApi(
path: string,
httpMethod: string,
pathParams?: any,
queryParams?: any,
headerParams?: any,
formParams?: any,
bodyParam?: any,
contentTypes?: string[],
accepts?: string[],
returnType?: any,
contextRoot?: string,
responseType?: string
): Promise<any>;
}
21 changes: 5 additions & 16 deletions src/api/activiti-rest-api/api/base.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,11 @@
* limitations under the License.
*/

import { AlfrescoApi } from '../../../alfrescoApi';
import { AlfrescoApiClient } from '../../../alfrescoApiClient';
import { ApiClient } from '../../../api-clients/api-client';
import { HttpClient } from '../../../api-clients/http-client.interface';

export class BaseApi {

protected alfrescoApi: AlfrescoApi;

get apiClient(): AlfrescoApiClient {
return this.alfrescoApi.processClient;
}

constructor(alfrescoApi?: AlfrescoApi) {
this.alfrescoApi = alfrescoApi;
}

errorMessage(param: string, methodName: string) {
return `Missing param ${param} in ${methodName}`;
export abstract class BaseApi extends ApiClient {
override get apiClient(): HttpClient {
return this.httpClient ?? this.alfrescoApi.processClient;
}
}
37 changes: 5 additions & 32 deletions src/api/auth-rest-api/api/base.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,38 +15,11 @@
* limitations under the License.
*/

import { AlfrescoApi } from '../../../alfrescoApi';
import { AlfrescoApiClient, RequestOptions } from '../../../alfrescoApiClient';
import { ApiClient } from '../../../api-clients/api-client';
import { HttpClient } from '../../../api-clients/http-client.interface';

export class BaseApi {

protected alfrescoApi: AlfrescoApi;

get apiClient(): AlfrescoApiClient {
return this.alfrescoApi.authClient;
}

constructor(alfrescoApi?: AlfrescoApi) {
this.alfrescoApi = alfrescoApi;
}

post<T = any>(options: RequestOptions): Promise<T> {
return this.apiClient.post<T>(options);
}

put<T = any>(options: RequestOptions): Promise<T> {
return this.apiClient.put<T>(options);
}

get<T = any>(options: RequestOptions): Promise<T> {
return this.apiClient.get<T>(options);
}

delete<T = void>(options: RequestOptions): Promise<T> {
return this.apiClient.delete(options);
}

errorMessage(param: string, methodName: string) {
return `Missing param ${param} in ${methodName}`;
export abstract class BaseApi extends ApiClient {
override get apiClient(): HttpClient {
return this.httpClient ?? this.alfrescoApi.authClient;
}
}
Loading

0 comments on commit 2730595

Please sign in to comment.