diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/TypeScriptDependency.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/TypeScriptDependency.java index 8552809af34..c48d1f2b06f 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/TypeScriptDependency.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/TypeScriptDependency.java @@ -74,6 +74,8 @@ public enum TypeScriptDependency implements SymbolDependencyContainer { AWS_SDK_UTIL_DEFAULTS_MODE_NODE("dependencies", "@aws-sdk/util-defaults-mode-node", true), AWS_SDK_UTIL_DEFAULTS_MODE_BROWSER("dependencies", "@aws-sdk/util-defaults-mode-browser", true), + NODE_CONFIG_PROVIDER("dependencies", "@aws-sdk/node-config-provider", false), + // Conditionally added when httpChecksumRequired trait exists MD5_BROWSER("dependencies", "@aws-sdk/md5-js", false), STREAM_HASHER_NODE("dependencies", "@aws-sdk/hash-stream-node", false), diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/AddClientRuntimeConfig.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/AddClientRuntimeConfig.java new file mode 100644 index 00000000000..aefae528889 --- /dev/null +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/AddClientRuntimeConfig.java @@ -0,0 +1,132 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + +package software.amazon.smithy.typescript.codegen.integration; + +import java.util.Collections; +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.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.utils.MapUtils; +import software.amazon.smithy.utils.SmithyInternalApi; + +/** + * All clients need to know the max attempt to retry a request and logger + * instance to print the log. + * + *

This plugin adds the following config interface fields: + * + *

+ * + *

This plugin adds the following Node runtime specific values: + * + *

+ * + *

This plugin adds the following Browser runtime specific values: + * + *

+ */ +@SmithyInternalApi +public final class AddClientRuntimeConfig implements TypeScriptIntegration { + + @Override + public void addConfigInterfaceFields( + TypeScriptSettings settings, + Model model, + SymbolProvider symbolProvider, + TypeScriptWriter writer + ) { + writer.addImport("Provider", "__Provider", TypeScriptDependency.AWS_SDK_TYPES.packageName); + writer.addImport("Logger", "__Logger", TypeScriptDependency.AWS_SDK_TYPES.packageName); + + writer.writeDocs("Value for how many times a request will be made at most in case of retry.") + .write("maxAttempts?: number | __Provider;\n"); + writer.writeDocs("Specifies which retry algorithm to use.") + .write("retryMode?: string | __Provider;\n"); + writer.writeDocs("Optional logger for logging debug/info/warn/error.") + .write("logger?: __Logger;\n"); + } + + @Override + public Map> getRuntimeConfigWriters( + TypeScriptSettings settings, + Model model, + SymbolProvider symbolProvider, + LanguageTarget target + ) { + switch (target) { + case SHARED: + return MapUtils.of( + "logger", writer -> { + writer.addImport("NoOpLogger", null, "@aws-sdk/smithy-client"); + writer.write("new NoOpLogger()"); + } + ); + case BROWSER: + return MapUtils.of( + "maxAttempts", writer -> { + writer.addDependency(TypeScriptDependency.UTIL_RETRY); + writer.addImport("DEFAULT_MAX_ATTEMPTS", "DEFAULT_MAX_ATTEMPTS", + TypeScriptDependency.UTIL_RETRY.packageName); + writer.write("DEFAULT_MAX_ATTEMPTS"); + }, + "retryMode", writer -> { + writer.addDependency(TypeScriptDependency.UTIL_RETRY); + writer.addImport("DEFAULT_RETRY_MODE", "DEFAULT_RETRY_MODE", + TypeScriptDependency.UTIL_RETRY.packageName); + writer.write( + "(async () => (await defaultConfigProvider()).retryMode || DEFAULT_RETRY_MODE)"); + } + ); + case NODE: + return MapUtils.of( + "maxAttempts", writer -> { + writer.addDependency(TypeScriptDependency.NODE_CONFIG_PROVIDER); + writer.addImport("loadConfig", "loadNodeConfig", + TypeScriptDependency.NODE_CONFIG_PROVIDER.packageName); + writer.addImport("NODE_MAX_ATTEMPT_CONFIG_OPTIONS", "NODE_MAX_ATTEMPT_CONFIG_OPTIONS", + TypeScriptDependency.MIDDLEWARE_RETRY.packageName); + writer.write("loadNodeConfig(NODE_MAX_ATTEMPT_CONFIG_OPTIONS)"); + }, + "retryMode", writer -> { + writer.addDependency(TypeScriptDependency.NODE_CONFIG_PROVIDER); + writer.addImport("loadConfig", "loadNodeConfig", + TypeScriptDependency.NODE_CONFIG_PROVIDER.packageName); + writer.addDependency(TypeScriptDependency.MIDDLEWARE_RETRY); + writer.addImport("NODE_RETRY_MODE_CONFIG_OPTIONS", "NODE_RETRY_MODE_CONFIG_OPTIONS", + TypeScriptDependency.MIDDLEWARE_RETRY.packageName); + writer.addImport("DEFAULT_RETRY_MODE", "DEFAULT_RETRY_MODE", + TypeScriptDependency.UTIL_RETRY.packageName); + writer.openBlock("loadNodeConfig({", "})", () -> { + writer.write("...NODE_RETRY_MODE_CONFIG_OPTIONS,"); + writer.write("default: async () => " + + "(await defaultConfigProvider()).retryMode || DEFAULT_RETRY_MODE,"); + }); + } + ); + default: + return Collections.emptyMap(); + } + } +} 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 e37bfb1b2f0..af4d1f12f86 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.integration.AddClientRuntimeConfig software.amazon.smithy.typescript.codegen.integration.AddEventStreamDependency software.amazon.smithy.typescript.codegen.integration.AddChecksumRequiredDependency software.amazon.smithy.typescript.codegen.integration.AddDefaultsModeDependency