|
3 | 3 | * SPDX-License-Identifier: Apache-2.0
|
4 | 4 | */
|
5 | 5 |
|
6 |
| - package software.amazon.smithy.aws.typescript.codegen; |
| 6 | +package software.amazon.smithy.aws.typescript.codegen; |
7 | 7 |
|
8 |
| - import static software.amazon.smithy.aws.typescript.codegen.AwsTraitsUtils.isAwsService; |
9 |
| - import static software.amazon.smithy.aws.typescript.codegen.AwsTraitsUtils.isSigV4Service; |
| 8 | +import static software.amazon.smithy.aws.typescript.codegen.AwsTraitsUtils.isAwsService; |
| 9 | +import static software.amazon.smithy.aws.typescript.codegen.AwsTraitsUtils.isSigV4Service; |
10 | 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; |
| 11 | +import java.util.HashMap; |
| 12 | +import java.util.List; |
| 13 | +import java.util.Map; |
| 14 | +import java.util.Optional; |
| 15 | +import java.util.function.Consumer; |
| 16 | +import java.util.logging.Logger; |
| 17 | +import software.amazon.smithy.codegen.core.Symbol; |
| 18 | +import software.amazon.smithy.codegen.core.SymbolProvider; |
| 19 | +import software.amazon.smithy.model.Model; |
| 20 | +import software.amazon.smithy.model.shapes.ServiceShape; |
| 21 | +import software.amazon.smithy.rulesengine.traits.EndpointRuleSetTrait; |
| 22 | +import software.amazon.smithy.typescript.codegen.LanguageTarget; |
| 23 | +import software.amazon.smithy.typescript.codegen.TypeScriptDependency; |
| 24 | +import software.amazon.smithy.typescript.codegen.TypeScriptSettings; |
| 25 | +import software.amazon.smithy.typescript.codegen.TypeScriptWriter; |
| 26 | +import software.amazon.smithy.typescript.codegen.endpointsV2.RuleSetParameterFinder; |
| 27 | +import software.amazon.smithy.typescript.codegen.integration.RuntimeClientPlugin; |
| 28 | +import software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration; |
| 29 | +import software.amazon.smithy.utils.SmithyInternalApi; |
27 | 30 |
|
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 { |
| 31 | +/** |
| 32 | + * Generates accountIdEndpointMode configuration field for service clients |
| 33 | + * that have the AccountIdEndpointMode built-in param in the ruleset. |
| 34 | + */ |
| 35 | +@SmithyInternalApi |
| 36 | +public final class AddAccountIdEndpointModeRuntimeConfig implements TypeScriptIntegration { |
| 37 | + |
| 38 | + private static final Logger LOGGER = Logger.getLogger(AddAccountIdEndpointModeRuntimeConfig.class.getName()); |
| 39 | + |
| 40 | + @Override |
| 41 | + public void addConfigInterfaceFields( |
| 42 | + TypeScriptSettings settings, |
| 43 | + Model model, |
| 44 | + SymbolProvider symbolProvider, |
| 45 | + TypeScriptWriter writer |
| 46 | + ) { |
| 47 | + if (isAwsService(settings, model)) { |
| 48 | + ServiceShape service = settings.getService(model); |
| 49 | + if (hasAccountIdEndpointParam(service)) { |
| 50 | + writer.addImportSubmodule("AccountIdEndpointMode", null, |
| 51 | + AwsDependency.AWS_SDK_CORE, "/account-id-endpoint"); |
| 52 | + writer.writeDocs("Defines if the AWS AccountId will be used for endpoint routing."); |
| 53 | + writer.write("accountIdEndpointMode?: AccountIdEndpointMode | " |
| 54 | + + "__Provider<AccountIdEndpointMode>;\n"); |
| 55 | + writer.addImportSubmodule("resolveAccountIdEndpointModeConfig", null, |
| 56 | + AwsDependency.AWS_SDK_CORE, "/account-id-endpoint"); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
34 | 60 |
|
35 |
| - private static final Logger LOGGER = Logger.getLogger(AddAccountIdEndpointModeRuntimeConfig.class.getName()); |
| 61 | + @Override |
| 62 | + public Map<String, Consumer<TypeScriptWriter>> getRuntimeConfigWriters( |
| 63 | + TypeScriptSettings settings, |
| 64 | + Model model, |
| 65 | + SymbolProvider symbolProvider, |
| 66 | + LanguageTarget target |
| 67 | + ) { |
| 68 | + ServiceShape service = settings.getService(model); |
| 69 | + Map<String, Consumer<TypeScriptWriter>> runtimeConfigs = new HashMap<>(); |
| 70 | + if (isAwsService(settings, model) || isSigV4Service(settings, model)) { |
| 71 | + Optional<EndpointRuleSetTrait> endpointRuleSetTrait = service.getTrait(EndpointRuleSetTrait.class); |
| 72 | + if (endpointRuleSetTrait.isPresent()) { |
| 73 | + RuleSetParameterFinder ruleSetParameterFinder = new RuleSetParameterFinder(service); |
| 74 | + if (ruleSetParameterFinder.getBuiltInParams().containsKey("AccountIdEndpointMode")) { |
| 75 | + switch (target) { |
| 76 | + case BROWSER: |
| 77 | + runtimeConfigs.put("accountIdEndpointMode", writer -> { |
| 78 | + writer.addImportSubmodule("DEFAULT_ACCOUNT_ID_ENDPOINT_MODE", null, |
| 79 | + AwsDependency.AWS_SDK_CORE, |
| 80 | + "/account-id-endpoint"); |
| 81 | + writer.write("(() => Promise.resolve(DEFAULT_ACCOUNT_ID_ENDPOINT_MODE))"); |
| 82 | + }); |
| 83 | + break; |
| 84 | + case NODE: |
| 85 | + runtimeConfigs.put("accountIdEndpointMode", writer -> { |
| 86 | + writer.addImport("loadConfig", "loadNodeConfig", |
| 87 | + TypeScriptDependency.NODE_CONFIG_PROVIDER); |
| 88 | + writer.addImportSubmodule("NODE_ACCOUNT_ID_ENDPOINT_MODE_CONFIG_OPTIONS", |
| 89 | + null, AwsDependency.AWS_SDK_CORE, |
| 90 | + "/account-id-endpoint"); |
| 91 | + writer.write( |
| 92 | + "loadNodeConfig(NODE_ACCOUNT_ID_ENDPOINT_MODE_CONFIG_OPTIONS)"); |
| 93 | + }); |
| 94 | + break; |
| 95 | + default: |
| 96 | + LOGGER.warning("AccountIdEndpointMode config not supported for target: " + target); |
| 97 | + break; |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + return runtimeConfigs; |
| 103 | + } |
36 | 104 |
|
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 |
| - } |
| 105 | + // AccountIdEndpointMode resolver |
| 106 | + @Override |
| 107 | + public List<RuntimeClientPlugin> getClientPlugins() { |
| 108 | + return List.of( |
| 109 | + RuntimeClientPlugin.builder() |
| 110 | + .inputConfig( |
| 111 | + Symbol.builder() |
| 112 | + .namespace( |
| 113 | + AwsDependency.AWS_SDK_CORE.getPackageName() + "/account-id-endpoint", "/" |
| 114 | + ) |
| 115 | + .name("AccountIdEndpointModeInputConfig") |
| 116 | + .build() |
| 117 | + ) |
| 118 | + .resolvedConfig( |
| 119 | + Symbol.builder() |
| 120 | + .namespace( |
| 121 | + AwsDependency.AWS_SDK_CORE.getPackageName() + "/account-id-endpoint", "/" |
| 122 | + ) |
| 123 | + .name("AccountIdEndpointModeResolvedConfig") |
| 124 | + .build() |
| 125 | + ) |
| 126 | + .resolveFunction( |
| 127 | + Symbol.builder() |
| 128 | + .namespace( |
| 129 | + AwsDependency.AWS_SDK_CORE.getPackageName() + "/account-id-endpoint", "/" |
| 130 | + ) |
| 131 | + .name("resolveAccountIdEndpointModeConfig") |
| 132 | + .build() |
| 133 | + ) |
| 134 | + .servicePredicate((m, s) -> hasAccountIdEndpointParam(s)) |
| 135 | + .build() |
| 136 | + ); |
| 137 | + } |
61 | 138 |
|
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 |
| - } |
| 139 | + private boolean hasAccountIdEndpointParam(ServiceShape service) { |
| 140 | + Optional<EndpointRuleSetTrait> endpointRuleSetTrait = service.getTrait(EndpointRuleSetTrait.class); |
| 141 | + if (endpointRuleSetTrait.isPresent()) { |
| 142 | + RuleSetParameterFinder ruleSetParameterFinder = new RuleSetParameterFinder(service); |
| 143 | + if (ruleSetParameterFinder.getBuiltInParams().containsKey("AccountIdEndpointMode")) { |
| 144 | + return true; |
| 145 | + } |
| 146 | + } |
| 147 | + return false; |
| 148 | + } |
| 149 | +} |
0 commit comments