Skip to content

Commit 15a3c98

Browse files
Raghav Boorgapallyclaude
andcommitted
refactor: address PR feedback on User-Agent implementation
Address feedback from PR review: 1. Remove unused error parameters from catch blocks - Changed `} catch (error) {` to `} catch {` throughout userAgent.ts - Cleaner syntax when error is not used 2. Remove userAgent utility exports from public API - Removed `export * from './util/userAgent'` from index.ts - These are internal utilities, not part of public SDK interface - Kept EnvoyAPIOptions type export for public use 3. Update documentation to remove "legacy" terminology - Constructor JSDoc now treats both signatures as equal alternatives - Changed "Legacy usage" to "Simple usage with access token only" - Changed "New usage" to "Usage with custom User-Agent" - Updated inline comment from "legacy" to neutral language 4. Add test for emoji and unexpected characters - New test verifies SDK handles unusual characters gracefully - Tests emoji (🚀) and non-ASCII characters (中文) - Ensures JSON serialization works correctly with special chars All 63 tests pass. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent e5b4c22 commit 15a3c98

File tree

4 files changed

+30
-12
lines changed

4 files changed

+30
-12
lines changed

src/base/EnvoyAPI.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,22 +80,22 @@ export default class EnvoyAPI {
8080
/**
8181
* Create an EnvoyAPI client instance
8282
*
83-
* @param options - Either an access token string (for backward compatibility)
84-
* or an EnvoyAPIOptions object with accessToken and optional userAgent
83+
* @param options - Either an access token string or an EnvoyAPIOptions object
84+
* with accessToken and optional userAgent
8585
*
8686
* @example
87-
* // Legacy usage (still supported)
87+
* // Simple usage with access token only
8888
* const client = new EnvoyAPI('access-token-here');
8989
*
9090
* @example
91-
* // New usage with custom User-Agent
91+
* // Usage with custom User-Agent for tracking and debugging
9292
* const client = new EnvoyAPI({
9393
* accessToken: 'access-token-here',
9494
* userAgent: 'MyApp/1.0.0'
9595
* });
9696
*/
9797
constructor(options: EnvoyAPIOptions | string) {
98-
// Support both string (legacy) and options object (new)
98+
// Support both string and options object formats
9999
const { accessToken, userAgent } = typeof options === 'string'
100100
? { accessToken: options, userAgent: undefined }
101101
: options;

src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ export * from './sdk/middleware';
6262
export * from './util/EnvoySignatureVerifier';
6363
export * from './util/axiosConstructor';
6464
export * from './util/errorHandling';
65-
export * from './util/userAgent';
6665

6766
export type { EnvoyAPIOptions } from './base/EnvoyAPI';
6867

src/util/userAgent.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ try {
66
// Import version from package.json
77
// Note: In compiled code, this resolves correctly
88
version = require('../../package.json').version;
9-
} catch (error) {
9+
} catch {
1010
// If package.json can't be loaded, use fallback version
1111
// This ensures SDK initialization never fails
1212
// Silently fail - User-Agent is telemetry, not critical functionality
@@ -39,7 +39,7 @@ function getNodeVersion(): string {
3939
if (process?.version) {
4040
return process.version.replace('v', '');
4141
}
42-
} catch (error) {
42+
} catch {
4343
// Ignore error, use fallback
4444
}
4545
return 'unknown';
@@ -52,7 +52,7 @@ function getNodeVersion(): string {
5252
function getPlatform(): string {
5353
try {
5454
return os.platform();
55-
} catch (error) {
55+
} catch {
5656
// If os.platform() fails, return fallback
5757
return 'unknown';
5858
}
@@ -74,7 +74,7 @@ export function buildUserAgent(customUserAgent?: string): string {
7474
const nodeVersion = getNodeVersion();
7575
const baseUA = `envoy-integrations-sdk/${version} node/${nodeVersion}`;
7676
return customUserAgent ? `${baseUA} ${customUserAgent}` : baseUA;
77-
} catch (error) {
77+
} catch {
7878
// Critical fallback - should never happen, but ensures SDK always works
7979
// Silently fail - User-Agent is telemetry, not critical functionality
8080
return 'envoy-integrations-sdk/unknown node/unknown';
@@ -109,7 +109,7 @@ export function buildClientInfo(customUserAgent?: string): ClientInfo {
109109
}
110110

111111
return clientInfo;
112-
} catch (error) {
112+
} catch {
113113
// Critical fallback - return minimal safe info
114114
// Silently fail - User-Agent is telemetry, not critical functionality
115115
return {
@@ -135,7 +135,7 @@ export function buildClientInfoHeader(customUserAgent?: string): string {
135135
try {
136136
const clientInfo = buildClientInfo(customUserAgent);
137137
return JSON.stringify(clientInfo);
138-
} catch (error) {
138+
} catch {
139139
// Critical fallback - return minimal valid JSON
140140
// Silently fail - User-Agent is telemetry, not critical functionality
141141
// Return minimal valid JSON that won't break parsing

test/base/EnvoyAPI.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,25 @@ describe('EnvoyAPI', () => {
145145
const parsedClientInfo = JSON.parse(clientInfoHeader);
146146
expect(parsedClientInfo.application).toBe(customUserAgent);
147147
});
148+
149+
it('handles userAgent with emoji and unexpected characters', () => {
150+
const customUserAgent = 'MyApp🚀/1.0.0 (test版本)';
151+
const api = new EnvoyAPI({
152+
accessToken: testAccessToken,
153+
userAgent: customUserAgent,
154+
});
155+
156+
// SDK should not fail even with unusual characters
157+
expect(api).toBeDefined();
158+
expect(api.axios.defaults.headers.authorization).toBe(`Bearer ${testAccessToken}`);
159+
expect(api.axios.defaults.headers['User-Agent']).toContain(customUserAgent);
160+
161+
// JSON serialization should handle these characters
162+
const clientInfoHeader = api.axios.defaults.headers['X-Envoy-Client-Info'] as string;
163+
expect(() => JSON.parse(clientInfoHeader)).not.toThrow();
164+
const parsedClientInfo = JSON.parse(clientInfoHeader);
165+
expect(parsedClientInfo.application).toBe(customUserAgent);
166+
});
148167
});
149168

150169
describe('authorization header', () => {

0 commit comments

Comments
 (0)