Skip to content

Commit bcfe71c

Browse files
committed
feat(codegen): export Mux for http binding for server
Closes #1342 It's useful to export the multiplexer that is automatically generated for a service, e.g. for use in anauthorizor lambda. This commit exports the generated multiplexer that is generated inside the service handler so that the multiplexer can be used in other packages.
1 parent 9d22987 commit bcfe71c

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/ServerSymbolVisitor.java

+2
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ public Symbol serviceShape(ServiceShape shape) {
106106
intermediate.toBuilder().name(serviceName + "Operations").build());
107107
builder.putProperty("handler",
108108
intermediate.toBuilder().name(serviceName + "Handler").build());
109+
builder.putProperty("mux",
110+
intermediate.toBuilder().name(serviceName + "Mux").build());
109111
return builder.build();
110112
}
111113

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

+10-4
Original file line numberDiff line numberDiff line change
@@ -296,10 +296,13 @@ private void generateServiceMux(GenerationContext context) {
296296
writer.addImport("httpbinding", null, TypeScriptDependency.SERVER_COMMON);
297297

298298
Symbol serviceSymbol = context.getSymbolProvider().toSymbol(context.getService());
299+
Symbol operationsSymbol = serviceSymbol.expectProperty("operations", Symbol.class);
300+
Symbol muxSymbol = serviceSymbol.expectProperty("mux", Symbol.class);
299301

300-
writer.openBlock("const mux = new httpbinding.HttpBindingMux<$S, keyof $T<Context>>([", "]);",
302+
writer.openBlock("export const $T = new httpbinding.HttpBindingMux<$S, $T>([", "]);",
303+
muxSymbol,
301304
context.getService().getId().getName(),
302-
serviceSymbol,
305+
operationsSymbol,
303306
() -> {
304307
for (OperationShape operation : topDownIndex.getContainedOperations(context.getService())) {
305308
OptionalUtils.ifPresentOrElse(
@@ -392,8 +395,11 @@ public void generateServiceHandlerFactory(GenerationContext context) {
392395

393396
Symbol serviceSymbol = symbolProvider.toSymbol(context.getService());
394397
Symbol handlerSymbol = serviceSymbol.expectProperty("handler", Symbol.class);
398+
Symbol muxSymbol = serviceSymbol.expectProperty("mux", Symbol.class);
395399
Symbol operationsSymbol = serviceSymbol.expectProperty("operations", Symbol.class);
396400

401+
generateServiceMux(context);
402+
397403
if (context.getSettings().isDisableDefaultValidation()) {
398404
writer.write("export const get$L = <Context>(service: $T<Context>, "
399405
+ "customizer: __ValidationCustomizer<$T>): "
@@ -406,7 +412,6 @@ public void generateServiceHandlerFactory(GenerationContext context) {
406412
}
407413
writer.indent();
408414

409-
generateServiceMux(context);
410415
writer.addImport("ServiceException", "__ServiceException", TypeScriptDependency.SERVER_COMMON);
411416
writer.openBlock("const serFn: (op: $1T) => __OperationSerializer<$2T<Context>, $1T, __ServiceException> = "
412417
+ "(op) => {", "};", operationsSymbol, serviceSymbol, () -> {
@@ -430,7 +435,8 @@ public void generateServiceHandlerFactory(GenerationContext context) {
430435
);
431436
}
432437

433-
writer.write("return new $T(service, mux, serFn, serializeFrameworkException, customizer);", handlerSymbol);
438+
writer.write("return new $T(service, $T, serFn, serializeFrameworkException, customizer);",
439+
handlerSymbol, muxSymbol);
434440

435441
writer.dedent().write("}");
436442
}

0 commit comments

Comments
 (0)