Skip to content

Commit

Permalink
fix(ts): add visibility to functions
Browse files Browse the repository at this point in the history
EME-5329

Co-authored-by: Gabor Nemeth <gabor.nemeth@emarsys.com>
  • Loading branch information
sonicoder86 and ngabor84 committed Sep 2, 2022
1 parent 545e739 commit c497215
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 52 deletions.
74 changes: 37 additions & 37 deletions src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ import { createLogger } from '@emartech/json-logger';
const logger = createLogger('suiterequest');

export class EscherRequest {
static EscherConstants = {
private static EscherConstants = {
algoPrefix: 'EMS',
vendorKey: 'EMS',
credentialScope: 'eu/suite/ems_request',
authHeaderName: 'X-Ems-Auth',
dateHeaderName: 'X-Ems-Date'
};
_escher: Escher;
_options: EscherRequestOption;
httpAgent?: HttpAgent;
httpsAgent?: HttpsAgent;
private escher: Escher;
private options: EscherRequestOption;
private readonly httpAgent?: HttpAgent;
private readonly httpsAgent?: HttpsAgent;

static create(accessKeyId: string, apiSecret: string, requestOptions: EscherRequestOption) {
public static create(accessKeyId: string, apiSecret: string, requestOptions: EscherRequestOption) {
return new EscherRequest(accessKeyId, apiSecret, requestOptions);
}

Expand All @@ -33,59 +33,59 @@ export class EscherRequest {
credentialScope: requestOptions.credentialScope || EscherRequest.EscherConstants.credentialScope
});

this._escher = new Escher(escherConfig);
this._options = requestOptions;
this.escher = new Escher(escherConfig);
this.options = requestOptions;

if (requestOptions.keepAlive) {
this.httpAgent = new HttpAgent({ keepAlive: true });
this.httpsAgent = new HttpsAgent({ keepAlive: true });
}
}

get<T = any>(path: string, data: any): Promise<TransformedResponse<T>> {
return this._request('GET', path, data);
public get<T = any>(path: string, data: any): Promise<TransformedResponse<T>> {
return this.request('GET', path, data);
}

patch<T = any>(path: string, data: any): Promise<TransformedResponse<T>> {
return this._request('PATCH', path, data);
public patch<T = any>(path: string, data: any): Promise<TransformedResponse<T>> {
return this.request('PATCH', path, data);
}

post<T = any>(path: string, data: any): Promise<TransformedResponse<T>> {
return this._request('POST', path, data);
public post<T = any>(path: string, data: any): Promise<TransformedResponse<T>> {
return this.request('POST', path, data);
}

put<T = any>(path: string, data: any): Promise<TransformedResponse<T>> {
return this._request('PUT', path, data);
public put<T = any>(path: string, data: any): Promise<TransformedResponse<T>> {
return this.request('PUT', path, data);
}

delete<T = any>(path: string): Promise<TransformedResponse<T>> {
return this._request('DELETE', path);
public delete<T = any>(path: string): Promise<TransformedResponse<T>> {
return this.request('DELETE', path);
}

setOptions(requestOptions: EscherRequestOption): void {
this._options = requestOptions;
public setOptions(requestOptions: EscherRequestOption): void {
this.options = requestOptions;
}

getOptions(): EscherRequestOption {
return this._options;
public getOptions(): EscherRequestOption {
return this.options;
}

_request(method: string, path: string, data?: any) {
const options = this._getOptionsFor(method, path);
const payload = data ? this._getPayload(data) : '';
const signedOptions = this._signRequest(options, payload);
private request(method: string, path: string, data?: any) {
const options = this.getOptionsFor(method, path);
const payload = data ? this.getPayload(data) : '';
const signedOptions = this.signRequest(options, payload);

logger.info('send', this._getLogParameters(options));
return this._getRequestFor(signedOptions, payload).send();
logger.info('send', this.getLogParameters(options));
return this.getRequestFor(signedOptions, payload).send();
}

_getRequestFor(requestOptions: ExtendedRequestOption, payload: any) {
const protocol = (this._options.secure) ? 'https:' : 'http:';
private getRequestFor(requestOptions: ExtendedRequestOption, payload: any) {
const protocol = (this.options.secure) ? 'https:' : 'http:';
return new RequestWrapper(requestOptions, protocol, payload);
}

_getOptionsFor(method: string, path: string): ExtendedRequestOption {
const defaultOptions = this._options.toHash();
private getOptionsFor(method: string, path: string): ExtendedRequestOption {
const defaultOptions = this.options.toHash();
const realPath = defaultOptions.prefix + path;

return Object.assign({}, defaultOptions, {
Expand All @@ -97,21 +97,21 @@ export class EscherRequest {
});
}

_signRequest(options: ExtendedRequestOption, payload: any) {
private signRequest(options: ExtendedRequestOption, payload: any) {
const headerNames = options.headers ? options.headers.map(function(header) {
return header[0];
}) : [];

return (this._escher.signRequest(options, payload, headerNames) as ExtendedRequestOption);
return (this.escher.signRequest(options, payload, headerNames) as ExtendedRequestOption);
}

_getLogParameters(options: ExtendedRequestOption) {
private getLogParameters(options: ExtendedRequestOption) {
const { method, host, url } = options;
return { method, host, url };
}

_getPayload(data: any) {
if (this._options?.getHeader('content-type')?.indexOf('application/json') === -1) {
private getPayload(data: any) {
if (this.options?.getHeader('content-type')?.indexOf('application/json') === -1) {
return data;
}

Expand Down
6 changes: 3 additions & 3 deletions src/requestError.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { AxiosResponse } from 'axios';

export class EscherRequestError extends Error {
code: number;
originalCode: string | undefined;
data: any;
public code: number;
public originalCode: string | undefined;
public data: any;

constructor(message: string, code: number, response?: string | AxiosResponse, originalCode?: string) {
super(message);
Expand Down
24 changes: 12 additions & 12 deletions src/requestOption.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ export class EscherRequestOption implements RequestOptions {
keepAlive = false;
credentialScope = '';

static createForInternalApi(host: string | RequestOptions, rejectUnauthorized: boolean) {
public static createForInternalApi(host: string | RequestOptions, rejectUnauthorized: boolean) {
return this.create(host, '/api/v2/internal', rejectUnauthorized);
}

static createForServiceApi(host: string | RequestOptions, rejectUnauthorized: boolean) {
public static createForServiceApi(host: string | RequestOptions, rejectUnauthorized: boolean) {
return this.create(host, '/api/services', rejectUnauthorized);
}

static create(host: string | RequestOptions, prefix = '', rejectUnauthorized = true) {
public static create(host: string | RequestOptions, prefix = '', rejectUnauthorized = true) {
let options: RequestOptions = {};

if (typeof host === 'object') {
Expand Down Expand Up @@ -68,48 +68,48 @@ export class EscherRequestOption implements RequestOptions {
Object.assign(this, options);
}

setToSecure(port: number, rejectUnauthorized: boolean) {
public setToSecure(port: number, rejectUnauthorized: boolean) {
this.port = port || 443;
this.secure = true;
this.rejectUnauthorized = rejectUnauthorized;
}

setToUnsecure(port: number) {
public setToUnsecure(port: number) {
this.port = port || 80;
this.secure = false;
}

setHost(host: string) {
public setHost(host: string) {
this.host = host;
}

setPort(port: number) {
public setPort(port: number) {
this.port = port;
}

setHeader(headerToSet: string[]) {
public setHeader(headerToSet: string[]) {
this.headers = this.headers
.filter(existingHeader => existingHeader[0] !== headerToSet[0])
.concat([headerToSet]);
}

getHeader(name: string) {
public getHeader(name: string) {
const result = this.headers.find((header) => {
return header[0].toLowerCase() === name.toLowerCase();
});

return result ? result[1] : null;
}

setTimeout(timeout: number) {
public setTimeout(timeout: number) {
this.timeout = timeout;
}

getTimeout() {
public getTimeout() {
return this.timeout;
}

toHash(): RequestOptions {
public toHash(): RequestOptions {
const hash: RequestOptions = {
port: this.port,
host: this.host,
Expand Down

0 comments on commit c497215

Please sign in to comment.