@@ -57,36 +57,41 @@ public final class AddHttpApiKeyAuthPlugin implements TypeScriptIntegration {
57
57
@ Override
58
58
public List <RuntimeClientPlugin > getClientPlugins () {
59
59
return ListUtils .of (
60
- // Add the config if the service uses HTTP API key authorization
60
+ // Add the config if the service uses HTTP API key authorization.
61
61
RuntimeClientPlugin .builder ()
62
- .inputConfig (Symbol .builder ().namespace (
63
- "./" + CodegenUtils .SOURCE_FOLDER + "/middleware/HttpApiKeyAuth" , "/"
64
- ).name ("HttpApiKeyAuthInputConfig" ).build ())
65
- .resolvedConfig (Symbol .builder ().namespace (
66
- "./" + CodegenUtils .SOURCE_FOLDER + "/middleware/HttpApiKeyAuth" , "/"
67
- ).name ("HttpApiKeyAuthResolvedConfig" ).build ())
68
- .resolveFunction (Symbol .builder ().namespace (
69
- "./" + CodegenUtils .SOURCE_FOLDER + "/middleware/HttpApiKeyAuth" , "/"
70
- ).name ("resolveHttpApiKeyAuthConfig" ).build ())
71
- .servicePredicate ((m , s ) -> hasHttpApiKeyAuthTrait (s ) && !areAllOptionalAuthOperations (m , s ))
72
- .build (),
62
+ .inputConfig (Symbol .builder ()
63
+ .namespace ("./" + CodegenUtils .SOURCE_FOLDER + "/middleware/HttpApiKeyAuth" , "/" )
64
+ .name ("HttpApiKeyAuthInputConfig" )
65
+ .build ())
66
+ .resolvedConfig (Symbol .builder ()
67
+ .namespace ("./" + CodegenUtils .SOURCE_FOLDER + "/middleware/HttpApiKeyAuth" , "/" )
68
+ .name ("HttpApiKeyAuthResolvedConfig" )
69
+ .build ())
70
+ .resolveFunction (Symbol .builder ()
71
+ .namespace ("./" + CodegenUtils .SOURCE_FOLDER + "/middleware/HttpApiKeyAuth" , "/" )
72
+ .name ("resolveHttpApiKeyAuthConfig" )
73
+ .build ())
74
+ .servicePredicate ((m , s ) -> s .hasTrait (HttpApiKeyAuthTrait .class )
75
+ && !areAllOptionalAuthOperations (m , s ))
76
+ .build (),
73
77
74
- // Add the middleware to operations that use HTTP API key authorization
78
+ // Add the middleware to operations that use HTTP API key authorization.
75
79
RuntimeClientPlugin .builder ()
76
- .pluginFunction (Symbol .builder ()
77
- .namespace ("./" + CodegenUtils .SOURCE_FOLDER + "/middleware/HttpApiKeyAuth" , "/" )
78
- .name ("getHttpApiKeyAuthPlugin" )
79
- .build ())
80
- .additionalPluginFunctionParamsSupplier ((m , s , o ) -> new HashMap <String , Object >() {{
81
- // It's safe to do getTrait().get() because the operation predicate ensures that the trait exists
82
- // `in` and `name` are required attributes of the trait, `scheme` is optional
83
- put ("in" , s .getTrait (HttpApiKeyAuthTrait .class ).get ().getIn ().toString ());
84
- put ("name" , s .getTrait (HttpApiKeyAuthTrait .class ).get ().getName ());
85
- put ("scheme" , s .getTrait (HttpApiKeyAuthTrait .class ).get ().getScheme ().orElse (null ));
86
- }})
87
- .operationPredicate ((m , s , o ) -> hasHttpApiKeyAuthTrait (s )
88
- && !operationUsesOptionalAuth (m , s , o ))
89
- .build ()
80
+ .pluginFunction (Symbol .builder ()
81
+ .namespace ("./" + CodegenUtils .SOURCE_FOLDER + "/middleware/HttpApiKeyAuth" , "/" )
82
+ .name ("getHttpApiKeyAuthPlugin" )
83
+ .build ())
84
+ .additionalPluginFunctionParamsSupplier ((m , s , o ) -> new HashMap <String , Object >() {{
85
+ // It's safe to do getTrait().get() because the operation predicate ensures that the trait
86
+ // exists `in` and `name` are required attributes of the trait, `scheme` is optional.
87
+ put ("in" , s .getTrait (HttpApiKeyAuthTrait .class ).get ().getIn ().toString ());
88
+ put ("name" , s .getTrait (HttpApiKeyAuthTrait .class ).get ().getName ());
89
+ s .getTrait (HttpApiKeyAuthTrait .class ).get ().getScheme ().ifPresent (scheme ->
90
+ put ("scheme" , scheme ));
91
+ }})
92
+ .operationPredicate ((m , s , o ) -> s .hasTrait (HttpApiKeyAuthTrait .class )
93
+ && !o .hasTrait (OptionalAuthTrait .class ))
94
+ .build ()
90
95
);
91
96
}
92
97
@@ -101,15 +106,15 @@ public void writeAdditionalFiles(
101
106
102
107
// If the service doesn't use HTTP API keys, we don't need to do anything and the generated
103
108
// code doesn't need any additional files.
104
- if (!hasHttpApiKeyAuthTrait ( service ) || areAllOptionalAuthOperations (model , service )) {
109
+ if (!service . hasTrait ( HttpApiKeyAuthTrait . class ) || areAllOptionalAuthOperations (model , service )) {
105
110
return ;
106
111
}
107
112
108
113
String noTouchNoticePrefix = "// Please do not touch this file. It's generated from a template in:\n "
109
114
+ "// https://github.com/awslabs/smithy-typescript/blob/main/smithy-typescript-codegen/"
110
115
+ "src/main/resources/software/amazon/smithy/aws/typescript/codegen/integration/" ;
111
116
112
- // write the middleware source
117
+ // Write the middleware source.
113
118
writerFactory .accept (
114
119
Paths .get (CodegenUtils .SOURCE_FOLDER , "middleware" , "HttpApiKeyAuth" , "index.ts" ).toString (),
115
120
writer -> {
@@ -118,7 +123,7 @@ public void writeAdditionalFiles(
118
123
writer .write ("$L" , source );
119
124
});
120
125
121
- // write the middleware tests
126
+ // Write the middleware tests.
122
127
writerFactory .accept (
123
128
Paths .get (CodegenUtils .SOURCE_FOLDER , "middleware" , "HttpApiKeyAuth" , "index.spec.ts" ).toString (),
124
129
writer -> {
@@ -128,51 +133,15 @@ public void writeAdditionalFiles(
128
133
});
129
134
}
130
135
131
- /**
132
- * Check if the service has the @httpApiKeyAuth trait.
133
- *
134
- * @param service the service shape
135
- *
136
- * @return true if the service has the @httpApiKeyAuth trait
137
- */
138
- private static boolean hasHttpApiKeyAuthTrait (ServiceShape service ) {
139
- return service .hasTrait (HttpApiKeyAuthTrait .class );
140
- }
141
-
142
- // derived from https://github.com/aws/aws-sdk-js-v3/blob/main/codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AddAwsAuthPlugin.java
136
+ // This is derived from https://github.com/aws/aws-sdk-js-v3/blob/main/codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AddAwsAuthPlugin.java.
143
137
private static boolean areAllOptionalAuthOperations (Model model , ServiceShape service ) {
144
138
TopDownIndex topDownIndex = TopDownIndex .of (model );
145
139
Set <OperationShape > operations = topDownIndex .getContainedOperations (service );
146
140
for (OperationShape operation : operations ) {
147
- if (!operationUsesOptionalAuth ( model , service , operation )) {
141
+ if (!operation . hasTrait ( OptionalAuthTrait . class )) {
148
142
return false ;
149
143
}
150
144
}
151
145
return true ;
152
146
}
153
-
154
- // derived from https://github.com/aws/aws-sdk-js-v3/blob/main/codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AddAwsAuthPlugin.java
155
- private static boolean hasOptionalAuthOperation (Model model , ServiceShape service ) {
156
- TopDownIndex topDownIndex = TopDownIndex .of (model );
157
- Set <OperationShape > operations = topDownIndex .getContainedOperations (service );
158
- for (OperationShape operation : operations ) {
159
- if (operationUsesOptionalAuth (model , service , operation )) {
160
- return true ;
161
- }
162
- }
163
- return false ;
164
- }
165
-
166
- /**
167
- * Check if the operation has @optionalAuth, implying it doesn't need the middleware.
168
- *
169
- * @param model the model
170
- * @param service the service shape
171
- * @param operation the operation shape
172
- *
173
- * @return true if the operation has the @optionalAuth trait
174
- */
175
- private static boolean operationUsesOptionalAuth (Model model , ServiceShape service , OperationShape operation ) {
176
- return operation .hasTrait (OptionalAuthTrait .class );
177
- }
178
147
}
0 commit comments