Skip to content

Commit 0151159

Browse files
authored
Define headers while declaring (#175)
1 parent b3b5e8e commit 0151159

File tree

2 files changed

+45
-36
lines changed

2 files changed

+45
-36
lines changed

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

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -352,40 +352,46 @@ private void writeHeaders(
352352
SymbolProvider symbolProvider = context.getSymbolProvider();
353353

354354
// Headers are always present either from the default document or the payload.
355-
writer.write("const headers: any = {};");
356-
writer.write("headers['Content-Type'] = $S;", bindingIndex.determineRequestContentType(
357-
operation, getDocumentContentType()));
358-
writeDefaultHeaders(context, operation);
355+
writer.openBlock("const headers: any = {", "};",
356+
() -> {
357+
writer.write("'Content-Type': $S,", bindingIndex.determineRequestContentType(
358+
operation, getDocumentContentType()));
359+
writeDefaultHeaders(context, operation);
359360

360-
operation.getInput().ifPresent(outputId -> {
361-
Model model = context.getModel();
362-
for (HttpBinding binding : bindingIndex.getRequestBindings(operation, Location.HEADER)) {
363-
String memberLocation = "input." + symbolProvider.toMemberName(binding.getMember());
364-
writer.openBlock("if (isSerializableHeaderValue($1L)) {", "}", memberLocation, () -> {
365-
Shape target = model.expectShape(binding.getMember().getTarget());
366-
String headerValue = getInputValue(context, binding.getLocation(), memberLocation + "!",
367-
binding.getMember(), target);
368-
writer.write("headers[$S] = $L;", binding.getLocationName(), headerValue);
369-
});
370-
}
371-
372-
// Handle assembling prefix headers.
373-
for (HttpBinding binding : bindingIndex.getRequestBindings(operation, Location.PREFIX_HEADERS)) {
374-
String memberLocation = "input." + symbolProvider.toMemberName(binding.getMember());
375-
writer.openBlock("if ($L !== undefined) {", "}", memberLocation, () -> {
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("Object.keys($L).forEach(suffix => {", "});", memberLocation, () -> {
380-
// Use a ! since we already validated the input member is defined above.
381-
String headerValue = getInputValue(context, binding.getLocation(),
382-
memberLocation + "![suffix]", binding.getMember(), target);
383-
// Append the suffix to the defined prefix and serialize the value in to that key.
384-
writer.write("headers[$S + suffix] = $L;", binding.getLocationName(), headerValue);
385-
});
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+
Shape target = model.expectShape(binding.getMember().getTarget());
366+
String headerValue = getInputValue(context, binding.getLocation(), memberLocation + "!",
367+
binding.getMember(), target);
368+
writer.write("...isSerializableHeaderValue($L) && { $S: $L },",
369+
memberLocation, binding.getLocationName(), headerValue);
370+
}
371+
372+
// Handle assembling prefix headers.
373+
for (HttpBinding binding : bindingIndex.getRequestBindings(operation, Location.PREFIX_HEADERS)) {
374+
String memberLocation = "input." + symbolProvider.toMemberName(binding.getMember());
375+
MapShape prefixMap = model.expectShape(binding.getMember().getTarget()).asMapShape().get();
376+
Shape target = model.expectShape(prefixMap.getValue().getTarget());
377+
// Iterate through each entry in the member.
378+
writer.openBlock("...($1L !== undefined) && Object.keys($1L).reduce(", "),", memberLocation,
379+
() -> {
380+
writer.openBlock("(acc: any, suffix: string) => {", "}, {}",
381+
() -> {
382+
// Use a ! since we already validated the input member is defined above.
383+
String headerValue = getInputValue(context, binding.getLocation(),
384+
memberLocation + "![suffix]", binding.getMember(), target);
385+
// Append the prefix to key.
386+
writer.write("acc[$S + suffix] = $L;", binding.getLocationName(), headerValue);
387+
writer.write("return acc;");
388+
});
389+
}
390+
);
391+
}
386392
});
387393
}
388-
});
394+
);
389395
}
390396

391397
private List<HttpBinding> writeRequestBody(
@@ -601,7 +607,7 @@ private String getTimestampInputParam(
601607
* <p>For example:
602608
*
603609
* <pre>{@code
604-
* headers['foo'] = "This is a custom header";
610+
* "foo": "This is a custom header",
605611
* }</pre>
606612
*
607613
* @param context The generation context.

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

Lines changed: 7 additions & 4 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) {
@@ -237,7 +240,7 @@ private boolean writeRequestBody(GenerationContext context, OperationShape opera
237240
* <p>For example:
238241
*
239242
* <pre>{@code
240-
* headers['foo'] = "This is a custom header";
243+
* "foo": "This is a custom header",
241244
* }</pre>
242245
*
243246
* @param context The generation context.

0 commit comments

Comments
 (0)