Skip to content

Commit 340634a

Browse files
authored
feat: add requestHandler ctor pass through type (#1089)
* feat: add requestHandler ctor pass through type * move handler init types to @smithy/types * formatting
1 parent 0793a52 commit 340634a

File tree

6 files changed

+121
-50
lines changed

6 files changed

+121
-50
lines changed

.changeset/sixty-fireants-pay.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@smithy/fetch-http-handler": minor
3+
"@smithy/node-http-handler": minor
4+
"@smithy/types": minor
5+
---
6+
7+
move default fetch and http handler ctor types to the types package

packages/fetch-http-handler/src/fetch-http-handler.ts

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,13 @@
11
import { HttpHandler, HttpRequest, HttpResponse } from "@smithy/protocol-http";
22
import { buildQueryString } from "@smithy/querystring-builder";
3+
import type { FetchHttpHandlerOptions } from "@smithy/types";
34
import { HeaderBag, HttpHandlerOptions, Provider } from "@smithy/types";
45

56
import { requestTimeout } from "./request-timeout";
67

78
declare let AbortController: any;
89

9-
/**
10-
* Represents the http options that can be passed to a browser http client.
11-
*/
12-
export interface FetchHttpHandlerOptions {
13-
/**
14-
* The number of milliseconds a request can take before being automatically
15-
* terminated.
16-
*/
17-
requestTimeout?: number;
18-
19-
/**
20-
* Whether to allow the request to outlive the page. Default value is false.
21-
*
22-
* There may be limitations to the payload size, number of concurrent requests,
23-
* request duration etc. when using keepalive in browsers.
24-
*
25-
* These may change over time, so look for up to date information about
26-
* these limitations before enabling keepalive.
27-
*/
28-
keepAlive?: boolean;
29-
}
10+
export { FetchHttpHandlerOptions };
3011

3112
type FetchHttpHandlerConfig = FetchHttpHandlerOptions;
3213

@@ -47,6 +28,19 @@ export class FetchHttpHandler implements HttpHandler<FetchHttpHandlerConfig> {
4728
private config?: FetchHttpHandlerConfig;
4829
private configProvider: Promise<FetchHttpHandlerConfig>;
4930

31+
/**
32+
* @returns the input if it is an HttpHandler of any class,
33+
* or instantiates a new instance of this handler.
34+
*/
35+
public static create(instanceOrOptions?: HttpHandler<any> | FetchHttpHandlerConfig) {
36+
if (typeof (instanceOrOptions as any)?.handle === "function") {
37+
// is already an instance of HttpHandler.
38+
return instanceOrOptions as HttpHandler<any>;
39+
}
40+
// input is ctor options or undefined.
41+
return new FetchHttpHandler(instanceOrOptions as FetchHttpHandlerConfig);
42+
}
43+
5044
constructor(options?: FetchHttpHandlerOptions | Provider<FetchHttpHandlerOptions | undefined>) {
5145
if (typeof options === "function") {
5246
this.configProvider = options().then((opts) => opts || {});

packages/node-http-handler/src/node-http-handler.ts

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { HttpHandler, HttpRequest, HttpResponse } from "@smithy/protocol-http";
22
import { buildQueryString } from "@smithy/querystring-builder";
3+
import type { NodeHttpHandlerOptions } from "@smithy/types";
34
import { HttpHandlerOptions, Provider } from "@smithy/types";
45
import { Agent as hAgent, request as hRequest } from "http";
56
import { Agent as hsAgent, request as hsRequest, RequestOptions } from "https";
@@ -11,35 +12,7 @@ import { setSocketKeepAlive } from "./set-socket-keep-alive";
1112
import { setSocketTimeout } from "./set-socket-timeout";
1213
import { writeRequestBody } from "./write-request-body";
1314

14-
/**
15-
* Represents the http options that can be passed to a node http client.
16-
*/
17-
export interface NodeHttpHandlerOptions {
18-
/**
19-
* The maximum time in milliseconds that the connection phase of a request
20-
* may take before the connection attempt is abandoned.
21-
*
22-
* Defaults to 0, which disables the timeout.
23-
*/
24-
connectionTimeout?: number;
25-
26-
/**
27-
* The number of milliseconds a request can take before automatically being terminated.
28-
* Defaults to 0, which disables the timeout.
29-
*/
30-
requestTimeout?: number;
31-
32-
/**
33-
* @deprecated Use {@link requestTimeout}
34-
*
35-
* The maximum time in milliseconds that a socket may remain idle before it
36-
* is closed.
37-
*/
38-
socketTimeout?: number;
39-
40-
httpAgent?: hAgent;
41-
httpsAgent?: hsAgent;
42-
}
15+
export { NodeHttpHandlerOptions };
4316

4417
interface ResolvedNodeHttpHandlerConfig {
4518
requestTimeout?: number;
@@ -57,6 +30,19 @@ export class NodeHttpHandler implements HttpHandler<NodeHttpHandlerOptions> {
5730
// Node http handler is hard-coded to http/1.1: https://github.com/nodejs/node/blob/ff5664b83b89c55e4ab5d5f60068fb457f1f5872/lib/_http_server.js#L286
5831
public readonly metadata = { handlerProtocol: "http/1.1" };
5932

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(instanceOrOptions?: HttpHandler<any> | NodeHttpHandlerOptions) {
38+
if (typeof (instanceOrOptions as any)?.handle === "function") {
39+
// is already an instance of HttpHandler.
40+
return instanceOrOptions as HttpHandler<any>;
41+
}
42+
// input is ctor options or undefined.
43+
return new NodeHttpHandler(instanceOrOptions as NodeHttpHandlerOptions);
44+
}
45+
6046
constructor(options?: NodeHttpHandlerOptions | Provider<NodeHttpHandlerOptions | void>) {
6147
this.configProvider = new Promise((resolve, reject) => {
6248
if (typeof options === "function") {

packages/node-http-handler/src/node-http2-handler.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,19 @@ export class NodeHttp2Handler implements HttpHandler<NodeHttp2HandlerOptions> {
4949

5050
private readonly connectionManager: NodeHttp2ConnectionManager = new NodeHttp2ConnectionManager({});
5151

52+
/**
53+
* @returns the input if it is an HttpHandler of any class,
54+
* or instantiates a new instance of this handler.
55+
*/
56+
public static create(instanceOrOptions?: HttpHandler<any> | NodeHttp2HandlerOptions) {
57+
if (typeof (instanceOrOptions as any)?.handle === "function") {
58+
// is already an instance of HttpHandler.
59+
return instanceOrOptions as HttpHandler<any>;
60+
}
61+
// input is ctor options or undefined.
62+
return new NodeHttp2Handler(instanceOrOptions as NodeHttp2HandlerOptions);
63+
}
64+
5265
constructor(options?: NodeHttp2HandlerOptions | Provider<NodeHttp2HandlerOptions | void>) {
5366
this.configProvider = new Promise((resolve, reject) => {
5467
if (typeof options === "function") {
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import type { Agent as hAgent } from "http";
2+
import type { Agent as hsAgent } from "https";
3+
4+
/**
5+
*
6+
* This type represents an alternate client constructor option for the entry
7+
* "requestHandler". Instead of providing an instance of a requestHandler, the user
8+
* may provide the requestHandler's constructor options for either the
9+
* NodeHttpHandler or FetchHttpHandler.
10+
*
11+
* For other RequestHandlers like HTTP2 or WebSocket,
12+
* constructor parameter passthrough is not currently available.
13+
*
14+
* @public
15+
*/
16+
export type RequestHandlerParams = NodeHttpHandlerOptions | FetchHttpHandlerOptions;
17+
18+
/**
19+
* Represents the http options that can be passed to a node http client.
20+
* @public
21+
*/
22+
export interface NodeHttpHandlerOptions {
23+
/**
24+
* The maximum time in milliseconds that the connection phase of a request
25+
* may take before the connection attempt is abandoned.
26+
*
27+
* Defaults to 0, which disables the timeout.
28+
*/
29+
connectionTimeout?: number;
30+
31+
/**
32+
* The number of milliseconds a request can take before automatically being terminated.
33+
* Defaults to 0, which disables the timeout.
34+
*/
35+
requestTimeout?: number;
36+
37+
/**
38+
* @deprecated Use {@link requestTimeout}
39+
*
40+
* The maximum time in milliseconds that a socket may remain idle before it
41+
* is closed.
42+
*/
43+
socketTimeout?: number;
44+
45+
httpAgent?: hAgent;
46+
httpsAgent?: hsAgent;
47+
}
48+
49+
/**
50+
* Represents the http options that can be passed to a browser http client.
51+
* @public
52+
*/
53+
export interface FetchHttpHandlerOptions {
54+
/**
55+
* The number of milliseconds a request can take before being automatically
56+
* terminated.
57+
*/
58+
requestTimeout?: number;
59+
60+
/**
61+
* Whether to allow the request to outlive the page. Default value is false.
62+
*
63+
* There may be limitations to the payload size, number of concurrent requests,
64+
* request duration etc. when using keepalive in browsers.
65+
*
66+
* These may change over time, so look for up to date information about
67+
* these limitations before enabling keepalive.
68+
*/
69+
keepAlive?: boolean;
70+
}

packages/types/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export * from "./endpoints";
1212
export * from "./eventStream";
1313
export * from "./extensions";
1414
export * from "./http";
15+
export * from "./http/httpHandlerInitialization";
1516
export * from "./identity";
1617
export * from "./logger";
1718
export * from "./middleware";

0 commit comments

Comments
 (0)