I'm working in cdk and producing an api gateway resource. I need to supply a request template that looks like this:
$util.escapeJavaScript(data).replaceAll("\\'","'")
but I can't figure out how to format this in the typescript cdk code:. Here's the error code when you just blindly type it in without making any effort:
lib/factory/api-gateway/api-gw-factory.ts:119:68 - error TS1127: Invalid character.
119 bodyjson : "$util.escapeJavaScript($input.body).replaceAll("\\'","'")",
lib/factory/api-gateway/api-gw-factory.ts:119:74 - error TS1005: ':' expected.
119 bodyjson : "$util.escapeJavaScript($input.body).replaceAll("\\'","'")",
doesn't work obviously. I've tried a heap of other things. I've found that the formatting I need to get the code to compile just gets fed straight into the output template.
I.e. the following compiles, but the escape characters end up in the generated output...
bodyjson : "$util.escapeJavaScript($input.body).replaceAll(\"\\'\",\"'\")",
leads to :
{
"bodyjson": "$util.escapeJavaScript($input.body).replaceAll(\"\\'\",\"'\")",
in the template... Which is clearly not sensible apache velocity.
I even tried this:
const testStr = "$util.escapeJavaScript($input.json('$')).replaceAll(\"\\'\",\"'\")";
const escapedStr = testStr.replace(/\"/g,'"');
let requestTemplate: {[index: string]:any} = {
"bodyjson" : escapedStr, // escapes reappear in template!
but the escape characters reappear in the cursed template afterwards!
How do I achieve the desired output format?
Did you try to use String.raw()
Like
String.raw`$util.escapeJavaScript(data).replaceAll("\\'","'")`
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With