Skip to content

Commit bc0e30d

Browse files
committed
feat: enable ctor arg passthrough for requestHandler
1 parent 1e23f96 commit bc0e30d

File tree

5 files changed

+68
-17
lines changed

5 files changed

+68
-17
lines changed

.changeset/three-paws-add.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@smithy/protocol-http": minor
3+
"@smithy/smithy-client": minor
4+
---
5+
6+
allow constructor parameters pass-through when initializing requestHandler

packages/protocol-http/src/httpHandler.ts

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1-
import { HttpHandlerOptions, RequestHandler } from "@smithy/types";
1+
import type {
2+
FetchHttpHandlerOptions,
3+
HttpHandlerOptions,
4+
NodeHttpHandlerOptions,
5+
RequestHandler,
6+
} from "@smithy/types";
27

3-
import { HttpRequest } from "./httpRequest";
4-
import { HttpResponse } from "./httpResponse";
8+
import type { HttpRequest } from "./httpRequest";
9+
import type { HttpResponse } from "./httpResponse";
510

611
/**
712
* @internal
@@ -23,3 +28,21 @@ export type HttpHandler<HttpHandlerConfig extends object = {}> = RequestHandler<
2328
*/
2429
httpHandlerConfigs(): HttpHandlerConfig;
2530
};
31+
32+
/**
33+
* @public
34+
*
35+
* A type representing the accepted user inputs for the `requestHandler` field
36+
* of a client's constructor object.
37+
*
38+
* You may provide an instance of an HttpHandler, or alternatively
39+
* provide the constructor arguments as an object which will be passed
40+
* to the constructor of the default request handler.
41+
*
42+
* The default class constructor to which your arguments will be passed
43+
* varies. The Node.js default is the NodeHttpHandler and the browser/react-native
44+
* default is the FetchHttpHandler. In rarer cases specific clients may be
45+
* configured to use other default implementations such as Websocket or HTTP2.
46+
*
47+
*/
48+
export type HttpHandlerUserInput = HttpHandler | NodeHttpHandlerOptions | FetchHttpHandlerOptions;

packages/smithy-client/src/client.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
import { constructStack } from "@smithy/middleware-stack";
2-
import { Client as IClient, Command, MetadataBearer, MiddlewareStack, RequestHandler } from "@smithy/types";
2+
import {
3+
Client as IClient,
4+
Command,
5+
FetchHttpHandlerOptions,
6+
MetadataBearer,
7+
MiddlewareStack,
8+
NodeHttpHandlerOptions,
9+
RequestHandler,
10+
} from "@smithy/types";
311

412
/**
513
* @public
614
*/
715
export interface SmithyConfiguration<HandlerOptions> {
8-
requestHandler: RequestHandler<any, any, HandlerOptions>;
16+
requestHandler: RequestHandler<any, any, HandlerOptions> | NodeHttpHandlerOptions | FetchHttpHandlerOptions | any;
917
/**
1018
* The API version set internally by the SDK, and is
1119
* not planned to be used by customer code.
@@ -17,7 +25,10 @@ export interface SmithyConfiguration<HandlerOptions> {
1725
/**
1826
* @internal
1927
*/
20-
export type SmithyResolvedConfiguration<HandlerOptions> = SmithyConfiguration<HandlerOptions>;
28+
export type SmithyResolvedConfiguration<HandlerOptions> = {
29+
requestHandler: RequestHandler<any, any, HandlerOptions>;
30+
readonly apiVersion: string;
31+
};
2132

2233
/**
2334
* @public

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/RuntimeConfigGenerator.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ final class RuntimeConfigGenerator {
5858
writer.addDependency(TypeScriptDependency.AWS_SDK_NODE_HTTP_HANDLER);
5959
writer.addImport("NodeHttpHandler", "RequestHandler",
6060
TypeScriptDependency.AWS_SDK_NODE_HTTP_HANDLER);
61-
writer.write("new RequestHandler(defaultConfigProvider)");
61+
writer.write("RequestHandler.create(config.requestHandler ?? defaultConfigProvider)");
6262
},
6363
"sha256", writer -> {
6464
writer.addDependency(TypeScriptDependency.AWS_SDK_HASH_NODE);
@@ -81,7 +81,7 @@ final class RuntimeConfigGenerator {
8181
writer.addDependency(TypeScriptDependency.AWS_SDK_FETCH_HTTP_HANDLER);
8282
writer.addImport("FetchHttpHandler", "RequestHandler",
8383
TypeScriptDependency.AWS_SDK_FETCH_HTTP_HANDLER);
84-
writer.write("new RequestHandler(defaultConfigProvider)");
84+
writer.write("RequestHandler.create(config.requestHandler ?? defaultConfigProvider)");
8585
},
8686
"sha256", writer -> {
8787
writer.addDependency(TypeScriptDependency.AWS_CRYPTO_SHA256_BROWSER);
@@ -197,11 +197,20 @@ void generate(LanguageTarget target) {
197197
}
198198
int indentation = target.equals(LanguageTarget.SHARED) ? 1 : 2;
199199
configs.forEach((key, value) -> {
200-
writer.indent(indentation).disableNewlines().openBlock("$1L: config?.$1L ?? ", ",\n", key,
201-
() -> {
202-
value.accept(writer);
203-
});
204-
writer.dedent(indentation);
200+
String defaultPrefix = "config?.$1L ?? ";
201+
if (key.equals("requestHandler")) {
202+
defaultPrefix = "";
203+
}
204+
writer
205+
.indent(indentation)
206+
.disableNewlines()
207+
.openBlock(
208+
"$1L: " + defaultPrefix, ",\n", key,
209+
() -> {
210+
value.accept(writer);
211+
}
212+
)
213+
.dedent(indentation);
205214
});
206215
});
207216
writer.dedent();

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/ServiceBareBonesClientGenerator.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -253,11 +253,13 @@ private void generateClientDefaults() {
253253

254254
writer.writeDocs("@public")
255255
.openBlock("export interface ClientDefaults\n"
256-
+ " extends Partial<__SmithyResolvedConfiguration<$T>> {", "}",
256+
+ " extends Partial<__SmithyConfiguration<$T>> {", "}",
257257
applicationProtocol.getOptionsType(), () -> {
258-
writer.addImport("HttpHandler", "__HttpHandler", TypeScriptDependency.PROTOCOL_HTTP);
259-
writer.writeDocs("The HTTP handler to use. Fetch in browser and Https in Nodejs.");
260-
writer.write("requestHandler?: __HttpHandler;\n");
258+
writer.addImport("HttpHandlerUserInput", "__HttpHandlerUserInput", TypeScriptDependency.PROTOCOL_HTTP);
259+
writer.writeDocs(
260+
"The HTTP handler to use or its constructor options. Fetch in browser and Https in Nodejs."
261+
);
262+
writer.write("requestHandler?: __HttpHandlerUserInput;\n");
261263

262264
writer.addImport("HashConstructor", "__HashConstructor", TypeScriptDependency.SMITHY_TYPES);
263265
writer.addImport("ChecksumConstructor", "__ChecksumConstructor", TypeScriptDependency.SMITHY_TYPES);

0 commit comments

Comments
 (0)