Update streamableHttp custom fetch test
This commit is contained in:
parent
1b14bd7fa4
commit
e5167cbe20
1 changed files with 82 additions and 5 deletions
|
|
@ -3,6 +3,8 @@ import { Transport } from "../shared/transport.js";
|
|||
import { JSONRPCMessage, JSONRPCMessageSchema } from "../types.js";
|
||||
import { auth, AuthResult, extractResourceMetadataUrl, OAuthClientProvider, UnauthorizedError } from "./auth.js";
|
||||
|
||||
export type FetchLike = (url: string | URL, init?: RequestInit) => Promise<Response>;
|
||||
|
||||
export class SseError extends Error {
|
||||
constructor(
|
||||
public readonly code: number | undefined,
|
||||
|
|
@ -47,6 +49,11 @@ export type SSEClientTransportOptions = {
|
|||
* Customizes recurring POST requests to the server.
|
||||
*/
|
||||
requestInit?: RequestInit;
|
||||
|
||||
/**
|
||||
* Custom fetch implementation used for all network requests.
|
||||
*/
|
||||
fetch?: FetchLike;
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
@ -62,6 +69,7 @@ export class SSEClientTransport implements Transport {
|
|||
private _eventSourceInit?: EventSourceInit;
|
||||
private _requestInit?: RequestInit;
|
||||
private _authProvider?: OAuthClientProvider;
|
||||
private _fetch?: FetchLike;
|
||||
private _protocolVersion?: string;
|
||||
|
||||
onclose?: () => void;
|
||||
|
|
@ -77,6 +85,7 @@ export class SSEClientTransport implements Transport {
|
|||
this._eventSourceInit = opts?.eventSourceInit;
|
||||
this._requestInit = opts?.requestInit;
|
||||
this._authProvider = opts?.authProvider;
|
||||
this._fetch = opts?.fetch;
|
||||
}
|
||||
|
||||
private async _authThenStart(): Promise<void> {
|
||||
|
|
@ -117,7 +126,7 @@ export class SSEClientTransport implements Transport {
|
|||
}
|
||||
|
||||
private _startOrAuth(): Promise<void> {
|
||||
const fetchImpl = (this?._eventSourceInit?.fetch || fetch) as typeof fetch
|
||||
const fetchImpl = (this?._eventSourceInit?.fetch || this._fetch || fetch) as typeof fetch
|
||||
return new Promise((resolve, reject) => {
|
||||
this._eventSource = new EventSource(
|
||||
this._url.href,
|
||||
|
|
@ -242,7 +251,7 @@ export class SSEClientTransport implements Transport {
|
|||
signal: this._abortController?.signal,
|
||||
};
|
||||
|
||||
const response = await fetch(this._endpoint, init);
|
||||
const response = await (this._fetch ?? fetch)(this._endpoint, init);
|
||||
if (!response.ok) {
|
||||
if (response.status === 401 && this._authProvider) {
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue