Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update README #1057

Merged
merged 5 commits into from
Nov 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
696 changes: 599 additions & 97 deletions README.md

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions scripts/build-generated-test-packages.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,34 @@ const codegenTestDir = path.join(
const weatherClientDir = path.join(
codegenTestDir,
"source",
"typescript-codegen"
"typescript-client-codegen"
);

// TODO(experimentalIdentityAndAuth): build generic client for integration tests
const weatherExperimentalIdentityAndAuthClientDir = path.join(
codegenTestDir,
"client-experimental-identity-and-auth",
"typescript-codegen"
"typescript-client-codegen"
);

const weatherSsdkDir = path.join(
codegenTestDir,
"ssdk-test",
"typescript-ssdk-codegen"
"typescript-server-codegen"
)

// TODO(experimentalIdentityAndAuth): add `@httpApiKeyAuth` client for integration tests
const httpApiKeyAuthClientDir = path.join(
codegenTestDir,
"identity-and-auth-http-api-key-auth",
"typescript-codegen"
"typescript-client-codegen"
);

// TODO(experimentalIdentityAndAuth): add `@httpBearerAuth` client for integration tests
const httpBearerAuthClientDir = path.join(
codegenTestDir,
"identity-and-auth-http-bearer-auth",
"typescript-codegen"
"typescript-client-codegen"
);

const nodeModulesDir = path.join(root, "node_modules");
Expand Down
18 changes: 6 additions & 12 deletions smithy-typescript-codegen-test/smithy-build.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@
}
],
"plugins": {
"typescript-ssdk-codegen": {
"typescript-server-codegen": {
"service": "example.weather#Weather",
"targetNamespace": "Weather",
"package": "weather-ssdk",
"packageVersion": "0.0.1",
"packageJson": {
Expand All @@ -40,9 +39,8 @@
}
],
"plugins": {
"typescript-codegen": {
"typescript-client-codegen": {
"service": "example.weather#Weather",
"targetNamespace": "Weather",
"package": "@smithy/weather-experimental-identity-and-auth",
"packageVersion": "0.0.1",
"packageJson": {
Expand All @@ -63,9 +61,8 @@
}
],
"plugins": {
"typescript-codegen": {
"typescript-client-codegen": {
"service": "example.weather#Weather",
"targetNamespace": "Weather",
"package": "weather",
"packageVersion": "0.0.1",
"packageJson": {
Expand All @@ -85,9 +82,8 @@
}
],
"plugins": {
"typescript-codegen": {
"typescript-client-codegen": {
"service": "identity.auth.httpApiKeyAuth#HttpApiKeyAuthService",
"targetNamespace": "HttpApiKeyAuthService",
"package": "@smithy/identity-and-auth-http-api-key-auth-service",
"packageVersion": "0.0.1",
"packageJson": {
Expand All @@ -108,9 +104,8 @@
}
],
"plugins": {
"typescript-codegen": {
"typescript-client-codegen": {
"service": "identity.auth.httpBearerAuth#HttpBearerAuthService",
"targetNamespace": "HttpBearerAuthService",
"package": "@smithy/identity-and-auth-http-bearer-auth-service",
"packageVersion": "0.0.1",
"packageJson": {
Expand All @@ -123,9 +118,8 @@
}
},
"plugins": {
"typescript-codegen": {
"typescript-client-codegen": {
"service": "example.weather#Weather",
"targetNamespace": "Weather",
"package": "weather",
"packageVersion": "0.0.1",
"packageJson": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,19 +98,24 @@ private static void writeClientExports(
) {
ServiceShape service = settings.getService(model);
Symbol symbol = symbolProvider.toSymbol(service);
// Normalizes client name, e.g. WeatherClient => Weather
String normalizedClientName = symbol.getName().replace("Client", "");

// Write export statement for bare-bones client.
writer.write("export * from \"./$L\";", symbol.getName());

// Write export statement for aggregated client.
String aggregatedClientName = symbol.getName().replace("Client", "");
writer.write("export * from \"./$L\";", aggregatedClientName);
writer.write("export * from \"./$L\";", normalizedClientName);

// export endpoints config interface
if (service.hasTrait(EndpointRuleSetTrait.class)) {
writer.write("export { ClientInputEndpointParameters } from \"./endpoint/EndpointParameters\";");
}

// Export Runtime Extension and Client ExtensionConfiguration interfaces
writer.write("export { RuntimeExtension } from \"./runtimeExtensions\";");
writer.write("export { $LExtensionConfiguration } from \"./extensionConfiguration\";", normalizedClientName);

// Write export statement for commands.
writer.write("export * from \"./commands\";");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
final class ServerCommandGenerator implements Runnable {

static final String COMMANDS_FOLDER = "operations";
private static final String NO_PROTOCOL_FOUND_SERDE_FUNCTION =
"(async (...args: any[]) => { throw new Error(\"No supported protocol was found\"); })";

private final TypeScriptSettings settings;
private final Model model;
Expand Down Expand Up @@ -163,18 +165,23 @@ private void writeOperationSerializer() {
writer.addImport("OperationSerializer", "__OperationSerializer", TypeScriptDependency.SERVER_COMMON);
writer.openBlock("export class $L implements __OperationSerializer<$T<any>, $S, $T> {", "}",
serializerName, serverSymbol, operationSymbol.getName(), errorsType, () -> {
String serializerFunction = ProtocolGenerator.getGenericSerFunctionName(operationSymbol) + "Response";
String deserializerFunction = ProtocolGenerator.getGenericDeserFunctionName(operationSymbol) + "Request";

writer.addRelativeImport(serializerFunction, null,
Paths.get(".", CodegenUtils.SOURCE_FOLDER, ProtocolGenerator.PROTOCOLS_FOLDER,
ProtocolGenerator.getSanitizedName(protocolGenerator.getName())));
writer.addRelativeImport(deserializerFunction, null,
Paths.get(".", CodegenUtils.SOURCE_FOLDER, ProtocolGenerator.PROTOCOLS_FOLDER,
ProtocolGenerator.getSanitizedName(protocolGenerator.getName())));

writer.write("serialize = $L;", serializerFunction);
writer.write("deserialize = $L;", deserializerFunction);
if (protocolGenerator == null) {
writer.write("serialize = $L as any;", NO_PROTOCOL_FOUND_SERDE_FUNCTION);
writer.write("deserialize = $L as any;", NO_PROTOCOL_FOUND_SERDE_FUNCTION);
} else {
String serializerFunction =
ProtocolGenerator.getGenericSerFunctionName(operationSymbol) + "Response";
writer.addRelativeImport(serializerFunction, null,
Paths.get(".", CodegenUtils.SOURCE_FOLDER, ProtocolGenerator.PROTOCOLS_FOLDER,
ProtocolGenerator.getSanitizedName(protocolGenerator.getName())));
writer.write("serialize = $L;", serializerFunction);
String deserializerFunction =
ProtocolGenerator.getGenericDeserFunctionName(operationSymbol) + "Request";
writer.addRelativeImport(deserializerFunction, null,
Paths.get(".", CodegenUtils.SOURCE_FOLDER, ProtocolGenerator.PROTOCOLS_FOLDER,
ProtocolGenerator.getSanitizedName(protocolGenerator.getName())));
writer.write("deserialize = $L;", deserializerFunction);
}
writer.write("");
writeErrorChecker();
writeErrorHandler();
Expand Down Expand Up @@ -221,12 +228,16 @@ private void writeErrorHandler() {

private void writeErrorHandlerCase(StructureShape error) {
Symbol errorSymbol = symbolProvider.toSymbol(error);
String serializerFunction = ProtocolGenerator.getGenericSerFunctionName(errorSymbol) + "Error";
writer.addRelativeImport(serializerFunction, null,
Paths.get(".", CodegenUtils.SOURCE_FOLDER, ProtocolGenerator.PROTOCOLS_FOLDER,
ProtocolGenerator.getSanitizedName(protocolGenerator.getName())));
writer.openBlock("case $S: {", "}", error.getId().getName(), () -> {
writer.write("return $L(error, ctx);", serializerFunction);
if (protocolGenerator == null) {
writer.write("return $L(error, ctx);", NO_PROTOCOL_FOUND_SERDE_FUNCTION);
} else {
String serializerFunction = ProtocolGenerator.getGenericSerFunctionName(errorSymbol) + "Error";
writer.addRelativeImport(serializerFunction, null,
Paths.get(".", CodegenUtils.SOURCE_FOLDER, ProtocolGenerator.PROTOCOLS_FOLDER,
ProtocolGenerator.getSanitizedName(protocolGenerator.getName())));
writer.write("return $L(error, ctx);", serializerFunction);
}
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

package software.amazon.smithy.typescript.codegen;

import java.util.ServiceLoader;
import java.util.ServiceLoader.Provider;
import java.util.logging.Logger;
import software.amazon.smithy.build.PluginContext;
import software.amazon.smithy.build.SmithyBuildPlugin;
import software.amazon.smithy.codegen.core.directed.CodegenDirector;
import software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration;
import software.amazon.smithy.utils.SmithyInternalApi;

/**
* Plugin to trigger TypeScript client code generation.
*/
@SmithyInternalApi
public class TypeScriptClientCodegenPlugin implements SmithyBuildPlugin {
private static final Logger LOGGER = Logger.getLogger(TypeScriptClientCodegenPlugin.class.getName());

@Override
public String getName() {
return "typescript-client-codegen";
}

@Override
public void execute(PluginContext context) {
CodegenDirector<TypeScriptWriter, TypeScriptIntegration, TypeScriptCodegenContext, TypeScriptSettings> runner
= new CodegenDirector<>();

runner.directedCodegen(new DirectedTypeScriptCodegen());

// Set the SmithyIntegration class to look for and apply using SPI.
runner.integrationClass(TypeScriptIntegration.class);

// Set the FileManifest and Model from the plugin.
runner.fileManifest(context.getFileManifest());
runner.model(context.getModel());

// Create the TypeScriptSettings object from the plugin settings.
TypeScriptSettings settings = TypeScriptSettings.from(context.getModel(), context.getSettings(),
TypeScriptSettings.ArtifactType.CLIENT);
runner.settings(settings);

// Only add integrations if the integrations match the settings
// This uses {@link TypeScriptIntegration#matchesSettings}, which is a
// Smithy internal API. This may be removed at any point.
runner.integrationFinder(() ->
() -> ServiceLoader.load(TypeScriptIntegration.class, CodegenDirector.class.getClassLoader())
hpmellema marked this conversation as resolved.
Show resolved Hide resolved
.stream()
.map(Provider::get)
.filter(integration -> {
boolean matchesSettings = integration.matchesSettings(settings);
if (!matchesSettings) {
LOGGER.info(() -> "Skipping TypeScript integration based on settings: " + integration.name());
}
return matchesSettings;
})
.iterator());

runner.service(settings.getService());

// Configure the director to perform some common model transforms.
runner.performDefaultCodegenTransforms();

// TODO: Not using below because it would break existing AWS SDKs. Maybe it should be configurable
// so generic SDKs call this by default, but AWS SDKs can opt-out of it via a setting.
// runner.createDedicatedInputsAndOutputs();

// Run it!
runner.run();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,72 +15,16 @@

package software.amazon.smithy.typescript.codegen;

import java.util.ServiceLoader;
import java.util.ServiceLoader.Provider;
import java.util.logging.Logger;
import software.amazon.smithy.build.PluginContext;
import software.amazon.smithy.build.SmithyBuildPlugin;
import software.amazon.smithy.codegen.core.directed.CodegenDirector;
import software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration;
import software.amazon.smithy.utils.SmithyInternalApi;

/**
* Plugin to trigger TypeScript code generation.
*/
@SmithyInternalApi
public final class TypeScriptCodegenPlugin implements SmithyBuildPlugin {
private static final Logger LOGGER = Logger.getLogger(TypeScriptCodegenPlugin.class.getName());

@Deprecated
public final class TypeScriptCodegenPlugin extends TypeScriptClientCodegenPlugin {
@Override
public String getName() {
return "typescript-codegen";
}

@Override
public void execute(PluginContext context) {
CodegenDirector<TypeScriptWriter, TypeScriptIntegration, TypeScriptCodegenContext, TypeScriptSettings> runner
= new CodegenDirector<>();

runner.directedCodegen(new DirectedTypeScriptCodegen());

// Set the SmithyIntegration class to look for and apply using SPI.
runner.integrationClass(TypeScriptIntegration.class);

// Set the FileManifest and Model from the plugin.
runner.fileManifest(context.getFileManifest());
runner.model(context.getModel());

// Create the TypeScriptSettings object from the plugin settings.
TypeScriptSettings settings = TypeScriptSettings.from(context.getModel(), context.getSettings(),
TypeScriptSettings.ArtifactType.CLIENT);
runner.settings(settings);

// Only add integrations if the integrations match the settings
// This uses {@link TypeScriptIntegration#matchesSettings}, which is a
// Smithy internal API. This may be removed at any point.
runner.integrationFinder(() ->
() -> ServiceLoader.load(TypeScriptIntegration.class, CodegenDirector.class.getClassLoader())
.stream()
.map(Provider::get)
.filter(integration -> {
boolean matchesSettings = integration.matchesSettings(settings);
if (!matchesSettings) {
LOGGER.info(() -> "Skipping TypeScript integration based on settings: " + integration.name());
}
return matchesSettings;
})
.iterator());

runner.service(settings.getService());

// Configure the director to perform some common model transforms.
runner.performDefaultCodegenTransforms();

// TODO: Not using below because it would break existing AWS SDKs. Maybe it should be configurable
// so generic SDKs call this by default, but AWS SDKs can opt-out of it via a setting.
// runner.createDedicatedInputsAndOutputs();

// Run it!
runner.run();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

package software.amazon.smithy.typescript.codegen;

import software.amazon.smithy.utils.SmithyInternalApi;

/**
* Plugin to trigger TypeScript SSDK code generation.
*/
@SmithyInternalApi
@Deprecated
public class TypeScriptSSDKCodegenPlugin extends TypeScriptServerCodegenPlugin {
@Override
public String getName() {
return "typescript-ssdk-codegen";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class TypeScriptServerCodegenPlugin implements SmithyBuildPlugin {

@Override
public String getName() {
return "typescript-ssdk-codegen";
return "typescript-server-codegen";
}

@Override
Expand Down
Loading
Loading