|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | +package software.amazon.smithy.aws.typescript.codegen; |
| 6 | + |
| 7 | +import java.util.Collections; |
| 8 | +import java.util.List; |
| 9 | +import java.util.Map; |
| 10 | +import java.util.function.Consumer; |
| 11 | +import software.amazon.smithy.aws.traits.ServiceTrait; |
| 12 | +import software.amazon.smithy.codegen.core.Symbol; |
| 13 | +import software.amazon.smithy.codegen.core.SymbolProvider; |
| 14 | +import software.amazon.smithy.model.Model; |
| 15 | +import software.amazon.smithy.model.shapes.OperationShape; |
| 16 | +import software.amazon.smithy.model.shapes.ServiceShape; |
| 17 | +import software.amazon.smithy.model.shapes.Shape; |
| 18 | +import software.amazon.smithy.typescript.codegen.LanguageTarget; |
| 19 | +import software.amazon.smithy.typescript.codegen.TypeScriptDependency; |
| 20 | +import software.amazon.smithy.typescript.codegen.TypeScriptSettings; |
| 21 | +import software.amazon.smithy.typescript.codegen.TypeScriptWriter; |
| 22 | +import software.amazon.smithy.typescript.codegen.integration.RuntimeClientPlugin; |
| 23 | +import software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration; |
| 24 | +import software.amazon.smithy.utils.SmithyInternalApi; |
| 25 | + |
| 26 | +/** |
| 27 | + * Adds long poll designator for specific operations. |
| 28 | + * Adds custom retryStrategy for DynamoDB. |
| 29 | + */ |
| 30 | +@SmithyInternalApi |
| 31 | +public class AddRetryCustomizations implements TypeScriptIntegration { |
| 32 | + |
| 33 | + @Override |
| 34 | + public List<RuntimeClientPlugin> getClientPlugins() { |
| 35 | + return List.of( |
| 36 | + RuntimeClientPlugin.builder() |
| 37 | + .pluginFunction( |
| 38 | + Symbol.builder() |
| 39 | + .namespace(AwsDependency.AWS_SDK_CORE.getPackageName() + "/client", "/") |
| 40 | + .name("getLongPollPlugin") |
| 41 | + .addDependency(AwsDependency.AWS_SDK_CORE) |
| 42 | + .build() |
| 43 | + ) |
| 44 | + .operationPredicate((m, s, o) -> isLongPoll(s, o)) |
| 45 | + .build() |
| 46 | + ); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public Map<String, Consumer<TypeScriptWriter>> getRuntimeConfigWriters( |
| 51 | + TypeScriptSettings settings, |
| 52 | + Model model, |
| 53 | + SymbolProvider symbolProvider, |
| 54 | + LanguageTarget target |
| 55 | + ) { |
| 56 | + if (LanguageTarget.SHARED.equals(target) && isDynamoDB(settings.getService(model))) { |
| 57 | + return Map.of( |
| 58 | + "retryStrategy", |
| 59 | + (w) -> { |
| 60 | + w.addImport("StandardRetryStrategy", null, TypeScriptDependency.UTIL_RETRY); |
| 61 | + w.addImport("Retry", null, TypeScriptDependency.UTIL_RETRY); |
| 62 | + |
| 63 | + // todo(retry): Retry.v2026 condition can be removed when made permanent. |
| 64 | + w.write(""" |
| 65 | + ( |
| 66 | + config?.maxAttempts == null && config?.retryMode == null && Retry.v2026 |
| 67 | + ? new StandardRetryStrategy({ |
| 68 | + maxAttempts: 4, |
| 69 | + baseDelay: 25, |
| 70 | + }) |
| 71 | + : undefined |
| 72 | + )"""); |
| 73 | + } |
| 74 | + ); |
| 75 | + } |
| 76 | + return Collections.emptyMap(); |
| 77 | + } |
| 78 | + |
| 79 | + private static boolean isDynamoDB(Shape serviceShape) { |
| 80 | + String sdkId = serviceShape.getTrait(ServiceTrait.class).map(ServiceTrait::getSdkId).orElse(""); |
| 81 | + return sdkId.equals("DynamoDB") || sdkId.equals("DynamoDB Streams"); |
| 82 | + } |
| 83 | + |
| 84 | + private static boolean isLongPoll(ServiceShape service, OperationShape operation) { |
| 85 | + String sdkId = service.getTrait(ServiceTrait.class).map(ServiceTrait::getSdkId).orElse(""); |
| 86 | + String opName = operation.getId().getName(service); |
| 87 | + return switch (sdkId) { |
| 88 | + case "SQS" -> opName.equals("ReceiveMessage"); |
| 89 | + case "SWF" -> opName.equals("PollForActivityTask") || opName.equals("PollForDecisionTask"); |
| 90 | + case "SFN" -> opName.equals("GetActivityTask"); |
| 91 | + default -> false; |
| 92 | + }; |
| 93 | + } |
| 94 | +} |
0 commit comments