|
| 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.aws.typescript.codegen; |
| 7 | + |
| 8 | + import static software.amazon.smithy.aws.typescript.codegen.AwsTraitsUtils.isAwsService; |
| 9 | + import static software.amazon.smithy.aws.typescript.codegen.AwsTraitsUtils.isSigV4Service; |
| 10 | + |
| 11 | + import java.util.HashMap; |
| 12 | + import java.util.Map; |
| 13 | + import java.util.Optional; |
| 14 | + import java.util.function.Consumer; |
| 15 | + import java.util.logging.Logger; |
| 16 | + import software.amazon.smithy.codegen.core.SymbolProvider; |
| 17 | + import software.amazon.smithy.model.Model; |
| 18 | + import software.amazon.smithy.model.shapes.ServiceShape; |
| 19 | + import software.amazon.smithy.rulesengine.traits.EndpointRuleSetTrait; |
| 20 | + import software.amazon.smithy.typescript.codegen.LanguageTarget; |
| 21 | + import software.amazon.smithy.typescript.codegen.TypeScriptDependency; |
| 22 | + import software.amazon.smithy.typescript.codegen.TypeScriptSettings; |
| 23 | + import software.amazon.smithy.typescript.codegen.TypeScriptWriter; |
| 24 | + import software.amazon.smithy.typescript.codegen.endpointsV2.RuleSetParameterFinder; |
| 25 | + import software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration; |
| 26 | + import software.amazon.smithy.utils.SmithyInternalApi; |
| 27 | + |
| 28 | + /** |
| 29 | + * Generates accountIdEndpointMode configuration field for service clients |
| 30 | + * that have the AccountIdEndpointMode built-in param in the ruleset. |
| 31 | + */ |
| 32 | + @SmithyInternalApi |
| 33 | + public final class AddAccountIdEndpointModeRuntimeConfig implements TypeScriptIntegration { |
| 34 | + |
| 35 | + private static final Logger LOGGER = Logger.getLogger(AddAccountIdEndpointModeRuntimeConfig.class.getName()); |
| 36 | + |
| 37 | + @Override |
| 38 | + public void addConfigInterfaceFields( |
| 39 | + TypeScriptSettings settings, |
| 40 | + Model model, |
| 41 | + SymbolProvider symbolProvider, |
| 42 | + TypeScriptWriter writer |
| 43 | + ) { |
| 44 | + if (isAwsService(settings, model)) { |
| 45 | + ServiceShape service = settings.getService(model); |
| 46 | + Optional<EndpointRuleSetTrait> endpointRuleSetTrait = service.getTrait(EndpointRuleSetTrait.class); |
| 47 | + if (endpointRuleSetTrait.isPresent()) { |
| 48 | + RuleSetParameterFinder ruleSetParameterFinder = new RuleSetParameterFinder(service); |
| 49 | + if (ruleSetParameterFinder.getBuiltInParams().containsKey("AccountIdEndpointMode")) { |
| 50 | + writer.addDependency(AwsDependency.AWS_SDK_CORE); |
| 51 | + // TODO: change to addImportSubmodule when available; smithy-ts, #pull-1280 |
| 52 | + writer.addImport("AccountIdEndpointMode", "AccountIdEndpointMode", |
| 53 | + "@aws-sdk/core/account-id-endpoint"); |
| 54 | + writer.writeDocs("Defines if the AWS AccountId will be used for endpoint routing."); |
| 55 | + writer.write("accountIdEndpointMode?: AccountIdEndpointMode | " |
| 56 | + + "__Provider<AccountIdEndpointMode>;\n"); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public Map<String, Consumer<TypeScriptWriter>> getRuntimeConfigWriters( |
| 64 | + TypeScriptSettings settings, |
| 65 | + Model model, |
| 66 | + SymbolProvider symbolProvider, |
| 67 | + LanguageTarget target |
| 68 | + ) { |
| 69 | + ServiceShape service = settings.getService(model); |
| 70 | + Map<String, Consumer<TypeScriptWriter>> runtimeConfigs = new HashMap<>(); |
| 71 | + if (isAwsService(settings, model) || isSigV4Service(settings, model)) { |
| 72 | + Optional<EndpointRuleSetTrait> endpointRuleSetTrait = service.getTrait(EndpointRuleSetTrait.class); |
| 73 | + if (endpointRuleSetTrait.isPresent()) { |
| 74 | + RuleSetParameterFinder ruleSetParameterFinder = new RuleSetParameterFinder(service); |
| 75 | + if (ruleSetParameterFinder.getBuiltInParams().containsKey("AccountIdEndpointMode")) { |
| 76 | + switch (target) { |
| 77 | + case BROWSER: |
| 78 | + runtimeConfigs.put("accountIdEndpointMode", writer -> { |
| 79 | + writer.addDependency(AwsDependency.AWS_SDK_CORE); |
| 80 | + // TODO: change to addImportSubmodule when available |
| 81 | + writer.addImport("DEFAULT_ACCOUNT_ID_ENDPOINT_MODE", "DEFAULT_ACCOUNT_ID_ENDPOINT_MODE", |
| 82 | + "@aws-sdk/core/account-id-endpoint"); |
| 83 | + writer.write("(() => Promise.resolve(DEFAULT_ACCOUNT_ID_ENDPOINT_MODE))"); |
| 84 | + }); |
| 85 | + break; |
| 86 | + case NODE: |
| 87 | + runtimeConfigs.put("accountIdEndpointMode", writer -> { |
| 88 | + writer.addDependency(TypeScriptDependency.NODE_CONFIG_PROVIDER); |
| 89 | + writer.addImport("loadConfig", "loadNodeConfig", |
| 90 | + TypeScriptDependency.NODE_CONFIG_PROVIDER); |
| 91 | + writer.addDependency(AwsDependency.AWS_SDK_CORE); |
| 92 | + // TODO: change to addImportSubmodule when available |
| 93 | + writer.addImport("NODE_ACCOUNT_ID_ENDPOINT_MODE_CONFIG_OPTIONS", |
| 94 | + "NODE_ACCOUNT_ID_ENDPOINT_MODE_CONFIG_OPTIONS", |
| 95 | + "@aws-sdk/core/account-id-endpoint"); |
| 96 | + writer.write( |
| 97 | + "loadNodeConfig(NODE_ACCOUNT_ID_ENDPOINT_MODE_CONFIG_OPTIONS)"); |
| 98 | + }); |
| 99 | + break; |
| 100 | + default: |
| 101 | + LOGGER.warning("AccountIdEndpointMode config not supported for target: " + target); |
| 102 | + break; |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + return runtimeConfigs; |
| 108 | + } |
| 109 | + } |
0 commit comments