Skip to content

Commit a0ef41a

Browse files
JordonPhillipssrchase
authored andcommitted
Create and use ServerSerdeContext interface
1 parent a327bff commit a0ef41a

File tree

4 files changed

+21
-14
lines changed

4 files changed

+21
-14
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,8 @@ private void writeErrorChecker() {
180180
}
181181

182182
private void writeErrorHandler() {
183-
writer.addImport("SerdeContext", null, "@aws-sdk/types");
184-
writer.openBlock("serializeError(error: $T, ctx: Omit<SerdeContext, 'endpoint'>): Promise<$T> {", "}",
183+
writer.addImport("ServerSerdeContext", null, "@aws-smithy/server-common");
184+
writer.openBlock("serializeError(error: $T, ctx: ServerSerdeContext): Promise<$T> {", "}",
185185
errorsType, applicationProtocol.getResponseType(), () -> {
186186
if (operation.getErrors().isEmpty()) {
187187
writer.write("throw error;");

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ static void generateServiceHandler(SymbolProvider symbolProvider,
5252
Symbol serviceSymbol = symbolProvider.toSymbol(serviceShape);
5353
Symbol handlerSymbol = serviceSymbol.expectProperty("handler", Symbol.class);
5454
Symbol operationsType = serviceSymbol.expectProperty("operations", Symbol.class);
55+
writer.addImport("ServerSerdeContext", null, "@aws-smithy/server-common");
5556

5657
writeSerdeContextBase(writer);
5758
writeHandleFunction(writer);
@@ -62,7 +63,7 @@ static void generateServiceHandler(SymbolProvider symbolProvider,
6263
writer.write("private serializerFactory: <T extends $T>(operation: T) => "
6364
+ "__OperationSerializer<$T, T, __SmithyException>;", operationsType, serviceSymbol);
6465
writer.write("private serializeFrameworkException: (e: __SmithyFrameworkException, "
65-
+ "ctx: Omit<SerdeContext, 'endpoint'>) => Promise<__HttpResponse>;");
66+
+ "ctx: ServerSerdeContext) => Promise<__HttpResponse>;");
6667
writer.writeDocs(() -> {
6768
writer.write("Construct a $T handler.", serviceSymbol);
6869
writer.write("@param service The {@link $1T} implementation that supplies the business logic for $1T",
@@ -81,8 +82,8 @@ static void generateServiceHandler(SymbolProvider symbolProvider,
8182
writer.write("mux: __Mux<$S, $T>,", serviceShape.getId().getName(), operationsType);
8283
writer.write("serializerFactory:<T extends $T>(op: T) => "
8384
+ "__OperationSerializer<$T, T, __SmithyException>,", operationsType, serviceSymbol);
84-
writer.write("serializeFrameworkException: (e: __SmithyFrameworkException, ctx: Omit<SerdeContext, "
85-
+ "'endpoint'>) => Promise<__HttpResponse>");
85+
writer.write("serializeFrameworkException: (e: __SmithyFrameworkException, ctx: ServerSerdeContext) => "
86+
+ "Promise<__HttpResponse>");
8687
});
8788
writer.indent();
8889
writer.write("this.service = service;");
@@ -127,14 +128,15 @@ static void generateOperationHandler(SymbolProvider symbolProvider,
127128
Symbol outputSymbol = operationSymbol.expectProperty("outputType", Symbol.class);
128129
Symbol handlerSymbol = operationSymbol.expectProperty("handler", Symbol.class);
129130
Symbol errorsSymbol = operationSymbol.expectProperty("errorsType", Symbol.class);
131+
writer.addImport("ServerSerdeContext", null, "@aws-smithy/server-common");
130132

131133
writer.openBlock("export class $L implements __ServiceHandler {", "}", handlerSymbol.getName(), () -> {
132134
writer.write("private operation: __Operation<$T, $T>;", inputSymbol, outputSymbol);
133135
writer.write("private mux: __Mux<$S, $S>;", serviceShape.getId().getName(), operationName);
134136
writer.write("private serializer: __OperationSerializer<$T, $S, $T>;",
135137
serviceSymbol, operationName, errorsSymbol);
136138
writer.write("private serializeFrameworkException: (e: __SmithyFrameworkException, "
137-
+ "ctx: Omit<SerdeContext, 'endpoint'>) => Promise<__HttpResponse>;");
139+
+ "ctx: ServerSerdeContext) => Promise<__HttpResponse>;");
138140
writer.writeDocs(() -> {
139141
writer.write("Construct a $T handler.", operationSymbol);
140142
writer.write("@param service The {@link __Operation} implementation that supplies the business "
@@ -152,8 +154,8 @@ static void generateOperationHandler(SymbolProvider symbolProvider,
152154
writer.write("mux: __Mux<$S, $S>,", serviceShape.getId().getName(), operationName);
153155
writer.write("serializer: __OperationSerializer<$T, $S, $T>,",
154156
serviceSymbol, operationName, errorsSymbol);
155-
writer.write("serializeFrameworkException: (e: __SmithyFrameworkException, ctx: Omit<SerdeContext, "
156-
+ "'endpoint'>) => Promise<__HttpResponse>");
157+
writer.write("serializeFrameworkException: (e: __SmithyFrameworkException, ctx: ServerSerdeContext) => "
158+
+ "Promise<__HttpResponse>");
157159
});
158160
writer.indent();
159161
writer.write("this.operation = operation;");
@@ -198,7 +200,7 @@ private static void writeHandleFunction(TypeScriptWriter writer) {
198200
writer.write("serializer: __OperationSerializer<S, O, __SmithyException>,");
199201
writer.write("operation: __Operation<__OperationInput<S[O]>, __OperationOutput<S[O]>>,");
200202
writer.write("serializeFrameworkException: (e: __SmithyFrameworkException, "
201-
+ "ctx: Omit<SerdeContext, 'endpoint'>) => Promise<__HttpResponse>");
203+
+ "ctx: ServerSerdeContext) => Promise<__HttpResponse>");
202204
});
203205
writer.indent();
204206
writer.write("let input;");

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,10 +218,11 @@ public void generateFrameworkErrorSerializer(GenerationContext inputContext) {
218218

219219
writer.addImport("SmithyFrameworkException", "__SmithyFrameworkException", "@aws-smithy/server-common");
220220
writer.addUseImports(responseType);
221+
writer.addImport("ServerSerdeContext", null, "@aws-smithy/server-common");
221222

222223
writer.openBlock("export const serializeFrameworkException = async(\n"
223224
+ " input: __SmithyFrameworkException,\n"
224-
+ " ctx: Omit<__SerdeContext, 'endpoint'>\n"
225+
+ " ctx: ServerSerdeContext\n"
225226
+ "): Promise<$T> => {", "}", responseType, () -> {
226227

227228
writeEmptyEndpoint(context);
@@ -419,10 +420,11 @@ private void generateOperationResponseSerializer(
419420
String methodName = ProtocolGenerator.getGenericSerFunctionName(symbol) + "Response";
420421
Symbol outputType = symbol.expectProperty("outputType", Symbol.class);
421422
String contextType = CodegenUtils.getOperationSerializerContextType(writer, context.getModel(), operation);
423+
writer.addImport("ServerSerdeContext", null, "@aws-smithy/server-common");
422424

423425
writer.openBlock("export const $L = async(\n"
424426
+ " input: $T,\n"
425-
+ " ctx: Omit<$L, 'endpoint'>\n"
427+
+ " ctx: ServerSerdeContext\n"
426428
+ "): Promise<$T> => {", "}", methodName, outputType, contextType, responseType, () -> {
427429
writeEmptyEndpoint(context);
428430
writeOperationStatusCode(context, operation, bindingIndex, trait);
@@ -459,10 +461,11 @@ private void generateErrorSerializer(GenerationContext context, StructureShape e
459461

460462
writer.addUseImports(responseType);
461463
String methodName = ProtocolGenerator.getGenericSerFunctionName(symbol) + "Error";
464+
writer.addImport("ServerSerdeContext", null, "@aws-smithy/server-common");
462465

463466
writer.openBlock("export const $L = async(\n"
464467
+ " input: $T,\n"
465-
+ " ctx: Omit<__SerdeContext, 'endpoint'>\n"
468+
+ " ctx: ServerSerdeContext\n"
466469
+ "): Promise<$T> => {", "}", methodName, symbol, responseType, () -> {
467470
writeEmptyEndpoint(context);
468471
generateErrorSerializationImplementation(context, error, responseType, bindingIndex);

smithy-typescript-ssdk-libs/server-common/src/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ export type OperationInput<T> = T extends Operation<infer I, any> ? I : never;
2626
export type OperationOutput<T> = T extends Operation<any, infer O> ? O : never;
2727

2828
export interface OperationSerializer<T, K extends keyof T, E extends SmithyException> {
29-
serialize(input: OperationOutput<T[K]>, ctx: Omit<SerdeContext, "endpoint">): Promise<HttpResponse>;
29+
serialize(input: OperationOutput<T[K]>, ctx: ServerSerdeContext): Promise<HttpResponse>;
3030
deserialize(input: HttpRequest, ctx: SerdeContext): Promise<OperationInput<T[K]>>;
3131
isOperationError(error: any): error is E;
32-
serializeError(error: E, ctx: Omit<SerdeContext, "endpoint">): Promise<HttpResponse>;
32+
serializeError(error: E, ctx: ServerSerdeContext): Promise<HttpResponse>;
3333
}
3434

3535
export interface ServiceHandler<RequestType = HttpRequest, ResponseType = HttpResponse> {
@@ -43,3 +43,5 @@ export interface ServiceCoordinate<S extends string, O extends string> {
4343
export interface Mux<S extends string, O extends string> {
4444
match(req: HttpRequest): ServiceCoordinate<S, O> | undefined;
4545
}
46+
47+
export interface ServerSerdeContext extends Omit<SerdeContext, "endpoint"> {}

0 commit comments

Comments
 (0)