@@ -31,6 +31,33 @@ class {{spec.title | caseUcfirst}}Exception extends Error {
3131 }
3232}
3333
34+ function getUserAgent() {
35+ let ua = '{{spec .title | caseUcfirst }}{{language .name | caseUcfirst }}SDK/{{ sdk .version }}';
36+
37+ // `process` is a global in Node.js, but not fully available in all runtimes.
38+ const platform: string[] = [];
39+ if (typeof process !== 'undefined') {
40+ if (typeof process.platform === 'string') platform.push(process.platform);
41+ if (typeof process.arch === 'string') platform.push(process.arch);
42+ }
43+ if (platform.length > 0) {
44+ ua += ` (${platform.join('; ')})`;
45+ }
46+
47+ // `navigator.userAgent` is available in Node.js 21 and later.
48+ // It's also part of the WinterCG spec, so many edge runtimes provide it.
49+ // https://common-min-api.proposal.wintercg.org/#requirements-for-navigatoruseragent
50+ if (typeof navigator !== 'undefined' && typeof navigator.userAgent === 'string') {
51+ ua += ` ${navigator.userAgent}`;
52+
53+ // Older Node.js versions don't have `navigator.userAgent`, so we have to use `process.version`.
54+ } else if (typeof process !== 'undefined') {
55+ ua += ` Node.js/${process.version}`;
56+ }
57+
58+ return ua;
59+ }
60+
3461class Client {
3562 static CHUNK_SIZE = 1024 * 1024 * 5;
3663
@@ -46,7 +73,7 @@ class Client {
4673 'x-sdk-platform': '{{ sdk .platform }}',
4774 'x-sdk-language': '{{ language .name | caseLower }}',
4875 'x-sdk-version': '{{ sdk .version }}',
49- 'user-agent' : '{{ spec . title | caseUcfirst }}{{ language . name | caseUcfirst }}SDK/{{ sdk . version }}' ,
76+ 'user-agent' : getUserAgent() ,
5077 {%~ for key ,header in spec .global .defaultHeaders %}
5178 '{{key }}': '{{header }}',
5279 {%~ endfor %}
0 commit comments