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

Extend user agent only on backend #528

Merged
merged 4 commits into from
Nov 11, 2024
Merged
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 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";
}
Loading