Skip to content

Commit

Permalink
fix(comments): make class logger private, remove comments
Browse files Browse the repository at this point in the history
  • Loading branch information
atticusofsparta committed Feb 14, 2024
1 parent 4ea04a7 commit f735750
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 62 deletions.
6 changes: 2 additions & 4 deletions src/common/ArIO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { ContractStateProvider, HTTPServiceInterface } from '../types.js';
import { AxiosHTTPService } from './http.js';
import { ContractStateProvider, HTTPClient } from '../types.js';
import { DefaultLogger } from './logger.js';

export class ArIO implements ContractStateProvider {
private contractStateProvider: ContractStateProvider;
http: HTTPServiceInterface;
http: HTTPClient;
logger: DefaultLogger;

constructor({
Expand All @@ -35,7 +34,6 @@ export class ArIO implements ContractStateProvider {
}) {
this.contractStateProvider = contractStateProvider;
this.logger = logger;
this.http = new AxiosHTTPService({ url: '', logger }); // empty url allows for full url to be passed in get and post requests instead of the endpoint
}

/**
Expand Down
32 changes: 8 additions & 24 deletions src/common/ContractStateProviders/ArNSRemoteCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,30 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { ContractStateProvider, HTTPServiceInterface } from '../../types.js';
import { ContractStateProvider, HTTPClient } from '../../types.js';
import { validateArweaveId } from '../../utils/index.js';
import { BadRequest } from '../error.js';
import { AxiosHTTPService } from '../http.js';
import { DefaultLogger } from '../logger.js';

export class ArNSRemoteCache implements ContractStateProvider {
protected logger: DefaultLogger;
http: HTTPServiceInterface;
http: HTTPClient;
constructor({
url = 'api.arns.app',
protocol = 'https',
url = 'https://api.arns.app',
logger = new DefaultLogger({
level: 'debug',
logFormat: 'simple',
}),
version = 'v1',
}: {
protocol?: 'http' | 'https' | 'ws';
url?: string;
logger?: DefaultLogger;
version?: string;
}) {
this.logger = logger;
this.http = new AxiosHTTPService({
url: `${protocol}://${url}/${version}`,
url: `${url}/${version}`,
logger,
});
}
Expand All @@ -54,24 +52,10 @@ export class ArNSRemoteCache implements ContractStateProvider {
}
this.logger.debug(`Fetching contract state`);

const response = await this.http
.get<ContractState>({ endpoint: `/contract/${contractTxId}` })
.catch((error) => {
this.logger.debug(`Failed to fetch contract state: ${error}`);
return error;
});

if (!response) {
throw new BadRequest(
`Failed to fetch contract state. ${response?.status} ${response?.statusText()}`,
);
}
const result = await response.json();

this.logger.debug(
`Fetched contract state. Size: ${response?.headers?.get('content-length')} bytes.`,
);
const state = await this.http.get<ContractState>({
endpoint: `/contract/${contractTxId}`,
});

return result;
return state;
}
}
9 changes: 3 additions & 6 deletions src/common/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,21 @@ import { AxiosInstance } from 'axios';
import { Readable } from 'stream';
import { ReadableStream } from 'stream/web';

import { HTTPServiceInterface, Logger } from '../types.js';
import { HTTPClient, Logger } from '../types.js';
import { createAxiosInstance } from '../utils/httpClient.js';
import { FailedRequestError } from './error.js';

export class AxiosHTTPService implements HTTPServiceInterface {
export class AxiosHTTPService implements HTTPClient {
protected axios: AxiosInstance;
protected logger: Logger;

// TODO: re-implement axios-retry. Currently that package is broken for nodenext.
constructor({
url,
// retryConfig,
logger,
}: {
url: string;
// retryConfig?: IAxiosRetryConfig;

logger: Logger;
}) {
this.logger = logger;
Expand All @@ -52,8 +51,6 @@ export class AxiosHTTPService implements HTTPServiceInterface {
}
},
},
// retryConfig,
// logger: this.logger,
});
}
async get<T>({
Expand Down
2 changes: 1 addition & 1 deletion src/common/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { Logger } from '../types.js';
import { version } from '../version.js';

export class DefaultLogger implements Logger {
logger: winston.Logger;
private logger: winston.Logger;
constructor({
level = 'info',
logFormat = 'simple',
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export interface Logger {
}
/* eslint-enable @typescript-eslint/no-explicit-any */

export interface HTTPServiceInterface {
export interface HTTPClient {
get<T>({
endpoint,
signal,
Expand Down
25 changes: 0 additions & 25 deletions src/utils/httpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,34 +16,13 @@
*/
import axios, { AxiosInstance, AxiosRequestConfig } from 'axios';

// import { DefaultLogger } from '../common/logger.js';
// import { Logger } from '../types.js';

// import { version } from '../version.js';

// TODO: re-implement axios-retry. Currently latest version of axios-retry is broken for node-next builds on v4.0.0
export interface AxiosInstanceParameters {
axiosConfig?: Omit<AxiosRequestConfig, 'validateStatus'>;
// retryConfig?: Record<string, string>;
// logger?: Logger;
}

export const createAxiosInstance = ({
//logger = new DefaultLogger(),
axiosConfig = {},
// retryConfig = {
// retryDelay: axiosRetry.exponentialDelay,
// retries: 3,
// retryCondition: (error) => {
// return (
// !(error instanceof CanceledError) &&
// axiosRetry.isNetworkOrIdempotentRequestError(error)
// );
// },
// onRetry: (retryCount, error) => {
// logger.debug(`Request failed, ${error}. Retry attempt #${retryCount}...`);
// },
// },
}: AxiosInstanceParameters = {}): AxiosInstance => {
const axiosInstance = axios.create({
...axiosConfig,
Expand All @@ -53,9 +32,5 @@ export const createAxiosInstance = ({
validateStatus: () => true, // don't throw on non-200 status codes
});

// eslint-disable-next-line
// if (retryConfig.retries && retryConfig.retries > 0) {
// axiosRetry(axiosInstance, retryConfig);
// }
return axiosInstance;
};
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"compilerOptions": {
"target": "es2021",
"target": "esnext",
"allowSyntheticDefaultImports": true,
"lib": ["esnext", "dom"],
"outDir": "./lib/esm",
Expand Down

0 comments on commit f735750

Please sign in to comment.