Skip to content

Commit

Permalink
Use @smithy scoped packages (#766)
Browse files Browse the repository at this point in the history
* Use @smithy scoped packages

* Use updated addImport method
  • Loading branch information
srchase authored May 16, 2023
1 parent ba18bd8 commit afce279
Show file tree
Hide file tree
Showing 13 changed files with 32 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ public static ApplicationProtocol createDefaultHttpApplicationProtocol() {
.alias("__HttpHandlerOptions")
.build(),
SymbolReference.builder()
.symbol(createHttpSymbol(TypeScriptDependency.AWS_SDK_PROTOCOL_HTTP, "HttpRequest"))
.symbol(createHttpSymbol(TypeScriptDependency.PROTOCOL_HTTP, "HttpRequest"))
.alias("__HttpRequest")
.build(),
SymbolReference.builder()
.symbol(createHttpSymbol(TypeScriptDependency.AWS_SDK_PROTOCOL_HTTP, "HttpResponse"))
.symbol(createHttpSymbol(TypeScriptDependency.PROTOCOL_HTTP, "HttpResponse"))
.alias("__HttpResponse")
.build()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public static String getOperationDeserializerContextType(
private static List<String> getDefaultOperationSerdeContextTypes(TypeScriptWriter writer) {
List<String> contextInterfaceList = new ArrayList<>();
// Get default SerdeContext.
writer.addImport("SerdeContext", "__SerdeContext", "@aws-sdk/types");
writer.addImport("SerdeContext", "__SerdeContext", TypeScriptDependency.SMITHY_TYPES);
contextInterfaceList.add("__SerdeContext");
return contextInterfaceList;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,8 @@ private void initializeWriterIfNeeded() {
if (writer == null) {
context.getWriterDelegator().useFileWriter(createTestCaseFilename(), writer -> this.writer = writer);
writer.addDependency(TypeScriptDependency.AWS_SDK_TYPES);
writer.addDependency(TypeScriptDependency.AWS_SDK_PROTOCOL_HTTP);
writer.addDependency(TypeScriptDependency.SMITHY_TYPES);
writer.addDependency(TypeScriptDependency.PROTOCOL_HTTP);
// Add the template to each generated test.
writer.write(IoUtils.readUtf8Resource(getClass(), "protocol-test-stub.ts"));
}
Expand Down Expand Up @@ -580,7 +581,7 @@ private String registerBodyComparatorStub(String mediaType) {
additionalStubs.add("protocol-test-xml-stub.ts");
return "compareEquivalentXmlBodies(bodyString, r.body.toString())";
case "application/octet-stream":
writer.addImport("Encoder", "__Encoder", "@aws-sdk/types");
writer.addImport("Encoder", "__Encoder", TypeScriptDependency.SMITHY_TYPES);
additionalStubs.add("protocol-test-octet-stream-stub.ts");
return "compareEquivalentOctetStreamBodies(utf8Encoder, bodyString, r.body)";
case "text/plain":
Expand All @@ -589,7 +590,7 @@ private String registerBodyComparatorStub(String mediaType) {
default:
LOGGER.warning("Unable to compare bodies with unknown media type `" + mediaType
+ "`, defaulting to direct comparison.");
writer.addImport("Encoder", "__Encoder", "@aws-sdk/types");
writer.addImport("Encoder", "__Encoder", TypeScriptDependency.SMITHY_TYPES);
additionalStubs.add("protocol-test-unknown-type-stub.ts");
return "compareEquivalentUnknownTypeBodies(utf8Encoder, bodyString, r.body)";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,8 @@ private static void addCommonHandlerImports(TypeScriptWriter writer) {
writer.addImport("InternalFailureException", "__InternalFailureException", "@aws-smithy/server-common");
writer.addImport("SerializationException", "__SerializationException", "@aws-smithy/server-common");
writer.addImport("SmithyFrameworkException", "__SmithyFrameworkException", "@aws-smithy/server-common");
writer.addImport("HttpRequest", "__HttpRequest", "@aws-sdk/protocol-http");
writer.addImport("HttpResponse", "__HttpResponse", "@aws-sdk/protocol-http");
writer.addImport("HttpRequest", "__HttpRequest", TypeScriptDependency.PROTOCOL_HTTP.packageName);
writer.addImport("HttpResponse", "__HttpResponse", TypeScriptDependency.PROTOCOL_HTTP.packageName);
writer.addImport("ServiceException", "__ServiceException", "@aws-smithy/server-common");
writer.addImport("ValidationCustomizer", "__ValidationCustomizer", "@aws-smithy/server-common");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ private void generateClientDefaults() {
.openBlock("export interface ClientDefaults\n"
+ " extends Partial<__SmithyResolvedConfiguration<$T>> {", "}",
applicationProtocol.getOptionsType(), () -> {
writer.addImport("HttpHandler", "__HttpHandler", "@aws-sdk/protocol-http");
writer.addImport("HttpHandler", "__HttpHandler", TypeScriptDependency.PROTOCOL_HTTP.packageName);
writer.writeDocs("The HTTP handler to use. Fetch in browser and Https in Nodejs.");
writer.write("requestHandler?: __HttpHandler;\n");

Expand All @@ -262,14 +262,14 @@ private void generateClientDefaults() {
+ "@internal");
writer.write("bodyLengthChecker?: __BodyLengthCalculator;\n");

writer.addImport("StreamCollector", "__StreamCollector", "@aws-sdk/types");
writer.addImport("StreamCollector", "__StreamCollector", TypeScriptDependency.SMITHY_TYPES);
writer.writeDocs("A function that converts a stream into an array of bytes.\n"
+ "@internal");
writer.write("streamCollector?: __StreamCollector;\n");

// Note: Encoder and Decoder are both used for base64 and UTF.
writer.addImport("Encoder", "__Encoder", "@aws-sdk/types");
writer.addImport("Decoder", "__Decoder", "@aws-sdk/types");
writer.addImport("Encoder", "__Encoder", TypeScriptDependency.SMITHY_TYPES);
writer.addImport("Decoder", "__Decoder", TypeScriptDependency.SMITHY_TYPES);

writer.writeDocs("The function that will be used to convert a base64-encoded string to a byte array.\n"
+ "@internal");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public enum TypeScriptDependency implements SymbolDependencyContainer {

AWS_SDK_CLIENT_DOCGEN("devDependencies", "@aws-sdk/service-client-documentation-generator", true),
AWS_SDK_TYPES("dependencies", "@aws-sdk/types", true),
SMITHY_TYPES("dependencies", "@smithy/types", "^1.0.0", true),
AWS_SMITHY_CLIENT("dependencies", "@aws-sdk/smithy-client", true),
INVALID_DEPENDENCY("dependencies", "@aws-sdk/invalid-dependency", true),
CONFIG_RESOLVER("dependencies", "@aws-sdk/config-resolver", true),
Expand Down Expand Up @@ -83,7 +84,7 @@ public enum TypeScriptDependency implements SymbolDependencyContainer {
BODY_CHECKSUM("dependencies", "@aws-sdk/middleware-apply-body-checksum", false),

// Conditionally added when using an HTTP application protocol.
AWS_SDK_PROTOCOL_HTTP("dependencies", "@aws-sdk/protocol-http", false),
PROTOCOL_HTTP("dependencies", "@smithy/protocol-http", "^1.0.1", false),
AWS_SDK_FETCH_HTTP_HANDLER("dependencies", "@aws-sdk/fetch-http-handler", false),
AWS_SDK_NODE_HTTP_HANDLER("dependencies", "@aws-sdk/node-http-handler", false),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ private void generateEndpointParameters() {
Paths.get(CodegenUtils.SOURCE_FOLDER, ENDPOINT_FOLDER, ENDPOINT_PARAMETERS_FILE).toString(),
writer -> {
writer.addImport("EndpointParameters", "__EndpointParameters", "@aws-sdk/types");
writer.addImport("Provider", null, "@aws-sdk/types");
writer.addImport("Provider", null, TypeScriptDependency.SMITHY_TYPES);

writer.openBlock(
"export interface ClientInputEndpointParameters {",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import software.amazon.smithy.model.node.NodeVisitor;
import software.amazon.smithy.model.node.ObjectNode;
import software.amazon.smithy.model.node.StringNode;
import software.amazon.smithy.typescript.codegen.TypeScriptDependency;
import software.amazon.smithy.typescript.codegen.TypeScriptWriter;

/**
Expand Down Expand Up @@ -65,9 +66,9 @@ public Void objectNode(ObjectNode node) {
ParameterGenerator parameterGenerator = new ParameterGenerator(localKey, param);

if (localKey.equals("endpoint")) {
writer.addImport("Endpoint", null, "@aws-sdk/types");
writer.addImport("Endpoint", null, TypeScriptDependency.SMITHY_TYPES);
writer.addImport("EndpointV2", null, "@aws-sdk/types");
writer.addImport("Provider", null, "@aws-sdk/types");
writer.addImport("Provider", null, TypeScriptDependency.SMITHY_TYPES);
}

if (writeDefaults) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void addConfigInterfaceFields(
SymbolProvider symbolProvider,
TypeScriptWriter writer
) {
writer.addImport("Provider", "__Provider", TypeScriptDependency.AWS_SDK_TYPES.packageName);
writer.addImport("Provider", "__Provider", TypeScriptDependency.SMITHY_TYPES);
writer.addImport("Logger", "__Logger", TypeScriptDependency.AWS_SDK_TYPES.packageName);

writer.writeDocs("Value for how many times a request will be made at most in case of retry.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void addConfigInterfaceFields(
writer.addDependency(TypeScriptDependency.AWS_SDK_UTIL_DEFAULTS_MODE_BROWSER);
writer.addDependency(TypeScriptDependency.AWS_SDK_UTIL_DEFAULTS_MODE_NODE);
writer.addImport("DefaultsMode", "__DefaultsMode", TypeScriptDependency.AWS_SMITHY_CLIENT.packageName);
writer.addImport("Provider", "__Provider", TypeScriptDependency.AWS_SDK_TYPES.packageName);
writer.addImport("Provider", "__Provider", TypeScriptDependency.SMITHY_TYPES);
writer.writeDocs("The {@link @aws-sdk/smithy-client#DefaultsMode} that "
+ "will be used to determine how certain default configuration "
+ "options are resolved in the SDK.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -384,8 +384,8 @@ public void generateServiceHandlerFactory(GenerationContext context) {
Paths.get(".", CodegenUtils.SOURCE_FOLDER, PROTOCOLS_FOLDER,
ProtocolGenerator.getSanitizedName(getName())).toString());
writer.addImport("ValidationCustomizer", "__ValidationCustomizer", "@aws-smithy/server-common");
writer.addImport("HttpRequest", "__HttpRequest", "@aws-sdk/protocol-http");
writer.addImport("HttpResponse", "__HttpResponse", "@aws-sdk/protocol-http");
writer.addImport("HttpRequest", "__HttpRequest", TypeScriptDependency.PROTOCOL_HTTP.packageName);
writer.addImport("HttpResponse", "__HttpResponse", TypeScriptDependency.PROTOCOL_HTTP.packageName);

Symbol serviceSymbol = symbolProvider.toSymbol(context.getService());
Symbol handlerSymbol = serviceSymbol.expectProperty("handler", Symbol.class);
Expand Down Expand Up @@ -438,8 +438,8 @@ public void generateOperationHandlerFactory(GenerationContext context, Operation
writer.addImport("serializeFrameworkException", null,
Paths.get(".", CodegenUtils.SOURCE_FOLDER, PROTOCOLS_FOLDER,
ProtocolGenerator.getSanitizedName(getName())).toString());
writer.addImport("HttpRequest", "__HttpRequest", "@aws-sdk/protocol-http");
writer.addImport("HttpResponse", "__HttpResponse", "@aws-sdk/protocol-http");
writer.addImport("HttpRequest", "__HttpRequest", TypeScriptDependency.PROTOCOL_HTTP.packageName);
writer.addImport("HttpResponse", "__HttpResponse", TypeScriptDependency.PROTOCOL_HTTP.packageName);

final Symbol operationSymbol = symbolProvider.toSymbol(operation);
final Symbol inputType = operationSymbol.expectProperty("inputType", Symbol.class);
Expand Down Expand Up @@ -650,7 +650,7 @@ private void generateOperationRequestSerializer(

// Ensure that the request type is imported.
writer.addUseImports(requestType);
writer.addImport("Endpoint", "__Endpoint", "@aws-sdk/types");
writer.addImport("Endpoint", "__Endpoint", TypeScriptDependency.SMITHY_TYPES);

// e.g., se_ES
String methodName = ProtocolGenerator.getSerFunctionShortName(symbol);
Expand Down Expand Up @@ -1729,7 +1729,7 @@ private void generateOperationRequestDeserializer(

// Ensure that the request type is imported.
writer.addUseImports(requestType);
writer.addImport("Endpoint", "__Endpoint", "@aws-sdk/types");
writer.addImport("Endpoint", "__Endpoint", TypeScriptDependency.SMITHY_TYPES);
String methodName = ProtocolGenerator.getGenericDeserFunctionName(symbol) + "Request";
// Add the normalized input type.
Symbol inputType = symbol.expectProperty("inputType", Symbol.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ static void generateMetadataDeserializer(GenerationContext context, SymbolRefere
static void generateCollectBody(GenerationContext context) {
TypeScriptWriter writer = context.getWriter();

writer.addImport("SerdeContext", "__SerdeContext", "@aws-sdk/types");
writer.addImport("SerdeContext", "__SerdeContext", TypeScriptDependency.SMITHY_TYPES);
writer.write("// Collect low-level response body stream to Uint8Array.");
writer.openBlock("const collectBody = (streamBody: any = new Uint8Array(), context: __SerdeContext): "
+ "Promise<Uint8Array> => {", "};", () -> {
Expand All @@ -278,7 +278,7 @@ static void generateCollectBody(GenerationContext context) {
static void generateCollectBodyString(GenerationContext context) {
TypeScriptWriter writer = context.getWriter();

writer.addImport("SerdeContext", "__SerdeContext", "@aws-sdk/types");
writer.addImport("SerdeContext", "__SerdeContext", TypeScriptDependency.SMITHY_TYPES);
writer.write("// Encode Uint8Array data into string with utf-8.");
writer.write("const collectBodyString = (streamBody: any, context: __SerdeContext): Promise<string> => "
+ "collectBody(streamBody, context).then(body => context.utf8Encoder(body))");
Expand Down Expand Up @@ -435,8 +435,7 @@ static void writeHostPrefix(GenerationContext context, OperationShape operation)
writer.write("let { hostname: resolvedHostname } = await context.endpoint();");
// Check if disableHostPrefixInjection has been set to true at runtime
writer.openBlock("if (context.disableHostPrefix !== true) {", "}", () -> {
writer.addImport("isValidHostname", "__isValidHostname",
TypeScriptDependency.AWS_SDK_PROTOCOL_HTTP.packageName);
writer.addImport("isValidHostname", "__isValidHostname", TypeScriptDependency.PROTOCOL_HTTP);
writer.write("resolvedHostname = $S + resolvedHostname;", trait.getHostPrefix().toString());
if (operation.getInput().isPresent()) {
List<SmithyPattern.Segment> prefixLabels = trait.getHostPrefix().getLabels();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public void generateSharedComponents(GenerationContext context) {
// Write a function to generate HTTP requests since they're so similar.
SymbolReference requestType = getApplicationProtocol().getRequestType();
writer.addUseImports(requestType);
writer.addImport("SerdeContext", "__SerdeContext", "@aws-sdk/types");
writer.addImport("SerdeContext", "__SerdeContext", TypeScriptDependency.SMITHY_TYPES);
writer.addImport("HeaderBag", "__HeaderBag", "@aws-sdk/types");
writer.openBlock("const buildHttpRpcRequest = async (\n"
+ " context: __SerdeContext,\n"
Expand Down Expand Up @@ -231,7 +231,7 @@ private void generateOperationSerializer(GenerationContext context, OperationSha

// Ensure that the request type is imported.
writer.addUseImports(requestType);
writer.addImport("Endpoint", "__Endpoint", "@aws-sdk/types");
writer.addImport("Endpoint", "__Endpoint", TypeScriptDependency.SMITHY_TYPES);
// e.g., se_ES
String methodName = ProtocolGenerator.getSerFunctionShortName(symbol);
// e.g., serializeAws_restJson1_1ExecuteStatement
Expand Down

0 comments on commit afce279

Please sign in to comment.