Skip to content

Commit

Permalink
Extract if in function
Browse files Browse the repository at this point in the history
  • Loading branch information
danielailie committed Nov 11, 2024
1 parent 3c99fb7 commit 3b42356
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
6 changes: 2 additions & 4 deletions src/networkProviders/apiNetworkProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { DefinitionOfFungibleTokenOnNetwork, DefinitionOfTokenCollectionOnNetwor
import { FungibleTokenOfAccountOnNetwork, NonFungibleTokenOfAccountOnNetwork } from "./tokens";
import { TransactionOnNetwork, prepareTransactionForBroadcasting } from "./transactions";
import { TransactionStatus } from "./transactionStatus";
import { extendUserAgent, isBackend } from "./userAgent";
import { extendUserAgentIfBackend } from "./userAgent";

// TODO: Find & remove duplicate code between "ProxyNetworkProvider" and "ApiNetworkProvider".
export class ApiNetworkProvider implements INetworkProvider {
Expand All @@ -34,9 +34,7 @@ export class ApiNetworkProvider implements INetworkProvider {
this.config = { ...defaultAxiosConfig, ...config };
this.backingProxyNetworkProvider = new ProxyNetworkProvider(url, proxyConfig);
this.axios = getAxios();
if (isBackend()) {
extendUserAgent(this.userAgentPrefix, this.config);
}
extendUserAgentIfBackend(this.userAgentPrefix, this.config);
}

private getProxyConfig(config: NetworkProviderConfig | undefined) {
Expand Down
6 changes: 2 additions & 4 deletions src/networkProviders/proxyNetworkProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { DefinitionOfFungibleTokenOnNetwork, DefinitionOfTokenCollectionOnNetwor
import { FungibleTokenOfAccountOnNetwork, NonFungibleTokenOfAccountOnNetwork } from "./tokens";
import { TransactionOnNetwork, prepareTransactionForBroadcasting } from "./transactions";
import { TransactionStatus } from "./transactionStatus";
import { extendUserAgent, isBackend } from "./userAgent";
import { extendUserAgentIfBackend } from "./userAgent";

// TODO: Find & remove duplicate code between "ProxyNetworkProvider" and "ApiNetworkProvider".
export class ProxyNetworkProvider implements INetworkProvider {
Expand All @@ -28,9 +28,7 @@ export class ProxyNetworkProvider implements INetworkProvider {
this.url = url;
this.config = { ...defaultAxiosConfig, ...config };
this.axios = getAxios();
if (isBackend()) {
extendUserAgent(this.userAgentPrefix, this.config);
}
extendUserAgentIfBackend(this.userAgentPrefix, this.config);
}

async getNetworkConfig(): Promise<NetworkConfig> {
Expand Down
10 changes: 8 additions & 2 deletions src/networkProviders/userAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ import { AxiosHeaders } from "axios";
import { UnknownClientName } from "./constants";
import { NetworkProviderConfig } from "./networkProviderConfig";

export function extendUserAgent(userAgentPrefix: string, config: NetworkProviderConfig) {
export function extendUserAgentIfBackend(userAgentPrefix: string, config: NetworkProviderConfig) {
if (isBackend()) {
extendUserAgent(userAgentPrefix, config);
}
}

function extendUserAgent(userAgentPrefix: string, config: NetworkProviderConfig) {
if (!config.headers) {
config.headers = new AxiosHeaders({});
}
Expand All @@ -22,6 +28,6 @@ export function extendUserAgent(userAgentPrefix: string, config: NetworkProvider
headers.setUserAgent(newUserAgent, true);
}

export function isBackend(): boolean {
function isBackend(): boolean {
return typeof window === "undefined";
}

0 comments on commit 3b42356

Please sign in to comment.