|
| 1 | +/* |
| 2 | + * Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package software.amazon.smithy.typescript.codegen.integration; |
| 17 | + |
| 18 | +import java.util.Collections; |
| 19 | +import java.util.Map; |
| 20 | +import java.util.Set; |
| 21 | +import java.util.function.Consumer; |
| 22 | +import software.amazon.smithy.codegen.core.SymbolProvider; |
| 23 | +import software.amazon.smithy.model.Model; |
| 24 | +import software.amazon.smithy.model.knowledge.TopDownIndex; |
| 25 | +import software.amazon.smithy.model.shapes.BlobShape; |
| 26 | +import software.amazon.smithy.model.shapes.MemberShape; |
| 27 | +import software.amazon.smithy.model.shapes.OperationShape; |
| 28 | +import software.amazon.smithy.model.shapes.ServiceShape; |
| 29 | +import software.amazon.smithy.model.shapes.Shape; |
| 30 | +import software.amazon.smithy.model.shapes.StructureShape; |
| 31 | +import software.amazon.smithy.model.traits.StreamingTrait; |
| 32 | +import software.amazon.smithy.typescript.codegen.LanguageTarget; |
| 33 | +import software.amazon.smithy.typescript.codegen.TypeScriptDependency; |
| 34 | +import software.amazon.smithy.typescript.codegen.TypeScriptSettings; |
| 35 | +import software.amazon.smithy.typescript.codegen.TypeScriptWriter; |
| 36 | +import software.amazon.smithy.utils.MapUtils; |
| 37 | +import software.amazon.smithy.utils.SmithyInternalApi; |
| 38 | + |
| 39 | +/** |
| 40 | + * Add runtime config for injecting utility functions to consume the JavaScript |
| 41 | + * runtime-specific stream implementations. |
| 42 | + */ |
| 43 | +@SmithyInternalApi |
| 44 | +public final class SdkStreamUtilsMixin implements TypeScriptIntegration { |
| 45 | + |
| 46 | + @Override |
| 47 | + public void addConfigInterfaceFields( |
| 48 | + TypeScriptSettings settings, |
| 49 | + Model model, |
| 50 | + SymbolProvider symbolProvider, |
| 51 | + TypeScriptWriter writer |
| 52 | + ) { |
| 53 | + if (!hasStreamingBlobResponse(model, settings.getService(model))) { |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + writer.addImport("SdkStreamMixinInjector", "__SdkStreamMixinInjector", |
| 58 | + TypeScriptDependency.AWS_SDK_TYPES.packageName); |
| 59 | + writer.writeDocs("The internal function that inject utilities to runtime-specific stream to help users" |
| 60 | + + " consume the data"); |
| 61 | + writer.write("sdkStreamMixin?: __SdkStreamMixinInjector;\n"); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public Map<String, Consumer<TypeScriptWriter>> getRuntimeConfigWriters( |
| 66 | + TypeScriptSettings settings, |
| 67 | + Model model, |
| 68 | + SymbolProvider symbolProvider, |
| 69 | + LanguageTarget target |
| 70 | + ) { |
| 71 | + if (!hasStreamingBlobResponse(model, settings.getService(model))) { |
| 72 | + return Collections.emptyMap(); |
| 73 | + } |
| 74 | + switch (target) { |
| 75 | + case NODE: |
| 76 | + return MapUtils.of("sdkStreamMixin", writer -> { |
| 77 | + writer.addDependency(TypeScriptDependency.UTIL_STREAM_NODE); |
| 78 | + writer.addImport("sdkStreamMixin", "sdkStreamMixin", |
| 79 | + TypeScriptDependency.UTIL_STREAM_NODE.packageName); |
| 80 | + writer.write("sdkStreamMixin"); |
| 81 | + }); |
| 82 | + case BROWSER: |
| 83 | + return MapUtils.of("sdkStreamMixin", writer -> { |
| 84 | + writer.addDependency(TypeScriptDependency.UTIL_STREAM_BROWSER); |
| 85 | + writer.addImport("sdkStreamMixin", "sdkStreamMixin", |
| 86 | + TypeScriptDependency.UTIL_STREAM_BROWSER.packageName); |
| 87 | + writer.write("sdkStreamMixin"); |
| 88 | + }); |
| 89 | + default: |
| 90 | + return Collections.emptyMap(); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + private static boolean hasStreamingBlobResponse(Model model, ServiceShape serviceShape) { |
| 95 | + TopDownIndex topDownIndex = TopDownIndex.of(model); |
| 96 | + Set<OperationShape> operations = topDownIndex.getContainedOperations(serviceShape); |
| 97 | + for (OperationShape operation : operations) { |
| 98 | + StructureShape outputShape = model.expectShape(operation.getOutputShape()).asStructureShape().get(); |
| 99 | + for (MemberShape member : outputShape.members()) { |
| 100 | + Shape shape = model.expectShape(member.getTarget()); |
| 101 | + if (shape instanceof BlobShape && shape.hasTrait(StreamingTrait.class)) { |
| 102 | + return true; |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + return false; |
| 107 | + } |
| 108 | +} |
0 commit comments