|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package software.amazon.smithy.typescript.codegen.integration; |
| 7 | + |
| 8 | +import java.util.Collections; |
| 9 | +import java.util.Map; |
| 10 | +import java.util.function.Consumer; |
| 11 | +import software.amazon.smithy.codegen.core.SymbolProvider; |
| 12 | +import software.amazon.smithy.model.Model; |
| 13 | +import software.amazon.smithy.typescript.codegen.LanguageTarget; |
| 14 | +import software.amazon.smithy.typescript.codegen.TypeScriptDependency; |
| 15 | +import software.amazon.smithy.typescript.codegen.TypeScriptSettings; |
| 16 | +import software.amazon.smithy.typescript.codegen.TypeScriptWriter; |
| 17 | +import software.amazon.smithy.utils.MapUtils; |
| 18 | +import software.amazon.smithy.utils.SmithyInternalApi; |
| 19 | + |
| 20 | +/** |
| 21 | + * All clients need to know the max attempt to retry a request and logger |
| 22 | + * instance to print the log. |
| 23 | + * |
| 24 | + * <p>This plugin adds the following config interface fields: |
| 25 | + * |
| 26 | + * <ul> |
| 27 | + * <li>maxAttempts: Provides value for how many times a request will be |
| 28 | + * made at most in case of retry.</li> |
| 29 | + * <li>retryMode: Specifies which retry algorithm to use.</li> |
| 30 | + * <li>logger: Optional logger for logging debug/info/warn/error.</li> |
| 31 | + * </ul> |
| 32 | + * |
| 33 | + * <p>This plugin adds the following Node runtime specific values: |
| 34 | + * |
| 35 | + * <ul> |
| 36 | + * <li>maxAttempts: Uses the default maxAttempts provider that checks things |
| 37 | + * like environment variables and the AWS config file.</li> |
| 38 | + * <li>retryMode: Specifies which retry algorithm to use.</li> |
| 39 | + * <li>logger: Sets to empty as logger is passed in client configuration.</li> |
| 40 | + * </ul> |
| 41 | + * |
| 42 | + * <p>This plugin adds the following Browser runtime specific values: |
| 43 | + * |
| 44 | + * <ul> |
| 45 | + * <li>maxAttempts: Returns default value of 3.</li> |
| 46 | + * <li>retryMode: Provider which returns DEFAULT_RETRY_MODE.</li> |
| 47 | + * <li>logger: Sets to empty as logger is passed in client configuration.</li> |
| 48 | + * </ul> |
| 49 | + */ |
| 50 | +@SmithyInternalApi |
| 51 | +public final class AddClientRuntimeConfig implements TypeScriptIntegration { |
| 52 | + |
| 53 | + @Override |
| 54 | + public void addConfigInterfaceFields( |
| 55 | + TypeScriptSettings settings, |
| 56 | + Model model, |
| 57 | + SymbolProvider symbolProvider, |
| 58 | + TypeScriptWriter writer |
| 59 | + ) { |
| 60 | + writer.addImport("Provider", "__Provider", TypeScriptDependency.AWS_SDK_TYPES.packageName); |
| 61 | + writer.addImport("Logger", "__Logger", TypeScriptDependency.AWS_SDK_TYPES.packageName); |
| 62 | + |
| 63 | + writer.writeDocs("Value for how many times a request will be made at most in case of retry.") |
| 64 | + .write("maxAttempts?: number | __Provider<number>;\n"); |
| 65 | + writer.writeDocs("Specifies which retry algorithm to use.") |
| 66 | + .write("retryMode?: string | __Provider<string>;\n"); |
| 67 | + writer.writeDocs("Optional logger for logging debug/info/warn/error.") |
| 68 | + .write("logger?: __Logger;\n"); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public Map<String, Consumer<TypeScriptWriter>> getRuntimeConfigWriters( |
| 73 | + TypeScriptSettings settings, |
| 74 | + Model model, |
| 75 | + SymbolProvider symbolProvider, |
| 76 | + LanguageTarget target |
| 77 | + ) { |
| 78 | + switch (target) { |
| 79 | + case SHARED: |
| 80 | + return MapUtils.of( |
| 81 | + "logger", writer -> { |
| 82 | + writer.addImport("NoOpLogger", null, "@aws-sdk/smithy-client"); |
| 83 | + writer.write("new NoOpLogger()"); |
| 84 | + } |
| 85 | + ); |
| 86 | + case BROWSER: |
| 87 | + return MapUtils.of( |
| 88 | + "maxAttempts", writer -> { |
| 89 | + writer.addDependency(TypeScriptDependency.UTIL_RETRY); |
| 90 | + writer.addImport("DEFAULT_MAX_ATTEMPTS", "DEFAULT_MAX_ATTEMPTS", |
| 91 | + TypeScriptDependency.UTIL_RETRY.packageName); |
| 92 | + writer.write("DEFAULT_MAX_ATTEMPTS"); |
| 93 | + }, |
| 94 | + "retryMode", writer -> { |
| 95 | + writer.addDependency(TypeScriptDependency.UTIL_RETRY); |
| 96 | + writer.addImport("DEFAULT_RETRY_MODE", "DEFAULT_RETRY_MODE", |
| 97 | + TypeScriptDependency.UTIL_RETRY.packageName); |
| 98 | + writer.write( |
| 99 | + "(async () => (await defaultConfigProvider()).retryMode || DEFAULT_RETRY_MODE)"); |
| 100 | + } |
| 101 | + ); |
| 102 | + case NODE: |
| 103 | + return MapUtils.of( |
| 104 | + "maxAttempts", writer -> { |
| 105 | + writer.addDependency(TypeScriptDependency.NODE_CONFIG_PROVIDER); |
| 106 | + writer.addImport("loadConfig", "loadNodeConfig", |
| 107 | + TypeScriptDependency.NODE_CONFIG_PROVIDER.packageName); |
| 108 | + writer.addImport("NODE_MAX_ATTEMPT_CONFIG_OPTIONS", "NODE_MAX_ATTEMPT_CONFIG_OPTIONS", |
| 109 | + TypeScriptDependency.MIDDLEWARE_RETRY.packageName); |
| 110 | + writer.write("loadNodeConfig(NODE_MAX_ATTEMPT_CONFIG_OPTIONS)"); |
| 111 | + }, |
| 112 | + "retryMode", writer -> { |
| 113 | + writer.addDependency(TypeScriptDependency.NODE_CONFIG_PROVIDER); |
| 114 | + writer.addImport("loadConfig", "loadNodeConfig", |
| 115 | + TypeScriptDependency.NODE_CONFIG_PROVIDER.packageName); |
| 116 | + writer.addDependency(TypeScriptDependency.MIDDLEWARE_RETRY); |
| 117 | + writer.addImport("NODE_RETRY_MODE_CONFIG_OPTIONS", "NODE_RETRY_MODE_CONFIG_OPTIONS", |
| 118 | + TypeScriptDependency.MIDDLEWARE_RETRY.packageName); |
| 119 | + writer.addImport("DEFAULT_RETRY_MODE", "DEFAULT_RETRY_MODE", |
| 120 | + TypeScriptDependency.UTIL_RETRY.packageName); |
| 121 | + writer.openBlock("loadNodeConfig({", "})", () -> { |
| 122 | + writer.write("...NODE_RETRY_MODE_CONFIG_OPTIONS,"); |
| 123 | + writer.write("default: async () => " |
| 124 | + + "(await defaultConfigProvider()).retryMode || DEFAULT_RETRY_MODE,"); |
| 125 | + }); |
| 126 | + } |
| 127 | + ); |
| 128 | + default: |
| 129 | + return Collections.emptyMap(); |
| 130 | + } |
| 131 | + } |
| 132 | +} |
0 commit comments