@digitraffic/common 2024.9.24-1 → 2024.9.24-3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -9,24 +9,40 @@ describe("integration tests", () => {
9
9
  // assert template parses
10
10
  const response = createResponseFromTemplate(template);
11
11
  // assert response parses
12
- JSON.parse(response);
13
- return template;
12
+ console.info("response " + response);
13
+ return JSON.parse(response);
14
14
  }
15
15
  function createResponseFromTemplate(template) {
16
+ console.info("compile " + template);
16
17
  const compile = new velocity.Compile(velocity.parse(template));
17
18
  return compile.render({
19
+ method: {
20
+ request: {
21
+ multivaluequerystring: {
22
+ m1: ["multi1", "multi2"]
23
+ }
24
+ }
25
+ },
18
26
  input: {
19
- path: () => ({
20
- body: "",
21
- }),
27
+ path: () => ({}),
28
+ params: () => ({
29
+ p1: "path1",
30
+ q1: "query1",
31
+ q2: "query2",
32
+ h1: "header1",
33
+ querystring: {
34
+ qs1: "querystring1",
35
+ qs2: "querystring2"
36
+ }
37
+ })
22
38
  },
23
39
  util: {
24
40
  base64Decode: (data) => Buffer.from(data, "base64").toString(),
41
+ escapeJavaScript: (data) => encodeURIComponent(data),
42
+ parseJson: (data) => JSON.stringify(data)
25
43
  },
26
44
  context: {
27
- responseOverride: {
28
- c1: "value"
29
- }
45
+ c1: "context1"
30
46
  }
31
47
  });
32
48
  }
@@ -40,51 +56,62 @@ describe("integration tests", () => {
40
56
  });
41
57
  return new DigitrafficIntegration(f);
42
58
  }
43
- function expectAssignmentInTemplate(t, name) {
44
- expect(t).toContain(`"${name}":`);
45
- }
46
59
  test("no parameters", () => {
47
60
  const i = createIntegration();
48
61
  const t = createTemplate(i);
49
- expect(JSON.parse(t)).toEqual({});
62
+ expect(t).toEqual({});
50
63
  });
51
64
  test("query parameter", () => {
52
65
  const i = createIntegration()
53
66
  .addQueryParameter("q1");
54
67
  const t = createTemplate(i);
55
- expectAssignmentInTemplate(t, "q1");
68
+ expect(t).toEqual({
69
+ q1: "query1"
70
+ });
56
71
  });
57
72
  test("multivaluequery parameter", () => {
58
73
  const i = createIntegration()
59
74
  .addMultiValueQueryParameter("m1");
60
75
  const t = createTemplate(i);
61
- expectAssignmentInTemplate(t, "m1");
76
+ expect(t).toEqual({
77
+ m1: ["multi1", "multi2"]
78
+ });
62
79
  });
63
80
  test("all parameters", () => {
64
81
  const i = createIntegration()
65
82
  .passAllQueryParameters();
66
83
  const t = createTemplate(i);
67
- expectAssignmentInTemplate(t, "$paramName");
84
+ expect(t).toEqual({
85
+ qs1: "querystring1",
86
+ qs2: "querystring2"
87
+ });
68
88
  });
69
89
  test("path parameter", () => {
70
90
  const i = createIntegration()
71
91
  .addPathParameter("p1");
72
92
  const t = createTemplate(i);
73
- expectAssignmentInTemplate(t, "p1");
93
+ expect(t).toEqual({
94
+ p1: "path1"
95
+ });
74
96
  });
75
97
  test("context parameter", () => {
76
98
  const i = createIntegration()
77
99
  .addContextParameter("c1");
78
100
  const t = createTemplate(i);
79
- expectAssignmentInTemplate(t, "c1");
101
+ expect(t).toEqual({
102
+ c1: "context1"
103
+ });
80
104
  });
81
105
  test("all parameters and header", () => {
82
106
  const i = createIntegration()
83
107
  .passAllQueryParameters()
84
108
  .addHeaderParameter("h1");
85
109
  const t = createTemplate(i);
86
- expectAssignmentInTemplate(t, "$paramName");
87
- expectAssignmentInTemplate(t, "h1");
110
+ expect(t).toEqual({
111
+ "h1": "header1",
112
+ qs1: "querystring1",
113
+ qs2: "querystring2"
114
+ });
88
115
  });
