Skip to content

Commit 422db80

Browse files
gosarJordonPhillips
authored andcommitted
chore(codegen,config-resolver): refactor how endpoint is resolved for non-AWS client (aws#2287)
* chore(config-resolver): refactor EndpointsConfig for non AWS services This reverts an earlier change to EndpointsConfig.ts and instead provides the new functionality in separate CustomEndpointsConfig.ts. This will be used as a separate plugin for endpoint resolution in non AWS clients. * feat(codegen): Use separate CustomEndpointsConfig for non-AWS clients This depends on newly added CustomEndpointsConfig in @aws-sdk/config-resolver package.
1 parent 45d352d commit 422db80

File tree

4 files changed

+54
-6
lines changed

4 files changed

+54
-6
lines changed

codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AddBuiltinPlugins.java

+6
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,14 @@ public List<RuntimeClientPlugin> getClientPlugins() {
4747
.withConventions(TypeScriptDependency.CONFIG_RESOLVER.dependency, "Region", HAS_CONFIG)
4848
.servicePredicate((m, s) -> isAwsService(s))
4949
.build(),
50+
// Only one of Endpoints or CustomEndpoints should be used
5051
RuntimeClientPlugin.builder()
5152
.withConventions(TypeScriptDependency.CONFIG_RESOLVER.dependency, "Endpoints", HAS_CONFIG)
53+
.servicePredicate((m, s) -> isAwsService(s))
54+
.build(),
55+
RuntimeClientPlugin.builder()
56+
.withConventions(TypeScriptDependency.CONFIG_RESOLVER.dependency, "CustomEndpoints", HAS_CONFIG)
57+
.servicePredicate((m, s) -> !isAwsService(s))
5258
.build(),
5359
RuntimeClientPlugin.builder()
5460
.withConventions(TypeScriptDependency.MIDDLEWARE_RETRY.dependency, "Retry")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// TODO: Create a .spec.ts for this
2+
import { Endpoint, Provider, UrlParser } from "@aws-sdk/types";
3+
4+
export interface CustomEndpointsInputConfig {
5+
/**
6+
* The fully qualified endpoint of the webservice.
7+
*/
8+
endpoint: string | Endpoint | Provider<Endpoint>;
9+
10+
/**
11+
* Whether TLS is enabled for requests.
12+
*/
13+
tls?: boolean;
14+
}
15+
16+
interface PreviouslyResolved {
17+
urlParser: UrlParser;
18+
}
19+
20+
export interface CustomEndpointsResolvedConfig extends Required<CustomEndpointsInputConfig> {
21+
endpoint: Provider<Endpoint>;
22+
isCustomEndpoint: true; // TODO: Can this be removed or some other logic depends on this?
23+
}
24+
25+
export const resolveCustomEndpointsConfig = <T>(
26+
input: T & CustomEndpointsInputConfig & PreviouslyResolved
27+
): T & CustomEndpointsResolvedConfig => ({
28+
...input,
29+
tls: input.tls ?? true,
30+
endpoint: normalizeEndpoint(input),
31+
isCustomEndpoint: true,
32+
});
33+
34+
// TODO: can this be shared with EndpointsConfig.ts
35+
const normalizeEndpoint = (input: CustomEndpointsInputConfig & PreviouslyResolved): Provider<Endpoint> => {
36+
const { endpoint, urlParser } = input;
37+
if (typeof endpoint === "string") {
38+
const promisified = Promise.resolve(urlParser(endpoint));
39+
return () => promisified;
40+
} else if (typeof endpoint === "object") {
41+
const promisified = Promise.resolve(endpoint);
42+
return () => promisified;
43+
}
44+
return endpoint;
45+
};

packages/config-resolver/src/EndpointsConfig.ts

+2-6
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ export interface EndpointsInputConfig {
1313
}
1414

1515
interface PreviouslyResolved {
16-
regionInfoProvider?: RegionInfoProvider;
16+
regionInfoProvider: RegionInfoProvider;
1717
urlParser: UrlParser;
18-
region?: Provider<string>;
18+
region: Provider<string>;
1919
}
2020

2121
export interface EndpointsResolvedConfig extends Required<EndpointsInputConfig> {
@@ -45,10 +45,6 @@ const normalizeEndpoint = (input: EndpointsInputConfig & PreviouslyResolved): Pr
4545
};
4646

4747
const getEndPointFromRegion = async (input: EndpointsInputConfig & PreviouslyResolved) => {
48-
if (input.region === undefined || input.regionInfoProvider === undefined) {
49-
throw new Error("No endpoint specified and region is not defined");
50-
}
51-
5248
const { tls = true } = input;
5349
const region = await input.region();
5450

packages/config-resolver/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
export * from "./CustomEndpointsConfig";
12
export * from "./EndpointsConfig";
23
export * from "./RegionConfig";

0 commit comments

Comments
 (0)