diff --git a/codegen/smithy-ts-codegen/src/main/java/software/amazon/smithy/ts/codegen/templates/AuthGenerator.java b/codegen/smithy-ts-codegen/src/main/java/software/amazon/smithy/ts/codegen/templates/AuthGenerator.java index 098297c..95d6c40 100644 --- a/codegen/smithy-ts-codegen/src/main/java/software/amazon/smithy/ts/codegen/templates/AuthGenerator.java +++ b/codegen/smithy-ts-codegen/src/main/java/software/amazon/smithy/ts/codegen/templates/AuthGenerator.java @@ -2,6 +2,8 @@ import software.amazon.smithy.model.Model; import software.amazon.smithy.model.shapes.ServiceShape; +import software.amazon.smithy.aws.traits.auth.SigV4Trait; +import software.amazon.smithy.ts.codegen.RuntimeTypes; import software.amazon.smithy.ts.codegen.TypeScriptWriter; /** @@ -22,5 +24,8 @@ public AuthGenerator(Model model, ServiceShape service) { // * operation generator has declared request of type HTTPRequest in-scope // * client (and its options) are in-scope from operation signature public void render(TypeScriptWriter writer) { + writer.write("$T(request, client.options.credentials, $S, client.options.region)", + RuntimeTypes.SIGV4.signRequest, + service.expectTrait(SigV4Trait.class).getName()); } } diff --git a/codegen/smithy-ts-codegen/src/main/java/software/amazon/smithy/ts/codegen/templates/ClientGenerator.java b/codegen/smithy-ts-codegen/src/main/java/software/amazon/smithy/ts/codegen/templates/ClientGenerator.java index 03ada28..4eb9fd4 100644 --- a/codegen/smithy-ts-codegen/src/main/java/software/amazon/smithy/ts/codegen/templates/ClientGenerator.java +++ b/codegen/smithy-ts-codegen/src/main/java/software/amazon/smithy/ts/codegen/templates/ClientGenerator.java @@ -1,6 +1,7 @@ package software.amazon.smithy.ts.codegen.templates; import software.amazon.smithy.model.Model; +import software.amazon.smithy.model.knowledge.TopDownIndex; import software.amazon.smithy.model.shapes.ServiceShape; import software.amazon.smithy.ts.codegen.TypeScriptWriter; @@ -35,9 +36,24 @@ public void render(TypeScriptWriter writer) { writer.openBlock("export class $L {", "}", STRUCTURE_NAME, () -> { // 1. render constructor // ... + writer.write("public readonly options: $L", ClientOptionsGenerator.STRUCTURE_NAME); + writer.write(""); + writer.openBlock("constructor(options: $L) {", "}", ClientOptionsGenerator.STRUCTURE_NAME, () -> { + writer.write("this.options = options"); + }); + writer.write(""); // 2. render operation APIs // ... + for (var operation : TopDownIndex.of(model).getContainedOperations(service)) { + var name = operation.getId().getName(); + writer.openBlock("public async $L(input: $L): Promise<$L> {", "}", + name, operation.getInputShape().getName(), operation.getOutputShape().getName(), + () -> { + writer.write("return await do$L(this, input)", name); + }); + writer.write(""); + } }); } } diff --git a/codegen/smithy-ts-codegen/src/main/java/software/amazon/smithy/ts/codegen/templates/ClientOptionsGenerator.java b/codegen/smithy-ts-codegen/src/main/java/software/amazon/smithy/ts/codegen/templates/ClientOptionsGenerator.java index d6a7c00..41f166b 100644 --- a/codegen/smithy-ts-codegen/src/main/java/software/amazon/smithy/ts/codegen/templates/ClientOptionsGenerator.java +++ b/codegen/smithy-ts-codegen/src/main/java/software/amazon/smithy/ts/codegen/templates/ClientOptionsGenerator.java @@ -1,5 +1,6 @@ package software.amazon.smithy.ts.codegen.templates; +import software.amazon.smithy.ts.codegen.RuntimeTypes; import software.amazon.smithy.ts.codegen.TypeScriptWriter; /** @@ -13,7 +14,8 @@ public class ClientOptionsGenerator { public void render(TypeScriptWriter writer) { writer.openBlock("export type $L = {", "}", STRUCTURE_NAME, () -> { - // render config fields... + writer.write("region: string"); + writer.write("credentials: $T", RuntimeTypes.CREDENTIALS.Credentials); }); } } diff --git a/codegen/smithy-ts-codegen/src/main/java/software/amazon/smithy/ts/codegen/templates/EnumGenerator.java b/codegen/smithy-ts-codegen/src/main/java/software/amazon/smithy/ts/codegen/templates/EnumGenerator.java index 7315866..91fcb02 100644 --- a/codegen/smithy-ts-codegen/src/main/java/software/amazon/smithy/ts/codegen/templates/EnumGenerator.java +++ b/codegen/smithy-ts-codegen/src/main/java/software/amazon/smithy/ts/codegen/templates/EnumGenerator.java @@ -2,6 +2,7 @@ import software.amazon.smithy.model.Model; import software.amazon.smithy.model.shapes.EnumShape; +import software.amazon.smithy.model.traits.EnumValueTrait; import software.amazon.smithy.ts.codegen.TypeScriptWriter; public class EnumGenerator { @@ -14,5 +15,14 @@ public EnumGenerator(Model model, EnumShape enumShape) { } public void render(TypeScriptWriter writer) { + var name = enumShape.getId().getName(); + writer.write("export type $L = ", name); + for (var variant : enumShape.getAllMembers().values()) { + var variantValue = variant + .expectTrait(EnumValueTrait.class) + .expectStringValue(); + writer.write("| $S", variantValue); + } + writer.write(""); } } diff --git a/codegen/smithy-ts-codegen/src/main/java/software/amazon/smithy/ts/codegen/templates/OperationGenerator.java b/codegen/smithy-ts-codegen/src/main/java/software/amazon/smithy/ts/codegen/templates/OperationGenerator.java index f065759..ab5b7cb 100644 --- a/codegen/smithy-ts-codegen/src/main/java/software/amazon/smithy/ts/codegen/templates/OperationGenerator.java +++ b/codegen/smithy-ts-codegen/src/main/java/software/amazon/smithy/ts/codegen/templates/OperationGenerator.java @@ -1,9 +1,11 @@ package software.amazon.smithy.ts.codegen.templates; +import software.amazon.smithy.aws.traits.ServiceTrait; import software.amazon.smithy.model.Model; import software.amazon.smithy.model.shapes.OperationShape; import software.amazon.smithy.model.shapes.ServiceShape; import software.amazon.smithy.model.shapes.StructureShape; +import software.amazon.smithy.ts.codegen.RuntimeTypes; import software.amazon.smithy.ts.codegen.TypeScriptWriter; /** @@ -36,10 +38,42 @@ public void render(TypeScriptWriter writer) { String inputName = input.getId().getName(); String outputName = output.getId().getName(); - //writer.openBlock("const do$L = (client: $L, input: $L): Promise<$L> => {", "}", - // opName, ClientGenerator.STRUCTURE_NAME, inputName, outputName, - // () -> { - // // ... - // }); + writer.openBlock("const do$L = async (client: $L, input: $L): Promise<$L> => {", "}", + opName, ClientGenerator.STRUCTURE_NAME, inputName, outputName, + () -> { + // "resolve" our endpoint. we will assume standard AWS regional endpoint scheme. + // normally this happens dynamically after serialization, but in this simplified workshop we can + // just do it now + var endpointPrefix = service.expectTrait(ServiceTrait.class).getEndpointPrefix(); + writer.write("const endpoint = `$L.$${client.options.region}.amazonaws.com`", endpointPrefix); + + // initialize our http request + writer.openBlock("const request: $T = {", "}", RuntimeTypes.HTTP.HTTPRequest, () -> { + writer.write("method: $S,", protocolGenerator.getHttpMethod()); + writer.write("path: $S,", protocolGenerator.getHttpPath()); + writer.write("host: endpoint,"); + writer.write("headers: {},"); + }); + + // serialize the input + protocolGenerator.renderSerialize(writer); + writer.write(""); + + // generate auth code to sign the request with sigv4 + new AuthGenerator(model, service).render(writer); + writer.write(""); + + // do the HTTP request + writer.write("const response = await $T(request)", RuntimeTypes.HTTP.doRequest); + writer.openBlock("if (response.statusCode !== 200) {", "}", () -> { + writer.write("throw new Error('operation failed!')"); + }); + writer.write(""); + + // deserialize and return the output + protocolGenerator.renderDeserialize(writer); + writer.write("return output"); + }); + writer.write(""); } } diff --git a/codegen/smithy-ts-codegen/src/main/java/software/amazon/smithy/ts/codegen/templates/ProtocolGenerator.java b/codegen/smithy-ts-codegen/src/main/java/software/amazon/smithy/ts/codegen/templates/ProtocolGenerator.java index 8ba2b30..3986ece 100644 --- a/codegen/smithy-ts-codegen/src/main/java/software/amazon/smithy/ts/codegen/templates/ProtocolGenerator.java +++ b/codegen/smithy-ts-codegen/src/main/java/software/amazon/smithy/ts/codegen/templates/ProtocolGenerator.java @@ -41,11 +41,19 @@ public String getHttpPath() { // * one line to serialize the request body // * three lines to set headers required by the protocol public void renderSerialize(TypeScriptWriter writer) { + writer.write("request.body = JSON.stringify(input)"); + writer.write("request.headers['X-Amz-Target'] = '$L.$L'", service.getId().getName(), operation.getId().getName()); + writer.write("request.headers['Content-Type'] = 'application/x-amz-json-1.0'"); + writer.write("request.headers['Content-Length'] = request.body.length"); } // implicit contracts: // * operation codegen has declared a variable "response" of type HTTPResponse in-scope // * this method will declare a variable "output" of type corresponding to the operation's output type + // + // HINT: You can ignore deserialization of unions, since SQS does not use any. The solution is one line. public void renderDeserialize(TypeScriptWriter writer) { + var output = operation.getOutputShape().getName(); + writer.write("const output: $L = JSON.parse(response.body || '{}')", output); } } diff --git a/codegen/smithy-ts-codegen/src/main/java/software/amazon/smithy/ts/codegen/templates/StructureGenerator.java b/codegen/smithy-ts-codegen/src/main/java/software/amazon/smithy/ts/codegen/templates/StructureGenerator.java index 74cb36d..f49c7da 100644 --- a/codegen/smithy-ts-codegen/src/main/java/software/amazon/smithy/ts/codegen/templates/StructureGenerator.java +++ b/codegen/smithy-ts-codegen/src/main/java/software/amazon/smithy/ts/codegen/templates/StructureGenerator.java @@ -1,6 +1,12 @@ package software.amazon.smithy.ts.codegen.templates; +import software.amazon.smithy.codegen.core.CodegenException; import software.amazon.smithy.model.Model; +import software.amazon.smithy.model.shapes.CollectionShape; +import software.amazon.smithy.model.shapes.ListShape; +import software.amazon.smithy.model.shapes.MapShape; +import software.amazon.smithy.model.shapes.MemberShape; +import software.amazon.smithy.model.shapes.Shape; import software.amazon.smithy.model.shapes.StructureShape; import software.amazon.smithy.ts.codegen.TypeScriptWriter; @@ -18,5 +24,34 @@ public StructureGenerator(Model model, StructureShape structure) { } public void render(TypeScriptWriter writer) { + writer.openBlock("export type $L = {", "}", structure.getId().getName(), () -> { + for (var member : structure.getAllMembers().entrySet()) { + var memberName = member.getKey(); + var memberShape = model.expectShape(member.getValue().getTarget()); + + writer.write("$L?: $L", memberName, toSymbol(memberShape)); + } + }); + writer.write(""); + } + + private String toSymbol(Shape shape) { + return switch (shape.getType()) { + case BOOLEAN -> "boolean"; + case STRING -> "string"; + case TIMESTAMP -> "Date"; + case BYTE, SHORT, INTEGER, LONG, FLOAT, DOUBLE -> "number"; + case LIST -> { + var listTarget = ((ListShape) shape).getMember().getTarget(); + yield toSymbol(model.expectShape(listTarget)) + "[]"; + } + case MAP -> { + var mapTarget = ((MapShape) shape).getValue().getTarget(); + yield "Record"; + } + case STRUCTURE, ENUM -> shape.getId().getName(); + case BLOB -> "Blob"; + default -> throw new CodegenException("Unsupported type: " + shape.getType()); + }; } } diff --git a/test-client.ts b/test-client.ts new file mode 100644 index 0000000..f28e0b9 --- /dev/null +++ b/test-client.ts @@ -0,0 +1,18 @@ +import { Client } from "./client.ts"; + +(async function () { + const sqsClient = new Client({ + region: process.env.AWS_REGION as string, + credentials: { + akid: process.env.AWS_ACCESS_KEY_ID as string, + secret: process.env.AWS_SECRET_ACCESS_KEY as string, + sessionToken: process.env.AWS_SESSION_TOKEN, + }, + }); + + console.log('credentials', sqsClient.options.credentials); + console.log('region', sqsClient.options.region); + + const response = await sqsClient.ListQueues({}); + console.log(response); +})(); pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy