Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

API Gateway Request map format for json with single quotes

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?

like image 882
monkey Avatar asked Sep 19 '25 03:09

monkey


1 Answers

Did you try to use String.raw()

Like

String.raw`$util.escapeJavaScript(data).replaceAll("\\'","'")`
like image 98
emilio Avatar answered Sep 20 '25 18:09

emilio