89
116
  test("all parameters & parameter - fail", () => {
90
117
  expect(() => {
@@ -95,9 +122,14 @@ describe("integration tests", () => {
95
122
  });
96
123
  test("path parameters & pass all ", () => {
97
124
  const i = createIntegration()
98
- .addPathParameter("p")
125
+ .addPathParameter("p1")
99
126
  .passAllQueryParameters();
100
- createTemplate(i);
127
+ const t = createTemplate(i);
128
+ expect(t).toEqual({
129
+ p1: "path1",
130
+ qs1: "querystring1",
131
+ qs2: "querystring2"
132
+ });
101
133
  });
102
134
  });
103
135
  //# sourceMappingURL=integration.test.js.map
@@ -1,11 +1,9 @@
1
1
  import { LambdaIntegration, PassthroughBehavior } from "aws-cdk-lib/aws-apigateway";
2
2
  import { MediaType } from "../../types/mediatypes.js";
3
3
  import { DigitrafficIntegrationResponse } from "../../runtime/digitraffic-integration-response.js";
4
- const VELOCITY_ALL_PARAMS = `#set($params = $input.params().get("querystring"))
5
- #foreach($paramName in $params.keySet())
6
- "$paramName":"$util.escapeJavaScript($params.get($paramName))" #if($foreach.hasNext),#end
7
- #end
8
- `;
4
+ const VELOCITY_ALL_PARAMS = `#foreach($paramName in $params.keySet())
5
+ #set($tmp = $paramMap.put($paramName, $params[$paramName]))
6
+ #end`;
9
7
  export class DigitrafficIntegration {
10
8
  lambda;
11
9
  mediaType;
@@ -84,21 +82,33 @@ export class DigitrafficIntegration {
84
82
  const parameterAssignments = [];
85
83
  this.parameters.forEach((parameter) => {
86
84
  if (parameter.type === "context") {
87
- parameterAssignments.push(`"${parameter.name}":"$util.parseJson($context.${parameter.name})"`);
85
+ parameterAssignments.push(`#set($tmp = $paramMap.put('${parameter.name}', $util.escapeJavaScript($context['${parameter.name}'])))`);
88
86
  }
89
87
  else if (parameter.type === "multivaluequerystring") {
90
88
  // make multivaluequerystring values to array
91
- parameterAssignments.push(`"${parameter.name}":[#foreach($val in $method.request.multivaluequerystring.get('${parameter.name}'))"$util.escapeJavaScript($val)"#if($foreach.hasNext),#end#end]`);
89
+ parameterAssignments.push(`#set($tmp = $paramMap.put('_${parameter.name}', $util.parseJson($method.request.multivaluequerystring['${parameter.name}'])))`);
92
90
  }
93
91
  else {
94
- parameterAssignments.push(`"${parameter.name}":"$util.escapeJavaScript($input.params('${parameter.name}'))"`);
92
+ parameterAssignments.push(`#set($tmp = $paramMap.put('${parameter.name}', $util.escapeJavaScript($input.params()['${parameter.name}'])))`);
95
93
  }
96
94
  });
97
- const templateString = (this._passAllQueryParameters ? VELOCITY_ALL_PARAMS : "")
98
- + (parameterAssignments.length > 0 ? parameterAssignments.join(",\n ") + "\n" : "");
95
+ // parameters starting with _ will be handled as json, and will not be in quotes
96
+ // (for example multivalueparameters)
99
97
  return {
100
- [MediaType.APPLICATION_JSON]: `{
101
- ${templateString}}`,
98
+ [MediaType.APPLICATION_JSON]: `
99
+ #set($paramMap = {})
100
+ #set($params = $input.params().get("querystring"))
101
+ ${parameterAssignments.join("\n")}
102
+ ${this._passAllQueryParameters ? VELOCITY_ALL_PARAMS : ""}
103
+ {
104
+ #foreach($paramName in $paramMap.keySet())
105
+ #if( $paramName[0] != '_')
106
+ "$paramName":"$paramMap.get($paramName)" #if($foreach.hasNext),\n#end
107
+ #else
108
+ "$paramName.substring(1)":$paramMap.get($paramName) #if($foreach.hasNext),\n#end
109
+ #end
110
+ #end
111
+ }`,
102
112
  };
103
113
  }
104
114
  createResponses() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@digitraffic/common",
3
- "version": "2024.9.24-1",
3
+ "version": "2024.9.24-3",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "repository": {