Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP - Breaking change: decouple from "axios" #53

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 38 additions & 18 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@multiversx/sdk-network-providers",
"version": "2.2.0",
"version": "3.0.0-alpha.0",
"lockfileVersion": 2,
"requires": true,
"author": "MultiversX",
Expand All @@ -13,19 +13,19 @@
"types": "out/index.d.js",
"packages": {},
"dependencies": {
"axios": "1.6.1",
"bech32": "1.1.4",
"bignumber.js": "9.0.1",
"buffer": "6.0.3",
"json-bigint": "1.0.0"
"buffer": "6.0.3"
},
"devDependencies": {
"@types/assert": "1.4.6",
"@types/chai": "4.2.11",
"@types/mocha": "9.1.0",
"@types/node": "13.13.2",
"assert": "2.0.0",
"axios": "1.6.2",
"chai": "4.2.0",
"json-bigint": "1.0.0",
"mocha": "9.2.2",
"ts-node": "9.1.1",
"tslint": "6.1.3",
Expand Down
40 changes: 18 additions & 22 deletions src/apiNetworkProvider.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import axios, { AxiosRequestConfig } from "axios";
import { AccountOnNetwork, GuardianData } from "./accounts";
import { defaultAxiosConfig, defaultPagination } from "./config";
import { defaultPagination } from "./config";
import { ContractQueryRequest } from "./contractQueryRequest";
import { ContractQueryResponse } from "./contractQueryResponse";
import { ErrContractQuery, ErrNetworkProvider } from "./errors";
import { IAddress, IContractQuery, INetworkProvider, IPagination, ITransaction } from "./interface";
import { IAddress, IContractQuery, IHttpProvider, INetworkProvider, IPagination, ITransaction } from "./interface";
import { NetworkConfig } from "./networkConfig";
import { NetworkGeneralStatistics } from "./networkGeneralStatistics";
import { NetworkStake } from "./networkStake";
Expand All @@ -19,14 +18,18 @@ import { TransactionStatus } from "./transactionStatus";

// TODO: Find & remove duplicate code between "ProxyNetworkProvider" and "ApiNetworkProvider".
export class ApiNetworkProvider implements INetworkProvider {
private url: string;
private config: AxiosRequestConfig;
private baseUrl: string;
private httpProvider: IHttpProvider;
private backingProxyNetworkProvider;

constructor(url: string, config?: AxiosRequestConfig) {
this.url = url;
this.config = { ...defaultAxiosConfig, ...config };
this.backingProxyNetworkProvider = new ProxyNetworkProvider(url, config);
constructor(options: { baseUrl: string, httpProvider: IHttpProvider }) {
this.baseUrl = options.baseUrl;
this.httpProvider = options.httpProvider;

this.backingProxyNetworkProvider = new ProxyNetworkProvider({
baseUrl: options.baseUrl,
httpProvider: options.httpProvider
});
}

async getNetworkConfig(): Promise<NetworkConfig> {
Expand Down Expand Up @@ -176,29 +179,22 @@ export class ApiNetworkProvider implements INetworkProvider {
}

private async doGet(resourceUrl: string): Promise<any> {
let url = `${this.url}/${resourceUrl}`;
const url = `${this.baseUrl}/${resourceUrl}`;

try {
let response = await axios.get(url, this.config);
return response.data;
const response = await this.httpProvider.get(url);
return response;
} catch (error) {
this.handleApiError(error, resourceUrl);
}
}

private async doPost(resourceUrl: string, payload: any): Promise<any> {
let url = `${this.url}/${resourceUrl}`;
const url = `${this.baseUrl}/${resourceUrl}`;

try {
let response = await axios.post(url, payload, {
...this.config,
headers: {
"Content-Type": "application/json",
...this.config.headers,
},
});
let responsePayload = response.data;
return responsePayload;
const response = await this.httpProvider.post(url, payload);
return response;
} catch (error) {
this.handleApiError(error, resourceUrl);
}
Expand Down
12 changes: 0 additions & 12 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
import { IPagination } from "./interface";

const JSONbig = require("json-bigint")({ constructorAction: 'ignore' });

export const defaultAxiosConfig = {
timeout: 5000,
// See: https://github.com/axios/axios/issues/983 regarding transformResponse
transformResponse: [
function (data: any) {
return JSONbig.parse(data);
}
]
};

export const defaultPagination: IPagination = {
from: 0,
size: 100
Expand Down
5 changes: 5 additions & 0 deletions src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,8 @@ export interface ITransaction {
}

export interface IAddress { bech32(): string; }

export interface IHttpProvider {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd use generics here for those who know the response type:

export interface IHttpProvider {
    get<T = any>(url: string): Promise<T>;
    post<T = any>(url: string, payload: any): Promise<T>;
}

Copy link

@CiprianDraghici CiprianDraghici Dec 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, the consumer is responsible for error handling.
eg.

try {
// the actual request
} catch(e){ 
// do something the the error
}

get<T = any>(url: string): Promise<T>;
post<T = any>(url: string, payload: any): Promise<T>;
}
Loading
Loading