mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-09-10 14:11:42 +00:00
AAE-30878 Migrating from event-emitter to eventemitter3 which is … (#11116)
This commit is contained in:
@@ -21,6 +21,7 @@ import { HttpClientTestingModule, HttpTestingController } from '@angular/common/
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
import { AdfHttpClient } from './adf-http-client.service';
|
||||
import { AlfrescoApiResponseError } from './alfresco-api/alfresco-api.response-error';
|
||||
import EventEmitter from 'eventemitter3';
|
||||
|
||||
const securityOptions: SecurityOptions = {
|
||||
authentications: {},
|
||||
@@ -30,16 +31,9 @@ const securityOptions: SecurityOptions = {
|
||||
withCredentials: false
|
||||
};
|
||||
|
||||
const emitter = {
|
||||
emit: () => {},
|
||||
off: () => {},
|
||||
on: () => {},
|
||||
once: () => {}
|
||||
};
|
||||
|
||||
const emitters: Emitters = {
|
||||
eventEmitter: emitter,
|
||||
apiClientEmitter: emitter
|
||||
eventEmitter: new EventEmitter(),
|
||||
apiClientEmitter: new EventEmitter()
|
||||
};
|
||||
|
||||
const mockResponse = {
|
||||
@@ -124,10 +118,10 @@ describe('AdfHttpClient', () => {
|
||||
httpMethod: 'POST'
|
||||
};
|
||||
|
||||
const spy = spyOn(emitter, 'emit').and.callThrough();
|
||||
const eventSpy = spyOn(emitters.eventEmitter, 'emit').and.callThrough();
|
||||
|
||||
angularHttpClient.request('http://example.com', options, securityOptions, emitters).catch(() => {
|
||||
expect(spy).toHaveBeenCalledWith('unauthorized');
|
||||
expect(eventSpy).toHaveBeenCalledWith('unauthorized');
|
||||
done();
|
||||
});
|
||||
|
||||
|
@@ -34,23 +34,24 @@ import { AlfrescoApiParamEncoder } from './alfresco-api/alfresco-api.param-encod
|
||||
import { AlfrescoApiResponseError } from './alfresco-api/alfresco-api.response-error';
|
||||
import { Constructor } from './types';
|
||||
import { RequestOptions, SecurityOptions } from './interfaces';
|
||||
import ee, { Emitter } from 'event-emitter';
|
||||
import { EventEmitter } from 'eventemitter3';
|
||||
|
||||
type EventEmitterInstance = InstanceType<typeof EventEmitter>;
|
||||
|
||||
type EventEmitterEvents = 'progress' | 'success' | 'error' | 'forbidden' | 'abort' | 'unauthorized' | string;
|
||||
|
||||
export interface Emitters {
|
||||
readonly eventEmitter: Emitter;
|
||||
readonly apiClientEmitter: Emitter;
|
||||
readonly eventEmitter: EventEmitterInstance;
|
||||
readonly apiClientEmitter: EventEmitterInstance;
|
||||
}
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class AdfHttpClient implements ee.Emitter, JsApiHttpClient {
|
||||
on: ee.EmitterMethod;
|
||||
off: ee.EmitterMethod;
|
||||
once: ee.EmitterMethod;
|
||||
_disableCsrf: boolean;
|
||||
export class AdfHttpClient implements JsApiHttpClient {
|
||||
private eventEmitter = new EventEmitter();
|
||||
|
||||
emit: (type: string, ...args: any[]) => void;
|
||||
_disableCsrf: boolean;
|
||||
|
||||
get disableCsrf(): boolean {
|
||||
return this._disableCsrf;
|
||||
@@ -68,7 +69,27 @@ export class AdfHttpClient implements ee.Emitter, JsApiHttpClient {
|
||||
};
|
||||
|
||||
constructor(private httpClient: HttpClient) {
|
||||
ee(this);
|
||||
// No need for ee(this) anymore - we use composition instead of inheritance
|
||||
}
|
||||
|
||||
// EventEmitter delegation methods
|
||||
on(event: EventEmitterEvents, fn: (...args: any[]) => void, context?: any): this {
|
||||
this.eventEmitter.on(event, fn, context);
|
||||
return this;
|
||||
}
|
||||
|
||||
off(event: EventEmitterEvents, fn?: (...args: any[]) => void, context?: any): this {
|
||||
this.eventEmitter.off(event, fn, context);
|
||||
return this;
|
||||
}
|
||||
|
||||
once(event: EventEmitterEvents, fn: (...args: any[]) => void, context?: any): this {
|
||||
this.eventEmitter.once(event, fn, context);
|
||||
return this;
|
||||
}
|
||||
|
||||
emit(event: EventEmitterEvents, ...args: any[]): boolean {
|
||||
return this.eventEmitter.emit(event, ...args);
|
||||
}
|
||||
|
||||
setDefaultSecurityOption(options: any) {
|
||||
@@ -136,24 +157,19 @@ export class AdfHttpClient implements ee.Emitter, JsApiHttpClient {
|
||||
|
||||
private addPromiseListeners<T = any>(promise: Promise<T>, eventEmitter: any) {
|
||||
const eventPromise = Object.assign(promise, {
|
||||
on() {
|
||||
// eslint-disable-next-line prefer-spread, prefer-rest-params
|
||||
eventEmitter.on.apply(eventEmitter, arguments);
|
||||
on<K extends string | symbol>(event: K, fn: (...args: any[]) => void, context?: any) {
|
||||
eventEmitter.on(event, fn, context);
|
||||
return this;
|
||||
},
|
||||
once() {
|
||||
// eslint-disable-next-line prefer-spread, prefer-rest-params
|
||||
eventEmitter.once.apply(eventEmitter, arguments);
|
||||
once<K extends string | symbol>(event: K, fn: (...args: any[]) => void, context?: any) {
|
||||
eventEmitter.once(event, fn, context);
|
||||
return this;
|
||||
},
|
||||
emit() {
|
||||
// eslint-disable-next-line prefer-spread, prefer-rest-params
|
||||
eventEmitter.emit.apply(eventEmitter, arguments);
|
||||
return this;
|
||||
emit<K extends string | symbol>(event: K, ...args: any[]): boolean {
|
||||
return eventEmitter.emit(event, ...args);
|
||||
},
|
||||
off() {
|
||||
// eslint-disable-next-line prefer-spread, prefer-rest-params
|
||||
eventEmitter.off.apply(eventEmitter, arguments);
|
||||
off<K extends string | symbol>(event: K, fn?: (...args: any[]) => void, context?: any) {
|
||||
eventEmitter.off(event, fn, context);
|
||||
return this;
|
||||
}
|
||||
});
|
||||
@@ -162,16 +178,17 @@ export class AdfHttpClient implements ee.Emitter, JsApiHttpClient {
|
||||
}
|
||||
|
||||
private getEventEmitters(): Emitters {
|
||||
const apiClientEmitter = {
|
||||
on: this.on.bind(this),
|
||||
off: this.off.bind(this),
|
||||
once: this.once.bind(this),
|
||||
emit: this.emit.bind(this)
|
||||
};
|
||||
const apiClientEmitter: EventEmitterInstance = new EventEmitter();
|
||||
|
||||
// Bind this instance's methods to the apiClientEmitter for backward compatibility
|
||||
apiClientEmitter.on = this.on.bind(this);
|
||||
apiClientEmitter.off = this.off.bind(this);
|
||||
apiClientEmitter.once = this.once.bind(this);
|
||||
apiClientEmitter.emit = this.emit.bind(this);
|
||||
|
||||
return {
|
||||
apiClientEmitter,
|
||||
eventEmitter: ee({})
|
||||
eventEmitter: new EventEmitter()
|
||||
};
|
||||
}
|
||||
|
||||
|
@@ -16,18 +16,20 @@
|
||||
*/
|
||||
|
||||
import { HttpHeaders } from '@angular/common/http';
|
||||
import ee from 'event-emitter';
|
||||
import { EventEmitter } from 'eventemitter3';
|
||||
import { Observable } from 'rxjs';
|
||||
|
||||
type EventEmitterInstance = InstanceType<typeof EventEmitter>;
|
||||
|
||||
export interface AuthenticationServiceInterface {
|
||||
onError: any;
|
||||
onLogin: any;
|
||||
onLogout: any;
|
||||
|
||||
on: ee.EmitterMethod;
|
||||
off: ee.EmitterMethod;
|
||||
once: ee.EmitterMethod;
|
||||
emit: (type: string, ...args: any[]) => void;
|
||||
on: EventEmitterInstance['on'];
|
||||
off: EventEmitterInstance['off'];
|
||||
once: EventEmitterInstance['once'];
|
||||
emit: EventEmitterInstance['emit'];
|
||||
|
||||
getToken(): string;
|
||||
isLoggedIn(): boolean;
|
||||
|
@@ -21,13 +21,13 @@ import { BasicAlfrescoAuthService } from '../basic-auth/basic-alfresco-auth.serv
|
||||
import { Observable, Subject, from } from 'rxjs';
|
||||
import { HttpHeaders } from '@angular/common/http';
|
||||
import { AuthenticationServiceInterface } from '../interfaces/authentication-service.interface';
|
||||
import ee from 'event-emitter';
|
||||
import { RedirectAuthService } from '../oidc/redirect-auth.service';
|
||||
|
||||
import { EventEmitter } from 'eventemitter3';
|
||||
type EventEmitterInstance = InstanceType<typeof EventEmitter>;
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class AuthenticationService implements AuthenticationServiceInterface, ee.Emitter {
|
||||
export class AuthenticationService implements AuthenticationServiceInterface {
|
||||
onLogin: Subject<any> = new Subject<any>();
|
||||
onLogout: Subject<any> = new Subject<any>();
|
||||
onTokenReceived: Subject<any> = new Subject<any>();
|
||||
@@ -49,19 +49,19 @@ export class AuthenticationService implements AuthenticationServiceInterface, ee
|
||||
}
|
||||
}
|
||||
|
||||
get on(): ee.EmitterMethod {
|
||||
get on(): EventEmitterInstance['on'] {
|
||||
return this.isOauth() ? this.oidcAuthenticationService.on : this.basicAlfrescoAuthService.on;
|
||||
}
|
||||
|
||||
get off(): ee.EmitterMethod {
|
||||
get off(): EventEmitterInstance['off'] {
|
||||
return this.isOauth() ? this.oidcAuthenticationService.off : this.basicAlfrescoAuthService.off;
|
||||
}
|
||||
|
||||
get once(): ee.EmitterMethod {
|
||||
get once(): EventEmitterInstance['once'] {
|
||||
return this.isOauth() ? this.oidcAuthenticationService.once : this.basicAlfrescoAuthService.once;
|
||||
}
|
||||
|
||||
get emit(): (type: string, ...args: any[]) => void {
|
||||
get emit(): EventEmitterInstance['emit'] {
|
||||
return this.isOauth() ? this.oidcAuthenticationService.emit : this.basicAlfrescoAuthService.emit;
|
||||
}
|
||||
|
||||
|
@@ -21,13 +21,15 @@ import { Observable, Observer, ReplaySubject, throwError } from 'rxjs';
|
||||
import { AppConfigService, AppConfigValues } from '../../app-config/app-config.service';
|
||||
import { CookieService } from '../../common/services/cookie.service';
|
||||
import { AuthenticationServiceInterface } from '../interfaces/authentication-service.interface';
|
||||
import ee from 'event-emitter';
|
||||
import { EventEmitter } from 'eventemitter3';
|
||||
|
||||
export abstract class BaseAuthenticationService implements AuthenticationServiceInterface, ee.Emitter {
|
||||
on: ee.EmitterMethod;
|
||||
off: ee.EmitterMethod;
|
||||
once: ee.EmitterMethod;
|
||||
emit: (type: string, ...args: any[]) => void;
|
||||
type EventEmitterInstance = InstanceType<typeof EventEmitter>;
|
||||
|
||||
export abstract class BaseAuthenticationService implements AuthenticationServiceInterface {
|
||||
on: EventEmitterInstance['on'];
|
||||
off: EventEmitterInstance['off'];
|
||||
once: EventEmitterInstance['once'];
|
||||
emit: EventEmitterInstance['emit'];
|
||||
|
||||
protected redirectUrl: RedirectionModel = null;
|
||||
|
||||
@@ -38,9 +40,7 @@ export abstract class BaseAuthenticationService implements AuthenticationService
|
||||
protected constructor(
|
||||
protected appConfig: AppConfigService,
|
||||
protected cookie: CookieService
|
||||
) {
|
||||
ee(this);
|
||||
}
|
||||
) {}
|
||||
|
||||
abstract getAuthHeaders(requestUrl: string, header: HttpHeaders): HttpHeaders;
|
||||
abstract getToken(): string;
|
||||
|
@@ -15,7 +15,7 @@
|
||||
"url": "https://github.com/Alfresco/alfresco-ng2-components/issues"
|
||||
},
|
||||
"dependencies": {
|
||||
"event-emitter": "^0.3.5",
|
||||
"eventemitter3": "^5.0.1",
|
||||
"superagent": "^9.0.1",
|
||||
"tslib": "^2.6.1"
|
||||
},
|
||||
|
@@ -15,7 +15,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import ee, { EmitterMethod, Emitter } from 'event-emitter';
|
||||
import { EventEmitter } from 'eventemitter3';
|
||||
import { ContentAuth } from './authentication/contentAuth';
|
||||
import { ProcessAuth } from './authentication/processAuth';
|
||||
import { Oauth2Auth } from './authentication/oauth2Auth';
|
||||
@@ -26,11 +26,12 @@ import { AlfrescoApiConfig } from './alfrescoApiConfig';
|
||||
import { Authentication } from './authentication/authentication';
|
||||
import { AlfrescoApiType } from './to-deprecate/alfresco-api-type';
|
||||
import { HttpClient } from './api-clients/http-client.interface';
|
||||
import { AlfrescoApiClient, AlfrescoApiClientPromise } from './alfrescoApiClient';
|
||||
|
||||
export class AlfrescoApi implements Emitter, AlfrescoApiType {
|
||||
type EventEmitterInstance = InstanceType<typeof EventEmitter>;
|
||||
|
||||
export class AlfrescoApi extends AlfrescoApiClient implements AlfrescoApiType {
|
||||
__type = 'legacy-client';
|
||||
storage: Storage;
|
||||
config: AlfrescoApiConfig;
|
||||
contentClient: ContentClient;
|
||||
contentPrivateClient: ContentClient;
|
||||
processClient: ProcessClient;
|
||||
@@ -43,18 +44,11 @@ export class AlfrescoApi implements Emitter, AlfrescoApiType {
|
||||
processAuth: ProcessAuth;
|
||||
contentAuth: ContentAuth;
|
||||
|
||||
on: EmitterMethod;
|
||||
off: EmitterMethod;
|
||||
once: EmitterMethod;
|
||||
|
||||
bufferEvents: string[] = [];
|
||||
|
||||
emit: (type: string, ...args: any[]) => void;
|
||||
|
||||
username: string;
|
||||
|
||||
constructor(config?: AlfrescoApiConfig, public httpClient?: HttpClient) {
|
||||
ee(this);
|
||||
constructor(config?: AlfrescoApiConfig, httpClient?: HttpClient) {
|
||||
super(undefined, httpClient);
|
||||
|
||||
if (config) {
|
||||
this.setConfig(config);
|
||||
@@ -392,14 +386,15 @@ export class AlfrescoApi implements Emitter, AlfrescoApiType {
|
||||
return this.contentAuth.validateTicket();
|
||||
}
|
||||
|
||||
private loginBPMECM(username: string, password: string): Promise<[string, string]> {
|
||||
private loginBPMECM(username: string, password: string): AlfrescoApiClientPromise<[string, string]> {
|
||||
const contentPromise = this.contentAuth.login(username, password);
|
||||
const processPromise = this.processAuth.login(username, password);
|
||||
|
||||
const promise: any = new Promise<[string, string]>((resolve, reject) => {
|
||||
const eventEmitter: EventEmitterInstance = new EventEmitter();
|
||||
const promise = new Promise<[string, string]>((resolve, reject) => {
|
||||
Promise.all([contentPromise, processPromise]).then(
|
||||
(data) => {
|
||||
promise.emit('success');
|
||||
eventEmitter.emit('success');
|
||||
resolve(data);
|
||||
},
|
||||
(error) => {
|
||||
@@ -407,16 +402,15 @@ export class AlfrescoApi implements Emitter, AlfrescoApiType {
|
||||
this.processAuth.invalidateSession();
|
||||
|
||||
if (error.status === 401) {
|
||||
promise.emit('unauthorized');
|
||||
eventEmitter.emit('unauthorized');
|
||||
}
|
||||
promise.emit('error');
|
||||
eventEmitter.emit('error');
|
||||
reject(error);
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
ee(promise); // jshint ignore:line
|
||||
return promise;
|
||||
return this.addPromiseListeners(promise, eventEmitter);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -449,29 +443,29 @@ export class AlfrescoApi implements Emitter, AlfrescoApiType {
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
private _logoutBPMECM(): Promise<void> {
|
||||
private _logoutBPMECM(): AlfrescoApiClientPromise<void> {
|
||||
const contentPromise = this.contentAuth.logout();
|
||||
const processPromise = this.processAuth.logout();
|
||||
|
||||
const promise: any = new Promise<void>((resolve, reject) => {
|
||||
const eventEmitter: EventEmitterInstance = new EventEmitter();
|
||||
const promise = new Promise<void>((resolve, reject) => {
|
||||
Promise.all([contentPromise, processPromise]).then(
|
||||
() => {
|
||||
this.config.ticket = undefined;
|
||||
promise.emit('logout');
|
||||
eventEmitter.emit('logout');
|
||||
resolve();
|
||||
},
|
||||
(error) => {
|
||||
if (error.status === 401) {
|
||||
promise.emit('unauthorized');
|
||||
eventEmitter.emit('unauthorized');
|
||||
}
|
||||
promise.emit('error');
|
||||
eventEmitter.emit('error');
|
||||
reject(error);
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
ee(promise); // jshint ignore:line
|
||||
return promise;
|
||||
return this.addPromiseListeners(promise, eventEmitter);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -15,7 +15,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import ee from 'event-emitter';
|
||||
import { EventEmitter } from 'eventemitter3';
|
||||
import { AlfrescoApiConfig } from './alfrescoApiConfig';
|
||||
import { Authentication } from './authentication/authentication';
|
||||
import { SuperagentHttpClient } from './superagentHttpClient';
|
||||
@@ -25,11 +25,13 @@ import { Storage } from './storage';
|
||||
|
||||
declare const Buffer: any;
|
||||
|
||||
type EventEmitterInstance = InstanceType<typeof EventEmitter>;
|
||||
|
||||
export type AlfrescoApiClientPromise<T = any> = Promise<T> & {
|
||||
on: ee.EmitterMethod;
|
||||
off: ee.EmitterMethod;
|
||||
once: ee.EmitterMethod;
|
||||
emit: (type: string, ...args: any[]) => void;
|
||||
on: <K extends string | symbol>(event: K, fn: (...args: any[]) => void, context?: any) => AlfrescoApiClientPromise<T>;
|
||||
off: <K extends string | symbol>(event: K, fn?: (...args: any[]) => void, context?: any) => AlfrescoApiClientPromise<T>;
|
||||
once: <K extends string | symbol>(event: K, fn: (...args: any[]) => void, context?: any) => AlfrescoApiClientPromise<T>;
|
||||
emit: <K extends string | symbol>(event: K, ...args: any[]) => boolean;
|
||||
abort?: () => void;
|
||||
};
|
||||
|
||||
@@ -62,11 +64,8 @@ export function buildCollectionParam(param: string[], collectionFormat: string):
|
||||
}
|
||||
}
|
||||
|
||||
export class AlfrescoApiClient implements ee.Emitter, LegacyHttpClient {
|
||||
on: ee.EmitterMethod;
|
||||
off: ee.EmitterMethod;
|
||||
once: ee.EmitterMethod;
|
||||
emit: (type: string, ...args: any[]) => void;
|
||||
export class AlfrescoApiClient implements LegacyHttpClient {
|
||||
private eventEmitter = new EventEmitter();
|
||||
|
||||
storage: Storage;
|
||||
host: string;
|
||||
@@ -105,13 +104,29 @@ export class AlfrescoApiClient implements ee.Emitter, LegacyHttpClient {
|
||||
|
||||
constructor(host?: string, httpClient?: HttpClient) {
|
||||
this.host = host;
|
||||
|
||||
this.storage = Storage.getInstance();
|
||||
|
||||
// fallback for backward compatibility
|
||||
this.httpClient = httpClient || new SuperagentHttpClient();
|
||||
}
|
||||
|
||||
ee(this);
|
||||
// EventEmitter delegation methods
|
||||
on<K extends string | symbol>(event: K, fn: (...args: any[]) => void, context?: any): this {
|
||||
this.eventEmitter.on(event, fn, context);
|
||||
return this;
|
||||
}
|
||||
|
||||
off<K extends string | symbol>(event: K, fn?: (...args: any[]) => void, context?: any): this {
|
||||
this.eventEmitter.off(event, fn, context);
|
||||
return this;
|
||||
}
|
||||
|
||||
once<K extends string | symbol>(event: K, fn: (...args: any[]) => void, context?: any): this {
|
||||
this.eventEmitter.once(event, fn, context);
|
||||
return this;
|
||||
}
|
||||
|
||||
emit<K extends string | symbol>(event: K, ...args: any[]): boolean {
|
||||
return this.eventEmitter.emit(event, ...args);
|
||||
}
|
||||
|
||||
request<T = any>(options: RequestOptions): Promise<T> {
|
||||
@@ -325,7 +340,7 @@ export class AlfrescoApiClient implements ee.Emitter, LegacyHttpClient {
|
||||
|
||||
return {
|
||||
apiClientEmitter,
|
||||
eventEmitter: ee({})
|
||||
eventEmitter: new EventEmitter()
|
||||
};
|
||||
}
|
||||
|
||||
@@ -374,27 +389,22 @@ export class AlfrescoApiClient implements ee.Emitter, LegacyHttpClient {
|
||||
return Boolean(contentType?.match(/^application\/json(;.*)?$/i));
|
||||
}
|
||||
|
||||
private addPromiseListeners<T = any>(promise: Promise<T>, eventEmitter: ee.Emitter): AlfrescoApiClientPromise<T> {
|
||||
addPromiseListeners<T = any>(promise: Promise<T>, eventEmitter: EventEmitterInstance): AlfrescoApiClientPromise<T> {
|
||||
return Object.assign(promise, {
|
||||
on() {
|
||||
// eslint-disable-next-line prefer-spread,prefer-rest-params
|
||||
eventEmitter.on.apply(eventEmitter, arguments);
|
||||
return this;
|
||||
on<K extends string | symbol>(event: K, fn: (...args: any[]) => void, context?: any): AlfrescoApiClientPromise<T> {
|
||||
eventEmitter.on(event, fn, context);
|
||||
return this as AlfrescoApiClientPromise<T>;
|
||||
},
|
||||
once() {
|
||||
// eslint-disable-next-line prefer-spread,prefer-rest-params
|
||||
eventEmitter.once.apply(eventEmitter, arguments);
|
||||
return this;
|
||||
once<K extends string | symbol>(event: K, fn: (...args: any[]) => void, context?: any): AlfrescoApiClientPromise<T> {
|
||||
eventEmitter.once(event, fn, context);
|
||||
return this as AlfrescoApiClientPromise<T>;
|
||||
},
|
||||
emit() {
|
||||
// eslint-disable-next-line prefer-spread,prefer-rest-params
|
||||
eventEmitter.emit.apply(eventEmitter, arguments);
|
||||
return this;
|
||||
emit<K extends string | symbol>(event: K, ...args: any[]): boolean {
|
||||
return eventEmitter.emit(event, ...args);
|
||||
},
|
||||
off() {
|
||||
// eslint-disable-next-line prefer-spread,prefer-rest-params
|
||||
eventEmitter.off.apply(eventEmitter, arguments);
|
||||
return this;
|
||||
off<K extends string | symbol>(event: K, fn?: (...args: any[]) => void, context?: any): AlfrescoApiClientPromise<T> {
|
||||
eventEmitter.off(event, fn, context);
|
||||
return this as AlfrescoApiClientPromise<T>;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@@ -16,7 +16,9 @@
|
||||
*/
|
||||
|
||||
import { Authentication } from '../authentication/authentication';
|
||||
import { Emitter } from 'event-emitter';
|
||||
import { EventEmitter } from 'eventemitter3';
|
||||
|
||||
type EventEmitterInstance = InstanceType<typeof EventEmitter>;
|
||||
|
||||
export interface RequestOptions {
|
||||
path: string;
|
||||
@@ -93,8 +95,13 @@ export interface SecurityOptions {
|
||||
}
|
||||
|
||||
export interface Emitters {
|
||||
readonly eventEmitter: Emitter;
|
||||
readonly apiClientEmitter: Emitter;
|
||||
readonly eventEmitter: EventEmitterInstance;
|
||||
readonly apiClientEmitter: {
|
||||
on: EventEmitterInstance['on'];
|
||||
off: EventEmitterInstance['off'];
|
||||
once: EventEmitterInstance['once'];
|
||||
emit: EventEmitterInstance['emit'];
|
||||
};
|
||||
}
|
||||
|
||||
export interface HttpClient {
|
||||
|
@@ -15,7 +15,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import ee from 'event-emitter';
|
||||
import { EventEmitter } from 'eventemitter3';
|
||||
import { AuthenticationApi, TicketBody } from '../api/auth-rest-api';
|
||||
import { AlfrescoApiClient } from '../alfrescoApiClient';
|
||||
import { AlfrescoApiConfig } from '../alfrescoApiConfig';
|
||||
@@ -104,8 +104,7 @@ export class ContentAuth extends AlfrescoApiClient {
|
||||
});
|
||||
});
|
||||
|
||||
ee(promise); // jshint ignore:line
|
||||
return promise;
|
||||
return this.addPromiseListeners(promise, new EventEmitter());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -133,8 +132,7 @@ export class ContentAuth extends AlfrescoApiClient {
|
||||
);
|
||||
});
|
||||
|
||||
ee(promise); // jshint ignore:line
|
||||
return promise;
|
||||
return this.addPromiseListeners(promise, new EventEmitter());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -160,8 +158,7 @@ export class ContentAuth extends AlfrescoApiClient {
|
||||
);
|
||||
});
|
||||
|
||||
ee(promise); // jshint ignore:line
|
||||
return promise;
|
||||
return this.addPromiseListeners(promise, new EventEmitter());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -15,7 +15,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import ee from 'event-emitter';
|
||||
import { EventEmitter } from 'eventemitter3';
|
||||
import { AlfrescoApiClient } from '../alfrescoApiClient';
|
||||
import { AlfrescoApiConfig } from '../alfrescoApiConfig';
|
||||
import { Authentication } from './authentication';
|
||||
@@ -604,7 +604,7 @@ export class Oauth2Auth extends AlfrescoApiClient {
|
||||
}
|
||||
);
|
||||
|
||||
ee(promise); // jshint ignore:line
|
||||
return this.addPromiseListeners(promise, new EventEmitter());
|
||||
}
|
||||
|
||||
pollingRefreshToken() {
|
||||
@@ -654,9 +654,7 @@ export class Oauth2Auth extends AlfrescoApiClient {
|
||||
);
|
||||
});
|
||||
|
||||
ee(promise); // jshint ignore:line
|
||||
|
||||
return promise;
|
||||
return this.addPromiseListeners(promise, new EventEmitter());
|
||||
}
|
||||
|
||||
universalBtoa(stringToConvert: string) {
|
||||
|
@@ -15,7 +15,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import ee from 'event-emitter';
|
||||
import { EventEmitter } from 'eventemitter3';
|
||||
import { AlfrescoApiClient, AlfrescoApiClientPromise } from '../alfrescoApiClient';
|
||||
import { AlfrescoApiConfig } from '../alfrescoApiConfig';
|
||||
import { Authentication } from './authentication';
|
||||
@@ -123,8 +123,7 @@ export class ProcessAuth extends AlfrescoApiClient {
|
||||
);
|
||||
});
|
||||
|
||||
ee(promise); // jshint ignore:line
|
||||
return promise;
|
||||
return this.addPromiseListeners(promise, new EventEmitter());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -154,8 +153,7 @@ export class ProcessAuth extends AlfrescoApiClient {
|
||||
);
|
||||
});
|
||||
|
||||
ee(promise);
|
||||
return promise;
|
||||
return this.addPromiseListeners(promise, new EventEmitter());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -15,7 +15,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import ee, { Emitter } from 'event-emitter';
|
||||
import { EventEmitter } from 'eventemitter3';
|
||||
import superagent, { Response, SuperAgentRequest } from 'superagent';
|
||||
import { Authentication } from './authentication/authentication';
|
||||
import { RequestOptions, HttpClient, SecurityOptions, Emitters } from './api-clients/http-client.interface';
|
||||
@@ -26,6 +26,8 @@ import { isBrowser, paramToString } from './utils';
|
||||
declare const Blob: any;
|
||||
declare const Buffer: any;
|
||||
|
||||
type EventEmitterInstance = InstanceType<typeof EventEmitter>;
|
||||
|
||||
const isProgressEvent = (event: ProgressEvent | unknown): event is ProgressEvent => (event as ProgressEvent)?.lengthComputable;
|
||||
|
||||
export class SuperagentHttpClient implements HttpClient {
|
||||
@@ -134,7 +136,7 @@ export class SuperagentHttpClient implements HttpClient {
|
||||
contentType: string,
|
||||
accept: string,
|
||||
responseType: string,
|
||||
eventEmitter: ee.Emitter,
|
||||
eventEmitter: EventEmitterInstance,
|
||||
returnType: string,
|
||||
securityOptions: SecurityOptions
|
||||
) {
|
||||
@@ -268,7 +270,7 @@ export class SuperagentHttpClient implements HttpClient {
|
||||
}
|
||||
}
|
||||
|
||||
private progress(event: ProgressEvent | unknown, eventEmitter: Emitter): void {
|
||||
private progress(event: ProgressEvent | unknown, eventEmitter: EventEmitterInstance): void {
|
||||
if (isProgressEvent(event)) {
|
||||
const percent = Math.round((event.loaded / event.total) * 100);
|
||||
|
||||
|
Reference in New Issue
Block a user