@@ -31,6 +31,33 @@ class {{spec.title | caseUcfirst}}Exception extends Error {
31
31
}
32
32
}
33
33
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
+
34
61
class Client {
35
62
static CHUNK_SIZE = 1024 * 1024 * 5;
36
63
@@ -46,7 +73,7 @@ class Client {
46
73
'x-sdk-platform': '{{ sdk .platform }}',
47
74
'x-sdk-language': '{{ language .name | caseLower }}',
48
75
'x-sdk-version': '{{ sdk .version }}',
49
- 'user-agent' : '{{ spec . title | caseUcfirst }}{{ language . name | caseUcfirst }}SDK/{{ sdk . version }}' ,
76
+ 'user-agent' : getUserAgent() ,
50
77
{%~ for key ,header in spec .global .defaultHeaders %}
51
78
'{{key }}': '{{header }}',
52
79
{%~ endfor %}
0 commit comments