将api网关中的velocity模板转换为使用AWS CDK在typescript中传递

将api网关中的velocity模板转换为使用AWS CDK在typescript中传递,typescript,amazon-web-services,aws-api-gateway,aws-cdk,velocity-template-language,Typescript,Amazon Web Services,Aws Api Gateway,Aws Cdk,Velocity Template Language,我正在使用带有AWS CDK的typescript为api网关生成一个cloudFormation模板。我有一个Apache Velocity模板,可以帮助我转换响应。当我使用typescript创建API网关时。如何从代码本身传递模板。我需要在需要字符串的接口中传递模板。我还没有找到任何合理的方法将其转换为字符串 { "sellableQuantity": $inputRoot.quantity), "reservedQuantity": $inputRoot.reserved

我正在使用带有AWS CDK的typescript为api网关生成一个cloudFormation模板。我有一个Apache Velocity模板,可以帮助我转换响应。当我使用typescript创建API网关时。如何从代码本身传递模板。我需要在需要字符串的接口中传递模板。我还没有找到任何合理的方法将其转换为字符串

{
    "sellableQuantity": $inputRoot.quantity),
    "reservedQuantity": $inputRoot.reservedQuantity)
    "marketplaceInventories": [
        #foreach( $marketplaceInventory in $inputRoot.marketplaceInventories) )
            {
                "sellableQuantity": $marketplaceInventory.sellableQuantity,
                "marketplaceAttributes": {
                    #set( $marketplaceAttributes = $marketplaceInventory.marketplaceAttributes )
                    "marketplaceName": "$marketplaceAttributes.marketplaceName",
                    "channelName": "$marketplaceAttributes.channelName"
                }
            }
            #if( $foreach.hasNext ) , #end
        #end
    ] 
}
您的问题实际上是“如何定义长字符串而不用担心在javascript中转义特殊字符?”

我认为a是最好的选择,因为它允许您不必担心转义或行继续。在字符串和
string.raw周围使用反勾号,可以确保您定义的内容将被逐字传递:

let myVelocityTemplate = String.raw`{
    "sellableQuantity": $inputRoot.quantity),
    "reservedQuantity": $inputRoot.reservedQuantity)
    "marketplaceInventories": [
        #foreach( $marketplaceInventory in $inputRoot.marketplaceInventories) )
            {
                "sellableQuantity": $marketplaceInventory.sellableQuantity,
                "marketplaceAttributes": {
                    #set( $marketplaceAttributes = $marketplaceInventory.marketplaceAttributes )
                    "marketplaceName": "$marketplaceAttributes.marketplaceName",
                    "channelName": "$marketplaceAttributes.channelName"
                }
            }
            #if( $foreach.hasNext ) , #end
        #end
    ] 
}`

java
中,非常简单:

private GraphQLApi CreateGraphQLApi(字符串API_名称){
返回GraphQLApi.Builder.create(这个,API\u NAME+“\u AppSyncApi”)
.name(API_name.concat(“_AppSyncApi”))
.schemaDefinitionFile(Constants.SCHEMA_路径)
.build();
}
您可以传递模式路径,让
cdk
加载和部署资源

我认为
typescript
API与其他语言有1:1的匹配