Skip to content

Commit 37a8fa9

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 4005a0e commit 37a8fa9

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);
@@ -374,7 +373,7 @@ export class StreamableHTTPClientTransport implements Transport {
374373
authorizationCode,
375374
resourceMetadataUrl: this._resourceMetadataUrl,
376375
scope: this._scope,
377-
fetchFn: this._fetch
376+
fetchFn: this._fetchWithInit
378377
});
379378
if (result !== 'AUTHORIZED') {
380379
throw new UnauthorizedError('Failed to authorize');
@@ -438,7 +437,7 @@ export class StreamableHTTPClientTransport implements Transport {
438437
serverUrl: this._url,
439438
resourceMetadataUrl: this._resourceMetadataUrl,
440439
scope: this._scope,
441-
fetchFn: this._fetch
440+
fetchFn: this._fetchWithInit
442441
});
443442
if (result !== 'AUTHORIZED') {
444443
throw new UnauthorizedError();

0 commit comments

Comments
 (0)