1
1
/*
2
- * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3
- *
4
- * Licensed under the Apache License, Version 2.0 (the "License").
5
- * You may not use this file except in compliance with the License.
6
- * A copy of the License is located at
7
- *
8
- * http://aws.amazon.com/apache2.0
9
- *
10
- * or in the "license" file accompanying this file. This file is distributed
11
- * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12
- * express or implied. See the License for the specific language governing
13
- * permissions and limitations under the License.
2
+ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3
+ * SPDX-License-Identifier: Apache-2.0
14
4
*/
15
5
16
6
package software .amazon .smithy .aws .typescript .codegen ;
17
7
8
+ import static software .amazon .smithy .aws .typescript .codegen .AwsTraitsUtils .isAwsService ;
9
+
10
+ import java .util .Collections ;
11
+ import java .util .HashMap ;
12
+ import java .util .List ;
13
+ import java .util .Map ;
14
+ import java .util .function .Consumer ;
15
+ import java .util .logging .Logger ;
18
16
import software .amazon .smithy .aws .traits .ServiceTrait ;
19
17
import software .amazon .smithy .codegen .core .SymbolProvider ;
20
18
import software .amazon .smithy .model .Model ;
28
26
import software .amazon .smithy .typescript .codegen .integration .TypeScriptIntegration ;
29
27
import software .amazon .smithy .utils .SmithyInternalApi ;
30
28
31
- import java .util .Collections ;
32
- import java .util .HashMap ;
33
- import java .util .List ;
34
- import java .util .Map ;
35
- import java .util .function .Consumer ;
36
- import java .util .logging .Logger ;
37
-
38
29
/**
39
30
* Generates accountIdEndpointMode configuration field for service clients that have the eponymous built-in param in the ruleset.
40
31
*/
@@ -52,71 +43,62 @@ public void addConfigInterfaceFields(
52
43
) {
53
44
if (isAwsService (settings , model )) {
54
45
ServiceShape service = settings .getService (model );
55
- EndpointRuleSetTrait endpointRuleSetTrait = service .getTrait (EndpointRuleSetTrait .class )
56
- .orElseThrow (() -> new RuntimeException ("service missing EndpointRuleSetTrait" ));
57
- RuleSetParameterFinder ruleSetParameterFinder = new RuleSetParameterFinder (service );
58
- if (ruleSetParameterFinder .getBuiltInParams ().containsKey ("AccountIdEndpointMode" )) {
59
- writer .writeDocs ("Defines if the AWS AccountId will be used for endpoint routing." )
60
- .write ("accountIdEndpointMode?: AccountIdEndpointMode | __Provider<AccountIdEndpointMode>;\n " );
46
+ 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
+ writer .addImport ("AccountIdEndpointMode" , "AccountIdEndpointMode" , AwsDependency .AWS_SDK_CORE );
52
+ writer .writeDocs ("Defines if the AWS AccountId will be used for endpoint routing." );
53
+ writer .write ("accountIdEndpointMode?: AccountIdEndpointMode | __Provider<AccountIdEndpointMode>;\n " );
54
+ }
61
55
}
62
56
}
63
57
}
64
-
65
- @ Override
66
- public void prepareCustomizations (
67
- TypeScriptWriter writer ,
68
- LanguageTarget target ,
69
- TypeScriptSettings settings ,
70
- Model model
71
- ) {
72
- if (isAwsService (settings , model ) && target .equals (LanguageTarget .NODE )) {
73
- writer .addDependency (AwsDependency .AWS_SDK_CORE );
74
- writer .addImport ("emitWarningIfUnsupportedVersion" , "awsCheckVersion" , AwsDependency .AWS_SDK_CORE );
75
- writer .write ("awsCheckVersion(process.version);" );
76
- }
77
- }
58
+
78
59
79
60
@ Override
80
61
public Map <String , Consumer <TypeScriptWriter >> getRuntimeConfigWriters (
81
62
TypeScriptSettings settings ,
82
63
Model model ,
83
64
SymbolProvider symbolProvider ,
84
65
LanguageTarget target
85
- ) {
86
- ServiceShape service = settings .getService (model );
87
- Map <String , Consumer <TypeScriptWriter >> runtimeConfigs = new HashMap <>();
88
- if (isAwsService (settings , model )) {
89
- EndpointRuleSetTrait endpointRuleSetTrait = service .getTrait (EndpointRuleSetTrait .class )
90
- .orElseThrow (() -> new RuntimeException ("service missing EndpointRuleSetTrait" ));
91
- RuleSetParameterFinder ruleSetParameterFinder = new RuleSetParameterFinder (service );
92
- if (ruleSetParameterFinder .getBuiltInParams ().containsKey ("AccountIdEndpointMode" )) {
93
- switch (target ) {
94
- case BROWSER :
95
- runtimeConfigs .put ("accountIdEndpointMode" , writer -> {
96
- writer .addDependency (TypeScriptDependency .CONFIG_RESOLVER );
97
- writer .addImport ("DEFAULT_ACCOUNT_ID_ENDPOINT_MODE" , "DEFAULT_ACCOUNT_ID_ENDPOINT_MODE" ,
98
- TypeScriptDependency .CONFIG_RESOLVER );
99
- writer .write ("(() => Promise.resolve(DEFAULT_ACCOUNT_ID_ENDPOINT_MODE))" );
100
- });
101
- break ;
102
- case NODE :
103
- runtimeConfigs .put ("accountIdEndpointMode" , writer -> {
104
- writer .addDependency (TypeScriptDependency .NODE_CONFIG_PROVIDER );
105
- writer .addImport ("loadConfig" , "loadNodeConfig" ,
106
- TypeScriptDependency .NODE_CONFIG_PROVIDER );
107
- writer .addDependency (TypeScriptDependency .CONFIG_RESOLVER );
108
- writer .addImport ("NODE_ACCOUNT_ID_ENDPOINT_MODE_CONFIG_OPTIONS" , "NODE_ACCOUNT_ID_ENDPOINT_MODE_CONFIG_OPTIONS" ,
109
- TypeScriptDependency .CONFIG_RESOLVER );
110
- writer .write (
111
- "loadNodeConfig(NODE_ACCOUNT_ID_ENDPOINT_MODE_CONFIG_OPTIONS" );
112
- });
113
- break ;
114
- default :
115
- LOGGER .warning ("AccountIdEndpointMode config not supported for target: " + target );
116
- break ;
66
+ ) {
67
+ ServiceShape service = settings .getService (model );
68
+ Map <String , Consumer <TypeScriptWriter >> runtimeConfigs = new HashMap <>();
69
+ if (isAwsService (settings , model )) {
70
+ EndpointRuleSetTrait endpointRuleSetTrait = service .getTrait (EndpointRuleSetTrait .class );
71
+ if (endpointRuleSetTrait .isPresent ()) {
72
+ RuleSetParameterFinder ruleSetParameterFinder = new RuleSetParameterFinder (service );
73
+ if (ruleSetParameterFinder .getBuiltInParams ().containsKey ("AccountIdEndpointMode" )) {
74
+ switch (target ) {
75
+ case BROWSER :
76
+ runtimeConfigs .put ("accountIdEndpointMode" , writer -> {
77
+ writer .addDependency (AwsDependency .AWS_SDK_CORE );
78
+ writer .addImport ("DEFAULT_ACCOUNT_ID_ENDPOINT_MODE" , "DEFAULT_ACCOUNT_ID_ENDPOINT_MODE" ,
79
+ AwsDependency .AWS_SDK_CORE );
80
+ writer .write ("(() => Promise.resolve(DEFAULT_ACCOUNT_ID_ENDPOINT_MODE))" );
81
+ });
82
+ break ;
83
+ case NODE :
84
+ runtimeConfigs .put ("accountIdEndpointMode" , writer -> {
85
+ writer .addDependency (TypeScriptDependency .NODE_CONFIG_PROVIDER );
86
+ writer .addImport ("loadConfig" , "loadNodeConfig" ,
87
+ TypeScriptDependency .NODE_CONFIG_PROVIDER );
88
+ writer .addDependency (TypeScriptDependency .CONFIG_RESOLVER );
89
+ writer .addImport ("NODE_ACCOUNT_ID_ENDPOINT_MODE_CONFIG_OPTIONS" , "NODE_ACCOUNT_ID_ENDPOINT_MODE_CONFIG_OPTIONS" ,
90
+ AwsDependency .AWS_SDK_CORE );
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
+ }
117
100
}
118
101
}
102
+ return runtimeConfigs ;
119
103
}
120
- return runtimeConfigs ;
121
- }
122
104
}
0 commit comments