Skip to content

Commit

Permalink
chore(codegen): merge appId codegen into existing UA codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
siddsriv committed Sep 27, 2024
1 parent df70919 commit d731ec6
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 105 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
/*
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

package software.amazon.smithy.aws.typescript.codegen;
Expand All @@ -20,9 +10,11 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.util.function.Consumer;
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.LanguageTarget;
import software.amazon.smithy.typescript.codegen.TypeScriptDependency;
import software.amazon.smithy.typescript.codegen.TypeScriptSettings;
Expand All @@ -34,8 +26,8 @@
import software.amazon.smithy.utils.SmithyInternalApi;

/**
* Add client plubins and configs to support injecting user agent.
*/
* Add client plugins and configs to support injecting user agent.
*/
@SmithyInternalApi
public class AddUserAgentDependency implements TypeScriptIntegration {
@Override
Expand All @@ -57,6 +49,9 @@ public void addConfigInterfaceFields(
writer.writeDocs("The provider populating default tracking information to be sent with `user-agent`, "
+ "`x-amz-user-agent` header\n@internal");
writer.write("defaultUserAgentProvider?: Provider<__UserAgent>;\n");
writer.writeDocs("The application ID used to identify the application.");
writer.write("userAgentAppId?: string | "
+ "__Provider<string>;\n");
}

@Override
Expand All @@ -66,32 +61,44 @@ public Map<String, Consumer<TypeScriptWriter>> getRuntimeConfigWriters(
SymbolProvider symbolProvider,
LanguageTarget target
) {
Map<String, Consumer<TypeScriptWriter>> runtimeConfigs = new HashMap<>();

switch (target) {
case NODE:
return MapUtils.of(
"defaultUserAgentProvider", writer -> {
writer.addDependency(AwsDependency.AWS_SDK_UTIL_USER_AGENT_NODE.dependency);
writer.addImport("defaultUserAgent", "defaultUserAgent",
AwsDependency.AWS_SDK_UTIL_USER_AGENT_NODE);
writer.addIgnoredDefaultImport("packageInfo", "./package.json",
"package.json will be imported from dist folders");
writeDefaultUserAgentProvider(writer, settings, model);
}
);
runtimeConfigs.put("defaultUserAgentProvider", writer -> {
writer.addDependency(AwsDependency.AWS_SDK_UTIL_USER_AGENT_NODE.dependency);
writer.addImport("defaultUserAgent", "defaultUserAgent",
AwsDependency.AWS_SDK_UTIL_USER_AGENT_NODE);
writer.addIgnoredDefaultImport("packageInfo", "./package.json",
"package.json will be imported from dist folders");
writeDefaultUserAgentProvider(writer, settings, model);
});
runtimeConfigs.put("userAgentAppId", writer -> {
writer.addDependency(TypeScriptDependency.NODE_CONFIG_PROVIDER);
writer.addImport("loadConfig", "loadNodeConfig",
TypeScriptDependency.NODE_CONFIG_PROVIDER);
writer.addDependency(AwsDependency.AWS_SDK_UTIL_USER_AGENT_NODE);
writer.addImport("NODE_APP_ID_CONFIG_OPTIONS", "NODE_APP_ID_CONFIG_OPTIONS",
AwsDependency.AWS_SDK_UTIL_USER_AGENT_NODE);
writer.write("loadNodeConfig(NODE_APP_ID_CONFIG_OPTIONS)");
});
break;
case BROWSER:
return MapUtils.of(
"defaultUserAgentProvider", writer -> {
writer.addDependency(AwsDependency.AWS_SDK_UTIL_USER_AGENT_BROWSER.dependency);
writer.addImport("defaultUserAgent", "defaultUserAgent",
AwsDependency.AWS_SDK_UTIL_USER_AGENT_BROWSER);
writer.addIgnoredDefaultImport("packageInfo", "./package.json",
"package.json will be imported from dist folders");
writeDefaultUserAgentProvider(writer, settings, model);
}
);
runtimeConfigs.put("defaultUserAgentProvider", writer -> {
writer.addDependency(AwsDependency.AWS_SDK_UTIL_USER_AGENT_BROWSER.dependency);
writer.addImport("defaultUserAgent", "defaultUserAgent",
AwsDependency.AWS_SDK_UTIL_USER_AGENT_BROWSER);
writer.addIgnoredDefaultImport("packageInfo", "./package.json",
"package.json will be imported from dist folders");
writeDefaultUserAgentProvider(writer, settings, model);
});
// No userAgentAppId for browser target as it defaults to undefined
break;
default:
return Collections.emptyMap();
// No default case needed
}

return runtimeConfigs;
}

private void writeDefaultUserAgentProvider(TypeScriptWriter writer, TypeScriptSettings settings, Model model) {
Expand All @@ -100,4 +107,4 @@ private void writeDefaultUserAgentProvider(TypeScriptWriter writer, TypeScriptSe
+ (isAwsService(settings, model) ? "serviceId: clientSharedValues.serviceId, " : "")
+ "clientVersion: packageInfo.version})");
}
}
}

0 comments on commit d731ec6

Please sign in to comment.