Skip to content

Commit abea341

Browse files
committed
feat: add requestHandler ctor pass through type
1 parent 07ff207 commit abea341

File tree

9 files changed

+109
-0
lines changed

9 files changed

+109
-0
lines changed

.changeset/dry-balloons-smell.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@smithy/protocol-http": minor
3+
---
4+
5+
add RequestHandlerParams type for ctor passthrough

.changeset/many-ads-float.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/protocol-http": minor
5+
---
6+
7+
add static factory for http handlers

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,19 @@ export class FetchHttpHandler implements HttpHandler<FetchHttpHandlerConfig> {
4747
private config?: FetchHttpHandlerConfig;
4848
private configProvider: Promise<FetchHttpHandlerConfig>;
4949

50+
/**
51+
* @returns the input if it is an HttpHandler of any class,
52+
* or instantiates a new instance of this handler.
53+
*/
54+
public static create(instanceOrOptions?: HttpHandler<any> | FetchHttpHandlerConfig) {
55+
if (typeof (instanceOrOptions as any)?.handle === "function") {
56+
// is already an instance of HttpHandler.
57+
return instanceOrOptions as HttpHandler<any>;
58+
}
59+
// input is ctor options or undefined.
60+
return new FetchHttpHandler(instanceOrOptions as FetchHttpHandlerConfig);
61+
}
62+
5063
constructor(options?: FetchHttpHandlerOptions | Provider<FetchHttpHandlerOptions | undefined>) {
5164
if (typeof options === "function") {
5265
this.configProvider = options().then((opts) => opts || {});

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,19 @@ export class NodeHttpHandler implements HttpHandler<NodeHttpHandlerOptions> {
5757
// Node http handler is hard-coded to http/1.1: https://github.com/nodejs/node/blob/ff5664b83b89c55e4ab5d5f60068fb457f1f5872/lib/_http_server.js#L286
5858
public readonly metadata = { handlerProtocol: "http/1.1" };
5959

60+
/**
61+
* @returns the input if it is an HttpHandler of any class,
62+
* or instantiates a new instance of this handler.
63+
*/
64+
public static create(instanceOrOptions?: HttpHandler<any> | NodeHttpHandlerOptions) {
65+
if (typeof (instanceOrOptions as any)?.handle === "function") {
66+
// is already an instance of HttpHandler.
67+
return instanceOrOptions as HttpHandler<any>;
68+
}
69+
// input is ctor options or undefined.
70+
return new NodeHttpHandler(instanceOrOptions as NodeHttpHandlerOptions);
71+
}
72+
6073
constructor(options?: NodeHttpHandlerOptions | Provider<NodeHttpHandlerOptions | void>) {
6174
this.configProvider = new Promise((resolve, reject) => {
6275
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") {

packages/protocol-http/package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@
1616
"main": "./dist-cjs/index.js",
1717
"module": "./dist-es/index.js",
1818
"types": "./dist-types/index.d.ts",
19+
"browser": {
20+
"./dist-es/httpHandlerInitialization": "./dist-es/httpHandlerInitialization.browser"
21+
},
22+
"react-native": {
23+
"./dist-es/httpHandlerInitialization": "./dist-es/httpHandlerInitialization.browser",
24+
"./dist-cjs/httpHandlerInitialization": "./dist-cjs/httpHandlerInitialization.browser"
25+
},
1926
"author": {
2027
"name": "AWS Smithy Team",
2128
"email": "",
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Alternate compilation for browser excluding http Agent types.
3+
*
4+
* @internal
5+
*/
6+
export type DefaultRequestHandlerInitializationPassThroughParameters = FetchHttpHandlerOptions;
7+
8+
/**
9+
* Copy of the options in @smithy/fetch-http-handler FetchHttpHandler class.
10+
* Not imported due to dependency ordering.
11+
*/
12+
interface FetchHttpHandlerOptions {
13+
requestTimeout?: number;
14+
keepAlive?: boolean;
15+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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, constructor parameter passthrough is not available.
12+
*
13+
* @public
14+
*/
15+
export type RequestHandlerParams = NodeHttpHandlerOptions | FetchHttpHandlerOptions;
16+
17+
/**
18+
* Copy of the options in @smithy/node-http-handler NodeHttpHandler class.
19+
* Not imported due to dependency ordering.
20+
*/
21+
export interface NodeHttpHandlerOptions {
22+
connectionTimeout?: number;
23+
requestTimeout?: number;
24+
httpAgent?: hAgent;
25+
httpsAgent?: hsAgent;
26+
}
27+
28+
/**
29+
* Copy of the options in @smithy/fetch-http-handler FetchHttpHandler class.
30+
* Not imported due to dependency ordering.
31+
*/
32+
export interface FetchHttpHandlerOptions {
33+
requestTimeout?: number;
34+
keepAlive?: boolean;
35+
}

packages/protocol-http/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export * from "./extensions";
22
export * from "./Field";
33
export * from "./Fields";
44
export * from "./httpHandler";
5+
export * from "./httpHandlerInitialization";
56
export * from "./httpRequest";
67
export * from "./httpResponse";
78
export * from "./isValidHostname";

0 commit comments

Comments
 (0)