Skip to content

Commit

Permalink
Merge pull request #528 from multiversx/TOOL-327-extend-user-agent-on…
Browse files Browse the repository at this point in the history
…ly-on-backend

Extend user agent only on backend
  • Loading branch information
danielailie authored Nov 11, 2024
2 parents 0e17949 + 3b42356 commit 7a1480b
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 14 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@multiversx/sdk-core",
"version": "13.14.0",
"version": "13.14.2",
"description": "MultiversX SDK for JavaScript and TypeScript",
"author": "MultiversX",
"homepage": "https://multiversx.com",
Expand Down
4 changes: 2 additions & 2 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 } from "./userAgent";
import { extendUserAgentIfBackend } from "./userAgent";

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

private getProxyConfig(config: NetworkProviderConfig | undefined) {
Expand Down
4 changes: 2 additions & 2 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 } from "./userAgent";
import { extendUserAgentIfBackend } from "./userAgent";

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

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

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

export function extendUserAgent(userAgentPrefix: string, config: NetworkProviderConfig) {
function extendUserAgent(userAgentPrefix: string, config: NetworkProviderConfig) {
if (!config.headers) {
config.headers = new AxiosHeaders({})
};
config.headers = new AxiosHeaders({});
}
if (!config.clientName) {
console.log("Can you please provide the client name of the application that uses the SDK? It will be used for metrics.")
console.log(
"We recommend providing the `clientName` when instantiating a NetworkProvider (e.g. ProxyNetworkProvider, ApiNetworkProvider). This information will be used for metrics collection and improving our services.",
);
}
const headers = AxiosHeaders.from(config.headers as AxiosHeaders).normalize(true);
const resolvedClientName = config.clientName || UnknownClientName;

const currentUserAgent = headers.hasUserAgent() ? headers.getUserAgent() : '';
const newUserAgent = currentUserAgent ? `${currentUserAgent} ${userAgentPrefix}/${resolvedClientName}` : `${userAgentPrefix}/${resolvedClientName}`;
const currentUserAgent = headers.hasUserAgent() ? headers.getUserAgent() : "";
const newUserAgent = currentUserAgent
? `${currentUserAgent} ${userAgentPrefix}/${resolvedClientName}`
: `${userAgentPrefix}/${resolvedClientName}`;

headers.setUserAgent(newUserAgent, true);
}

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

0 comments on commit 7a1480b

Please sign in to comment.