From 20acec0d8b5992499514a6a87685962fd9551d2b Mon Sep 17 00:00:00 2001 From: Steven Yuan Date: Tue, 14 Nov 2023 19:46:10 -0800 Subject: [PATCH] feat(experimentalIdentityAndAuth): update code generation Make code generation changes for `experimentalIdentityAndAuth`. Bug fixes: - fix misused service name when importing the default auth scheme provider - add generic type to resolve config function input and output Updates: - add remove methods to `HttpAuthScheme` builder - update code generation for `ConfigField` (and more code sections) - update `customizeSupportedHttpAuthSchemes()` to include model and settings - add resolve functions codegen for `HttpAuthScheme`s - update various `String` parameters to take `Symbol` instead --- .../typescript/codegen/ConfigField.java | 103 ------ .../codegen/RuntimeConfigGenerator.java | 6 +- .../ServiceBareBonesClientGenerator.java | 6 +- .../typescript/codegen/auth/AuthUtils.java | 36 ++- .../codegen/auth/http/ConfigField.java | 156 +++++++++ .../codegen/auth/http/HttpAuthScheme.java | 68 +++- .../http/HttpAuthSchemeProviderGenerator.java | 33 +- .../auth/http/ResolveConfigFunction.java | 73 +++++ .../http/SupportedHttpAuthSchemesIndex.java | 10 +- .../integration/AddHttpApiKeyAuthPlugin.java | 107 ------- ...ware.java => AddHttpAuthSchemePlugin.java} | 303 ++++++++++++------ ...dleware.java => AddHttpSigningPlugin.java} | 2 +- .../HttpAuthRuntimeExtensionIntegration.java | 74 +++-- .../HttpAuthTypeScriptIntegration.java | 11 +- .../integration/SupportHttpApiKeyAuth.java | 119 +++++++ ...Plugin.java => SupportHttpBearerAuth.java} | 52 +-- ...ddNoAuthPlugin.java => SupportNoAuth.java} | 14 +- .../HttpAuthOptionFunctionCodeSection.java | 122 +++++++ .../HttpAuthOptionFunctionsCodeSection.java | 5 +- ...SchemeInputConfigInterfaceCodeSection.java | 123 +++++++ ...emeResolvedConfigInterfaceCodeSection.java | 123 +++++++ ...tpAuthSchemeConfigFunctionCodeSection.java | 123 +++++++ ...ConfigFunctionConfigFieldsCodeSection.java | 109 +++++++ ...eConfigFunctionReturnBlockCodeSection.java | 109 +++++++ ....codegen.integration.TypeScriptIntegration | 10 +- 25 files changed, 1491 insertions(+), 406 deletions(-) delete mode 100644 smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/ConfigField.java create mode 100644 smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/ConfigField.java create mode 100644 smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/ResolveConfigFunction.java delete mode 100644 smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/AddHttpApiKeyAuthPlugin.java rename smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/{AddHttpAuthSchemeMiddleware.java => AddHttpAuthSchemePlugin.java} (52%) rename smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/{AddHttpSigningMiddleware.java => AddHttpSigningPlugin.java} (95%) create mode 100644 smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/SupportHttpApiKeyAuth.java rename smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/{AddHttpBearerAuthPlugin.java => SupportHttpBearerAuth.java} (53%) rename smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/{AddNoAuthPlugin.java => SupportNoAuth.java} (82%) create mode 100644 smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/sections/HttpAuthOptionFunctionCodeSection.java create mode 100644 smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/sections/HttpAuthSchemeInputConfigInterfaceCodeSection.java create mode 100644 smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/sections/HttpAuthSchemeResolvedConfigInterfaceCodeSection.java create mode 100644 smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/sections/ResolveHttpAuthSchemeConfigFunctionCodeSection.java create mode 100644 smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/sections/ResolveHttpAuthSchemeConfigFunctionConfigFieldsCodeSection.java create mode 100644 smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/sections/ResolveHttpAuthSchemeConfigFunctionReturnBlockCodeSection.java diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/ConfigField.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/ConfigField.java deleted file mode 100644 index 7ccb911caca..00000000000 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/ConfigField.java +++ /dev/null @@ -1,103 +0,0 @@ -/* - * 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.function.Consumer; -import software.amazon.smithy.utils.SmithyBuilder; -import software.amazon.smithy.utils.SmithyUnstableApi; -import software.amazon.smithy.utils.ToSmithyBuilder; - -/** - * Definition of a Config field. - * - * Currently used to populate the ClientDefaults interface in `experimentalIdentityAndAuth`. - * - * @param name name of the config field - * @param type whether the config field is main or auxiliary - * @param inputType writer for the input type of the config field - * @param resolvedType writer for the resolved type of the config field - * @param docs writer for the docs of the config field - */ -@SmithyUnstableApi -public final record ConfigField( - String name, - Type type, - Consumer inputType, - Consumer resolvedType, - Consumer docs -) implements ToSmithyBuilder { - /** - * Defines the type of the config field. - */ - @SmithyUnstableApi - public enum Type { - /** - * Specifies the property is important, e.g. {@code apiKey} for {@code @httpApiKeyAuth} - */ - MAIN, - /** - * Specifies the property is auxiliary, e.g. {@code region} for {@code @aws.auth#sigv4} - */ - AUXILIARY - } - - public static Builder builder() { - return new Builder(); - } - - @Override - public Builder toBuilder() { - return builder() - .name(name) - .type(type) - .inputType(inputType) - .resolvedType(resolvedType) - .docs(docs); - } - - public static final class Builder implements SmithyBuilder { - private String name; - private Type type; - private Consumer inputType; - private Consumer resolvedType; - private Consumer docs; - - @Override - public ConfigField build() { - return new ConfigField( - SmithyBuilder.requiredState("name", name), - SmithyBuilder.requiredState("type", type), - SmithyBuilder.requiredState("inputType", inputType), - SmithyBuilder.requiredState("resolvedType", resolvedType), - SmithyBuilder.requiredState("docs", docs)); - } - - public Builder name(String name) { - this.name = name; - return this; - } - - public Builder type(Type type) { - this.type = type; - return this; - } - - public Builder inputType(Consumer inputType) { - this.inputType = inputType; - return this; - } - - public Builder resolvedType(Consumer resolvedType) { - this.resolvedType = resolvedType; - return this; - } - - public Builder docs(Consumer docs) { - this.docs = docs; - return this; - } - } -} diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/RuntimeConfigGenerator.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/RuntimeConfigGenerator.java index 046ba97ea21..b301062320d 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/RuntimeConfigGenerator.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/RuntimeConfigGenerator.java @@ -213,13 +213,15 @@ private void generateHttpAuthSchemeConfig( TypeScriptWriter writer, LanguageTarget target ) { - SupportedHttpAuthSchemesIndex authIndex = new SupportedHttpAuthSchemesIndex(integrations); + SupportedHttpAuthSchemesIndex authIndex = new SupportedHttpAuthSchemesIndex(integrations, model, settings); // feat(experimentalIdentityAndAuth): write the default imported HttpAuthSchemeProvider if (target.equals(LanguageTarget.SHARED)) { configs.put("httpAuthSchemeProvider", w -> { w.write("$T", Symbol.builder() - .name("default" + service.toShapeId().getName() + "HttpAuthSchemeProvider") + .name("default" + + CodegenUtils.getServiceName(settings, model, symbolProvider) + + "HttpAuthSchemeProvider") .namespace(AuthUtils.AUTH_HTTP_PROVIDER_DEPENDENCY.getPackageName(), "/") .build()); }); diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/ServiceBareBonesClientGenerator.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/ServiceBareBonesClientGenerator.java index 7e22bc49108..ef5cab55a41 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/ServiceBareBonesClientGenerator.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/ServiceBareBonesClientGenerator.java @@ -48,7 +48,7 @@ * Generates a bare-bones client and configuration for service using plugins. */ @SmithyInternalApi -final class ServiceBareBonesClientGenerator implements Runnable { +public final class ServiceBareBonesClientGenerator implements Runnable { private final TypeScriptSettings settings; private final Model model; @@ -88,11 +88,11 @@ final class ServiceBareBonesClientGenerator implements Runnable { resolvedConfigType = getResolvedConfigTypeName(symbol); } - static String getConfigTypeName(Symbol symbol) { + public static String getConfigTypeName(Symbol symbol) { return symbol.getName() + "Config"; } - static String getResolvedConfigTypeName(Symbol symbol) { + public static String getResolvedConfigTypeName(Symbol symbol) { return symbol.getName() + "ResolvedConfig"; } diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/AuthUtils.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/AuthUtils.java index 800dfbbb155..f98fb9e4852 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/AuthUtils.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/AuthUtils.java @@ -13,6 +13,7 @@ import java.util.Map; import java.util.TreeMap; import software.amazon.smithy.codegen.core.CodegenException; +import software.amazon.smithy.codegen.core.Symbol; import software.amazon.smithy.codegen.core.SymbolDependency; import software.amazon.smithy.model.knowledge.ServiceIndex; import software.amazon.smithy.model.knowledge.ServiceIndex.AuthSchemeMode; @@ -20,10 +21,11 @@ import software.amazon.smithy.model.shapes.ShapeId; import software.amazon.smithy.model.traits.Trait; import software.amazon.smithy.typescript.codegen.CodegenUtils; -import software.amazon.smithy.typescript.codegen.ConfigField; import software.amazon.smithy.typescript.codegen.Dependency; +import software.amazon.smithy.typescript.codegen.auth.http.ConfigField; import software.amazon.smithy.typescript.codegen.auth.http.HttpAuthScheme; import software.amazon.smithy.typescript.codegen.auth.http.HttpAuthSchemeParameter; +import software.amazon.smithy.typescript.codegen.auth.http.ResolveConfigFunction; import software.amazon.smithy.typescript.codegen.auth.http.SupportedHttpAuthSchemesIndex; import software.amazon.smithy.utils.SmithyInternalApi; @@ -110,7 +112,7 @@ public static Map collectConfigFields(Collection collectConfigFields(Collection collectResolveConfigFunctions( + Collection httpAuthSchemes + ) { + Map resolveConfigFunctions = new HashMap<>(); + for (HttpAuthScheme authScheme : httpAuthSchemes) { + if (authScheme == null) { + continue; + } + for (ResolveConfigFunction fn : authScheme.getResolveConfigFunctions()) { + if (resolveConfigFunctions.containsKey(fn.resolveConfigFunction())) { + ResolveConfigFunction existingFn = + resolveConfigFunctions.get(fn.resolveConfigFunction()); + if (!fn.equals(existingFn)) { + throw new CodegenException("Contradicting `ResolveConfigFunction` definitions for `" + + fn.resolveConfigFunction() + + "`; existing: " + + existingFn + + ", conflict: " + + fn); + } + } else { + resolveConfigFunctions.put(fn.resolveConfigFunction(), fn); + } + } + } + return resolveConfigFunctions; + } + public static Map collectHttpAuthSchemeParameters( Collection httpAuthSchemes) { Map httpAuthSchemeParameters = new HashMap<>(); @@ -136,7 +166,7 @@ public static Map collectHttpAuthSchemeParamete if (httpAuthSchemeParameters.containsKey(param.name())) { HttpAuthSchemeParameter existingParam = httpAuthSchemeParameters.get(param.name()); if (!param.equals(existingParam)) { - throw new CodegenException("Contradicting `HttpAuthSchemeParameter` defintions for `" + throw new CodegenException("Contradicting `HttpAuthSchemeParameter` definitions for `" + param.name() + "`; existing: " + existingParam diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/ConfigField.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/ConfigField.java new file mode 100644 index 00000000000..e10d0515b85 --- /dev/null +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/ConfigField.java @@ -0,0 +1,156 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + +package software.amazon.smithy.typescript.codegen.auth.http; + +import java.util.Optional; +import java.util.function.BiConsumer; +import java.util.function.Consumer; +import software.amazon.smithy.codegen.core.Symbol; +import software.amazon.smithy.typescript.codegen.TypeScriptDependency; +import software.amazon.smithy.typescript.codegen.TypeScriptWriter; +import software.amazon.smithy.utils.SmithyBuilder; +import software.amazon.smithy.utils.SmithyInternalApi; +import software.amazon.smithy.utils.SmithyUnstableApi; +import software.amazon.smithy.utils.ToSmithyBuilder; + +/** + * Definition of a Config field. + * + * Currently used to populate the ClientDefaults interface in `experimentalIdentityAndAuth`. + * + * @param name name of the config field + * @param type whether the config field is main or auxiliary + * @param inputType writer for the input type of the config field + * @param resolvedType writer for the resolved type of the config field + * @param docs writer for the docs of the config field + */ +@SmithyUnstableApi +public final record ConfigField( + String name, + Type type, + Symbol inputType, + Symbol resolvedType, + BiConsumer configFieldWriter, + Optional> docs +) implements ToSmithyBuilder { + + /** + * Defines the type of the config field. + */ + @SmithyUnstableApi + public enum Type { + /** + * Specifies the property is important, e.g. {@code apiKey} for {@code @httpApiKeyAuth} + */ + MAIN, + /** + * Specifies the property is auxiliary, e.g. {@code region} for {@code @aws.auth#sigv4} + */ + AUXILIARY + } + + public static Builder builder() { + return new Builder(); + } + + @Override + public Builder toBuilder() { + return builder() + .name(name) + .type(type) + .inputType(inputType) + .resolvedType(resolvedType) + .configFieldWriter(configFieldWriter) + .docs(docs.orElse(null)); + } + + public static final class Builder implements SmithyBuilder { + private String name; + private Type type; + private Symbol inputType; + private Symbol resolvedType; + private Consumer docs; + private BiConsumer configFieldWriter; + + @Override + public ConfigField build() { + if (configFieldWriter == null) { + configFieldWriter = type.equals(Type.MAIN) + ? ConfigField::defaultMainConfigFieldWriter + : ConfigField::defaultAuxiliaryConfigFieldWriter; + } + return new ConfigField( + SmithyBuilder.requiredState("name", name), + SmithyBuilder.requiredState("type", type), + SmithyBuilder.requiredState("inputType", inputType), + SmithyBuilder.requiredState("resolvedType", resolvedType), + SmithyBuilder.requiredState("configFieldWriter", configFieldWriter), + Optional.ofNullable(docs)); + } + + public Builder name(String name) { + this.name = name; + return this; + } + + public Builder type(Type type) { + this.type = type; + return this; + } + + public Builder inputType(Symbol inputType) { + this.inputType = inputType; + return this; + } + + public Builder resolvedType(Symbol resolvedType) { + this.resolvedType = resolvedType; + return this; + } + + public Builder docs(Consumer docs) { + this.docs = docs; + return this; + } + + public Builder configFieldWriter(BiConsumer configFieldWriter) { + this.configFieldWriter = configFieldWriter; + return this; + } + } + + @SmithyInternalApi + public static void defaultMainConfigFieldWriter( + TypeScriptWriter w, + ConfigField configField + ) { + w.addDependency(TypeScriptDependency.SMITHY_CORE); + w.addImport("memoizeIdentityProvider", null, + TypeScriptDependency.SMITHY_CORE); + w.addImport("isIdentityExpired", null, + TypeScriptDependency.SMITHY_CORE); + w.addImport("doesIdentityRequireRefresh", null, + TypeScriptDependency.SMITHY_CORE); + w.write(""" + const $L = memoizeIdentityProvider(config.$L, isIdentityExpired, \ + doesIdentityRequireRefresh);""", + configField.name(), + configField.name()); + } + + @SmithyInternalApi + public static void defaultAuxiliaryConfigFieldWriter( + TypeScriptWriter w, + ConfigField configField + ) { + w.addDependency(TypeScriptDependency.UTIL_MIDDLEWARE); + w.addImport("normalizeProvider", null, TypeScriptDependency.UTIL_MIDDLEWARE); + w.write("const $L = config.$L ? normalizeProvider(config.$L) : undefined;", + configField.name(), + configField.name(), + configField.name()); + } +} diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/HttpAuthScheme.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/HttpAuthScheme.java index b094fdefdd0..2078c34171a 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/HttpAuthScheme.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/HttpAuthScheme.java @@ -10,9 +10,9 @@ import java.util.Optional; import java.util.function.Consumer; import java.util.function.Function; +import software.amazon.smithy.codegen.core.Symbol; import software.amazon.smithy.model.shapes.ShapeId; import software.amazon.smithy.typescript.codegen.ApplicationProtocol; -import software.amazon.smithy.typescript.codegen.ConfigField; import software.amazon.smithy.typescript.codegen.LanguageTarget; import software.amazon.smithy.typescript.codegen.TypeScriptWriter; import software.amazon.smithy.typescript.codegen.auth.http.HttpAuthOptionProperty.Type; @@ -35,9 +35,10 @@ public final class HttpAuthScheme implements ToSmithyBuilder { private final Map> defaultIdentityProviders; private final Map> defaultSigners; private final List configFields; + private final List resolveConfigFunctions; private final List httpAuthSchemeParameters; private final List httpAuthOptionProperties; - private final Function> propertiesExtractor; + private final Function> propertiesExtractor; private HttpAuthScheme(Builder builder) { this.schemeId = SmithyBuilder.requiredState( @@ -51,6 +52,8 @@ private HttpAuthScheme(Builder builder) { "defaultSigners", builder.defaultSigners.copy()); this.configFields = SmithyBuilder.requiredState( "configFields", builder.configFields.copy()); + this.resolveConfigFunctions = SmithyBuilder.requiredState( + "resolveConfigFunctions", builder.resolveConfigFunctions.copy()); this.httpAuthSchemeParameters = SmithyBuilder.requiredState( "httpAuthSchemeParameters", builder.httpAuthSchemeParameters.copy()); this.httpAuthOptionProperties = SmithyBuilder.requiredState( @@ -107,6 +110,10 @@ public List getConfigFields() { return configFields; } + public List getResolveConfigFunctions() { + return resolveConfigFunctions; + } + /** * Gets the list of auth scheme parameters for an auth scheme. * @return httpAuthSchemeParameters @@ -136,7 +143,7 @@ public List getHttpAuthSchemeOptionParametersByType(Type * Gets the writer for the config properties extractor. * @return optional of config properties extractor */ - public Optional>> getPropertiesExtractor() { + public Optional>> getPropertiesExtractor() { return Optional.ofNullable(propertiesExtractor); } @@ -162,6 +169,7 @@ public Builder toBuilder() { .defaultIdentityProviders(defaultIdentityProviders) .defaultSigners(defaultSigners) .configFields(configFields) + .resolveConfigFunctions(resolveConfigFunctions) .httpAuthSchemeParameters(httpAuthSchemeParameters) .httpAuthOptionProperties(httpAuthOptionProperties) .propertiesExtractor(propertiesExtractor); @@ -180,11 +188,13 @@ public static final class Builder implements SmithyBuilder { BuilderRef.forOrderedMap(); private BuilderRef> configFields = BuilderRef.forList(); + private BuilderRef> resolveConfigFunctions = + BuilderRef.forList(); private BuilderRef> httpAuthSchemeParameters = BuilderRef.forList(); private BuilderRef> httpAuthOptionProperties = BuilderRef.forList(); - private Function> propertiesExtractor; + private Function> propertiesExtractor; private Builder() {} @@ -316,6 +326,32 @@ public Builder addConfigField(ConfigField configField) { return this; } + /** + * Removes a config field by name. + * @param configField name of the config field to remove + * @return the builder + */ + public Builder removeConfigField(String configField) { + this.configFields.get().removeIf(c -> c.name().equals(configField)); + return this; + } + + public Builder resolveConfigFunctions(List resolveConfigFunctions) { + this.resolveConfigFunctions.clear(); + this.resolveConfigFunctions.get().addAll(resolveConfigFunctions); + return this; + } + + public Builder addResolveConfigFunction(ResolveConfigFunction resolveConfigFunction) { + this.resolveConfigFunctions.get().add(resolveConfigFunction); + return this; + } + + public Builder removeResolveConfigFunction(Symbol resolveConfigFunction) { + this.resolveConfigFunctions.get().removeIf(c -> c.resolveConfigFunction().equals(resolveConfigFunction)); + return this; + } + /** * Sets the httpAuthSchemeParameters. * @param httpAuthSchemeParameters auth scheme parameters to set @@ -329,7 +365,7 @@ public Builder httpAuthSchemeParameters( } /** - * Adds a auth scheme parameter. + * Adds an auth scheme parameter. * @param httpAuthSchemeParameter parameter to add * @return the builder */ @@ -338,6 +374,16 @@ public Builder addHttpAuthSchemeParameter(HttpAuthSchemeParameter httpAuthScheme return this; } + /** + * Removes an auth scheme parameter by name. + * @param httpAuthSchemeParameter name of the auth scheme parameter to remove + * @return the builder + */ + public Builder removeHttpAuthSchemeParameter(String httpAuthSchemeParameter) { + this.httpAuthSchemeParameters.get().removeIf(p -> p.name().equals(httpAuthSchemeParameter)); + return this; + } + /** * Sets the httpAuthOptionProperties. * @param httpAuthOptionProperties properties to set @@ -360,13 +406,23 @@ public Builder addHttpAuthOptionProperty(HttpAuthOptionProperty httpAuthOptionPr return this; } + /** + * Removes an auth option property. + * @param httpAuthOptionProperty name of the auth option property to remove + * @return the builder + */ + public Builder removeHttpAuthOptionProperty(String httpAuthOptionProperty) { + this.httpAuthOptionProperties.get().removeIf(p -> p.name().equals(httpAuthOptionProperty)); + return this; + } + /** * Sets the propertiesExtractor. * @param propertiesExtractor writer for properties extractor * @return the builder */ public Builder propertiesExtractor( - Function> propertiesExtractor) { + Function> propertiesExtractor) { this.propertiesExtractor = propertiesExtractor; return this; } diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/HttpAuthSchemeProviderGenerator.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/HttpAuthSchemeProviderGenerator.java index a0dd6d34801..0219317e458 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/HttpAuthSchemeProviderGenerator.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/HttpAuthSchemeProviderGenerator.java @@ -10,6 +10,7 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.Optional; import software.amazon.smithy.codegen.core.Symbol; import software.amazon.smithy.codegen.core.SymbolProvider; import software.amazon.smithy.model.Model; @@ -19,6 +20,7 @@ import software.amazon.smithy.model.shapes.ShapeId; import software.amazon.smithy.model.traits.Trait; import software.amazon.smithy.typescript.codegen.CodegenUtils; +import software.amazon.smithy.typescript.codegen.ServiceBareBonesClientGenerator; import software.amazon.smithy.typescript.codegen.TypeScriptDelegator; import software.amazon.smithy.typescript.codegen.TypeScriptDependency; import software.amazon.smithy.typescript.codegen.TypeScriptSettings; @@ -27,6 +29,7 @@ import software.amazon.smithy.typescript.codegen.auth.http.HttpAuthOptionProperty.Type; import software.amazon.smithy.typescript.codegen.auth.http.sections.DefaultHttpAuthSchemeParametersProviderFunctionCodeSection; import software.amazon.smithy.typescript.codegen.auth.http.sections.DefaultHttpAuthSchemeProviderFunctionCodeSection; +import software.amazon.smithy.typescript.codegen.auth.http.sections.HttpAuthOptionFunctionCodeSection; import software.amazon.smithy.typescript.codegen.auth.http.sections.HttpAuthOptionFunctionsCodeSection; import software.amazon.smithy.typescript.codegen.auth.http.sections.HttpAuthSchemeParametersProviderInterfaceCodeSection; import software.amazon.smithy.typescript.codegen.auth.http.sections.HttpAuthSchemeProviderInterfaceCodeSection; @@ -82,7 +85,7 @@ public HttpAuthSchemeProviderGenerator( this.symbolProvider = symbolProvider; this.integrations = integrations; - this.authIndex = new SupportedHttpAuthSchemesIndex(integrations); + this.authIndex = new SupportedHttpAuthSchemesIndex(integrations, model, settings); this.serviceIndex = ServiceIndex.of(model); this.serviceShape = settings.getService(model); this.serviceSymbol = symbolProvider.toSymbol(serviceShape); @@ -233,7 +236,15 @@ private void generateHttpAuthOptionFunctions() { .effectiveHttpAuthSchemes(effectiveHttpAuthSchemes) .build()); for (Entry entry : effectiveHttpAuthSchemes.entrySet()) { - generateHttpAuthOptionFunction(w, entry.getKey(), entry.getValue()); + generateHttpAuthOptionFunction(w, HttpAuthOptionFunctionCodeSection.builder() + .service(serviceShape) + .settings(settings) + .model(model) + .symbolProvider(symbolProvider) + .effectiveHttpAuthSchemes(effectiveHttpAuthSchemes) + .schemeId(entry.getKey()) + .httpAuthScheme(entry.getValue()) + .build()); } w.popState(); }); @@ -258,10 +269,12 @@ function createSmithyApiHttpApiKeyAuthHttpAuthOption(authParameters: WeatherHttp */ private void generateHttpAuthOptionFunction( TypeScriptWriter w, - ShapeId shapeId, - HttpAuthScheme authScheme + HttpAuthOptionFunctionCodeSection s ) { - String normalizedAuthSchemeName = normalizeAuthSchemeName(shapeId); + w.pushState(s); + ShapeId schemeId = s.getSchemeId(); + String normalizedAuthSchemeName = normalizeAuthSchemeName(schemeId); + Optional authSchemeOptional = s.getHttpAuthScheme(); w.addDependency(TypeScriptDependency.SMITHY_TYPES); w.addImport("HttpAuthOption", null, TypeScriptDependency.SMITHY_TYPES); w.openBlock(""" @@ -270,11 +283,12 @@ private void generateHttpAuthOptionFunction( normalizedAuthSchemeName, serviceName, () -> { w.openBlock("return {", "};", () -> { - w.write("schemeId: $S,", shapeId.toString()); + w.write("schemeId: $S,", schemeId.toString()); // If no HttpAuthScheme is registered, there are no HttpAuthOptionProperties available. - if (authScheme == null) { + if (authSchemeOptional.isEmpty()) { return; } + HttpAuthScheme authScheme = authSchemeOptional.get(); Trait trait = serviceShape.findTrait(authScheme.getTraitId()).orElse(null); List identityProperties = authScheme.getHttpAuthSchemeOptionParametersByType(Type.IDENTITY); @@ -306,9 +320,12 @@ private void generateHttpAuthOptionFunction( } authScheme.getPropertiesExtractor() .ifPresent(extractor -> w.write("propertiesExtractor: $C", - extractor.apply(serviceSymbol.getName() + "ResolvedConfig"))); + extractor.apply(serviceSymbol.toBuilder() + .name(ServiceBareBonesClientGenerator.getConfigTypeName(serviceSymbol)) + .build()))); }); }); + w.popState(); } private static String normalizeAuthSchemeName(ShapeId shapeId) { diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/ResolveConfigFunction.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/ResolveConfigFunction.java new file mode 100644 index 00000000000..226d2fc3246 --- /dev/null +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/ResolveConfigFunction.java @@ -0,0 +1,73 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + +package software.amazon.smithy.typescript.codegen.auth.http; + +import java.util.Optional; +import software.amazon.smithy.codegen.core.Symbol; +import software.amazon.smithy.utils.SmithyBuilder; +import software.amazon.smithy.utils.SmithyUnstableApi; +import software.amazon.smithy.utils.ToSmithyBuilder; + +/** + * ResolveConfigFunction. + */ +@SmithyUnstableApi +public final record ResolveConfigFunction( + Symbol resolveConfigFunction, + Symbol inputConfig, + Symbol resolvedConfig, + Optional previouslyResolved +) implements ToSmithyBuilder { + + public static Builder builder() { + return new Builder(); + } + + @Override + public Builder toBuilder() { + return builder() + .resolveConfigFunction(resolveConfigFunction) + .inputConfig(inputConfig) + .resolvedConfig(resolvedConfig) + .previouslyResolved(previouslyResolved.orElse(null)); + } + + public static final class Builder implements SmithyBuilder { + private Symbol resolveConfigFunction; + private Symbol inputConfig; + private Symbol resolvedConfig; + private Symbol previouslyResolved; + + @Override + public ResolveConfigFunction build() { + return new ResolveConfigFunction( + SmithyBuilder.requiredState("resolveConfigFunction", resolveConfigFunction), + SmithyBuilder.requiredState("inputConfig", inputConfig), + SmithyBuilder.requiredState("resolvedConfig", resolvedConfig), + Optional.ofNullable(previouslyResolved)); + } + + public Builder resolveConfigFunction(Symbol resolveConfigFunction) { + this.resolveConfigFunction = resolveConfigFunction; + return this; + } + + public Builder inputConfig(Symbol inputConfig) { + this.inputConfig = inputConfig; + return this; + } + + public Builder resolvedConfig(Symbol resolvedConfig) { + this.resolvedConfig = resolvedConfig; + return this; + } + + public Builder previouslyResolved(Symbol previouslyResolved) { + this.previouslyResolved = previouslyResolved; + return this; + } + } +} diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/SupportedHttpAuthSchemesIndex.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/SupportedHttpAuthSchemesIndex.java index 7525af3c836..333d2f70c76 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/SupportedHttpAuthSchemesIndex.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/SupportedHttpAuthSchemesIndex.java @@ -8,7 +8,9 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import software.amazon.smithy.model.Model; import software.amazon.smithy.model.shapes.ShapeId; +import software.amazon.smithy.typescript.codegen.TypeScriptSettings; import software.amazon.smithy.typescript.codegen.auth.http.integration.HttpAuthTypeScriptIntegration; import software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration; import software.amazon.smithy.utils.SmithyInternalApi; @@ -30,7 +32,11 @@ public final class SupportedHttpAuthSchemesIndex { * Creates an index from registered {@link HttpAuthScheme}s in {@link TypeScriptIntegration}s. * @param integrations list of integrations to register HttpAuthSchemes */ - public SupportedHttpAuthSchemesIndex(List integrations) { + public SupportedHttpAuthSchemesIndex( + List integrations, + Model model, + TypeScriptSettings settings + ) { for (TypeScriptIntegration integration : integrations) { if (!(integration instanceof HttpAuthTypeScriptIntegration)) { continue; @@ -40,7 +46,7 @@ public SupportedHttpAuthSchemesIndex(List integrations) { HttpAuthScheme authScheme = httpAuthIntegration.getHttpAuthScheme().get(); this.putHttpAuthScheme(authScheme.getSchemeId(), authScheme); } - httpAuthIntegration.customizeSupportedHttpAuthSchemes(this); + httpAuthIntegration.customizeSupportedHttpAuthSchemes(this, model, settings); } } diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/AddHttpApiKeyAuthPlugin.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/AddHttpApiKeyAuthPlugin.java deleted file mode 100644 index 5f16d554265..00000000000 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/AddHttpApiKeyAuthPlugin.java +++ /dev/null @@ -1,107 +0,0 @@ -/* - * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. - * SPDX-License-Identifier: Apache-2.0 - */ - -package software.amazon.smithy.typescript.codegen.auth.http.integration; - -import java.util.Optional; -import java.util.function.Consumer; -import software.amazon.smithy.codegen.core.CodegenException; -import software.amazon.smithy.model.traits.HttpApiKeyAuthTrait; -import software.amazon.smithy.model.traits.HttpApiKeyAuthTrait.Location; -import software.amazon.smithy.typescript.codegen.ApplicationProtocol; -import software.amazon.smithy.typescript.codegen.ConfigField; -import software.amazon.smithy.typescript.codegen.LanguageTarget; -import software.amazon.smithy.typescript.codegen.TypeScriptDependency; -import software.amazon.smithy.typescript.codegen.TypeScriptSettings; -import software.amazon.smithy.typescript.codegen.TypeScriptWriter; -import software.amazon.smithy.typescript.codegen.auth.http.HttpAuthOptionProperty; -import software.amazon.smithy.typescript.codegen.auth.http.HttpAuthScheme; -import software.amazon.smithy.utils.SmithyInternalApi; - -/** - * Support for @httpApiKeyAuth. - * - * This is the experimental behavior for `experimentalIdentityAndAuth`. - */ -@SmithyInternalApi -public class AddHttpApiKeyAuthPlugin implements HttpAuthTypeScriptIntegration { - private static final Consumer HTTP_API_KEY_AUTH_SIGNER = w -> { - w.addDependency(TypeScriptDependency.SMITHY_CORE); - w.addImport("HttpApiKeyAuthSigner", null, TypeScriptDependency.SMITHY_CORE); - w.write("new HttpApiKeyAuthSigner()"); - }; - - /** - * Integration should only be used if `experimentalIdentityAndAuth` flag is true. - */ - @Override - public boolean matchesSettings(TypeScriptSettings settings) { - return settings.getExperimentalIdentityAndAuth(); - } - - @Override - public Optional getHttpAuthScheme() { - return Optional.of(HttpAuthScheme.builder() - .schemeId(HttpApiKeyAuthTrait.ID) - .applicationProtocol(ApplicationProtocol.createDefaultHttpApplicationProtocol()) - .addConfigField(ConfigField.builder() - .name("apiKey") - .type(ConfigField.Type.MAIN) - .docs(w -> w.write("The API key to use when making requests.")) - .inputType(w -> { - w.addDependency(TypeScriptDependency.SMITHY_TYPES); - w.addImport("ApiKeyIdentity", null, - TypeScriptDependency.SMITHY_TYPES); - w.addImport("ApiKeyIdentityProvider", null, - TypeScriptDependency.SMITHY_TYPES); - w.write("ApiKeyIdentity | ApiKeyIdentityProvider"); - }) - .resolvedType(w -> { - w.addDependency(TypeScriptDependency.SMITHY_TYPES); - w.addImport("ApiKeyIdentityProvider", null, - TypeScriptDependency.SMITHY_TYPES); - w.write("ApiKeyIdentityProvider"); - }) - .build()) - .addHttpAuthOptionProperty(HttpAuthOptionProperty.builder() - .name("name") - .type(HttpAuthOptionProperty.Type.SIGNING) - .source(s -> w -> { - HttpApiKeyAuthTrait t = (HttpApiKeyAuthTrait) s.trait(); - w.write("$S", t.getName()); - }) - .build()) - .addHttpAuthOptionProperty(HttpAuthOptionProperty.builder() - .name("in") - .type(HttpAuthOptionProperty.Type.SIGNING) - .source(s -> w -> { - w.addDependency(TypeScriptDependency.SMITHY_TYPES); - w.addImport("HttpApiKeyAuthLocation", null, - TypeScriptDependency.SMITHY_TYPES); - HttpApiKeyAuthTrait t = (HttpApiKeyAuthTrait) s.trait(); - if (t.getIn().equals(Location.HEADER)) { - w.write("HttpApiKeyAuthLocation.HEADER"); - } else if (t.getIn().equals(Location.QUERY)) { - w.write("HttpApiKeyAuthLocation.QUERY"); - } else { - throw new CodegenException("Encountered invalid `in` property on `@httpApiKeyAuth`: " - + t.getIn()); - } - }) - .build()) - .addHttpAuthOptionProperty(HttpAuthOptionProperty.builder() - .name("scheme") - .type(HttpAuthOptionProperty.Type.SIGNING) - .source(s -> w -> { - HttpApiKeyAuthTrait t = (HttpApiKeyAuthTrait) s.trait(); - t.getScheme().ifPresentOrElse( - scheme -> w.write("$S", scheme), - () -> w.write("undefined")); - }) - .build()) - .putDefaultSigner(LanguageTarget.SHARED, HTTP_API_KEY_AUTH_SIGNER) - .build()); - } -} diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/AddHttpAuthSchemeMiddleware.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/AddHttpAuthSchemePlugin.java similarity index 52% rename from smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/AddHttpAuthSchemeMiddleware.java rename to smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/AddHttpAuthSchemePlugin.java index 6729e1687fa..81e7a3926aa 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/AddHttpAuthSchemeMiddleware.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/AddHttpAuthSchemePlugin.java @@ -6,6 +6,7 @@ package software.amazon.smithy.typescript.codegen.auth.http.integration; import java.util.HashMap; +import java.util.Iterator; import java.util.List; import java.util.Map; import software.amazon.smithy.codegen.core.CodegenException; @@ -15,14 +16,20 @@ import software.amazon.smithy.model.shapes.ShapeId; import software.amazon.smithy.rulesengine.traits.EndpointRuleSetTrait; import software.amazon.smithy.typescript.codegen.CodegenUtils; -import software.amazon.smithy.typescript.codegen.ConfigField; import software.amazon.smithy.typescript.codegen.TypeScriptCodegenContext; import software.amazon.smithy.typescript.codegen.TypeScriptDependency; import software.amazon.smithy.typescript.codegen.TypeScriptSettings; import software.amazon.smithy.typescript.codegen.TypeScriptWriter; import software.amazon.smithy.typescript.codegen.auth.AuthUtils; +import software.amazon.smithy.typescript.codegen.auth.http.ConfigField; import software.amazon.smithy.typescript.codegen.auth.http.HttpAuthScheme; +import software.amazon.smithy.typescript.codegen.auth.http.ResolveConfigFunction; import software.amazon.smithy.typescript.codegen.auth.http.SupportedHttpAuthSchemesIndex; +import software.amazon.smithy.typescript.codegen.auth.http.sections.HttpAuthSchemeInputConfigInterfaceCodeSection; +import software.amazon.smithy.typescript.codegen.auth.http.sections.HttpAuthSchemeResolvedConfigInterfaceCodeSection; +import software.amazon.smithy.typescript.codegen.auth.http.sections.ResolveHttpAuthSchemeConfigFunctionCodeSection; +import software.amazon.smithy.typescript.codegen.auth.http.sections.ResolveHttpAuthSchemeConfigFunctionConfigFieldsCodeSection; +import software.amazon.smithy.typescript.codegen.auth.http.sections.ResolveHttpAuthSchemeConfigFunctionReturnBlockCodeSection; import software.amazon.smithy.typescript.codegen.integration.RuntimeClientPlugin; import software.amazon.smithy.typescript.codegen.integration.RuntimeClientPlugin.Convention; import software.amazon.smithy.typescript.codegen.sections.ClientPropertiesCodeSection; @@ -34,7 +41,7 @@ * Add config and middleware for {@code httpAuthSchemeMiddleware}. */ @SmithyInternalApi -public final class AddHttpAuthSchemeMiddleware implements HttpAuthTypeScriptIntegration { +public final class AddHttpAuthSchemePlugin implements HttpAuthTypeScriptIntegration { /** * Integration should only be used if `experimentalIdentityAndAuth` flag is true. */ @@ -134,7 +141,10 @@ private getIdentityProviderConfigProvider() { new DefaultIdentityProviderConfig({""", "});", s.getSymbolProvider().toSymbol(s.getService()).getName(), () -> { - SupportedHttpAuthSchemesIndex authIndex = new SupportedHttpAuthSchemesIndex(s.getIntegrations()); + SupportedHttpAuthSchemesIndex authIndex = new SupportedHttpAuthSchemesIndex( + s.getIntegrations(), + s.getModel(), + s.getSettings()); ServiceIndex serviceIndex = ServiceIndex.of(s.getModel()); Map httpAuthSchemes = AuthUtils.getAllEffectiveNoAuthAwareAuthSchemes(s.getService(), serviceIndex, authIndex); @@ -148,7 +158,7 @@ private getIdentityProviderConfigProvider() { if (visitedConfigFields.containsKey(configField.name())) { ConfigField visitedConfigField = visitedConfigFields.get(configField.name()); if (!configField.equals(visitedConfigField)) { - throw new CodegenException("Contradicting `ConfigField` defintions for `" + throw new CodegenException("Contradicting `ConfigField` definitions for `" + configField.name() + "`; existing: " + visitedConfigField @@ -177,19 +187,46 @@ public void customize(TypeScriptCodegenContext codegenContext) { } codegenContext.writerDelegator().useFileWriter(AuthUtils.HTTP_AUTH_SCHEME_PROVIDER_PATH, w -> { - SupportedHttpAuthSchemesIndex authIndex = new SupportedHttpAuthSchemesIndex(codegenContext.integrations()); - String serviceName = CodegenUtils.getServiceName( - codegenContext.settings(), codegenContext.model(), codegenContext.symbolProvider()); + SupportedHttpAuthSchemesIndex authIndex = new SupportedHttpAuthSchemesIndex( + codegenContext.integrations(), + codegenContext.model(), + codegenContext.settings()); ServiceShape serviceShape = codegenContext.settings().getService(codegenContext.model()); ServiceIndex serviceIndex = ServiceIndex.of(codegenContext.model()); - Map httpAuthSchemes - = AuthUtils.getAllEffectiveNoAuthAwareAuthSchemes(serviceShape, serviceIndex, authIndex); + Map httpAuthSchemes = + AuthUtils.getAllEffectiveNoAuthAwareAuthSchemes(serviceShape, serviceIndex, authIndex); Map configFields = AuthUtils.collectConfigFields(httpAuthSchemes.values()); + Map resolveConfigFunctions = + AuthUtils.collectResolveConfigFunctions(httpAuthSchemes.values()); - generateHttpAuthSchemeInputConfigInterface(w, configFields, serviceName); - generateHttpAuthSchemeResolvedConfigInterface(w, configFields, serviceName); - generateResolveHttpAuthSchemeConfigFunction(w, configFields, httpAuthSchemes, authIndex); + generateHttpAuthSchemeInputConfigInterface(w, HttpAuthSchemeInputConfigInterfaceCodeSection.builder() + .service(serviceShape) + .settings(codegenContext.settings()) + .model(codegenContext.model()) + .symbolProvider(codegenContext.symbolProvider()) + .integrations(codegenContext.integrations()) + .configFields(configFields) + .resolveConfigFunctions(resolveConfigFunctions) + .build()); + generateHttpAuthSchemeResolvedConfigInterface(w, HttpAuthSchemeResolvedConfigInterfaceCodeSection.builder() + .service(serviceShape) + .settings(codegenContext.settings()) + .model(codegenContext.model()) + .symbolProvider(codegenContext.symbolProvider()) + .integrations(codegenContext.integrations()) + .configFields(configFields) + .resolveConfigFunctions(resolveConfigFunctions) + .build()); + generateResolveHttpAuthSchemeConfigFunction(w, ResolveHttpAuthSchemeConfigFunctionCodeSection.builder() + .service(serviceShape) + .settings(codegenContext.settings()) + .model(codegenContext.model()) + .symbolProvider(codegenContext.symbolProvider()) + .integrations(codegenContext.integrations()) + .configFields(configFields) + .resolveConfigFunctions(resolveConfigFunctions) + .build()); }); } @@ -210,34 +247,51 @@ export interface HttpAuthSchemeInputConfig { */ private void generateHttpAuthSchemeInputConfigInterface( TypeScriptWriter w, - Map configFields, - String serviceName + HttpAuthSchemeInputConfigInterfaceCodeSection s ) { - w.openBlock(""" - /** - * @internal - */ - export interface HttpAuthSchemeInputConfig {""", "}\n", () -> { - w.addDependency(TypeScriptDependency.SMITHY_TYPES); - w.addImport("HttpAuthScheme", null, TypeScriptDependency.SMITHY_TYPES); - w.writeDocs(""" - experimentalIdentityAndAuth: Configuration of HttpAuthSchemes for a client which provides \ - default identity providers and signers per auth scheme. - @internal"""); - w.write("httpAuthSchemes?: HttpAuthScheme[];\n"); + w.pushState(s); + Map configFields = s.getConfigFields(); + Map resolveConfigFunctions = + s.getResolveConfigFunctions(); + String serviceName = CodegenUtils.getServiceName( + s.getSettings(), s.getModel(), s.getSymbolProvider()); + w.writeDocs("@internal"); + w.writeInline("export interface HttpAuthSchemeInputConfig"); + if (!resolveConfigFunctions.isEmpty()) { + w.writeInline(" extends "); + Iterator iter = resolveConfigFunctions.values().iterator(); + while (iter.hasNext()) { + ResolveConfigFunction entry = iter.next(); + w.writeInline("$T", entry.inputConfig()); + if (iter.hasNext()) { + w.writeInline(", "); + } + } + } + w.write(" {"); + w.indent(); + w.addDependency(TypeScriptDependency.SMITHY_TYPES); + w.addImport("HttpAuthScheme", null, TypeScriptDependency.SMITHY_TYPES); + w.writeDocs(""" + experimentalIdentityAndAuth: Configuration of HttpAuthSchemes for a client which provides \ + default identity providers and signers per auth scheme. + @internal"""); + w.write("httpAuthSchemes?: HttpAuthScheme[];\n"); - String httpAuthSchemeProviderName = serviceName + "HttpAuthSchemeProvider"; - w.writeDocs(""" - experimentalIdentityAndAuth: Configuration of an HttpAuthSchemeProvider for a client which \ - resolves which HttpAuthScheme to use. - @internal"""); - w.write("httpAuthSchemeProvider?: $L;\n", httpAuthSchemeProviderName); + String httpAuthSchemeProviderName = serviceName + "HttpAuthSchemeProvider"; + w.writeDocs(""" + experimentalIdentityAndAuth: Configuration of an HttpAuthSchemeProvider for a client which \ + resolves which HttpAuthScheme to use. + @internal"""); + w.write("httpAuthSchemeProvider?: $L;\n", httpAuthSchemeProviderName); - for (ConfigField configField : configFields.values()) { - w.writeDocs(() -> w.write("$C", configField.docs())); - w.write("$L?: $C;", configField.name(), configField.inputType()); - } - }); + for (ConfigField configField : configFields.values()) { + configField.docs().ifPresent(docs -> w.writeDocs(() -> w.write("$C", docs))); + w.write("$L?: $T;", configField.name(), configField.inputType()); + } + w.dedent(); + w.write("}\n"); + w.popState(); } /* @@ -257,34 +311,50 @@ export interface HttpAuthSchemeResolvedConfig { */ private void generateHttpAuthSchemeResolvedConfigInterface( TypeScriptWriter w, - Map configFields, - String serviceName + HttpAuthSchemeResolvedConfigInterfaceCodeSection s ) { - w.openBlock(""" - /** - * @internal - */ - export interface HttpAuthSchemeResolvedConfig {""", "}\n", () -> { - w.addDependency(TypeScriptDependency.SMITHY_TYPES); - w.addImport("HttpAuthScheme", null, TypeScriptDependency.SMITHY_TYPES); - w.writeDocs(""" - experimentalIdentityAndAuth: Configuration of HttpAuthSchemes for a client which provides \ - default identity providers and signers per auth scheme. - @internal"""); - w.write("readonly httpAuthSchemes: HttpAuthScheme[];\n"); - - String httpAuthSchemeProviderName = serviceName + "HttpAuthSchemeProvider"; - w.writeDocs(""" - experimentalIdentityAndAuth: Configuration of an HttpAuthSchemeProvider for a client which \ - resolves which HttpAuthScheme to use. - @internal"""); - w.write("readonly httpAuthSchemeProvider: $L;\n", httpAuthSchemeProviderName); - - for (ConfigField configField : configFields.values()) { - w.writeDocs(() -> w.write("$C", configField.docs())); - w.write("readonly $L?: $C;", configField.name(), configField.resolvedType()); + w.pushState(s); + Map configFields = s.getConfigFields(); + Map resolveConfigFunctions = + s.getResolveConfigFunctions(); + String serviceName = CodegenUtils.getServiceName( + s.getSettings(), s.getModel(), s.getSymbolProvider()); + w.writeDocs("@internal"); + w.writeInline("export interface HttpAuthSchemeResolvedConfig"); + if (!resolveConfigFunctions.isEmpty()) { + w.writeInline(" extends "); + Iterator iter = resolveConfigFunctions.values().iterator(); + while (iter.hasNext()) { + ResolveConfigFunction entry = iter.next(); + w.writeInline("$T", entry.resolvedConfig()); + if (iter.hasNext()) { + w.writeInline(", "); } - }); + } + } + w.write(" {"); + w.indent(); + w.addDependency(TypeScriptDependency.SMITHY_TYPES); + w.addImport("HttpAuthScheme", null, TypeScriptDependency.SMITHY_TYPES); + w.writeDocs(""" + experimentalIdentityAndAuth: Configuration of HttpAuthSchemes for a client which provides \ + default identity providers and signers per auth scheme. + @internal"""); + w.write("readonly httpAuthSchemes: HttpAuthScheme[];\n"); + + String httpAuthSchemeProviderName = serviceName + "HttpAuthSchemeProvider"; + w.writeDocs(""" + experimentalIdentityAndAuth: Configuration of an HttpAuthSchemeProvider for a client which \ + resolves which HttpAuthScheme to use. + @internal"""); + w.write("readonly httpAuthSchemeProvider: $L;\n", httpAuthSchemeProviderName); + for (ConfigField configField : configFields.values()) { + configField.docs().ifPresent(docs -> w.writeDocs(() -> w.write("$C", docs))); + w.write("readonly $L?: $T;", configField.name(), configField.resolvedType()); + } + w.dedent(); + w.write("}\n"); + w.popState(); } /* @@ -304,48 +374,73 @@ export interface HttpAuthSchemeResolvedConfig {""", "}\n", () -> { */ private void generateResolveHttpAuthSchemeConfigFunction( TypeScriptWriter w, - Map configFields, - Map httpAuthSchemes, - SupportedHttpAuthSchemesIndex authIndex + ResolveHttpAuthSchemeConfigFunctionCodeSection s ) { - w.openBlock(""" - /** - * @internal - */ - export const resolveHttpAuthSchemeConfig = (config: HttpAuthSchemeInputConfig): \ - HttpAuthSchemeResolvedConfig => {""", "};", () -> { - // TODO(experimentalIdentityAndAuth): figure out a better way to configure resolving identities - w.addDependency(TypeScriptDependency.SMITHY_CORE); - for (ConfigField configField : configFields.values()) { - if (configField.type().equals(ConfigField.Type.MAIN)) { - w.addDependency(TypeScriptDependency.SMITHY_CORE); - w.addImport("memoizeIdentityProvider", null, - TypeScriptDependency.SMITHY_CORE); - w.addImport("isIdentityExpired", null, - TypeScriptDependency.SMITHY_CORE); - w.addImport("doesIdentityRequireRefresh", null, - TypeScriptDependency.SMITHY_CORE); - w.write(""" - const $L = memoizeIdentityProvider(config.$L, isIdentityExpired, \ - doesIdentityRequireRefresh);""", - configField.name(), - configField.name()); - } - if (configField.type().equals(ConfigField.Type.AUXILIARY)) { - w.addDependency(TypeScriptDependency.UTIL_MIDDLEWARE); - w.addImport("normalizeProvider", null, TypeScriptDependency.UTIL_MIDDLEWARE); - w.write("const $L = config.$L ? normalizeProvider(config.$L) : undefined;", - configField.name(), - configField.name(), - configField.name()); - } + w.pushState(s); + Map configFields = s.getConfigFields(); + Map resolveConfigFunctions = s.getResolveConfigFunctions(); + w.writeDocs("@internal"); + w.writeInline("export const resolveHttpAuthSchemeConfig = (config: T & HttpAuthSchemeInputConfig"); + if (!resolveConfigFunctions.isEmpty()) { + w.writeInline(" & "); + Iterator iter = resolveConfigFunctions.values().iterator(); + while (iter.hasNext()) { + ResolveConfigFunction entry = iter.next(); + w.writeInline("$T", entry.inputConfig()); + entry.previouslyResolved().ifPresent(p -> w.writeInline(" & $T", p)); + if (iter.hasNext()) { + w.writeInline(" & "); } - w.openBlock("return {", "} as HttpAuthSchemeResolvedConfig;", () -> { - w.write("...config,"); - for (ConfigField configField : configFields.values()) { - w.write("$L,", configField.name()); - } - }); - }); + } + } + w.writeInline("): T & HttpAuthSchemeResolvedConfig"); + if (!resolveConfigFunctions.isEmpty()) { + w.writeInline(" & "); + Iterator iter = resolveConfigFunctions.values().iterator(); + while (iter.hasNext()) { + ResolveConfigFunction entry = iter.next(); + w.writeInline("$T", entry.resolvedConfig()); + if (iter.hasNext()) { + w.writeInline(" & "); + } + } + } + w.write(" => {"); + w.indent(); + w.pushState(ResolveHttpAuthSchemeConfigFunctionConfigFieldsCodeSection.builder() + .service(s.getService()) + .settings(s.getSettings()) + .model(s.getModel()) + .symbolProvider(s.getSymbolProvider()) + .integrations(s.getIntegrations()) + .configFields(configFields) + .build()); + w.addDependency(TypeScriptDependency.SMITHY_CORE); + for (ConfigField configField : configFields.values()) { + configField.configFieldWriter().accept(w, configField); + } + w.popState(); + w.pushState(ResolveHttpAuthSchemeConfigFunctionReturnBlockCodeSection.builder() + .service(s.getService()) + .settings(s.getSettings()) + .model(s.getModel()) + .symbolProvider(s.getSymbolProvider()) + .integrations(s.getIntegrations()) + .configFields(configFields) + .build()); + + for (ResolveConfigFunction resolveConfigFunction : resolveConfigFunctions.values()) { + w.write("config = $T(config);", resolveConfigFunction.resolveConfigFunction()); + } + w.openBlock("return {", "} as T & HttpAuthSchemeResolvedConfig;", () -> { + w.write("...config,"); + for (ConfigField configField : configFields.values()) { + w.write("$L,", configField.name()); + } + }); + w.popState(); + w.dedent(); + w.write("};"); + w.popState(); } } diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/AddHttpSigningMiddleware.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/AddHttpSigningPlugin.java similarity index 95% rename from smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/AddHttpSigningMiddleware.java rename to smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/AddHttpSigningPlugin.java index 573f0f1d225..fbdc5c99223 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/AddHttpSigningMiddleware.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/AddHttpSigningPlugin.java @@ -20,7 +20,7 @@ * This is the experimental behavior for `experimentalIdentityAndAuth`. */ @SmithyInternalApi -public class AddHttpSigningMiddleware implements TypeScriptIntegration { +public class AddHttpSigningPlugin implements TypeScriptIntegration { /** * Integration should only be used if `experimentalIdentityAndAuth` flag is true. */ diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/HttpAuthRuntimeExtensionIntegration.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/HttpAuthRuntimeExtensionIntegration.java index cfa8986c7d8..9cff57da8c6 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/HttpAuthRuntimeExtensionIntegration.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/HttpAuthRuntimeExtensionIntegration.java @@ -12,12 +12,12 @@ import software.amazon.smithy.model.shapes.ServiceShape; import software.amazon.smithy.model.shapes.ShapeId; import software.amazon.smithy.typescript.codegen.CodegenUtils; -import software.amazon.smithy.typescript.codegen.ConfigField; import software.amazon.smithy.typescript.codegen.TypeScriptCodegenContext; import software.amazon.smithy.typescript.codegen.TypeScriptDelegator; import software.amazon.smithy.typescript.codegen.TypeScriptDependency; import software.amazon.smithy.typescript.codegen.TypeScriptSettings; import software.amazon.smithy.typescript.codegen.auth.AuthUtils; +import software.amazon.smithy.typescript.codegen.auth.http.ConfigField; import software.amazon.smithy.typescript.codegen.auth.http.HttpAuthScheme; import software.amazon.smithy.typescript.codegen.auth.http.SupportedHttpAuthSchemesIndex; import software.amazon.smithy.typescript.codegen.extensions.ExtensionConfigurationInterface; @@ -55,8 +55,10 @@ public void customize(TypeScriptCodegenContext codegenContext) { return; } TypeScriptDelegator delegator = codegenContext.writerDelegator(); - SupportedHttpAuthSchemesIndex authIndex = - new SupportedHttpAuthSchemesIndex(codegenContext.integrations()); + SupportedHttpAuthSchemesIndex authIndex = new SupportedHttpAuthSchemesIndex( + codegenContext.integrations(), + codegenContext.model(), + codegenContext.settings()); ServiceIndex serviceIndex = ServiceIndex.of(codegenContext.model()); String serviceName = CodegenUtils.getServiceName( codegenContext.settings(), codegenContext.model(), codegenContext.symbolProvider()); @@ -134,12 +136,18 @@ export interface HttpAuthExtensionConfiguration {""", "}", serviceName); w.write("httpAuthSchemeProvider(): $LHttpAuthSchemeProvider;", serviceName); - for (ConfigField configField : configFields.values()) { - if (configField.type().equals(ConfigField.Type.MAIN)) { - String capitalizedName = StringUtils.capitalize(configField.name()); - w.write("set$L($L: $C): void;", capitalizedName, configField.name(), configField.inputType()); - w.write("$L(): $C | undefined;", configField.name(), configField.inputType()); - } + List mainConfigFields = configFields.values().stream() + .filter(c -> c.type().equals(ConfigField.Type.MAIN)) + .toList(); + for (ConfigField configField : mainConfigFields) { + String capitalizedName = StringUtils.capitalize(configField.name()); + w.write("set$L($L: $T): void;", + capitalizedName, + configField.name(), + configField.inputType()); + w.write("$L(): $T | undefined;", + configField.name(), + configField.inputType()); } }); }); @@ -190,10 +198,13 @@ private void generateHttpAuthExtensionRuntimeConfigType( w.write("httpAuthSchemes: HttpAuthScheme[];"); w.addImport(serviceName + "HttpAuthSchemeProvider", null, AuthUtils.AUTH_HTTP_PROVIDER_DEPENDENCY); w.write("httpAuthSchemeProvider: $LHttpAuthSchemeProvider;", serviceName); - for (ConfigField configField : configFields.values()) { - if (configField.type().equals(ConfigField.Type.MAIN)) { - w.write("$L: $C;", configField.name(), configField.inputType()); - } + List mainConfigFields = configFields.values().stream() + .filter(c -> c.type().equals(ConfigField.Type.MAIN)) + .toList(); + for (ConfigField configField : mainConfigFields) { + w.write("$L: $T;", + configField.name(), + configField.inputType()); } }); }); @@ -281,10 +292,11 @@ private void generateGetHttpAuthExtensionConfigurationFunction( w.write("let _httpAuthSchemes = runtimeConfig.httpAuthSchemes!;"); w.addImport(serviceName + "HttpAuthSchemeProvider", null, AuthUtils.AUTH_HTTP_PROVIDER_DEPENDENCY); w.write("let _httpAuthSchemeProvider = runtimeConfig.httpAuthSchemeProvider!;"); - for (ConfigField configField : configFields.values()) { - if (configField.type().equals(ConfigField.Type.MAIN)) { - w.write("let _$L = runtimeConfig.$L;", configField.name(), configField.name()); - } + List mainConfigFields = configFields.values().stream() + .filter(c -> c.type().equals(ConfigField.Type.MAIN)) + .toList(); + for (ConfigField configField : mainConfigFields) { + w.write("let _$L = runtimeConfig.$L;", configField.name(), configField.name()); } w.openBlock("return {", "}", @@ -309,21 +321,19 @@ private void generateGetHttpAuthExtensionConfigurationFunction( httpAuthSchemeProvider(): $LHttpAuthSchemeProvider { return _httpAuthSchemeProvider; },""", serviceName, serviceName); - for (ConfigField configField : configFields.values()) { - if (configField.type().equals(ConfigField.Type.MAIN)) { - String capitalizedName = StringUtils.capitalize(configField.name()); - w.write(""" - set$L($L: $C): void { - _$L = $L; - }, - $L(): $C | undefined { - return _$L; - },""", - capitalizedName, configField.name(), configField.inputType(), - configField.name(), configField.name(), - configField.name(), configField.inputType(), - configField.name()); - } + for (ConfigField configField : mainConfigFields) { + String capitalizedName = StringUtils.capitalize(configField.name()); + w.write(""" + set$L($L: $T): void { + _$L = $L; + }, + $L(): $T | undefined { + return _$L; + },""", + capitalizedName, configField.name(), configField.inputType(), + configField.name(), configField.name(), + configField.name(), configField.inputType(), + configField.name()); } }); }); diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/HttpAuthTypeScriptIntegration.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/HttpAuthTypeScriptIntegration.java index 6b29764b32a..77d4006972a 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/HttpAuthTypeScriptIntegration.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/HttpAuthTypeScriptIntegration.java @@ -6,6 +6,8 @@ package software.amazon.smithy.typescript.codegen.auth.http.integration; import java.util.Optional; +import software.amazon.smithy.model.Model; +import software.amazon.smithy.typescript.codegen.TypeScriptSettings; import software.amazon.smithy.typescript.codegen.auth.http.HttpAuthScheme; import software.amazon.smithy.typescript.codegen.auth.http.SupportedHttpAuthSchemesIndex; import software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration; @@ -31,7 +33,14 @@ default Optional getHttpAuthScheme() { * feat(experimentalIdentityAndAuth): Mutate an {@link SupportedHttpAuthSchemesIndex} to mutate registered * {@link HttpAuthScheme}s, e.g. default {@code IdentityProvider}s and {@code HttpSigner}s. * @param supportedHttpAuthSchemesIndex index to mutate. + * @param model model + * @param settings settings */ - default void customizeSupportedHttpAuthSchemes(SupportedHttpAuthSchemesIndex supportedHttpAuthSchemesIndex) { + default void customizeSupportedHttpAuthSchemes( + SupportedHttpAuthSchemesIndex supportedHttpAuthSchemesIndex, + Model model, + TypeScriptSettings settings + ) { + // pass } } diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/SupportHttpApiKeyAuth.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/SupportHttpApiKeyAuth.java new file mode 100644 index 00000000000..00b32335122 --- /dev/null +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/SupportHttpApiKeyAuth.java @@ -0,0 +1,119 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + +package software.amazon.smithy.typescript.codegen.auth.http.integration; + +import java.util.Optional; +import java.util.function.Consumer; +import software.amazon.smithy.codegen.core.CodegenException; +import software.amazon.smithy.codegen.core.Symbol; +import software.amazon.smithy.model.traits.HttpApiKeyAuthTrait; +import software.amazon.smithy.model.traits.HttpApiKeyAuthTrait.Location; +import software.amazon.smithy.typescript.codegen.ApplicationProtocol; +import software.amazon.smithy.typescript.codegen.LanguageTarget; +import software.amazon.smithy.typescript.codegen.TypeScriptDependency; +import software.amazon.smithy.typescript.codegen.TypeScriptSettings; +import software.amazon.smithy.typescript.codegen.TypeScriptWriter; +import software.amazon.smithy.typescript.codegen.auth.http.ConfigField; +import software.amazon.smithy.typescript.codegen.auth.http.HttpAuthOptionProperty; +import software.amazon.smithy.typescript.codegen.auth.http.HttpAuthScheme; +import software.amazon.smithy.utils.SmithyInternalApi; + +/** + * Support for @httpApiKeyAuth. + * + * This is the experimental behavior for `experimentalIdentityAndAuth`. + */ +@SmithyInternalApi +public class SupportHttpApiKeyAuth implements HttpAuthTypeScriptIntegration { + private static final Consumer HTTP_API_KEY_AUTH_SIGNER = w -> + w.write("new $T()", Symbol.builder() + .name("HttpApiKeyAuthSigner") + .namespace(TypeScriptDependency.SMITHY_CORE.getPackageName(), "/") + .addDependency(TypeScriptDependency.SMITHY_CORE) + .build()); + private static final Symbol API_KEY_IDENTITY = Symbol.builder() + .name("ApiKeyIdentity") + .namespace(TypeScriptDependency.SMITHY_TYPES.getPackageName(), "/") + .addDependency(TypeScriptDependency.SMITHY_TYPES) + .build(); + private static final Symbol API_KEY_IDENTITY_PROVIDER = Symbol.builder() + .name("ApiKeyIdentityProvider") + .namespace(TypeScriptDependency.SMITHY_TYPES.getPackageName(), "/") + .addDependency(TypeScriptDependency.SMITHY_TYPES) + .build(); + private static final Symbol HTTP_API_KEY_LOCATION = Symbol.builder() + .name("HttpApiKeyAuthLocation") + .namespace(TypeScriptDependency.SMITHY_TYPES.getPackageName(), "/") + .addDependency(TypeScriptDependency.SMITHY_TYPES) + .build(); + + /** + * Integration should only be used if `experimentalIdentityAndAuth` flag is true. + */ + @Override + public boolean matchesSettings(TypeScriptSettings settings) { + return settings.getExperimentalIdentityAndAuth(); + } + + @Override + public Optional getHttpAuthScheme() { + return Optional.of(HttpAuthScheme.builder() + .schemeId(HttpApiKeyAuthTrait.ID) + .applicationProtocol(ApplicationProtocol.createDefaultHttpApplicationProtocol()) + .addConfigField(ConfigField.builder() + .name("apiKey") + .type(ConfigField.Type.MAIN) + .docs(w -> w.write("The API key to use when making requests.")) + .inputType(Symbol.builder() + .name("ApiKeyIdentity | ApiKeyIdentityProvider") + .addReference(API_KEY_IDENTITY) + .addReference(API_KEY_IDENTITY_PROVIDER) + .build()) + .resolvedType(Symbol.builder() + .name("ApiKeyIdentityProvider") + .addReference(API_KEY_IDENTITY_PROVIDER) + .build()) + .configFieldWriter(ConfigField::defaultMainConfigFieldWriter) + .build()) + .addHttpAuthOptionProperty(HttpAuthOptionProperty.builder() + .name("name") + .type(HttpAuthOptionProperty.Type.SIGNING) + .source(s -> w -> w.write("$S", ((HttpApiKeyAuthTrait) s.trait()).getName())) + .build()) + .addHttpAuthOptionProperty(HttpAuthOptionProperty.builder() + .name("in") + .type(HttpAuthOptionProperty.Type.SIGNING) + .source(s -> w -> { + Location in = ((HttpApiKeyAuthTrait) s.trait()).getIn(); + switch (in) { + case HEADER: { + w.write("$T.HEADER", HTTP_API_KEY_LOCATION); + break; + } + case QUERY: { + w.write("$T.QUERY", HTTP_API_KEY_LOCATION); + break; + } + default: { + throw new CodegenException( + "Encountered unsupported `in` property on " + + "`@httpApiKeyAuth`: " + + in); + } + } + }) + .build()) + .addHttpAuthOptionProperty(HttpAuthOptionProperty.builder() + .name("scheme") + .type(HttpAuthOptionProperty.Type.SIGNING) + .source(s -> w -> ((HttpApiKeyAuthTrait) s.trait()).getScheme().ifPresentOrElse( + scheme -> w.write("$S", scheme), + () -> w.write("undefined"))) + .build()) + .putDefaultSigner(LanguageTarget.SHARED, HTTP_API_KEY_AUTH_SIGNER) + .build()); + } +} diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/AddHttpBearerAuthPlugin.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/SupportHttpBearerAuth.java similarity index 53% rename from smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/AddHttpBearerAuthPlugin.java rename to smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/SupportHttpBearerAuth.java index 0ed1d172389..f5ff5d94482 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/AddHttpBearerAuthPlugin.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/SupportHttpBearerAuth.java @@ -7,14 +7,15 @@ import java.util.Optional; import java.util.function.Consumer; +import software.amazon.smithy.codegen.core.Symbol; import software.amazon.smithy.model.traits.HttpBearerAuthTrait; import software.amazon.smithy.typescript.codegen.ApplicationProtocol; -import software.amazon.smithy.typescript.codegen.ConfigField; -import software.amazon.smithy.typescript.codegen.ConfigField.Type; import software.amazon.smithy.typescript.codegen.LanguageTarget; import software.amazon.smithy.typescript.codegen.TypeScriptDependency; import software.amazon.smithy.typescript.codegen.TypeScriptSettings; import software.amazon.smithy.typescript.codegen.TypeScriptWriter; +import software.amazon.smithy.typescript.codegen.auth.http.ConfigField; +import software.amazon.smithy.typescript.codegen.auth.http.ConfigField.Type; import software.amazon.smithy.typescript.codegen.auth.http.HttpAuthScheme; import software.amazon.smithy.utils.SmithyInternalApi; @@ -24,12 +25,23 @@ * This is the experimental behavior for `experimentalIdentityAndAuth`. */ @SmithyInternalApi -public final class AddHttpBearerAuthPlugin implements HttpAuthTypeScriptIntegration { - private static final Consumer HTTP_BEARER_AUTH_SIGNER = w -> { - w.addDependency(TypeScriptDependency.SMITHY_CORE); - w.addImport("HttpBearerAuthSigner", null, TypeScriptDependency.SMITHY_CORE); - w.write("new HttpBearerAuthSigner()"); - }; +public final class SupportHttpBearerAuth implements HttpAuthTypeScriptIntegration { + private static final Consumer HTTP_BEARER_AUTH_SIGNER = w -> + w.write("new $T()", Symbol.builder() + .name("HttpBearerAuthSigner") + .namespace(TypeScriptDependency.SMITHY_CORE.getPackageName(), "/") + .addDependency(TypeScriptDependency.SMITHY_CORE) + .build()); + private static final Symbol TOKEN_IDENTITY = Symbol.builder() + .name("TokenIdentity") + .namespace(TypeScriptDependency.SMITHY_TYPES.getPackageName(), "/") + .addDependency(TypeScriptDependency.SMITHY_TYPES) + .build(); + private static final Symbol TOKEN_IDENTITY_PROVIDER = Symbol.builder() + .name("TokenIdentityProvider") + .namespace(TypeScriptDependency.SMITHY_TYPES.getPackageName(), "/") + .addDependency(TypeScriptDependency.SMITHY_TYPES) + .build(); /** * Integration should only be used if `experimentalIdentityAndAuth` flag is true. @@ -48,20 +60,16 @@ public Optional getHttpAuthScheme() { .name("token") .type(Type.MAIN) .docs(w -> w.write("The token used to authenticate requests.")) - .inputType(w -> { - w.addDependency(TypeScriptDependency.SMITHY_TYPES); - w.addImport("TokenIdentity", null, - TypeScriptDependency.SMITHY_TYPES); - w.addImport("TokenIdentityProvider", null, - TypeScriptDependency.SMITHY_TYPES); - w.write("TokenIdentity | TokenIdentityProvider"); - }) - .resolvedType(w -> { - w.addDependency(TypeScriptDependency.SMITHY_TYPES); - w.addImport("TokenIdentityProvider", null, - TypeScriptDependency.SMITHY_TYPES); - w.write("TokenIdentityProvider"); - }) + .inputType(Symbol.builder() + .name("TokenIdentity | TokenIdentityProvider") + .addReference(TOKEN_IDENTITY) + .addReference(TOKEN_IDENTITY_PROVIDER) + .build()) + .resolvedType(Symbol.builder() + .name("TokenIdentityProvider") + .addReference(TOKEN_IDENTITY_PROVIDER) + .build()) + .configFieldWriter(ConfigField::defaultMainConfigFieldWriter) .build()) .putDefaultSigner(LanguageTarget.SHARED, HTTP_BEARER_AUTH_SIGNER) .build()); diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/AddNoAuthPlugin.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/SupportNoAuth.java similarity index 82% rename from smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/AddNoAuthPlugin.java rename to smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/SupportNoAuth.java index 821a811f31d..5025c26b18c 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/AddNoAuthPlugin.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/SupportNoAuth.java @@ -7,6 +7,7 @@ import java.util.Optional; import java.util.function.Consumer; +import software.amazon.smithy.codegen.core.Symbol; import software.amazon.smithy.model.traits.synthetic.NoAuthTrait; import software.amazon.smithy.typescript.codegen.ApplicationProtocol; import software.amazon.smithy.typescript.codegen.LanguageTarget; @@ -20,14 +21,15 @@ * Add config and middleware to support the synthetic @noAuth auth scheme. */ @SmithyInternalApi -public final class AddNoAuthPlugin implements HttpAuthTypeScriptIntegration { +public final class SupportNoAuth implements HttpAuthTypeScriptIntegration { private static final Consumer NO_AUTH_IDENTITY_PROVIDER_WRITER = w -> w.write("async () => ({})"); - private static final Consumer NO_AUTH_SIGNER_WRITER = w -> { - w.addDependency(TypeScriptDependency.SMITHY_CORE); - w.addImport("NoAuthSigner", null, TypeScriptDependency.SMITHY_CORE); - w.write("new NoAuthSigner()"); - }; + private static final Consumer NO_AUTH_SIGNER_WRITER = w -> + w.write("new $T()", Symbol.builder() + .name("NoAuthSigner") + .namespace(TypeScriptDependency.SMITHY_CORE.getPackageName(), "/") + .addDependency(TypeScriptDependency.SMITHY_CORE) + .build()); /** * Integration should only be used if `experimentalIdentityAndAuth` flag is true. diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/sections/HttpAuthOptionFunctionCodeSection.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/sections/HttpAuthOptionFunctionCodeSection.java new file mode 100644 index 00000000000..d83c808f293 --- /dev/null +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/sections/HttpAuthOptionFunctionCodeSection.java @@ -0,0 +1,122 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + +package software.amazon.smithy.typescript.codegen.auth.http.sections; + +import java.util.Map; +import java.util.Optional; +import software.amazon.smithy.codegen.core.SymbolProvider; +import software.amazon.smithy.model.Model; +import software.amazon.smithy.model.shapes.ServiceShape; +import software.amazon.smithy.model.shapes.ShapeId; +import software.amazon.smithy.typescript.codegen.TypeScriptSettings; +import software.amazon.smithy.typescript.codegen.auth.http.HttpAuthScheme; +import software.amazon.smithy.utils.CodeSection; +import software.amazon.smithy.utils.SmithyBuilder; +import software.amazon.smithy.utils.SmithyInternalApi; + +@SmithyInternalApi +public final class HttpAuthOptionFunctionCodeSection implements CodeSection { + private final ServiceShape service; + private final TypeScriptSettings settings; + private final Model model; + private final SymbolProvider symbolProvider; + private final Map effectiveHttpAuthSchemes; + private final ShapeId schemeId; + private final HttpAuthScheme httpAuthScheme; + + private HttpAuthOptionFunctionCodeSection(Builder builder) { + service = SmithyBuilder.requiredState("service", builder.service); + settings = SmithyBuilder.requiredState("settings", builder.settings); + model = SmithyBuilder.requiredState("model", builder.model); + symbolProvider = SmithyBuilder.requiredState("symbolProvider", builder.symbolProvider); + effectiveHttpAuthSchemes = + SmithyBuilder.requiredState("effectiveHttpAuthSchemes", builder.effectiveHttpAuthSchemes); + schemeId = SmithyBuilder.requiredState("schemeId", builder.schemeId); + httpAuthScheme = builder.httpAuthScheme; + } + + public ServiceShape getService() { + return service; + } + + public TypeScriptSettings getSettings() { + return settings; + } + + public Model getModel() { + return model; + } + + public SymbolProvider getSymbolProvider() { + return symbolProvider; + } + + public Map getEffectiveHttpAuthSchemes() { + return effectiveHttpAuthSchemes; + } + + public ShapeId getSchemeId() { + return schemeId; + } + + public Optional getHttpAuthScheme() { + return Optional.ofNullable(httpAuthScheme); + } + + public static Builder builder() { + return new Builder(); + } + + public static class Builder implements SmithyBuilder { + private ServiceShape service; + private TypeScriptSettings settings; + private Model model; + private SymbolProvider symbolProvider; + private Map effectiveHttpAuthSchemes; + private ShapeId schemeId; + private HttpAuthScheme httpAuthScheme; + + @Override + public HttpAuthOptionFunctionCodeSection build() { + return new HttpAuthOptionFunctionCodeSection(this); + } + + public Builder service(ServiceShape service) { + this.service = service; + return this; + } + + public Builder settings(TypeScriptSettings settings) { + this.settings = settings; + return this; + } + + public Builder model(Model model) { + this.model = model; + return this; + } + + public Builder symbolProvider(SymbolProvider symbolProvider) { + this.symbolProvider = symbolProvider; + return this; + } + + public Builder effectiveHttpAuthSchemes(Map effectiveHttpAuthSchemes) { + this.effectiveHttpAuthSchemes = effectiveHttpAuthSchemes; + return this; + } + + public Builder schemeId(ShapeId schemeId) { + this.schemeId = schemeId; + return this; + } + + public Builder httpAuthScheme(HttpAuthScheme httpAuthScheme) { + this.httpAuthScheme = httpAuthScheme; + return this; + } + } +} diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/sections/HttpAuthOptionFunctionsCodeSection.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/sections/HttpAuthOptionFunctionsCodeSection.java index 7d337db9e4a..a4de6c5649f 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/sections/HttpAuthOptionFunctionsCodeSection.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/sections/HttpAuthOptionFunctionsCodeSection.java @@ -24,7 +24,6 @@ public final class HttpAuthOptionFunctionsCodeSection implements CodeSection { private final SymbolProvider symbolProvider; private final Map effectiveHttpAuthSchemes; - private HttpAuthOptionFunctionsCodeSection(Builder builder) { service = SmithyBuilder.requiredState("service", builder.service); settings = SmithyBuilder.requiredState("settings", builder.settings); @@ -50,6 +49,10 @@ public SymbolProvider getSymbolProvider() { return symbolProvider; } + public Map getEffectiveHttpAuthSchemes() { + return effectiveHttpAuthSchemes; + } + public static Builder builder() { return new Builder(); } diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/sections/HttpAuthSchemeInputConfigInterfaceCodeSection.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/sections/HttpAuthSchemeInputConfigInterfaceCodeSection.java new file mode 100644 index 00000000000..fe5eed05ec6 --- /dev/null +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/sections/HttpAuthSchemeInputConfigInterfaceCodeSection.java @@ -0,0 +1,123 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + +package software.amazon.smithy.typescript.codegen.auth.http.sections; + +import java.util.List; +import java.util.Map; +import software.amazon.smithy.codegen.core.Symbol; +import software.amazon.smithy.codegen.core.SymbolProvider; +import software.amazon.smithy.model.Model; +import software.amazon.smithy.model.shapes.ServiceShape; +import software.amazon.smithy.typescript.codegen.TypeScriptSettings; +import software.amazon.smithy.typescript.codegen.auth.http.ConfigField; +import software.amazon.smithy.typescript.codegen.auth.http.ResolveConfigFunction; +import software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration; +import software.amazon.smithy.utils.CodeSection; +import software.amazon.smithy.utils.SmithyBuilder; +import software.amazon.smithy.utils.SmithyInternalApi; + +@SmithyInternalApi +public final class HttpAuthSchemeInputConfigInterfaceCodeSection implements CodeSection { + private final ServiceShape service; + private final TypeScriptSettings settings; + private final Model model; + private final SymbolProvider symbolProvider; + private final List integrations; + private final Map configFields; + private final Map resolveConfigFunctions; + + private HttpAuthSchemeInputConfigInterfaceCodeSection(Builder builder) { + service = SmithyBuilder.requiredState("service", builder.service); + settings = SmithyBuilder.requiredState("settings", builder.settings); + model = SmithyBuilder.requiredState("model", builder.model); + symbolProvider = SmithyBuilder.requiredState("symbolProvider", builder.symbolProvider); + integrations = SmithyBuilder.requiredState("integrations", builder.integrations); + configFields = SmithyBuilder.requiredState("configFields", builder.configFields); + resolveConfigFunctions = SmithyBuilder.requiredState("resolveConfigFunctions", builder.resolveConfigFunctions); + } + + public ServiceShape getService() { + return service; + } + + public TypeScriptSettings getSettings() { + return settings; + } + + public Model getModel() { + return model; + } + + public SymbolProvider getSymbolProvider() { + return symbolProvider; + } + + public List getIntegrations() { + return integrations; + } + + public Map getConfigFields() { + return configFields; + } + + public Map getResolveConfigFunctions() { + return resolveConfigFunctions; + } + + public static Builder builder() { + return new Builder(); + } + + public static class Builder implements SmithyBuilder { + private ServiceShape service; + private TypeScriptSettings settings; + private Model model; + private SymbolProvider symbolProvider; + private List integrations; + private Map configFields; + private Map resolveConfigFunctions; + + @Override + public HttpAuthSchemeInputConfigInterfaceCodeSection build() { + return new HttpAuthSchemeInputConfigInterfaceCodeSection(this); + } + + public Builder service(ServiceShape service) { + this.service = service; + return this; + } + + public Builder settings(TypeScriptSettings settings) { + this.settings = settings; + return this; + } + + public Builder model(Model model) { + this.model = model; + return this; + } + + public Builder symbolProvider(SymbolProvider symbolProvider) { + this.symbolProvider = symbolProvider; + return this; + } + + public Builder integrations(List integrations) { + this.integrations = integrations; + return this; + } + + public Builder configFields(Map configFields) { + this.configFields = configFields; + return this; + } + + public Builder resolveConfigFunctions(Map resolveConfigFunctions) { + this.resolveConfigFunctions = resolveConfigFunctions; + return this; + } + } +} diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/sections/HttpAuthSchemeResolvedConfigInterfaceCodeSection.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/sections/HttpAuthSchemeResolvedConfigInterfaceCodeSection.java new file mode 100644 index 00000000000..85c95bce832 --- /dev/null +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/sections/HttpAuthSchemeResolvedConfigInterfaceCodeSection.java @@ -0,0 +1,123 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + +package software.amazon.smithy.typescript.codegen.auth.http.sections; + +import java.util.List; +import java.util.Map; +import software.amazon.smithy.codegen.core.Symbol; +import software.amazon.smithy.codegen.core.SymbolProvider; +import software.amazon.smithy.model.Model; +import software.amazon.smithy.model.shapes.ServiceShape; +import software.amazon.smithy.typescript.codegen.TypeScriptSettings; +import software.amazon.smithy.typescript.codegen.auth.http.ConfigField; +import software.amazon.smithy.typescript.codegen.auth.http.ResolveConfigFunction; +import software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration; +import software.amazon.smithy.utils.CodeSection; +import software.amazon.smithy.utils.SmithyBuilder; +import software.amazon.smithy.utils.SmithyInternalApi; + +@SmithyInternalApi +public final class HttpAuthSchemeResolvedConfigInterfaceCodeSection implements CodeSection { + private final ServiceShape service; + private final TypeScriptSettings settings; + private final Model model; + private final SymbolProvider symbolProvider; + private final List integrations; + private final Map configFields; + private final Map resolveConfigFunctions; + + private HttpAuthSchemeResolvedConfigInterfaceCodeSection(Builder builder) { + service = SmithyBuilder.requiredState("service", builder.service); + settings = SmithyBuilder.requiredState("settings", builder.settings); + model = SmithyBuilder.requiredState("model", builder.model); + symbolProvider = SmithyBuilder.requiredState("symbolProvider", builder.symbolProvider); + integrations = SmithyBuilder.requiredState("integrations", builder.integrations); + configFields = SmithyBuilder.requiredState("configFields", builder.configFields); + resolveConfigFunctions = SmithyBuilder.requiredState("resolveConfigFunctions", builder.resolveConfigFunctions); + } + + public ServiceShape getService() { + return service; + } + + public TypeScriptSettings getSettings() { + return settings; + } + + public Model getModel() { + return model; + } + + public SymbolProvider getSymbolProvider() { + return symbolProvider; + } + + public List getIntegrations() { + return integrations; + } + + public Map getConfigFields() { + return configFields; + } + + public Map getResolveConfigFunctions() { + return resolveConfigFunctions; + } + + public static Builder builder() { + return new Builder(); + } + + public static class Builder implements SmithyBuilder { + private ServiceShape service; + private TypeScriptSettings settings; + private Model model; + private SymbolProvider symbolProvider; + private List integrations; + private Map configFields; + private Map resolveConfigFunctions; + + @Override + public HttpAuthSchemeResolvedConfigInterfaceCodeSection build() { + return new HttpAuthSchemeResolvedConfigInterfaceCodeSection(this); + } + + public Builder service(ServiceShape service) { + this.service = service; + return this; + } + + public Builder settings(TypeScriptSettings settings) { + this.settings = settings; + return this; + } + + public Builder model(Model model) { + this.model = model; + return this; + } + + public Builder symbolProvider(SymbolProvider symbolProvider) { + this.symbolProvider = symbolProvider; + return this; + } + + public Builder integrations(List integrations) { + this.integrations = integrations; + return this; + } + + public Builder configFields(Map configFields) { + this.configFields = configFields; + return this; + } + + public Builder resolveConfigFunctions(Map resolveConfigFunctions) { + this.resolveConfigFunctions = resolveConfigFunctions; + return this; + } + } +} diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/sections/ResolveHttpAuthSchemeConfigFunctionCodeSection.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/sections/ResolveHttpAuthSchemeConfigFunctionCodeSection.java new file mode 100644 index 00000000000..cc4eed758d9 --- /dev/null +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/sections/ResolveHttpAuthSchemeConfigFunctionCodeSection.java @@ -0,0 +1,123 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + +package software.amazon.smithy.typescript.codegen.auth.http.sections; + +import java.util.List; +import java.util.Map; +import software.amazon.smithy.codegen.core.Symbol; +import software.amazon.smithy.codegen.core.SymbolProvider; +import software.amazon.smithy.model.Model; +import software.amazon.smithy.model.shapes.ServiceShape; +import software.amazon.smithy.typescript.codegen.TypeScriptSettings; +import software.amazon.smithy.typescript.codegen.auth.http.ConfigField; +import software.amazon.smithy.typescript.codegen.auth.http.ResolveConfigFunction; +import software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration; +import software.amazon.smithy.utils.CodeSection; +import software.amazon.smithy.utils.SmithyBuilder; +import software.amazon.smithy.utils.SmithyInternalApi; + +@SmithyInternalApi +public final class ResolveHttpAuthSchemeConfigFunctionCodeSection implements CodeSection { + private final ServiceShape service; + private final TypeScriptSettings settings; + private final Model model; + private final SymbolProvider symbolProvider; + private final List integrations; + private final Map configFields; + private final Map resolveConfigFunctions; + + private ResolveHttpAuthSchemeConfigFunctionCodeSection(Builder builder) { + service = SmithyBuilder.requiredState("service", builder.service); + settings = SmithyBuilder.requiredState("settings", builder.settings); + model = SmithyBuilder.requiredState("model", builder.model); + symbolProvider = SmithyBuilder.requiredState("symbolProvider", builder.symbolProvider); + integrations = SmithyBuilder.requiredState("integrations", builder.integrations); + configFields = SmithyBuilder.requiredState("configFields", builder.configFields); + resolveConfigFunctions = SmithyBuilder.requiredState("resolveConfigFunctions", builder.resolveConfigFunctions); + } + + public ServiceShape getService() { + return service; + } + + public TypeScriptSettings getSettings() { + return settings; + } + + public Model getModel() { + return model; + } + + public SymbolProvider getSymbolProvider() { + return symbolProvider; + } + + public List getIntegrations() { + return integrations; + } + + public Map getConfigFields() { + return configFields; + } + + public Map getResolveConfigFunctions() { + return resolveConfigFunctions; + } + + public static Builder builder() { + return new Builder(); + } + + public static class Builder implements SmithyBuilder { + private ServiceShape service; + private TypeScriptSettings settings; + private Model model; + private SymbolProvider symbolProvider; + private List integrations; + private Map configFields; + private Map resolveConfigFunctions; + + @Override + public ResolveHttpAuthSchemeConfigFunctionCodeSection build() { + return new ResolveHttpAuthSchemeConfigFunctionCodeSection(this); + } + + public Builder service(ServiceShape service) { + this.service = service; + return this; + } + + public Builder settings(TypeScriptSettings settings) { + this.settings = settings; + return this; + } + + public Builder model(Model model) { + this.model = model; + return this; + } + + public Builder symbolProvider(SymbolProvider symbolProvider) { + this.symbolProvider = symbolProvider; + return this; + } + + public Builder integrations(List integrations) { + this.integrations = integrations; + return this; + } + + public Builder configFields(Map configFields) { + this.configFields = configFields; + return this; + } + + public Builder resolveConfigFunctions(Map resolveConfigFunctions) { + this.resolveConfigFunctions = resolveConfigFunctions; + return this; + } + } +} diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/sections/ResolveHttpAuthSchemeConfigFunctionConfigFieldsCodeSection.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/sections/ResolveHttpAuthSchemeConfigFunctionConfigFieldsCodeSection.java new file mode 100644 index 00000000000..f4fa66616e9 --- /dev/null +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/sections/ResolveHttpAuthSchemeConfigFunctionConfigFieldsCodeSection.java @@ -0,0 +1,109 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + +package software.amazon.smithy.typescript.codegen.auth.http.sections; + +import java.util.List; +import java.util.Map; +import software.amazon.smithy.codegen.core.SymbolProvider; +import software.amazon.smithy.model.Model; +import software.amazon.smithy.model.shapes.ServiceShape; +import software.amazon.smithy.typescript.codegen.TypeScriptSettings; +import software.amazon.smithy.typescript.codegen.auth.http.ConfigField; +import software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration; +import software.amazon.smithy.utils.CodeSection; +import software.amazon.smithy.utils.SmithyBuilder; +import software.amazon.smithy.utils.SmithyInternalApi; + +@SmithyInternalApi +public final class ResolveHttpAuthSchemeConfigFunctionConfigFieldsCodeSection implements CodeSection { + private final ServiceShape service; + private final TypeScriptSettings settings; + private final Model model; + private final SymbolProvider symbolProvider; + private final List integrations; + private final Map configFields; + + private ResolveHttpAuthSchemeConfigFunctionConfigFieldsCodeSection(Builder builder) { + service = SmithyBuilder.requiredState("service", builder.service); + settings = SmithyBuilder.requiredState("settings", builder.settings); + model = SmithyBuilder.requiredState("model", builder.model); + symbolProvider = SmithyBuilder.requiredState("symbolProvider", builder.symbolProvider); + integrations = SmithyBuilder.requiredState("integrations", builder.integrations); + configFields = SmithyBuilder.requiredState("configFields", builder.configFields); + } + + public ServiceShape getService() { + return service; + } + + public TypeScriptSettings getSettings() { + return settings; + } + + public Model getModel() { + return model; + } + + public SymbolProvider getSymbolProvider() { + return symbolProvider; + } + + public List getIntegrations() { + return integrations; + } + + public Map getConfigFields() { + return configFields; + } + + public static Builder builder() { + return new Builder(); + } + + public static class Builder implements SmithyBuilder { + private ServiceShape service; + private TypeScriptSettings settings; + private Model model; + private SymbolProvider symbolProvider; + private List integrations; + private Map configFields; + + @Override + public ResolveHttpAuthSchemeConfigFunctionConfigFieldsCodeSection build() { + return new ResolveHttpAuthSchemeConfigFunctionConfigFieldsCodeSection(this); + } + + public Builder service(ServiceShape service) { + this.service = service; + return this; + } + + public Builder settings(TypeScriptSettings settings) { + this.settings = settings; + return this; + } + + public Builder model(Model model) { + this.model = model; + return this; + } + + public Builder symbolProvider(SymbolProvider symbolProvider) { + this.symbolProvider = symbolProvider; + return this; + } + + public Builder integrations(List integrations) { + this.integrations = integrations; + return this; + } + + public Builder configFields(Map configFields) { + this.configFields = configFields; + return this; + } + } +} diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/sections/ResolveHttpAuthSchemeConfigFunctionReturnBlockCodeSection.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/sections/ResolveHttpAuthSchemeConfigFunctionReturnBlockCodeSection.java new file mode 100644 index 00000000000..2fb3dc52652 --- /dev/null +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/sections/ResolveHttpAuthSchemeConfigFunctionReturnBlockCodeSection.java @@ -0,0 +1,109 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + +package software.amazon.smithy.typescript.codegen.auth.http.sections; + +import java.util.List; +import java.util.Map; +import software.amazon.smithy.codegen.core.SymbolProvider; +import software.amazon.smithy.model.Model; +import software.amazon.smithy.model.shapes.ServiceShape; +import software.amazon.smithy.typescript.codegen.TypeScriptSettings; +import software.amazon.smithy.typescript.codegen.auth.http.ConfigField; +import software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration; +import software.amazon.smithy.utils.CodeSection; +import software.amazon.smithy.utils.SmithyBuilder; +import software.amazon.smithy.utils.SmithyInternalApi; + +@SmithyInternalApi +public final class ResolveHttpAuthSchemeConfigFunctionReturnBlockCodeSection implements CodeSection { + private final ServiceShape service; + private final TypeScriptSettings settings; + private final Model model; + private final SymbolProvider symbolProvider; + private final List integrations; + private final Map configFields; + + private ResolveHttpAuthSchemeConfigFunctionReturnBlockCodeSection(Builder builder) { + service = SmithyBuilder.requiredState("service", builder.service); + settings = SmithyBuilder.requiredState("settings", builder.settings); + model = SmithyBuilder.requiredState("model", builder.model); + symbolProvider = SmithyBuilder.requiredState("symbolProvider", builder.symbolProvider); + integrations = SmithyBuilder.requiredState("integrations", builder.integrations); + configFields = SmithyBuilder.requiredState("configFields", builder.configFields); + } + + public ServiceShape getService() { + return service; + } + + public TypeScriptSettings getSettings() { + return settings; + } + + public Model getModel() { + return model; + } + + public SymbolProvider getSymbolProvider() { + return symbolProvider; + } + + public List getIntegrations() { + return integrations; + } + + public Map getConfigFields() { + return configFields; + } + + public static Builder builder() { + return new Builder(); + } + + public static class Builder implements SmithyBuilder { + private ServiceShape service; + private TypeScriptSettings settings; + private Model model; + private SymbolProvider symbolProvider; + private List integrations; + private Map configFields; + + @Override + public ResolveHttpAuthSchemeConfigFunctionReturnBlockCodeSection build() { + return new ResolveHttpAuthSchemeConfigFunctionReturnBlockCodeSection(this); + } + + public Builder service(ServiceShape service) { + this.service = service; + return this; + } + + public Builder settings(TypeScriptSettings settings) { + this.settings = settings; + return this; + } + + public Builder model(Model model) { + this.model = model; + return this; + } + + public Builder symbolProvider(SymbolProvider symbolProvider) { + this.symbolProvider = symbolProvider; + return this; + } + + public Builder integrations(List integrations) { + this.integrations = integrations; + return this; + } + + public Builder configFields(Map configFields) { + this.configFields = configFields; + return this; + } + } +} diff --git a/smithy-typescript-codegen/src/main/resources/META-INF/services/software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration b/smithy-typescript-codegen/src/main/resources/META-INF/services/software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration index 63f5a644416..ccce6dca0a5 100644 --- a/smithy-typescript-codegen/src/main/resources/META-INF/services/software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration +++ b/smithy-typescript-codegen/src/main/resources/META-INF/services/software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration @@ -2,12 +2,12 @@ software.amazon.smithy.typescript.codegen.integration.AddClientRuntimeConfig software.amazon.smithy.typescript.codegen.integration.AddEventStreamDependency software.amazon.smithy.typescript.codegen.integration.AddChecksumRequiredDependency software.amazon.smithy.typescript.codegen.integration.AddDefaultsModeDependency -software.amazon.smithy.typescript.codegen.auth.http.integration.AddHttpAuthSchemeMiddleware -software.amazon.smithy.typescript.codegen.auth.http.integration.AddHttpSigningMiddleware +software.amazon.smithy.typescript.codegen.auth.http.integration.AddHttpAuthSchemePlugin +software.amazon.smithy.typescript.codegen.auth.http.integration.AddHttpSigningPlugin software.amazon.smithy.typescript.codegen.auth.http.integration.HttpAuthRuntimeExtensionIntegration -software.amazon.smithy.typescript.codegen.auth.http.integration.AddNoAuthPlugin -software.amazon.smithy.typescript.codegen.auth.http.integration.AddHttpApiKeyAuthPlugin -software.amazon.smithy.typescript.codegen.auth.http.integration.AddHttpBearerAuthPlugin +software.amazon.smithy.typescript.codegen.auth.http.integration.SupportNoAuth +software.amazon.smithy.typescript.codegen.auth.http.integration.SupportHttpApiKeyAuth +software.amazon.smithy.typescript.codegen.auth.http.integration.SupportHttpBearerAuth software.amazon.smithy.typescript.codegen.integration.AddHttpApiKeyAuthPlugin software.amazon.smithy.typescript.codegen.integration.AddBaseServiceExceptionClass software.amazon.smithy.typescript.codegen.integration.AddSdkStreamMixinDependency