diff --git a/.changeset/ten-islands-itch.md b/.changeset/ten-islands-itch.md new file mode 100644 index 00000000000..c096f13edc2 --- /dev/null +++ b/.changeset/ten-islands-itch.md @@ -0,0 +1,6 @@ +--- +"@smithy/middleware-endpoint": patch +"@smithy/middleware-serde": patch +--- + +reduce usage of endpoints2.0 type adapter in public interfaces diff --git a/packages/middleware-endpoint/src/resolveEndpointConfig.ts b/packages/middleware-endpoint/src/resolveEndpointConfig.ts index 5935fad1c3d..9f380e556ec 100644 --- a/packages/middleware-endpoint/src/resolveEndpointConfig.ts +++ b/packages/middleware-endpoint/src/resolveEndpointConfig.ts @@ -56,7 +56,6 @@ export interface EndpointInputConfig { urlParser: UrlParser; - region: Provider; endpointProvider: (params: T, context?: { logger?: Logger }) => EndpointV2; logger?: Logger; serviceId?: string; diff --git a/packages/middleware-serde/src/serdePlugin.ts b/packages/middleware-serde/src/serdePlugin.ts index 2afe0d16694..cda2369799c 100644 --- a/packages/middleware-serde/src/serdePlugin.ts +++ b/packages/middleware-serde/src/serdePlugin.ts @@ -30,8 +30,12 @@ export const serializerMiddlewareOption: SerializeHandlerOptions = { override: true, }; -// Type the modifies the EndpointBearer to make it compatible with Endpoints 2.0 change. -// Must be removed after all clients has been onboard the Endpoints 2.0 +/** + * Modifies the EndpointBearer to make it compatible with Endpoints 2.0 change. + * + * @internal + * @deprecated + */ export type V1OrV2Endpoint = { // for v2 urlParser?: UrlParser; @@ -49,7 +53,7 @@ export function getSerdePlugin< CommandSerdeContext extends SerdeContext = any, OutputType extends MetadataBearer = any, >( - config: V1OrV2Endpoint & SerdeFunctions, + config: SerdeFunctions, serializer: RequestSerializer, deserializer: ResponseDeserializer ): Pluggable { diff --git a/packages/middleware-serde/src/serializerMiddleware.ts b/packages/middleware-serde/src/serializerMiddleware.ts index aed695760d4..ce8aea725dc 100644 --- a/packages/middleware-serde/src/serializerMiddleware.ts +++ b/packages/middleware-serde/src/serializerMiddleware.ts @@ -18,15 +18,17 @@ import type { V1OrV2Endpoint } from "./serdePlugin"; */ export const serializerMiddleware = ( - options: V1OrV2Endpoint & SerdeFunctions, + options: SerdeFunctions, serializer: RequestSerializer ): SerializeMiddleware => (next: SerializeHandler, context: HandlerExecutionContext): SerializeHandler => async (args: SerializeHandlerArguments): Promise> => { + const endpointConfig = options as V1OrV2Endpoint; + const endpoint: Provider = - context.endpointV2?.url && options.urlParser - ? async () => options.urlParser!(context.endpointV2!.url as URL) - : options.endpoint!; + context.endpointV2?.url && endpointConfig.urlParser + ? async () => endpointConfig.urlParser!(context.endpointV2!.url as URL) + : endpointConfig.endpoint!; if (!endpoint) { throw new Error("No valid endpoint provider available."); diff --git a/private/smithy-rpcv2-cbor/package.json b/private/smithy-rpcv2-cbor/package.json index a43d2fde771..bd315554e60 100644 --- a/private/smithy-rpcv2-cbor/package.json +++ b/private/smithy-rpcv2-cbor/package.json @@ -27,6 +27,7 @@ "@smithy/hash-node": "workspace:^", "@smithy/invalid-dependency": "workspace:^", "@smithy/middleware-content-length": "workspace:^", + "@smithy/middleware-endpoint": "workspace:^", "@smithy/middleware-retry": "workspace:^", "@smithy/middleware-serde": "workspace:^", "@smithy/middleware-stack": "workspace:^", @@ -41,6 +42,7 @@ "@smithy/util-body-length-node": "workspace:^", "@smithy/util-defaults-mode-browser": "workspace:^", "@smithy/util-defaults-mode-node": "workspace:^", + "@smithy/util-endpoints": "workspace:^", "@smithy/util-middleware": "workspace:^", "@smithy/util-retry": "workspace:^", "@smithy/util-utf8": "workspace:^", diff --git a/private/smithy-rpcv2-cbor/src/RpcV2ProtocolClient.ts b/private/smithy-rpcv2-cbor/src/RpcV2ProtocolClient.ts index 05e3283f2ea..bb39b2df6ea 100644 --- a/private/smithy-rpcv2-cbor/src/RpcV2ProtocolClient.ts +++ b/private/smithy-rpcv2-cbor/src/RpcV2ProtocolClient.ts @@ -33,15 +33,21 @@ import { SparseNullsOperationCommandInput, SparseNullsOperationCommandOutput, } from "./commands/SparseNullsOperationCommand"; +import { + ClientInputEndpointParameters, + ClientResolvedEndpointParameters, + EndpointParameters, + resolveClientEndpointParameters, +} from "./endpoint/EndpointParameters"; import { getRuntimeConfig as __getRuntimeConfig } from "./runtimeConfig"; import { RuntimeExtension, RuntimeExtensionsConfig, resolveRuntimeExtensions } from "./runtimeExtensions"; import { - CustomEndpointsInputConfig, - CustomEndpointsResolvedConfig, - resolveCustomEndpointsConfig, -} from "@smithy/config-resolver"; -import { DefaultIdentityProviderConfig, getHttpAuthSchemePlugin, getHttpSigningPlugin } from "@smithy/core"; + DefaultIdentityProviderConfig, + getHttpAuthSchemeEndpointRuleSetPlugin, + getHttpSigningPlugin, +} from "@smithy/core"; import { getContentLengthPlugin } from "@smithy/middleware-content-length"; +import { EndpointInputConfig, EndpointResolvedConfig, resolveEndpointConfig } from "@smithy/middleware-endpoint"; import { RetryInputConfig, RetryResolvedConfig, getRetryPlugin, resolveRetryConfig } from "@smithy/middleware-retry"; import { HttpHandlerUserInput as __HttpHandlerUserInput } from "@smithy/protocol-http"; import { @@ -205,9 +211,10 @@ export interface ClientDefaults extends Partial<__SmithyConfiguration<__HttpHand */ export type RpcV2ProtocolClientConfigType = Partial<__SmithyConfiguration<__HttpHandlerOptions>> & ClientDefaults & - CustomEndpointsInputConfig & RetryInputConfig & - HttpAuthSchemeInputConfig; + EndpointInputConfig & + HttpAuthSchemeInputConfig & + ClientInputEndpointParameters; /** * @public * @@ -221,9 +228,10 @@ export interface RpcV2ProtocolClientConfig extends RpcV2ProtocolClientConfigType export type RpcV2ProtocolClientResolvedConfigType = __SmithyResolvedConfiguration<__HttpHandlerOptions> & Required & RuntimeExtensionsConfig & - CustomEndpointsResolvedConfig & RetryResolvedConfig & - HttpAuthSchemeResolvedConfig; + EndpointResolvedConfig & + HttpAuthSchemeResolvedConfig & + ClientResolvedEndpointParameters; /** * @public * @@ -249,15 +257,16 @@ export class RpcV2ProtocolClient extends __Client< let _config_0 = __getRuntimeConfig(configuration || {}); super(_config_0 as any); this.initConfig = _config_0; - let _config_1 = resolveCustomEndpointsConfig(_config_0); + let _config_1 = resolveClientEndpointParameters(_config_0); let _config_2 = resolveRetryConfig(_config_1); - let _config_3 = resolveHttpAuthSchemeConfig(_config_2); - let _config_4 = resolveRuntimeExtensions(_config_3, configuration?.extensions || []); - this.config = _config_4; + let _config_3 = resolveEndpointConfig(_config_2); + let _config_4 = resolveHttpAuthSchemeConfig(_config_3); + let _config_5 = resolveRuntimeExtensions(_config_4, configuration?.extensions || []); + this.config = _config_5; this.middlewareStack.use(getRetryPlugin(this.config)); this.middlewareStack.use(getContentLengthPlugin(this.config)); this.middlewareStack.use( - getHttpAuthSchemePlugin(this.config, { + getHttpAuthSchemeEndpointRuleSetPlugin(this.config, { httpAuthSchemeParametersProvider: defaultRpcV2ProtocolHttpAuthSchemeParametersProvider, identityProviderConfigProvider: async (config: RpcV2ProtocolClientResolvedConfig) => new DefaultIdentityProviderConfig({}), diff --git a/private/smithy-rpcv2-cbor/src/commands/EmptyInputOutputCommand.ts b/private/smithy-rpcv2-cbor/src/commands/EmptyInputOutputCommand.ts index 818184736b8..90b316c817d 100644 --- a/private/smithy-rpcv2-cbor/src/commands/EmptyInputOutputCommand.ts +++ b/private/smithy-rpcv2-cbor/src/commands/EmptyInputOutputCommand.ts @@ -1,7 +1,9 @@ // smithy-typescript generated code import { RpcV2ProtocolClientResolvedConfig, ServiceInputTypes, ServiceOutputTypes } from "../RpcV2ProtocolClient"; +import { commonParams } from "../endpoint/EndpointParameters"; import { EmptyStructure } from "../models/models_0"; import { de_EmptyInputOutputCommand, se_EmptyInputOutputCommand } from "../protocols/Rpcv2cbor"; +import { getEndpointPlugin } from "@smithy/middleware-endpoint"; import { getSerdePlugin } from "@smithy/middleware-serde"; import { Command as $Command } from "@smithy/smithy-client"; import { MetadataBearer as __MetadataBearer } from "@smithy/types"; @@ -59,8 +61,12 @@ export class EmptyInputOutputCommand extends $Command ServiceInputTypes, ServiceOutputTypes >() + .ep(commonParams) .m(function (this: any, Command: any, cs: any, config: RpcV2ProtocolClientResolvedConfig, o: any) { - return [getSerdePlugin(config, this.serialize, this.deserialize)]; + return [ + getSerdePlugin(config, this.serialize, this.deserialize), + getEndpointPlugin(config, Command.getEndpointParameterInstructions()), + ]; }) .s("RpcV2Protocol", "EmptyInputOutput", {}) .n("RpcV2ProtocolClient", "EmptyInputOutputCommand") diff --git a/private/smithy-rpcv2-cbor/src/commands/Float16Command.ts b/private/smithy-rpcv2-cbor/src/commands/Float16Command.ts index fde2ec6d78f..1ac8a100af8 100644 --- a/private/smithy-rpcv2-cbor/src/commands/Float16Command.ts +++ b/private/smithy-rpcv2-cbor/src/commands/Float16Command.ts @@ -1,7 +1,9 @@ // smithy-typescript generated code import { RpcV2ProtocolClientResolvedConfig, ServiceInputTypes, ServiceOutputTypes } from "../RpcV2ProtocolClient"; +import { commonParams } from "../endpoint/EndpointParameters"; import { Float16Output } from "../models/models_0"; import { de_Float16Command, se_Float16Command } from "../protocols/Rpcv2cbor"; +import { getEndpointPlugin } from "@smithy/middleware-endpoint"; import { getSerdePlugin } from "@smithy/middleware-serde"; import { Command as $Command } from "@smithy/smithy-client"; import { MetadataBearer as __MetadataBearer } from "@smithy/types"; @@ -61,8 +63,12 @@ export class Float16Command extends $Command ServiceInputTypes, ServiceOutputTypes >() + .ep(commonParams) .m(function (this: any, Command: any, cs: any, config: RpcV2ProtocolClientResolvedConfig, o: any) { - return [getSerdePlugin(config, this.serialize, this.deserialize)]; + return [ + getSerdePlugin(config, this.serialize, this.deserialize), + getEndpointPlugin(config, Command.getEndpointParameterInstructions()), + ]; }) .s("RpcV2Protocol", "Float16", {}) .n("RpcV2ProtocolClient", "Float16Command") diff --git a/private/smithy-rpcv2-cbor/src/commands/FractionalSecondsCommand.ts b/private/smithy-rpcv2-cbor/src/commands/FractionalSecondsCommand.ts index df0aefff431..6265a0b7652 100644 --- a/private/smithy-rpcv2-cbor/src/commands/FractionalSecondsCommand.ts +++ b/private/smithy-rpcv2-cbor/src/commands/FractionalSecondsCommand.ts @@ -1,7 +1,9 @@ // smithy-typescript generated code import { RpcV2ProtocolClientResolvedConfig, ServiceInputTypes, ServiceOutputTypes } from "../RpcV2ProtocolClient"; +import { commonParams } from "../endpoint/EndpointParameters"; import { FractionalSecondsOutput } from "../models/models_0"; import { de_FractionalSecondsCommand, se_FractionalSecondsCommand } from "../protocols/Rpcv2cbor"; +import { getEndpointPlugin } from "@smithy/middleware-endpoint"; import { getSerdePlugin } from "@smithy/middleware-serde"; import { Command as $Command } from "@smithy/smithy-client"; import { MetadataBearer as __MetadataBearer } from "@smithy/types"; @@ -61,8 +63,12 @@ export class FractionalSecondsCommand extends $Command ServiceInputTypes, ServiceOutputTypes >() + .ep(commonParams) .m(function (this: any, Command: any, cs: any, config: RpcV2ProtocolClientResolvedConfig, o: any) { - return [getSerdePlugin(config, this.serialize, this.deserialize)]; + return [ + getSerdePlugin(config, this.serialize, this.deserialize), + getEndpointPlugin(config, Command.getEndpointParameterInstructions()), + ]; }) .s("RpcV2Protocol", "FractionalSeconds", {}) .n("RpcV2ProtocolClient", "FractionalSecondsCommand") diff --git a/private/smithy-rpcv2-cbor/src/commands/GreetingWithErrorsCommand.ts b/private/smithy-rpcv2-cbor/src/commands/GreetingWithErrorsCommand.ts index 3baab0929dd..bba02e401bc 100644 --- a/private/smithy-rpcv2-cbor/src/commands/GreetingWithErrorsCommand.ts +++ b/private/smithy-rpcv2-cbor/src/commands/GreetingWithErrorsCommand.ts @@ -1,7 +1,9 @@ // smithy-typescript generated code import { RpcV2ProtocolClientResolvedConfig, ServiceInputTypes, ServiceOutputTypes } from "../RpcV2ProtocolClient"; +import { commonParams } from "../endpoint/EndpointParameters"; import { GreetingWithErrorsOutput } from "../models/models_0"; import { de_GreetingWithErrorsCommand, se_GreetingWithErrorsCommand } from "../protocols/Rpcv2cbor"; +import { getEndpointPlugin } from "@smithy/middleware-endpoint"; import { getSerdePlugin } from "@smithy/middleware-serde"; import { Command as $Command } from "@smithy/smithy-client"; import { MetadataBearer as __MetadataBearer } from "@smithy/types"; @@ -74,8 +76,12 @@ export class GreetingWithErrorsCommand extends $Command ServiceInputTypes, ServiceOutputTypes >() + .ep(commonParams) .m(function (this: any, Command: any, cs: any, config: RpcV2ProtocolClientResolvedConfig, o: any) { - return [getSerdePlugin(config, this.serialize, this.deserialize)]; + return [ + getSerdePlugin(config, this.serialize, this.deserialize), + getEndpointPlugin(config, Command.getEndpointParameterInstructions()), + ]; }) .s("RpcV2Protocol", "GreetingWithErrors", {}) .n("RpcV2ProtocolClient", "GreetingWithErrorsCommand") diff --git a/private/smithy-rpcv2-cbor/src/commands/NoInputOutputCommand.ts b/private/smithy-rpcv2-cbor/src/commands/NoInputOutputCommand.ts index a71d4b936f5..ffa740733dd 100644 --- a/private/smithy-rpcv2-cbor/src/commands/NoInputOutputCommand.ts +++ b/private/smithy-rpcv2-cbor/src/commands/NoInputOutputCommand.ts @@ -1,6 +1,8 @@ // smithy-typescript generated code import { RpcV2ProtocolClientResolvedConfig, ServiceInputTypes, ServiceOutputTypes } from "../RpcV2ProtocolClient"; +import { commonParams } from "../endpoint/EndpointParameters"; import { de_NoInputOutputCommand, se_NoInputOutputCommand } from "../protocols/Rpcv2cbor"; +import { getEndpointPlugin } from "@smithy/middleware-endpoint"; import { getSerdePlugin } from "@smithy/middleware-serde"; import { Command as $Command } from "@smithy/smithy-client"; import { MetadataBearer as __MetadataBearer } from "@smithy/types"; @@ -58,8 +60,12 @@ export class NoInputOutputCommand extends $Command ServiceInputTypes, ServiceOutputTypes >() + .ep(commonParams) .m(function (this: any, Command: any, cs: any, config: RpcV2ProtocolClientResolvedConfig, o: any) { - return [getSerdePlugin(config, this.serialize, this.deserialize)]; + return [ + getSerdePlugin(config, this.serialize, this.deserialize), + getEndpointPlugin(config, Command.getEndpointParameterInstructions()), + ]; }) .s("RpcV2Protocol", "NoInputOutput", {}) .n("RpcV2ProtocolClient", "NoInputOutputCommand") diff --git a/private/smithy-rpcv2-cbor/src/commands/OperationWithDefaultsCommand.ts b/private/smithy-rpcv2-cbor/src/commands/OperationWithDefaultsCommand.ts index 62d2fd3f9c6..64b16e73d28 100644 --- a/private/smithy-rpcv2-cbor/src/commands/OperationWithDefaultsCommand.ts +++ b/private/smithy-rpcv2-cbor/src/commands/OperationWithDefaultsCommand.ts @@ -1,7 +1,9 @@ // smithy-typescript generated code import { RpcV2ProtocolClientResolvedConfig, ServiceInputTypes, ServiceOutputTypes } from "../RpcV2ProtocolClient"; +import { commonParams } from "../endpoint/EndpointParameters"; import { OperationWithDefaultsInput, OperationWithDefaultsOutput } from "../models/models_0"; import { de_OperationWithDefaultsCommand, se_OperationWithDefaultsCommand } from "../protocols/Rpcv2cbor"; +import { getEndpointPlugin } from "@smithy/middleware-endpoint"; import { getSerdePlugin } from "@smithy/middleware-serde"; import { Command as $Command } from "@smithy/smithy-client"; import { MetadataBearer as __MetadataBearer } from "@smithy/types"; @@ -127,8 +129,12 @@ export class OperationWithDefaultsCommand extends $Command ServiceInputTypes, ServiceOutputTypes >() + .ep(commonParams) .m(function (this: any, Command: any, cs: any, config: RpcV2ProtocolClientResolvedConfig, o: any) { - return [getSerdePlugin(config, this.serialize, this.deserialize)]; + return [ + getSerdePlugin(config, this.serialize, this.deserialize), + getEndpointPlugin(config, Command.getEndpointParameterInstructions()), + ]; }) .s("RpcV2Protocol", "OperationWithDefaults", {}) .n("RpcV2ProtocolClient", "OperationWithDefaultsCommand") diff --git a/private/smithy-rpcv2-cbor/src/commands/OptionalInputOutputCommand.ts b/private/smithy-rpcv2-cbor/src/commands/OptionalInputOutputCommand.ts index 8463c19135c..641abb1f8e0 100644 --- a/private/smithy-rpcv2-cbor/src/commands/OptionalInputOutputCommand.ts +++ b/private/smithy-rpcv2-cbor/src/commands/OptionalInputOutputCommand.ts @@ -1,7 +1,9 @@ // smithy-typescript generated code import { RpcV2ProtocolClientResolvedConfig, ServiceInputTypes, ServiceOutputTypes } from "../RpcV2ProtocolClient"; +import { commonParams } from "../endpoint/EndpointParameters"; import { SimpleStructure } from "../models/models_0"; import { de_OptionalInputOutputCommand, se_OptionalInputOutputCommand } from "../protocols/Rpcv2cbor"; +import { getEndpointPlugin } from "@smithy/middleware-endpoint"; import { getSerdePlugin } from "@smithy/middleware-serde"; import { Command as $Command } from "@smithy/smithy-client"; import { MetadataBearer as __MetadataBearer } from "@smithy/types"; @@ -63,8 +65,12 @@ export class OptionalInputOutputCommand extends $Command ServiceInputTypes, ServiceOutputTypes >() + .ep(commonParams) .m(function (this: any, Command: any, cs: any, config: RpcV2ProtocolClientResolvedConfig, o: any) { - return [getSerdePlugin(config, this.serialize, this.deserialize)]; + return [ + getSerdePlugin(config, this.serialize, this.deserialize), + getEndpointPlugin(config, Command.getEndpointParameterInstructions()), + ]; }) .s("RpcV2Protocol", "OptionalInputOutput", {}) .n("RpcV2ProtocolClient", "OptionalInputOutputCommand") diff --git a/private/smithy-rpcv2-cbor/src/commands/RecursiveShapesCommand.ts b/private/smithy-rpcv2-cbor/src/commands/RecursiveShapesCommand.ts index 1cb74b766ae..3b27d4a74fc 100644 --- a/private/smithy-rpcv2-cbor/src/commands/RecursiveShapesCommand.ts +++ b/private/smithy-rpcv2-cbor/src/commands/RecursiveShapesCommand.ts @@ -1,7 +1,9 @@ // smithy-typescript generated code import { RpcV2ProtocolClientResolvedConfig, ServiceInputTypes, ServiceOutputTypes } from "../RpcV2ProtocolClient"; +import { commonParams } from "../endpoint/EndpointParameters"; import { RecursiveShapesInputOutput } from "../models/models_0"; import { de_RecursiveShapesCommand, se_RecursiveShapesCommand } from "../protocols/Rpcv2cbor"; +import { getEndpointPlugin } from "@smithy/middleware-endpoint"; import { getSerdePlugin } from "@smithy/middleware-serde"; import { Command as $Command } from "@smithy/smithy-client"; import { MetadataBearer as __MetadataBearer } from "@smithy/types"; @@ -87,8 +89,12 @@ export class RecursiveShapesCommand extends $Command ServiceInputTypes, ServiceOutputTypes >() + .ep(commonParams) .m(function (this: any, Command: any, cs: any, config: RpcV2ProtocolClientResolvedConfig, o: any) { - return [getSerdePlugin(config, this.serialize, this.deserialize)]; + return [ + getSerdePlugin(config, this.serialize, this.deserialize), + getEndpointPlugin(config, Command.getEndpointParameterInstructions()), + ]; }) .s("RpcV2Protocol", "RecursiveShapes", {}) .n("RpcV2ProtocolClient", "RecursiveShapesCommand") diff --git a/private/smithy-rpcv2-cbor/src/commands/RpcV2CborDenseMapsCommand.ts b/private/smithy-rpcv2-cbor/src/commands/RpcV2CborDenseMapsCommand.ts index d9a87e0b479..0637ea4e6be 100644 --- a/private/smithy-rpcv2-cbor/src/commands/RpcV2CborDenseMapsCommand.ts +++ b/private/smithy-rpcv2-cbor/src/commands/RpcV2CborDenseMapsCommand.ts @@ -1,7 +1,9 @@ // smithy-typescript generated code import { RpcV2ProtocolClientResolvedConfig, ServiceInputTypes, ServiceOutputTypes } from "../RpcV2ProtocolClient"; +import { commonParams } from "../endpoint/EndpointParameters"; import { RpcV2CborDenseMapsInputOutput } from "../models/models_0"; import { de_RpcV2CborDenseMapsCommand, se_RpcV2CborDenseMapsCommand } from "../protocols/Rpcv2cbor"; +import { getEndpointPlugin } from "@smithy/middleware-endpoint"; import { getSerdePlugin } from "@smithy/middleware-serde"; import { Command as $Command } from "@smithy/smithy-client"; import { MetadataBearer as __MetadataBearer } from "@smithy/types"; @@ -104,8 +106,12 @@ export class RpcV2CborDenseMapsCommand extends $Command ServiceInputTypes, ServiceOutputTypes >() + .ep(commonParams) .m(function (this: any, Command: any, cs: any, config: RpcV2ProtocolClientResolvedConfig, o: any) { - return [getSerdePlugin(config, this.serialize, this.deserialize)]; + return [ + getSerdePlugin(config, this.serialize, this.deserialize), + getEndpointPlugin(config, Command.getEndpointParameterInstructions()), + ]; }) .s("RpcV2Protocol", "RpcV2CborDenseMaps", {}) .n("RpcV2ProtocolClient", "RpcV2CborDenseMapsCommand") diff --git a/private/smithy-rpcv2-cbor/src/commands/RpcV2CborListsCommand.ts b/private/smithy-rpcv2-cbor/src/commands/RpcV2CborListsCommand.ts index 41010e3cf9e..e5709869762 100644 --- a/private/smithy-rpcv2-cbor/src/commands/RpcV2CborListsCommand.ts +++ b/private/smithy-rpcv2-cbor/src/commands/RpcV2CborListsCommand.ts @@ -1,7 +1,9 @@ // smithy-typescript generated code import { RpcV2ProtocolClientResolvedConfig, ServiceInputTypes, ServiceOutputTypes } from "../RpcV2ProtocolClient"; +import { commonParams } from "../endpoint/EndpointParameters"; import { RpcV2CborListInputOutput } from "../models/models_0"; import { de_RpcV2CborListsCommand, se_RpcV2CborListsCommand } from "../protocols/Rpcv2cbor"; +import { getEndpointPlugin } from "@smithy/middleware-endpoint"; import { getSerdePlugin } from "@smithy/middleware-serde"; import { Command as $Command } from "@smithy/smithy-client"; import { MetadataBearer as __MetadataBearer } from "@smithy/types"; @@ -142,8 +144,12 @@ export class RpcV2CborListsCommand extends $Command ServiceInputTypes, ServiceOutputTypes >() + .ep(commonParams) .m(function (this: any, Command: any, cs: any, config: RpcV2ProtocolClientResolvedConfig, o: any) { - return [getSerdePlugin(config, this.serialize, this.deserialize)]; + return [ + getSerdePlugin(config, this.serialize, this.deserialize), + getEndpointPlugin(config, Command.getEndpointParameterInstructions()), + ]; }) .s("RpcV2Protocol", "RpcV2CborLists", {}) .n("RpcV2ProtocolClient", "RpcV2CborListsCommand") diff --git a/private/smithy-rpcv2-cbor/src/commands/RpcV2CborSparseMapsCommand.ts b/private/smithy-rpcv2-cbor/src/commands/RpcV2CborSparseMapsCommand.ts index 463f2ec8850..0ce5667d4cc 100644 --- a/private/smithy-rpcv2-cbor/src/commands/RpcV2CborSparseMapsCommand.ts +++ b/private/smithy-rpcv2-cbor/src/commands/RpcV2CborSparseMapsCommand.ts @@ -1,7 +1,9 @@ // smithy-typescript generated code import { RpcV2ProtocolClientResolvedConfig, ServiceInputTypes, ServiceOutputTypes } from "../RpcV2ProtocolClient"; +import { commonParams } from "../endpoint/EndpointParameters"; import { RpcV2CborSparseMapsInputOutput } from "../models/models_0"; import { de_RpcV2CborSparseMapsCommand, se_RpcV2CborSparseMapsCommand } from "../protocols/Rpcv2cbor"; +import { getEndpointPlugin } from "@smithy/middleware-endpoint"; import { getSerdePlugin } from "@smithy/middleware-serde"; import { Command as $Command } from "@smithy/smithy-client"; import { MetadataBearer as __MetadataBearer } from "@smithy/types"; @@ -104,8 +106,12 @@ export class RpcV2CborSparseMapsCommand extends $Command ServiceInputTypes, ServiceOutputTypes >() + .ep(commonParams) .m(function (this: any, Command: any, cs: any, config: RpcV2ProtocolClientResolvedConfig, o: any) { - return [getSerdePlugin(config, this.serialize, this.deserialize)]; + return [ + getSerdePlugin(config, this.serialize, this.deserialize), + getEndpointPlugin(config, Command.getEndpointParameterInstructions()), + ]; }) .s("RpcV2Protocol", "RpcV2CborSparseMaps", {}) .n("RpcV2ProtocolClient", "RpcV2CborSparseMapsCommand") diff --git a/private/smithy-rpcv2-cbor/src/commands/SimpleScalarPropertiesCommand.ts b/private/smithy-rpcv2-cbor/src/commands/SimpleScalarPropertiesCommand.ts index 9af9f13932e..8726c415825 100644 --- a/private/smithy-rpcv2-cbor/src/commands/SimpleScalarPropertiesCommand.ts +++ b/private/smithy-rpcv2-cbor/src/commands/SimpleScalarPropertiesCommand.ts @@ -1,7 +1,9 @@ // smithy-typescript generated code import { RpcV2ProtocolClientResolvedConfig, ServiceInputTypes, ServiceOutputTypes } from "../RpcV2ProtocolClient"; +import { commonParams } from "../endpoint/EndpointParameters"; import { SimpleScalarStructure } from "../models/models_0"; import { de_SimpleScalarPropertiesCommand, se_SimpleScalarPropertiesCommand } from "../protocols/Rpcv2cbor"; +import { getEndpointPlugin } from "@smithy/middleware-endpoint"; import { getSerdePlugin } from "@smithy/middleware-serde"; import { Command as $Command } from "@smithy/smithy-client"; import { MetadataBearer as __MetadataBearer } from "@smithy/types"; @@ -81,8 +83,12 @@ export class SimpleScalarPropertiesCommand extends $Command ServiceInputTypes, ServiceOutputTypes >() + .ep(commonParams) .m(function (this: any, Command: any, cs: any, config: RpcV2ProtocolClientResolvedConfig, o: any) { - return [getSerdePlugin(config, this.serialize, this.deserialize)]; + return [ + getSerdePlugin(config, this.serialize, this.deserialize), + getEndpointPlugin(config, Command.getEndpointParameterInstructions()), + ]; }) .s("RpcV2Protocol", "SimpleScalarProperties", {}) .n("RpcV2ProtocolClient", "SimpleScalarPropertiesCommand") diff --git a/private/smithy-rpcv2-cbor/src/commands/SparseNullsOperationCommand.ts b/private/smithy-rpcv2-cbor/src/commands/SparseNullsOperationCommand.ts index 4ebd26493ef..c998a533801 100644 --- a/private/smithy-rpcv2-cbor/src/commands/SparseNullsOperationCommand.ts +++ b/private/smithy-rpcv2-cbor/src/commands/SparseNullsOperationCommand.ts @@ -1,7 +1,9 @@ // smithy-typescript generated code import { RpcV2ProtocolClientResolvedConfig, ServiceInputTypes, ServiceOutputTypes } from "../RpcV2ProtocolClient"; +import { commonParams } from "../endpoint/EndpointParameters"; import { SparseNullsOperationInputOutput } from "../models/models_0"; import { de_SparseNullsOperationCommand, se_SparseNullsOperationCommand } from "../protocols/Rpcv2cbor"; +import { getEndpointPlugin } from "@smithy/middleware-endpoint"; import { getSerdePlugin } from "@smithy/middleware-serde"; import { Command as $Command } from "@smithy/smithy-client"; import { MetadataBearer as __MetadataBearer } from "@smithy/types"; @@ -73,8 +75,12 @@ export class SparseNullsOperationCommand extends $Command ServiceInputTypes, ServiceOutputTypes >() + .ep(commonParams) .m(function (this: any, Command: any, cs: any, config: RpcV2ProtocolClientResolvedConfig, o: any) { - return [getSerdePlugin(config, this.serialize, this.deserialize)]; + return [ + getSerdePlugin(config, this.serialize, this.deserialize), + getEndpointPlugin(config, Command.getEndpointParameterInstructions()), + ]; }) .s("RpcV2Protocol", "SparseNullsOperation", {}) .n("RpcV2ProtocolClient", "SparseNullsOperationCommand") diff --git a/private/smithy-rpcv2-cbor/src/endpoint/EndpointParameters.ts b/private/smithy-rpcv2-cbor/src/endpoint/EndpointParameters.ts new file mode 100644 index 00000000000..053a6856767 --- /dev/null +++ b/private/smithy-rpcv2-cbor/src/endpoint/EndpointParameters.ts @@ -0,0 +1,29 @@ +// smithy-typescript generated code +import { Endpoint, EndpointV2, Provider, EndpointParameters as __EndpointParameters } from "@smithy/types"; + +/** + * @public + */ +export interface ClientInputEndpointParameters { + endpoint?: string | Provider | Endpoint | Provider | EndpointV2 | Provider; +} + +export type ClientResolvedEndpointParameters = ClientInputEndpointParameters & { + defaultSigningName: string; +}; + +export const resolveClientEndpointParameters = ( + options: T & ClientInputEndpointParameters +): T & ClientResolvedEndpointParameters => { + return Object.assign(options, { + defaultSigningName: "", + }); +}; + +export const commonParams = { + endpoint: { type: "builtInParams", name: "endpoint" }, +} as const; + +export interface EndpointParameters extends __EndpointParameters { + endpoint?: string; +} diff --git a/private/smithy-rpcv2-cbor/src/endpoint/endpointResolver.ts b/private/smithy-rpcv2-cbor/src/endpoint/endpointResolver.ts new file mode 100644 index 00000000000..c7fbb6d7f36 --- /dev/null +++ b/private/smithy-rpcv2-cbor/src/endpoint/endpointResolver.ts @@ -0,0 +1,22 @@ +// smithy-typescript generated code +import { EndpointParameters } from "./EndpointParameters"; +import { ruleSet } from "./ruleset"; +import { EndpointV2, Logger } from "@smithy/types"; +import { EndpointCache, EndpointParams, resolveEndpoint } from "@smithy/util-endpoints"; + +const cache = new EndpointCache({ + size: 50, + params: ["endpoint"], +}); + +export const defaultEndpointResolver = ( + endpointParams: EndpointParameters, + context: { logger?: Logger } = {} +): EndpointV2 => { + return cache.get(endpointParams as EndpointParams, () => + resolveEndpoint(ruleSet, { + endpointParams: endpointParams as EndpointParams, + logger: context.logger, + }) + ); +}; diff --git a/private/smithy-rpcv2-cbor/src/endpoint/ruleset.ts b/private/smithy-rpcv2-cbor/src/endpoint/ruleset.ts new file mode 100644 index 00000000000..14416a50b26 --- /dev/null +++ b/private/smithy-rpcv2-cbor/src/endpoint/ruleset.ts @@ -0,0 +1,38 @@ +// smithy-typescript generated code +import { RuleSetObject } from "@smithy/types"; + +export const ruleSet: RuleSetObject = { + version: "1.0", + parameters: { + endpoint: { + type: "string", + builtIn: "SDK::Endpoint", + documentation: "Endpoint used for making requests. Should be formatted as a URI.", + }, + }, + rules: [ + { + conditions: [ + { + fn: "isSet", + argv: [ + { + ref: "endpoint", + }, + ], + }, + ], + endpoint: { + url: { + ref: "endpoint", + }, + }, + type: "endpoint", + }, + { + conditions: [], + error: "(default endpointRuleSet) endpoint is not set - you must configure an endpoint.", + type: "error", + }, + ], +}; diff --git a/private/smithy-rpcv2-cbor/src/index.ts b/private/smithy-rpcv2-cbor/src/index.ts index 9603ea77597..8a7113aa6bd 100644 --- a/private/smithy-rpcv2-cbor/src/index.ts +++ b/private/smithy-rpcv2-cbor/src/index.ts @@ -2,6 +2,7 @@ /* eslint-disable */ export * from "./RpcV2ProtocolClient"; export * from "./RpcV2Protocol"; +export { ClientInputEndpointParameters } from "./endpoint/EndpointParameters"; export type { RuntimeExtension } from "./runtimeExtensions"; export type { RpcV2ProtocolExtensionConfiguration } from "./extensionConfiguration"; export * from "./commands"; diff --git a/private/smithy-rpcv2-cbor/src/runtimeConfig.shared.ts b/private/smithy-rpcv2-cbor/src/runtimeConfig.shared.ts index b9a066d3f36..3711e9114ce 100644 --- a/private/smithy-rpcv2-cbor/src/runtimeConfig.shared.ts +++ b/private/smithy-rpcv2-cbor/src/runtimeConfig.shared.ts @@ -1,5 +1,6 @@ // smithy-typescript generated code import { defaultRpcV2ProtocolHttpAuthSchemeProvider } from "./auth/httpAuthSchemeProvider"; +import { defaultEndpointResolver } from "./endpoint/endpointResolver"; import { NoAuthSigner } from "@smithy/core"; import { NoOpLogger } from "@smithy/smithy-client"; import { IdentityProviderConfig } from "@smithy/types"; @@ -17,6 +18,7 @@ export const getRuntimeConfig = (config: RpcV2ProtocolClientConfig) => { base64Decoder: config?.base64Decoder ?? fromBase64, base64Encoder: config?.base64Encoder ?? toBase64, disableHostPrefix: config?.disableHostPrefix ?? false, + endpointProvider: config?.endpointProvider ?? defaultEndpointResolver, extensions: config?.extensions ?? [], httpAuthSchemeProvider: config?.httpAuthSchemeProvider ?? defaultRpcV2ProtocolHttpAuthSchemeProvider, httpAuthSchemes: config?.httpAuthSchemes ?? [ diff --git a/private/smithy-rpcv2-cbor/test/functional/rpcv2cbor.spec.ts b/private/smithy-rpcv2-cbor/test/functional/rpcv2cbor.spec.ts index 75b13e15ee3..5bcfa791a91 100644 --- a/private/smithy-rpcv2-cbor/test/functional/rpcv2cbor.spec.ts +++ b/private/smithy-rpcv2-cbor/test/functional/rpcv2cbor.spec.ts @@ -168,9 +168,9 @@ const clientParams = { endpoint: () => { const url = new URL("https://localhost/"); return Promise.resolve({ - ...url, + hostname: url.hostname, + protocol: url.protocol, path: url.pathname, - ...(url.port ? { port: Number(url.port) } : {}), }) as Promise; }, }; diff --git a/smithy-typescript-codegen-test/example-weather-customizations/src/main/java/example/weather/ExampleWeatherCustomEndpointsRuntimeConfig.java b/smithy-typescript-codegen-test/example-weather-customizations/src/main/java/example/weather/ExampleWeatherCustomEndpointsRuntimeConfig.java index 0502181bb1e..ee7b5ce98db 100644 --- a/smithy-typescript-codegen-test/example-weather-customizations/src/main/java/example/weather/ExampleWeatherCustomEndpointsRuntimeConfig.java +++ b/smithy-typescript-codegen-test/example-weather-customizations/src/main/java/example/weather/ExampleWeatherCustomEndpointsRuntimeConfig.java @@ -70,12 +70,6 @@ public void customize(TypeScriptCodegenContext codegenContext) { w.write("export * from \"./customEndpoints\";"); }); - codegenContext.writerDelegator().useFileWriter(getClientFile(codegenContext.settings().getService()), w -> { - if (codegenContext.settings().getService(codegenContext.model()).hasTrait(EndpointRuleSetTrait.ID)) { - w.addImport("EndpointParameters", null, EndpointsV2Generator.ENDPOINT_PARAMETERS_DEPENDENCY); - } - }); - codegenContext.writerDelegator().useFileWriter(ADD_CUSTOM_ENDPOINTS_FILE, w -> { w.addDependency(TypeScriptDependency.SMITHY_TYPES); w.addImport("Provider", "__Provider", TypeScriptDependency.SMITHY_TYPES); diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/CommandGenerator.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/CommandGenerator.java index 538b39d58e5..cc6ea0c1f29 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/CommandGenerator.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/CommandGenerator.java @@ -52,7 +52,6 @@ import software.amazon.smithy.model.traits.ExamplesTrait; import software.amazon.smithy.model.traits.InternalTrait; import software.amazon.smithy.model.traits.StreamingTrait; -import software.amazon.smithy.rulesengine.traits.EndpointRuleSetTrait; import software.amazon.smithy.typescript.codegen.documentation.DocumentationExampleGenerator; import software.amazon.smithy.typescript.codegen.documentation.StructureExampleGenerator; import software.amazon.smithy.typescript.codegen.endpointsV2.RuleSetParameterFinder; @@ -393,10 +392,6 @@ private String getThrownExceptions() { } private void generateEndpointParameterInstructionProvider() { - if (!service.hasTrait(EndpointRuleSetTrait.class)) { - return; - } - writer.addImport( "commonParams", null, Paths.get(".", CodegenUtils.SOURCE_FOLDER, "endpoint/EndpointParameters").toString() @@ -495,16 +490,14 @@ private void generateCommandMiddlewareResolver(String configType) { // Add serialization and deserialization plugin. writer.write("$T(config, this.serialize, this.deserialize),", serde); // EndpointsV2 - if (service.hasTrait(EndpointRuleSetTrait.class)) { - writer.addImport( - "getEndpointPlugin", - null, - TypeScriptDependency.MIDDLEWARE_ENDPOINTS_V2); - writer.write( - """ - getEndpointPlugin(config, Command.getEndpointParameterInstructions()),""" - ); - } + writer.addImport( + "getEndpointPlugin", + null, + TypeScriptDependency.MIDDLEWARE_ENDPOINTS_V2); + writer.write( + """ + getEndpointPlugin(config, Command.getEndpointParameterInstructions()),""" + ); // Add customizations. addCommandSpecificPlugins(); } diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/DirectedTypeScriptCodegen.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/DirectedTypeScriptCodegen.java index 2d9eddb4366..71bbc5ad745 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/DirectedTypeScriptCodegen.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/DirectedTypeScriptCodegen.java @@ -49,7 +49,6 @@ import software.amazon.smithy.model.shapes.ShapeId; import software.amazon.smithy.model.traits.PaginatedTrait; import software.amazon.smithy.model.validation.ValidationEvent; -import software.amazon.smithy.rulesengine.traits.EndpointRuleSetTrait; import software.amazon.smithy.typescript.codegen.auth.http.HttpAuthSchemeProviderGenerator; import software.amazon.smithy.typescript.codegen.endpointsV2.EndpointsV2Generator; import software.amazon.smithy.typescript.codegen.integration.ProtocolGenerator; @@ -333,10 +332,6 @@ private void generateCommands(GenerateServiceDirective directive) { - if (!directive.shape().hasTrait(EndpointRuleSetTrait.class)) { - return; - } - new EndpointsV2Generator(directive.context().writerDelegator(), directive.settings(), directive.model()).run(); } diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/IndexGenerator.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/IndexGenerator.java index 4ad3bb0da99..bffbc382ed9 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/IndexGenerator.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/IndexGenerator.java @@ -27,7 +27,6 @@ import software.amazon.smithy.model.shapes.ServiceShape; import software.amazon.smithy.model.traits.DocumentationTrait; import software.amazon.smithy.model.traits.PaginatedTrait; -import software.amazon.smithy.rulesengine.traits.EndpointRuleSetTrait; import software.amazon.smithy.typescript.codegen.integration.ProtocolGenerator; import software.amazon.smithy.typescript.codegen.validation.ReplaceLast; import software.amazon.smithy.utils.SmithyInternalApi; @@ -109,9 +108,7 @@ private static void writeClientExports( writer.write("export * from \"./$L\";", normalizedClientName); // export endpoints config interface - if (service.hasTrait(EndpointRuleSetTrait.class)) { - writer.write("export { ClientInputEndpointParameters } from \"./endpoint/EndpointParameters\";"); - } + writer.write("export { ClientInputEndpointParameters } from \"./endpoint/EndpointParameters\";"); // Export Runtime Extension and Client ExtensionConfiguration interfaces writer.write("export type { RuntimeExtension } from \"./runtimeExtensions\";"); diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/ServiceBareBonesClientGenerator.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/ServiceBareBonesClientGenerator.java index 1659b43bb47..fe2d8b544d0 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/ServiceBareBonesClientGenerator.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/ServiceBareBonesClientGenerator.java @@ -32,7 +32,6 @@ import software.amazon.smithy.model.knowledge.TopDownIndex; import software.amazon.smithy.model.shapes.OperationShape; import software.amazon.smithy.model.shapes.ServiceShape; -import software.amazon.smithy.rulesengine.traits.EndpointRuleSetTrait; import software.amazon.smithy.typescript.codegen.endpointsV2.EndpointsV2Generator; import software.amazon.smithy.typescript.codegen.integration.RuntimeClientPlugin; import software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration; @@ -190,18 +189,20 @@ private void generateConfig() { if (!inputTypes.isEmpty()) { writer.indent(); for (SymbolReference symbolReference : inputTypes) { - if (service.hasTrait(EndpointRuleSetTrait.class) - && symbolReference.getAlias().equals("EndpointInputConfig")) { + if (symbolReference.getAlias().equals("EndpointInputConfig")) { + writer.addImport( + "EndpointParameters", + null, + EndpointsV2Generator.ENDPOINT_PARAMETERS_DEPENDENCY + ); writer.write("& $T<$L>", symbolReference, "EndpointParameters"); } else { writer.write("& $T", symbolReference); } } - if (service.hasTrait(EndpointRuleSetTrait.class)) { - writer.addImport("ClientInputEndpointParameters", null, - EndpointsV2Generator.ENDPOINT_PARAMETERS_DEPENDENCY); - writer.write("& ClientInputEndpointParameters"); - } + writer.addImport("ClientInputEndpointParameters", null, + EndpointsV2Generator.ENDPOINT_PARAMETERS_DEPENDENCY); + writer.write("& ClientInputEndpointParameters"); writer.dedent(); } @@ -223,18 +224,20 @@ private void generateConfig() { runtimePlugins.stream() .flatMap(p -> OptionalUtils.stream(p.getResolvedConfig())) .forEach(symbol -> { - if (service.hasTrait(EndpointRuleSetTrait.class) - && symbol.getAlias().equals("EndpointResolvedConfig")) { + if (symbol.getAlias().equals("EndpointResolvedConfig")) { + writer.addImport( + "EndpointParameters", + null, + EndpointsV2Generator.ENDPOINT_PARAMETERS_DEPENDENCY + ); writer.write("& $T<$L>", symbol, "EndpointParameters"); } else { writer.write("& $T", symbol); } }); - if (service.hasTrait(EndpointRuleSetTrait.class)) { - writer.addImport("ClientResolvedEndpointParameters", null, - EndpointsV2Generator.ENDPOINT_PARAMETERS_DEPENDENCY); - writer.write("& ClientResolvedEndpointParameters"); - } + writer.addImport("ClientResolvedEndpointParameters", null, + EndpointsV2Generator.ENDPOINT_PARAMETERS_DEPENDENCY); + writer.write("& ClientResolvedEndpointParameters"); writer.dedent(); } @@ -381,15 +384,13 @@ private void generateConstructor() { writer.write("super($L as any);", initialConfigVar); writer.write("this.initConfig = $L;", initialConfigVar); - if (service.hasTrait(EndpointRuleSetTrait.class)) { - configVariable++; - writer.addImport("resolveClientEndpointParameters", null, - EndpointsV2Generator.ENDPOINT_PARAMETERS_DEPENDENCY); - writer.write("let $L = $L($L);", - generateConfigVariable(configVariable), - "resolveClientEndpointParameters", - generateConfigVariable(configVariable - 1)); - } + configVariable++; + writer.addImport("resolveClientEndpointParameters", null, + EndpointsV2Generator.ENDPOINT_PARAMETERS_DEPENDENCY); + writer.write("let $L = $L($L);", + generateConfigVariable(configVariable), + "resolveClientEndpointParameters", + generateConfigVariable(configVariable - 1)); // Add runtime plugin "resolve" method calls. These are invoked one // after the other until all of the runtime plugins have been called. diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/AddHttpAuthSchemePlugin.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/AddHttpAuthSchemePlugin.java index 91053c2db53..b7e932c7822 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/AddHttpAuthSchemePlugin.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/AddHttpAuthSchemePlugin.java @@ -14,7 +14,6 @@ import software.amazon.smithy.model.knowledge.TopDownIndex; import software.amazon.smithy.model.shapes.ServiceShape; import software.amazon.smithy.model.shapes.ShapeId; -import software.amazon.smithy.rulesengine.traits.EndpointRuleSetTrait; import software.amazon.smithy.typescript.codegen.CodegenUtils; import software.amazon.smithy.typescript.codegen.TypeScriptCodegenContext; import software.amazon.smithy.typescript.codegen.TypeScriptDependency; @@ -57,21 +56,12 @@ public List getClientPlugins() { ); return List.of( RuntimeClientPlugin.builder() - .servicePredicate((m, s) -> s.hasTrait(EndpointRuleSetTrait.ID)) .withConventions( TypeScriptDependency.SMITHY_CORE.dependency, "HttpAuthSchemeEndpointRuleSet", Convention.HAS_MIDDLEWARE) .withAdditionalClientParams(httpAuthSchemeParametersProvider) .build(), - RuntimeClientPlugin.builder() - .servicePredicate((m, s) -> !s.hasTrait(EndpointRuleSetTrait.ID)) - .withConventions( - TypeScriptDependency.SMITHY_CORE.dependency, - "HttpAuthScheme", - Convention.HAS_MIDDLEWARE) - .withAdditionalClientParams(httpAuthSchemeParametersProvider) - .build(), RuntimeClientPlugin.builder() .inputConfig(Symbol.builder() .namespace(AuthUtils.HTTP_AUTH_SCHEME_PROVIDER_MODULE, "/") diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/endpointsV2/AddDefaultEndpointRuleSet.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/endpointsV2/AddDefaultEndpointRuleSet.java new file mode 100644 index 00000000000..1d72a15fa75 --- /dev/null +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/endpointsV2/AddDefaultEndpointRuleSet.java @@ -0,0 +1,127 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + +package software.amazon.smithy.typescript.codegen.endpointsV2; + +import static software.amazon.smithy.typescript.codegen.integration.RuntimeClientPlugin.Convention.HAS_CONFIG; + +import java.nio.file.Paths; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.function.Consumer; +import software.amazon.smithy.codegen.core.SymbolProvider; +import software.amazon.smithy.model.Model; +import software.amazon.smithy.model.node.Node; +import software.amazon.smithy.rulesengine.traits.EndpointRuleSetTrait; +import software.amazon.smithy.typescript.codegen.CodegenUtils; +import software.amazon.smithy.typescript.codegen.LanguageTarget; +import software.amazon.smithy.typescript.codegen.TypeScriptDependency; +import software.amazon.smithy.typescript.codegen.TypeScriptSettings; +import software.amazon.smithy.typescript.codegen.TypeScriptWriter; +import software.amazon.smithy.typescript.codegen.integration.AddBuiltinPlugins; +import software.amazon.smithy.typescript.codegen.integration.RuntimeClientPlugin; +import software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration; +import software.amazon.smithy.utils.MapUtils; +import software.amazon.smithy.utils.SmithyInternalApi; + + +/** + * This class normalizes models without endpointRuleSet traits to use the same code paths as those with ruleSet, + * to make reasoning about models easier and less variable. + */ +@SmithyInternalApi +public class AddDefaultEndpointRuleSet implements TypeScriptIntegration { + public static final EndpointRuleSetTrait DEFAULT_RULESET = EndpointRuleSetTrait.builder() + .ruleSet(Node.parse(""" + { + "version": "1.0", + "parameters": { + "endpoint": { + "type": "string", + "builtIn": "SDK::Endpoint", + "documentation": "Endpoint used for making requests. Should be formatted as a URI." + } + }, + "rules": [ + { + "conditions": [ + { + "fn": "isSet", + "argv": [ + { + "ref": "endpoint" + } + ] + } + ], + "endpoint": { + "url": { + "ref": "endpoint" + } + }, + "type": "endpoint" + }, + { + "conditions": [], + "error": "(default endpointRuleSet) endpoint is not set - you must configure an endpoint.", + "type": "error" + } + ] + } + """)) + .build(); + + @Override + public List runAfter() { + return List.of(AddBuiltinPlugins.class.getCanonicalName()); + } + + @Override + public List getClientPlugins() { + return List.of( + RuntimeClientPlugin.builder() + .withConventions( + TypeScriptDependency.MIDDLEWARE_ENDPOINTS_V2.dependency, "Endpoint", HAS_CONFIG) + .build() + ); + } + + @Override + public Model preprocessModel(Model model, TypeScriptSettings settings) { + Model.Builder modelBuilder = model.toBuilder(); + + model.getServiceShapes().forEach(serviceShape -> { + if (!serviceShape.hasTrait(EndpointRuleSetTrait.class)) { + modelBuilder.removeShape(serviceShape.toShapeId()); + modelBuilder.addShape(serviceShape.toBuilder() + .addTrait(DEFAULT_RULESET) + .build()); + } + }); + + return modelBuilder.build(); + } + + @Override + public Map> getRuntimeConfigWriters( + TypeScriptSettings settings, + Model model, + SymbolProvider symbolProvider, + LanguageTarget target + ) { + if (!settings.generateClient()) { + return Collections.emptyMap(); + } + if (target == LanguageTarget.SHARED) { + return MapUtils.of("endpointProvider", writer -> { + writer.addImport("defaultEndpointResolver", null, + Paths.get(".", CodegenUtils.SOURCE_FOLDER, "endpoint/endpointResolver").toString()); + writer.write("defaultEndpointResolver"); + }); + } + return Collections.emptyMap(); + } +} diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/endpointsV2/EndpointsV2Generator.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/endpointsV2/EndpointsV2Generator.java index d93222c7fbf..31d8cef42f8 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/endpointsV2/EndpointsV2Generator.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/endpointsV2/EndpointsV2Generator.java @@ -91,7 +91,7 @@ public EndpointsV2Generator( service = settings.getService(model); this.settings = settings; endpointRuleSetTrait = service.getTrait(EndpointRuleSetTrait.class) - .orElseThrow(() -> new RuntimeException("service missing EndpointRuleSetTrait")); + .orElseThrow(() -> new RuntimeException("service or model preprocessor missing EndpointRuleSetTrait")); ruleSetParameterFinder = new RuleSetParameterFinder(service); } diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/AddBuiltinPlugins.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/AddBuiltinPlugins.java index efc424395a6..3a21dc919ba 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/AddBuiltinPlugins.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/AddBuiltinPlugins.java @@ -5,12 +5,9 @@ package software.amazon.smithy.typescript.codegen.integration; -import static software.amazon.smithy.typescript.codegen.integration.RuntimeClientPlugin.Convention.HAS_CONFIG; import static software.amazon.smithy.typescript.codegen.integration.RuntimeClientPlugin.Convention.HAS_MIDDLEWARE; import java.util.List; -import software.amazon.smithy.model.shapes.ServiceShape; -import software.amazon.smithy.rulesengine.traits.EndpointRuleSetTrait; import software.amazon.smithy.typescript.codegen.TypeScriptDependency; import software.amazon.smithy.utils.SmithyInternalApi; @@ -24,11 +21,6 @@ public List getClientPlugins() { // Note that order is significant because configurations might // rely on previously resolved values. return List.of( - RuntimeClientPlugin.builder() - .withConventions( - TypeScriptDependency.CONFIG_RESOLVER.dependency, "CustomEndpoints", HAS_CONFIG) - .servicePredicate((m, s) -> !isEndpointsV2Service(s)) - .build(), RuntimeClientPlugin.builder() .withConventions(TypeScriptDependency.MIDDLEWARE_RETRY.dependency, "Retry") .build(), @@ -37,8 +29,4 @@ public List getClientPlugins() { HAS_MIDDLEWARE) .build()); } - - private static boolean isEndpointsV2Service(ServiceShape serviceShape) { - return serviceShape.hasTrait(EndpointRuleSetTrait.class); - } } diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/HttpBindingProtocolGenerator.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/HttpBindingProtocolGenerator.java index e8e550760fd..c15b936ec6e 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/HttpBindingProtocolGenerator.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/HttpBindingProtocolGenerator.java @@ -68,7 +68,6 @@ import software.amazon.smithy.model.traits.MediaTypeTrait; import software.amazon.smithy.model.traits.StreamingTrait; import software.amazon.smithy.model.traits.TimestampFormatTrait.Format; -import software.amazon.smithy.rulesengine.traits.EndpointRuleSetTrait; import software.amazon.smithy.typescript.codegen.ApplicationProtocol; import software.amazon.smithy.typescript.codegen.CodegenUtils; import software.amazon.smithy.typescript.codegen.FrameworkErrorModel; @@ -767,20 +766,13 @@ private void writeResolvedPath( SymbolProvider symbolProvider = context.getSymbolProvider(); List labelBindings = bindingIndex.getRequestBindings(operation, Location.LABEL); - final boolean useEndpointsV2 = context.getService().hasTrait(EndpointRuleSetTrait.class); - final Map contextParams = useEndpointsV2 - ? new RuleSetParameterFinder(context.getService()) - .getContextParams(context.getModel().getShape(operation.getInputShape()).get()) - : Collections.emptyMap(); + final Map contextParams = new RuleSetParameterFinder(context.getService()) + .getContextParams(context.getModel().getShape(operation.getInputShape()).get()); // Always write the bound path, but only the actual segments. writer.write("b.bp(\"$L\");", "/" + trait.getUri().getSegments().stream() .filter(segment -> { - if (!useEndpointsV2) { - // only applicable in Endpoints 2.0 - return true; - } String content = segment.getContent(); boolean isContextParam = contextParams.containsKey(content); diff --git a/smithy-typescript-codegen/src/main/resources/META-INF/services/software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration b/smithy-typescript-codegen/src/main/resources/META-INF/services/software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration index 76c28ba977c..a00867554a6 100644 --- a/smithy-typescript-codegen/src/main/resources/META-INF/services/software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration +++ b/smithy-typescript-codegen/src/main/resources/META-INF/services/software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration @@ -1,3 +1,4 @@ +software.amazon.smithy.typescript.codegen.endpointsV2.AddDefaultEndpointRuleSet software.amazon.smithy.typescript.codegen.integration.AddBuiltinPlugins software.amazon.smithy.typescript.codegen.integration.AddClientRuntimeConfig software.amazon.smithy.typescript.codegen.integration.AddEventStreamDependency diff --git a/smithy-typescript-codegen/src/main/resources/software/amazon/smithy/typescript/codegen/protocol-test-stub.ts b/smithy-typescript-codegen/src/main/resources/software/amazon/smithy/typescript/codegen/protocol-test-stub.ts index 6dd72041699..5dcfc705337 100644 --- a/smithy-typescript-codegen/src/main/resources/software/amazon/smithy/typescript/codegen/protocol-test-stub.ts +++ b/smithy-typescript-codegen/src/main/resources/software/amazon/smithy/typescript/codegen/protocol-test-stub.ts @@ -151,9 +151,9 @@ const clientParams = { endpoint: () => { const url = new URL("https://localhost/"); return Promise.resolve({ - ...url, + hostname: url.hostname, + protocol: url.protocol, path: url.pathname, - ...(url.port ? { port: Number(url.port) } : {}), }) as Promise; }, }; diff --git a/yarn.lock b/yarn.lock index a60bac920b7..70eccb3bd59 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3010,6 +3010,7 @@ __metadata: "@smithy/hash-node": "workspace:^" "@smithy/invalid-dependency": "workspace:^" "@smithy/middleware-content-length": "workspace:^" + "@smithy/middleware-endpoint": "workspace:^" "@smithy/middleware-retry": "workspace:^" "@smithy/middleware-serde": "workspace:^" "@smithy/middleware-stack": "workspace:^" @@ -3024,6 +3025,7 @@ __metadata: "@smithy/util-body-length-node": "workspace:^" "@smithy/util-defaults-mode-browser": "workspace:^" "@smithy/util-defaults-mode-node": "workspace:^" + "@smithy/util-endpoints": "workspace:^" "@smithy/util-middleware": "workspace:^" "@smithy/util-retry": "workspace:^" "@smithy/util-utf8": "workspace:^" @@ -3186,7 +3188,7 @@ __metadata: languageName: unknown linkType: soft -"@smithy/util-endpoints@workspace:packages/util-endpoints": +"@smithy/util-endpoints@workspace:^, @smithy/util-endpoints@workspace:packages/util-endpoints": version: 0.0.0-use.local resolution: "@smithy/util-endpoints@workspace:packages/util-endpoints" dependencies: