Skip to content

Commit

Permalink
allow TypeScriptIntegration to write prior to the config object liter…
Browse files Browse the repository at this point in the history
…al (#1054)

* allow TypeScriptIntegration to write prior to the config object literal

* set new method to internalApi
  • Loading branch information
kuhe authored Oct 24, 2023
1 parent d5eacb5 commit 1debb82
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .changeset/great-schools-cheer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,17 @@ void generate(LanguageTarget target) {
.replaceFirst(CodegenUtils.SOURCE_FOLDER + "/", ""))
.replace("${clientConfigName}", symbolProvider.toSymbol(service).getName() + "Config")
.replace("${apiVersion}", service.getVersion())
.replace("$", "$$") // sanitize template place holders.
.replace("$${customizations}", "${L@customizations}");
.replace("${", "$${") // sanitize template place holders.
.replace("$${customizations}", "${L@customizations}")
.replace("$${prepareCustomizations}", "${L@prepareCustomizations}");

delegator.useFileWriter(target.getTargetFilename(), writer -> {
// Inject customizations into the ~template.
writer.onSection("prepareCustomizations", original -> {
for (TypeScriptIntegration integration : integrations) {
integration.prepareCustomizations(writer, target, settings, model);
}
});
writer.indent().onSection("customizations", original -> {
// Start with defaults, use a TreeMap for keeping entries sorted.
Map<String, Consumer<TypeScriptWriter>> configs =
Expand All @@ -198,7 +204,7 @@ void generate(LanguageTarget target) {
});
});
writer.dedent();
writer.write(contents, "");
writer.write(contents, "", "");
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,4 +253,17 @@ default Map<String, Consumer<TypeScriptWriter>> getRuntimeConfigWriters(
default List<ExtensionConfigurationInterface> getExtensionConfigurationInterfaces() {
return Collections.emptyList();
}

/**
* Allows the customization to write arbitrary preparatory code prior to the returned config object.
*/
@SmithyInternalApi
default void prepareCustomizations(
TypeScriptWriter writer,
LanguageTarget target,
TypeScriptSettings settings,
Model model
) {
return;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const getRuntimeConfig = (config: ${clientConfigName}) => {
const defaultsMode = resolveDefaultsModeConfig(config);
const defaultConfigProvider = () => defaultsMode().then(loadConfigsForDefaultMode);
const clientSharedValues = getSharedRuntimeConfig(config);
return {
${prepareCustomizations}return {
...clientSharedValues,
...config,
runtime: "browser",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { getRuntimeConfig as getBrowserRuntimeConfig } from "./runtimeConfig.bro
*/
export const getRuntimeConfig = (config: ${clientConfigName}) => {
const browserDefaults = getBrowserRuntimeConfig(config);
return {
${prepareCustomizations}return {
...browserDefaults,
...config,
runtime: "react-native",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import { ${clientConfigName} } from "${clientModuleName}";
/**
* @internal
*/
export const getRuntimeConfig = (config: ${clientConfigName}) => ({
apiVersion: "${apiVersion}",
${customizations}
});
export const getRuntimeConfig = (config: ${clientConfigName}) => {
${prepareCustomizations}return {
apiVersion: "${apiVersion}",
${customizations}
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const getRuntimeConfig = (config: ${clientConfigName}) => {
const defaultsMode = resolveDefaultsModeConfig(config);
const defaultConfigProvider = () => defaultsMode().then(loadConfigsForDefaultMode);
const clientSharedValues = getSharedRuntimeConfig(config);
return {
${prepareCustomizations}return {
...clientSharedValues,
...config,
runtime: "node",
Expand Down

0 comments on commit 1debb82

Please sign in to comment.