Skip to content

Commit

Permalink
feat(codegen): export Mux for http binding for server
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
fangyi-zhou committed Jul 18, 2024
1 parent 9d22987 commit bcfe71c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ public Symbol serviceShape(ServiceShape shape) {
intermediate.toBuilder().name(serviceName + "Operations").build());
builder.putProperty("handler",
intermediate.toBuilder().name(serviceName + "Handler").build());
builder.putProperty("mux",
intermediate.toBuilder().name(serviceName + "Mux").build());
return builder.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,10 +296,13 @@ private void generateServiceMux(GenerationContext context) {
writer.addImport("httpbinding", null, TypeScriptDependency.SERVER_COMMON);

Symbol serviceSymbol = context.getSymbolProvider().toSymbol(context.getService());
Symbol operationsSymbol = serviceSymbol.expectProperty("operations", Symbol.class);
Symbol muxSymbol = serviceSymbol.expectProperty("mux", Symbol.class);

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

Symbol serviceSymbol = symbolProvider.toSymbol(context.getService());
Symbol handlerSymbol = serviceSymbol.expectProperty("handler", Symbol.class);
Symbol muxSymbol = serviceSymbol.expectProperty("mux", Symbol.class);
Symbol operationsSymbol = serviceSymbol.expectProperty("operations", Symbol.class);

generateServiceMux(context);

if (context.getSettings().isDisableDefaultValidation()) {
writer.write("export const get$L = <Context>(service: $T<Context>, "
+ "customizer: __ValidationCustomizer<$T>): "
Expand All @@ -406,7 +412,6 @@ public void generateServiceHandlerFactory(GenerationContext context) {
}
writer.indent();

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

writer.write("return new $T(service, mux, serFn, serializeFrameworkException, customizer);", handlerSymbol);
writer.write("return new $T(service, $T, serFn, serializeFrameworkException, customizer);",
handlerSymbol, muxSymbol);

writer.dedent().write("}");
}
Expand Down

0 comments on commit bcfe71c

Please sign in to comment.