@digitraffic/common 2024.9.24-1 → 2024.9.25-1

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,41 @@ 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
+ params: () => ({
28
+ header: {
29
+ h1: "header1",
30
+ },
31
+ querystring: {
32
+ q1: "querystring1",
33
+ q2: "querystring2"
34
+ },
35
+ path: {
36
+ p1: "path1"
37
+ },
38
+ })
22
39
  },
23
40
  util: {
24
41
  base64Decode: (data) => Buffer.from(data, "base64").toString(),
42
+ escapeJavaScript: (data) => encodeURIComponent(data),
43
+ parseJson: (data) => JSON.stringify(data)
25
44
  },
26
45
  context: {
27
- responseOverride: {
28
- c1: "value"
29
- }
46
+ c1: "context1"
30
47
  }
31
48
  });
32
49
  }
@@ -40,51 +57,72 @@ describe("integration tests", () => {
40
57
  });
41
58
  return new DigitrafficIntegration(f);
42
59
  }
43
- function expectAssignmentInTemplate(t, name) {
44
- expect(t).toContain(`"${name}":`);
45
- }
46
60
  test("no parameters", () => {
47
61
  const i = createIntegration();
48
62
  const t = createTemplate(i);
49
- expect(JSON.parse(t)).toEqual({});
63
+ expect(t).toEqual({});
50
64
  });
51
65
  test("query parameter", () => {
52
66
  const i = createIntegration()
53
67
  .addQueryParameter("q1");
54
68
  const t = createTemplate(i);
55
- expectAssignmentInTemplate(t, "q1");
69
+ expect(t).toEqual({
70
+ q1: "querystring1"
71
+ });
72
+ });
73
+ test("two query parameters", () => {
74
+ const i = createIntegration()
75
+ .addQueryParameter("q1")
76
+ .addQueryParameter("q2");
77
+ const t = createTemplate(i);
78
+ expect(t).toEqual({
79
+ q1: "querystring1",
80
+ q2: "querystring2"
81
+ });
56
82
  });
57
83
  test("multivaluequery parameter", () => {
58
84
  const i = createIntegration()
59
85
  .addMultiValueQueryParameter("m1");
60
86
  const t = createTemplate(i);
61
- expectAssignmentInTemplate(t, "m1");
87
+ expect(t).toEqual({
88
+ m1: ["multi1", "multi2"]
89
+ });
62
90
  });
63
91
  test("all parameters", () => {
64
92
  const i = createIntegration()
65
93
  .passAllQueryParameters();
66
94
  const t = createTemplate(i);
67
- expectAssignmentInTemplate(t, "$paramName");
95
+ expect(t).toEqual({
96
+ q1: "querystring1",
97
+ q2: "querystring2"
98
+ });
68
99
  });
69
100
  test("path parameter", () => {
70
101
  const i = createIntegration()
71
102
  .addPathParameter("p1");
72
103
  const t = createTemplate(i);
73
- expectAssignmentInTemplate(t, "p1");
104
+ expect(t).toEqual({
105
+ p1: "path1"
106
+ });
74
107
  });
75
108
  test("context parameter", () => {
76
109
  const i = createIntegration()
77
110
  .addContextParameter("c1");
78
111
  const t = createTemplate(i);
79
- expectAssignmentInTemplate(t, "c1");
112
+ expect(t).toEqual({
113
+ c1: "context1"
114
+ });
80
115
  });
81
116
  test("all parameters and header", () => {
82
117
  const i = createIntegration()
83
118
  .passAllQueryParameters()
84
119
  .addHeaderParameter("h1");
85
120
  const t = createTemplate(i);
86
- expectAssignmentInTemplate(t, "$paramName");
87
- expectAssignmentInTemplate(t, "h1");
121
+ expect(t).toEqual({
122
+ h1: "header1",
123
+ q1: "querystring1",
124
+ q2: "querystring2"
125
+ });
88
126
  });
89
127
  test("all parameters & parameter - fail", () => {
90
128
  expect(() => {
@@ -95,9 +133,14 @@ describe("integration tests", () => {
95
133
  });
96
134
  test("path parameters & pass all ", () => {
97
135
  const i = createIntegration()
98
- .addPathParameter("p")
136
+ .addPathParameter("p1")
99
137
  .passAllQueryParameters();
100
- createTemplate(i);
138
+ const t = createTemplate(i);
139
+ expect(t).toEqual({
140
+ p1: "path1",
141
+ q1: "querystring1",
142
+ q2: "querystring2"
143
+ });
101
144
  });
102
145
  });
103
146
  //# 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,39 @@ 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}'])))`);
90
+ }
91
+ else if (parameter.type === "path") {
92
+ parameterAssignments.push(`#set($tmp = $paramMap.put('${parameter.name}', $util.escapeJavaScript($input.params().path['${parameter.name}'])))`);
93
+ }
94
+ else if (parameter.type === "header") {
95
+ parameterAssignments.push(`#set($tmp = $paramMap.put('${parameter.name}', $util.escapeJavaScript($input.params().header['${parameter.name}'])))`);
92
96
  }
93
97
  else {
94
- parameterAssignments.push(`"${parameter.name}":"$util.escapeJavaScript($input.params('${parameter.name}'))"`);
98
+ parameterAssignments.push(`#set($tmp = $paramMap.put('${parameter.name}', $util.escapeJavaScript($params['${parameter.name}'])))`);
95
99
  }
96
100
  });
97
- const templateString = (this._passAllQueryParameters ? VELOCITY_ALL_PARAMS : "")
98
- + (parameterAssignments.length > 0 ? parameterAssignments.join(",\n ") + "\n" : "");
101
+ // parameters starting with _ will be handled as json, and will not be in quotes
102
+ // (for example multivalueparameters)
99
103
  return {
100
- [MediaType.APPLICATION_JSON]: `{
101
- ${templateString}}`,
104
+ [MediaType.APPLICATION_JSON]: `
105
+ #set($paramMap = {})
106
+ #set($params = $input.params().get("querystring"))
107
+ ${parameterAssignments.join("\n")}
108
+ ${this._passAllQueryParameters ? VELOCITY_ALL_PARAMS : ""}
109
+ {
110
+ #foreach($paramName in $paramMap.keySet())
111
+ #if( $paramName[0] != '_')
112
+ "$paramName":"$paramMap.get($paramName)" #if($foreach.hasNext),\n#end
113
+ #else
114
+ "$paramName.substring(1)":$paramMap.get($paramName) #if($foreach.hasNext),\n#end
115
+ #end
116
+ #end
117
+ }`,
102
118
  };
103
119
  }
104
120
  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.25-1",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "repository": {