-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #528 from multiversx/TOOL-327-extend-user-agent-on…
…ly-on-backend Extend user agent only on backend
- Loading branch information
Showing
5 changed files
with
28 additions
and
14 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} |