|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package software.amazon.smithy.typescript.codegen.auth.http; |
| 7 | + |
| 8 | +import java.util.Optional; |
| 9 | +import java.util.function.BiConsumer; |
| 10 | +import java.util.function.Consumer; |
| 11 | +import software.amazon.smithy.codegen.core.Symbol; |
| 12 | +import software.amazon.smithy.typescript.codegen.TypeScriptDependency; |
| 13 | +import software.amazon.smithy.typescript.codegen.TypeScriptWriter; |
| 14 | +import software.amazon.smithy.utils.SmithyBuilder; |
| 15 | +import software.amazon.smithy.utils.SmithyInternalApi; |
| 16 | +import software.amazon.smithy.utils.SmithyUnstableApi; |
| 17 | +import software.amazon.smithy.utils.ToSmithyBuilder; |
| 18 | + |
| 19 | +/** |
| 20 | + * Definition of a Config field. |
| 21 | + * |
| 22 | + * Currently used to populate the ClientDefaults interface in `experimentalIdentityAndAuth`. |
| 23 | + * |
| 24 | + * @param name name of the config field |
| 25 | + * @param type whether the config field is main or auxiliary |
| 26 | + * @param inputType writer for the input type of the config field |
| 27 | + * @param resolvedType writer for the resolved type of the config field |
| 28 | + * @param docs writer for the docs of the config field |
| 29 | + */ |
| 30 | +@SmithyUnstableApi |
| 31 | +public final record ConfigField( |
| 32 | + String name, |
| 33 | + Type type, |
| 34 | + Symbol inputType, |
| 35 | + Symbol resolvedType, |
| 36 | + BiConsumer<TypeScriptWriter, ConfigField> configFieldWriter, |
| 37 | + Optional<Consumer<TypeScriptWriter>> docs |
| 38 | +) implements ToSmithyBuilder<ConfigField> { |
| 39 | + |
| 40 | + /** |
| 41 | + * Defines the type of the config field. |
| 42 | + */ |
| 43 | + @SmithyUnstableApi |
| 44 | + public enum Type { |
| 45 | + /** |
| 46 | + * Specifies the property is important, e.g. {@code apiKey} for {@code @httpApiKeyAuth} |
| 47 | + */ |
| 48 | + MAIN, |
| 49 | + /** |
| 50 | + * Specifies the property is auxiliary, e.g. {@code region} for {@code @aws.auth#sigv4} |
| 51 | + */ |
| 52 | + AUXILIARY |
| 53 | + } |
| 54 | + |
| 55 | + public static Builder builder() { |
| 56 | + return new Builder(); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public Builder toBuilder() { |
| 61 | + return builder() |
| 62 | + .name(name) |
| 63 | + .type(type) |
| 64 | + .inputType(inputType) |
| 65 | + .resolvedType(resolvedType) |
| 66 | + .configFieldWriter(configFieldWriter) |
| 67 | + .docs(docs.orElse(null)); |
| 68 | + } |
| 69 | + |
| 70 | + public static final class Builder implements SmithyBuilder<ConfigField> { |
| 71 | + private String name; |
| 72 | + private Type type; |
| 73 | + private Symbol inputType; |
| 74 | + private Symbol resolvedType; |
| 75 | + private Consumer<TypeScriptWriter> docs; |
| 76 | + private BiConsumer<TypeScriptWriter, ConfigField> configFieldWriter; |
| 77 | + |
| 78 | + @Override |
| 79 | + public ConfigField build() { |
| 80 | + if (configFieldWriter == null) { |
| 81 | + configFieldWriter = type.equals(Type.MAIN) |
| 82 | + ? ConfigField::defaultMainConfigFieldWriter |
| 83 | + : ConfigField::defaultAuxiliaryConfigFieldWriter; |
| 84 | + } |
| 85 | + return new ConfigField( |
| 86 | + SmithyBuilder.requiredState("name", name), |
| 87 | + SmithyBuilder.requiredState("type", type), |
| 88 | + SmithyBuilder.requiredState("inputType", inputType), |
| 89 | + SmithyBuilder.requiredState("resolvedType", resolvedType), |
| 90 | + SmithyBuilder.requiredState("configFieldWriter", configFieldWriter), |
| 91 | + Optional.ofNullable(docs)); |
| 92 | + } |
| 93 | + |
| 94 | + public Builder name(String name) { |
| 95 | + this.name = name; |
| 96 | + return this; |
| 97 | + } |
| 98 | + |
| 99 | + public Builder type(Type type) { |
| 100 | + this.type = type; |
| 101 | + return this; |
| 102 | + } |
| 103 | + |
| 104 | + public Builder inputType(Symbol inputType) { |
| 105 | + this.inputType = inputType; |
| 106 | + return this; |
| 107 | + } |
| 108 | + |
| 109 | + public Builder resolvedType(Symbol resolvedType) { |
| 110 | + this.resolvedType = resolvedType; |
| 111 | + return this; |
| 112 | + } |
| 113 | + |
| 114 | + public Builder docs(Consumer<TypeScriptWriter> docs) { |
| 115 | + this.docs = docs; |
| 116 | + return this; |
| 117 | + } |
| 118 | + |
| 119 | + public Builder configFieldWriter(BiConsumer<TypeScriptWriter, ConfigField> configFieldWriter) { |
| 120 | + this.configFieldWriter = configFieldWriter; |
| 121 | + return this; |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + @SmithyInternalApi |
| 126 | + public static void defaultMainConfigFieldWriter( |
| 127 | + TypeScriptWriter w, |
| 128 | + ConfigField configField |
| 129 | + ) { |
| 130 | + w.addDependency(TypeScriptDependency.SMITHY_CORE); |
| 131 | + w.addImport("memoizeIdentityProvider", null, |
| 132 | + TypeScriptDependency.SMITHY_CORE); |
| 133 | + w.addImport("isIdentityExpired", null, |
| 134 | + TypeScriptDependency.SMITHY_CORE); |
| 135 | + w.addImport("doesIdentityRequireRefresh", null, |
| 136 | + TypeScriptDependency.SMITHY_CORE); |
| 137 | + w.write(""" |
| 138 | + const $L = memoizeIdentityProvider(config.$L, isIdentityExpired, \ |
| 139 | + doesIdentityRequireRefresh);""", |
| 140 | + configField.name(), |
| 141 | + configField.name()); |
| 142 | + } |
| 143 | + |
| 144 | + @SmithyInternalApi |
| 145 | + public static void defaultAuxiliaryConfigFieldWriter( |
| 146 | + TypeScriptWriter w, |
| 147 | + ConfigField configField |
| 148 | + ) { |
| 149 | + w.addDependency(TypeScriptDependency.UTIL_MIDDLEWARE); |
| 150 | + w.addImport("normalizeProvider", null, TypeScriptDependency.UTIL_MIDDLEWARE); |
| 151 | + w.write("const $L = config.$L ? normalizeProvider(config.$L) : undefined;", |
| 152 | + configField.name(), |
| 153 | + configField.name(), |
| 154 | + configField.name()); |
| 155 | + } |
| 156 | +} |
0 commit comments