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: add support to configure axios timeout #309

Draft
wants to merge 2 commits into
base: old-dev
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
4 changes: 2 additions & 2 deletions src/api/axiosWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import axios from 'axios';
import { TIMEOUT } from '../constants';
import config from '../config';

/**
* Method that creates an axios instance
Expand All @@ -23,7 +23,7 @@ import { TIMEOUT } from '../constants';
*/
export const axiosWrapperCreateRequestInstance = (url, resolve, timeout) => {
if (timeout === undefined) {
timeout = TIMEOUT;
timeout = config.getAxiosTimeout();
}
const defaultOptions = {
baseURL: url,
Expand Down
9 changes: 3 additions & 6 deletions src/api/explorerServiceAxios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
*/

import axios from 'axios';
import {
TIMEOUT,
} from '../constants';
import config from '../config';

/**
Expand All @@ -23,10 +20,10 @@ import config from '../config';
* @param {string} network The network to access the explorer service
* @param {number} timeout Timeout in milliseconds for the request
*/
export const axiosInstance = async (network: string, timeout: number = TIMEOUT) => {
export const axiosInstance = async (network: string, timeout?: number) => {
const defaultOptions = {
baseURL: config.getExplorerServiceBaseUrl(network),
timeout: timeout,
timeout: timeout || config.getAxiosTimeout(),
headers: {
'Content-Type': 'application/json',
},
Expand All @@ -35,4 +32,4 @@ export const axiosInstance = async (network: string, timeout: number = TIMEOUT)
return axios.create(defaultOptions);
}

export default axiosInstance;
export default axiosInstance;
312 changes: 169 additions & 143 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import networkInstance from './network';
import Network from './models/network';
import storage from './storage';
import { GetWalletServiceUrlError, GetWalletServiceWsUrlError } from './errors';
import { TIMEOUT } from './constants';


// Default server user will connect when none have been chosen
Expand All @@ -16,167 +17,192 @@ const EXPLORER_SERVICE_MAINNET_BASE_URL = 'https://explorer-service.hathor.netw
const EXPLORER_SERVICE_TESTNET_BASE_URL = 'https://explorer-service.testnet.hathor.network/';

class Config {
TX_MINING_URL?: string;
WALLET_SERVICE_BASE_URL?: string;
WALLET_SERVICE_BASE_WS_URL?: string;
EXPLORER_SERVICE_BASE_URL?: string;
SERVER_URL?: string;

/**
* Sets the tx mining service url that will be returned by the config object.
*
* @param {string} url - The url to be set
*/
setTxMiningUrl(url: string) {
this.TX_MINING_URL = url;
TX_MINING_URL?: string;
WALLET_SERVICE_BASE_URL?: string;
WALLET_SERVICE_BASE_WS_URL?: string;
EXPLORER_SERVICE_BASE_URL?: string;
SERVER_URL?: string;
// Default axios timeout in milliseconds
AXIOS_TIMEOUT?: number;

/**
* Sets the tx mining service url that will be returned by the config object.
*
* @param {string} url - The url to be set
*/
setTxMiningUrl(url: string) {
this.TX_MINING_URL = url;
}

/**
* Returns the correct base url constant for tx mining.
* If the url was explicitly set using the config object, it is always returned.
* Otherwise, we return it based on the network set with networkInstance.
*
* @return {string} The tx mining service url
*/
getTxMiningUrl() {
if (this.TX_MINING_URL) {
return this.TX_MINING_URL;
}

/**
* Returns the correct base url constant for tx mining.
* If the url was explicitly set using the config object, it is always returned.
* Otherwise, we return it based on the network set with networkInstance.
*
* @return {string} The tx mining service url
*/
getTxMiningUrl() {
if (this.TX_MINING_URL) {
return this.TX_MINING_URL;
}

// Keeps the old behavior for cases that don't explicitly set a TX_MINING_URL
if (networkInstance.name == 'mainnet') {
return TX_MINING_MAINNET_URL;
} else if (networkInstance.name == 'testnet') {
return TX_MINING_TESTNET_URL;
} else {
throw new Error(`Network ${networkInstance.name} doesn't have a correspondent tx mining service url. You should set it explicitly.`);
}
// Keeps the old behavior for cases that don't explicitly set a TX_MINING_URL
if (networkInstance.name == 'mainnet') {
return TX_MINING_MAINNET_URL;
} else if (networkInstance.name == 'testnet') {
return TX_MINING_TESTNET_URL;
} else {
throw new Error(`Network ${networkInstance.name} doesn't have a correspondent tx mining service url. You should set it explicitly.`);
}

/**
* Sets the wallet service url that will be returned by the config object.
*
* @param {string} url - The url to be set
*/
setWalletServiceBaseUrl(url: string): void {
this.WALLET_SERVICE_BASE_URL = url;
}

/**
* Sets the wallet service url that will be returned by the config object.
*
* @param {string} url - The url to be set
*/
setWalletServiceBaseUrl(url: string): void {
this.WALLET_SERVICE_BASE_URL = url;
}

/**
* Returns the base url for wallet service set previously using setWalletServiceBaseUrl
*
* Throws an error if it is not yet set.
*
* @return {string} The wallet service url
*/
getWalletServiceBaseUrl(): string {
if (!this.WALLET_SERVICE_BASE_URL) {
throw new GetWalletServiceUrlError('Wallet service base URL not set.');
}

/**
* Returns the base url for wallet service set previously using setWalletServiceBaseUrl
*
* Throws an error if it is not yet set.
*
* @return {string} The wallet service url
*/
getWalletServiceBaseUrl(): string {
if (!this.WALLET_SERVICE_BASE_URL) {
throw new GetWalletServiceUrlError('Wallet service base URL not set.');
}

return this.WALLET_SERVICE_BASE_URL;
return this.WALLET_SERVICE_BASE_URL;
}

/**
* Returns the correct websocket base url constant for wallet service.
*
* If it is not set, throw an error.
*
* @return {string} The wallet service websocket url
*/
getWalletServiceBaseWsUrl(): string {
if (!this.WALLET_SERVICE_BASE_WS_URL) {
throw new GetWalletServiceWsUrlError('Wallet service base WebSocket URL not set.');
}

/**
* Returns the correct websocket base url constant for wallet service.
*
* If it is not set, throw an error.
*
* @return {string} The wallet service websocket url
*/
getWalletServiceBaseWsUrl(): string {
if (!this.WALLET_SERVICE_BASE_WS_URL) {
throw new GetWalletServiceWsUrlError('Wallet service base WebSocket URL not set.');
}

return this.WALLET_SERVICE_BASE_WS_URL;
return this.WALLET_SERVICE_BASE_WS_URL;
}

/**
* Sets the wallet service websocket url that will be returned by the config object.
*
* @param {string} url - The url to be set
*/
setWalletServiceBaseWsUrl(url: string): void {
this.WALLET_SERVICE_BASE_WS_URL = url;
}

/**
* Sets the explorer service url that will be returned by the config object.
*
* @param {string} url - The url to be set
*/
setExplorerServiceBaseUrl(url: string) {
this.EXPLORER_SERVICE_BASE_URL = url;
}

/**
* Returns the correct base url constant for explorer service.
* If the url was explicitly set using the config object, it is always returned.
* Otherwise, we return it based on the provided network object.
*
* If the url was not set in the config, and no network is provided, we throw an Error.
*
* @param {string} network - The name of the network to be used to select the url.
* @return {string} The explorer service url
*/
getExplorerServiceBaseUrl(network: string) {
if (this.EXPLORER_SERVICE_BASE_URL) {
return this.EXPLORER_SERVICE_BASE_URL;
}

/**
* Sets the wallet service websocket url that will be returned by the config object.
*
* @param {string} url - The url to be set
*/
setWalletServiceBaseWsUrl(url: string): void {
this.WALLET_SERVICE_BASE_WS_URL = url;
if (!network) {
throw new Error('You should either provide a network or call setExplorerServiceBaseUrl before calling this.');
}

/**
* Sets the explorer service url that will be returned by the config object.
*
* @param {string} url - The url to be set
*/
setExplorerServiceBaseUrl(url: string) {
this.EXPLORER_SERVICE_BASE_URL = url;
// Keeps the old behavior for cases that don't explicitly set a EXPLORER_SERVICE_BASE_URL
if (network == 'mainnet') {
return EXPLORER_SERVICE_MAINNET_BASE_URL;
} else if (network == 'testnet'){
return EXPLORER_SERVICE_TESTNET_BASE_URL;
} else {
throw new Error(`Network ${network} doesn't have a correspondent explorer service url. You should set it explicitly.`);
}

/**
* Returns the correct base url constant for explorer service.
* If the url was explicitly set using the config object, it is always returned.
* Otherwise, we return it based on the provided network object.
*
* If the url was not set in the config, and no network is provided, we throw an Error.
*
* @param {string} network - The name of the network to be used to select the url.
* @return {string} The explorer service url
*/
getExplorerServiceBaseUrl(network: string) {
if (this.EXPLORER_SERVICE_BASE_URL) {
return this.EXPLORER_SERVICE_BASE_URL;
}

if (!network) {
throw new Error('You should either provide a network or call setExplorerServiceBaseUrl before calling this.');
}

// Keeps the old behavior for cases that don't explicitly set a EXPLORER_SERVICE_BASE_URL
if (network == 'mainnet') {
return EXPLORER_SERVICE_MAINNET_BASE_URL;
} else if (network == 'testnet'){
return EXPLORER_SERVICE_TESTNET_BASE_URL;
} else {
throw new Error(`Network ${network} doesn't have a correspondent explorer service url. You should set it explicitly.`);
}
}

/**
* Sets the fullnode server url that will be returned by the config object.
*
* @param {string} url - The url to be set
*/
setServerUrl(url: string) {
this.SERVER_URL = url;
}

/**
* Get the server URL that the wallet is connected.
*
* There is more than one method of setting this.
* The priority will be given to the url set using the config object.
* If not set, we look next into the storage object keys.
* If still not set, the default url is returned
*
* @return {string} Server URL according to the priority described above
*/
getServerUrl() {
if (this.SERVER_URL) {
return this.SERVER_URL;
}

/**
* Sets the fullnode server url that will be returned by the config object.
*
* @param {string} url - The url to be set
*/
setServerUrl(url: string) {
this.SERVER_URL = url;
if (storage.isInitialized()) {
const server = storage.getItem('wallet:server');
const defaultServer = storage.getItem('wallet:defaultServer');

if (server !== null) {
return server;
} else if (defaultServer !== null) {
return defaultServer
}
}

/**
* Get the server URL that the wallet is connected.
*
* There is more than one method of setting this.
* The priority will be given to the url set using the config object.
* If not set, we look next into the storage object keys.
* If still not set, the default url is returned
*
* @return {string} Server URL according to the priority described above
*/
getServerUrl() {
if (this.SERVER_URL) {
return this.SERVER_URL;
}

if (storage.isInitialized()) {
const server = storage.getItem('wallet:server');
const defaultServer = storage.getItem('wallet:defaultServer');

if (server !== null) {
return server;
} else if (defaultServer !== null) {
return defaultServer
}
}

return DEFAULT_SERVER;
return DEFAULT_SERVER;
}

/**
* Sets the default axios timeout for requests
*
* @param {number} timeout - Timeout in milliseconds
*/
setAxiosTimeout(timeout: number) {
this.AXIOS_TIMEOUT = timeout;
}

/**
* Returns the axios timeout to be used in requests
* If set in config, use it. Otherwise, use the parameter in the constants file.
*
* @return {number} The axios timeout to be used in requests
*/
getAxiosTimeout() {
if (this.AXIOS_TIMEOUT) {
return this.AXIOS_TIMEOUT;
}

return TIMEOUT;
}

}

const instance = new Config();
Expand Down
Loading