|
1 | 1 | import { formatUrl } from "@aws-sdk/util-format-url"; |
2 | 2 | import { iterableToReadableStream, readableStreamtoIterable } from "@smithy/eventstream-serde-browser"; |
3 | 3 | import { FetchHttpHandler } from "@smithy/fetch-http-handler"; |
4 | | -import { HttpRequest, HttpResponse } from "@smithy/protocol-http"; |
| 4 | +import { HttpHandler, HttpRequest, HttpResponse } from "@smithy/protocol-http"; |
5 | 5 | import { Provider, RequestHandler, RequestHandlerMetadata } from "@smithy/types"; |
6 | 6 |
|
7 | 7 | import { isWebSocketRequest } from "./utils"; |
@@ -30,6 +30,25 @@ export class WebSocketFetchHandler { |
30 | 30 | private readonly httpHandler: RequestHandler<any, any>; |
31 | 31 | private readonly sockets: Record<string, WebSocket[]> = {}; |
32 | 32 |
|
| 33 | + /** |
| 34 | + * @returns the input if it is an HttpHandler of any class, |
| 35 | + * or instantiates a new instance of this handler. |
| 36 | + */ |
| 37 | + public static create( |
| 38 | + instanceOrOptions?: HttpHandler<any> | WebSocketFetchHandlerOptions | Provider<WebSocketFetchHandlerOptions | void>, |
| 39 | + httpHandler: RequestHandler<any, any> = new FetchHttpHandler() |
| 40 | + ) { |
| 41 | + if (typeof (instanceOrOptions as any)?.handle === "function") { |
| 42 | + // is already an instance of HttpHandler. |
| 43 | + return instanceOrOptions as HttpHandler<any>; |
| 44 | + } |
| 45 | + // input is ctor options or undefined. |
| 46 | + return new WebSocketFetchHandler( |
| 47 | + instanceOrOptions as undefined | WebSocketFetchHandlerOptions | Provider<WebSocketFetchHandlerOptions>, |
| 48 | + httpHandler |
| 49 | + ); |
| 50 | + } |
| 51 | + |
33 | 52 | constructor( |
34 | 53 | options?: WebSocketFetchHandlerOptions | Provider<WebSocketFetchHandlerOptions>, |
35 | 54 | httpHandler: RequestHandler<any, any> = new FetchHttpHandler() |
|
0 commit comments