Skip to content

Commit b7e1ef1

Browse files
committed
Define headers at declaration
1 parent 0b146f3 commit b7e1ef1

File tree

2 files changed

+46
-34
lines changed

2 files changed

+46
-34
lines changed

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/HttpBindingProtocolGenerator.java

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -353,40 +353,49 @@ private void writeHeaders(
353353
SymbolProvider symbolProvider = context.getSymbolProvider();
354354

355355
// Headers are always present either from the default document or the payload.
356-
writer.write("const headers: any = {};");
357-
writer.write("headers['Content-Type'] = $S;", bindingIndex.determineRequestContentType(
358-
operation, getDocumentContentType()));
359-
writeDefaultHeaders(context, operation);
360-
361-
operation.getInput().ifPresent(outputId -> {
362-
Model model = context.getModel();
363-
for (HttpBinding binding : bindingIndex.getRequestBindings(operation, Location.HEADER)) {
364-
String memberLocation = "input." + symbolProvider.toMemberName(binding.getMember());
365-
writer.openBlock("if (isSerializableHeaderValue($1L)) {", "}", memberLocation, () -> {
366-
Shape target = model.expectShape(binding.getMember().getTarget());
367-
String headerValue = getInputValue(context, binding.getLocation(), memberLocation + "!",
356+
writer.openBlock("const headers: any = {", "};",
357+
() -> {
358+
writer.write("'Content-Type': $S,", bindingIndex.determineRequestContentType(
359+
operation, getDocumentContentType()));
360+
writeDefaultHeaders(context, operation);
361+
362+
operation.getInput().ifPresent(outputId -> {
363+
Model model = context.getModel();
364+
for (HttpBinding binding : bindingIndex.getRequestBindings(operation, Location.HEADER)) {
365+
String memberLocation = "input." + symbolProvider.toMemberName(binding.getMember());
366+
Shape target = model.expectShape(binding.getMember().getTarget());
367+
String headerValue = getInputValue(context, binding.getLocation(), memberLocation + "!",
368368
binding.getMember(), target);
369-
writer.write("headers[$S] = $L;", binding.getLocationName(), headerValue);
370-
});
371-
}
372-
373-
// Handle assembling prefix headers.
374-
for (HttpBinding binding : bindingIndex.getRequestBindings(operation, Location.PREFIX_HEADERS)) {
375-
String memberLocation = "input." + symbolProvider.toMemberName(binding.getMember());
376-
writer.openBlock("if ($L !== undefined) {", "}", memberLocation, () -> {
377-
MapShape prefixMap = model.expectShape(binding.getMember().getTarget()).asMapShape().get();
378-
Shape target = model.expectShape(prefixMap.getValue().getTarget());
379-
// Iterate through each entry in the member.
380-
writer.openBlock("Object.keys($L).forEach(suffix => {", "});", memberLocation, () -> {
381-
// Use a ! since we already validated the input member is defined above.
382-
String headerValue = getInputValue(context, binding.getLocation(),
383-
memberLocation + "![suffix]", binding.getMember(), target);
384-
// Append the suffix to the defined prefix and serialize the value in to that key.
385-
writer.write("headers[$S + suffix] = $L;", binding.getLocationName(), headerValue);
386-
});
369+
writer.write("...isSerializableHeaderValue($L) && { $S: $L },",
370+
memberLocation, binding.getLocationName(), headerValue);
371+
}
372+
373+
// Handle assembling prefix headers.
374+
for (HttpBinding binding : bindingIndex.getRequestBindings(operation, Location.PREFIX_HEADERS)) {
375+
String memberLocation = "input." + symbolProvider.toMemberName(binding.getMember());
376+
MapShape prefixMap = model.expectShape(binding.getMember().getTarget()).asMapShape().get();
377+
Shape target = model.expectShape(prefixMap.getValue().getTarget());
378+
// Iterate through each entry in the member.
379+
writer.openBlock(
380+
"...($L !== undefined) && Object.keys($L).reduce(",
381+
"),",
382+
memberLocation, memberLocation,
383+
() -> {
384+
writer.openBlock("(acc: any, suffix: string) => {", "}, {}",
385+
() -> {
386+
// Use a ! since we already validated the input member is defined above.
387+
String headerValue = getInputValue(context, binding.getLocation(),
388+
memberLocation + "![suffix]", binding.getMember(), target);
389+
// Append the prefix to key.
390+
writer.write("acc[$S + suffix] = $L;", binding.getLocationName(), headerValue);
391+
writer.write("return acc;");
392+
});
393+
}
394+
);
395+
}
387396
});
388397
}
389-
});
398+
);
390399
}
391400

392401
private List<HttpBinding> writeRequestBody(

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/HttpRpcProtocolGenerator.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,12 @@ private void writeRequestHeaders(GenerationContext context, OperationShape opera
193193

194194
// The Content-Type header is always present.
195195
writer.addImport("HeaderBag", "__HeaderBag", "@aws-sdk/types");
196-
writer.write("const headers: __HeaderBag = {};");
197-
writer.write("headers['Content-Type'] = $S;", getDocumentContentType());
198-
writeDefaultHeaders(context, operation);
196+
writer.openBlock("const headers: __HeaderBag = {", "};",
197+
() -> {
198+
writer.write("'Content-Type': $S,", getDocumentContentType());
199+
writeDefaultHeaders(context, operation);
200+
}
201+
);
199202
}
200203

201204
private boolean writeRequestBody(GenerationContext context, OperationShape operation) {

0 commit comments

Comments
 (0)