Skip to content

Commit aa7df3f

Browse files
dsp-antclaude
andcommitted
refactor: Create fetchWithInit wrapper once in constructor
Move the createFetchWithInit() call from _authThenStart() to the constructor to avoid recreating the wrapper function on every auth attempt. This is more efficient and follows better practices. The wrapped fetch function is now stored in _fetchWithInit and reused across all auth-related calls in _authThenStart(), finishAuth(), and send(). Addresses code review feedback from @pcarleton 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent dfbfc70 commit aa7df3f

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

src/client/streamableHttp.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ export class StreamableHTTPClientTransport implements Transport {
129129
private _requestInit?: RequestInit;
130130
private _authProvider?: OAuthClientProvider;
131131
private _fetch?: FetchLike;
132+
private _fetchWithInit: FetchLike;
132133
private _sessionId?: string;
133134
private _reconnectionOptions: StreamableHTTPReconnectionOptions;
134135
private _protocolVersion?: string;
@@ -145,6 +146,7 @@ export class StreamableHTTPClientTransport implements Transport {
145146
this._requestInit = opts?.requestInit;
146147
this._authProvider = opts?.authProvider;
147148
this._fetch = opts?.fetch;
149+
this._fetchWithInit = createFetchWithInit(opts?.fetch, opts?.requestInit);
148150
this._sessionId = opts?.sessionId;
149151
this._reconnectionOptions = opts?.reconnectionOptions ?? DEFAULT_STREAMABLE_HTTP_RECONNECTION_OPTIONS;
150152
}
@@ -156,14 +158,11 @@ export class StreamableHTTPClientTransport implements Transport {
156158

157159
let result: AuthResult;
158160
try {
159-
// Wrap fetch to automatically include base RequestInit options
160-
const fetchFn = createFetchWithInit(this._fetch, this._requestInit);
161-
162161
result = await auth(this._authProvider, {
163162
serverUrl: this._url,
164163
resourceMetadataUrl: this._resourceMetadataUrl,
165164
scope: this._scope,
166-
fetchFn
165+
fetchFn: this._fetchWithInit
167166
});
168167
} catch (error) {
169168
this.onerror?.(error as Error);
@@ -377,7 +376,7 @@ export class StreamableHTTPClientTransport implements Transport {
377376
authorizationCode,
378377
resourceMetadataUrl: this._resourceMetadataUrl,
379378
scope: this._scope,
380-
fetchFn: this._fetch
379+
fetchFn: this._fetchWithInit
381380
});
382381
if (result !== 'AUTHORIZED') {
383382
throw new UnauthorizedError('Failed to authorize');
@@ -441,7 +440,7 @@ export class StreamableHTTPClientTransport implements Transport {
441440
serverUrl: this._url,
442441
resourceMetadataUrl: this._resourceMetadataUrl,
443442
scope: this._scope,
444-
fetchFn: this._fetch
443+
fetchFn: this._fetchWithInit
445444
});
446445
if (result !== 'AUTHORIZED') {
447446
throw new UnauthorizedError();

0 commit comments

Comments
 (0)