Skip to content

Commit afedbc9

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

File tree

6 files changed

+85
-0
lines changed

6 files changed

+85
-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

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": "",

packages/protocol-http/src/httpHandler.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { HttpHandlerOptions, RequestHandler } from "@smithy/types";
22

3+
import { RequestHandlerParams } from "./httpHandlerInitialization";
34
import { HttpRequest } from "./httpRequest";
45
import { HttpResponse } from "./httpResponse";
56

@@ -23,3 +24,24 @@ export type HttpHandler<HttpHandlerConfig extends object = {}> = RequestHandler<
2324
*/
2425
httpHandlerConfigs(): HttpHandlerConfig;
2526
};
27+
28+
/**
29+
* @internal
30+
*
31+
* @param requestHandlerOrOptions - a custom implementation or options for the default factory.
32+
* @param factory - creates a new instance if requestHandlerOrOptions is not one.
33+
*
34+
* @returns a request handler instance provided by the requestHandlerOrOptions, constructor, or factory.
35+
*/
36+
export function makeHandler<HandlerType extends HttpHandler<RequestHandlerParams>>(
37+
requestHandlerOrOptions: RequestHandlerParams | HandlerType,
38+
factory: (options?: RequestHandlerParams) => HandlerType
39+
) {
40+
if (typeof (requestHandlerOrOptions as any)?.handle === "function") {
41+
// is already an instance of RequestHandler.
42+
return requestHandlerOrOptions as HandlerType;
43+
}
44+
45+
// input is ctor options or undefined, use the factory.
46+
return factory(requestHandlerOrOptions as RequestHandlerParams);
47+
}
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)