@digitraffic/common 2024.8.30-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.
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,135 @@
1
+ import { DigitrafficIntegration } from "../../../aws/infra/api/integration.js";
2
+ import { Code, Function, Runtime } from "aws-cdk-lib/aws-lambda";
3
+ import { App, Stack } from "aws-cdk-lib";
4
+ import { MediaType } from "../../../aws/types/mediatypes.js";
5
+ import velocity from "velocityjs";
6
+ describe("integration tests", () => {
7
+ function createTemplate(i) {
8
+ const template = i.createRequestTemplates()[MediaType.APPLICATION_JSON].trim();
9
+ // assert template parses
10
+ const response = createResponseFromTemplate(template);
11
+ // assert response parses
12
+ console.info("response " + response);
13
+ return JSON.parse(response);
14
+ }
15
+ function createResponseFromTemplate(template) {
16
+ console.info("compile " + template);
17
+ const compile = new velocity.Compile(velocity.parse(template));
18
+ return compile.render({
19
+ method: {
20
+ request: {
21
+ multivaluequerystring: {
22
+ m1: ["multi1", "multi2"]
23
+ }
24
+ }
25
+ },
26
+ input: {
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
+ })
38
+ },
39
+ util: {
40
+ base64Decode: (data) => Buffer.from(data, "base64").toString(),
41
+ escapeJavaScript: (data) => encodeURIComponent(data),
42
+ parseJson: (data) => JSON.stringify(data)
43
+ },
44
+ context: {
45
+ c1: "context1"
46
+ }
47
+ });
48
+ }
49
+ function createIntegration() {
50
+ const app = new App();
51
+ const stack = new Stack(app);
52
+ const f = new Function(stack, "id", {
53
+ runtime: Runtime.NODEJS_20_X,
54
+ code: Code.fromInline("placeholder"),
55
+ handler: "handler",
56
+ });
57
+ return new DigitrafficIntegration(f);
58
+ }
59
+ test("no parameters", () => {
60
+ const i = createIntegration();
61
+ const t = createTemplate(i);
62
+ expect(t).toEqual({});
63
+ });
64
+ test("query parameter", () => {
65
+ const i = createIntegration()
66
+ .addQueryParameter("q1");
67
+ const t = createTemplate(i);
68
+ expect(t).toEqual({
69
+ q1: "query1"
70
+ });
71
+ });
72
+ test("multivaluequery parameter", () => {
73
+ const i = createIntegration()
74
+ .addMultiValueQueryParameter("m1");
75
+ const t = createTemplate(i);
76
+ expect(t).toEqual({
77
+ m1: ["multi1", "multi2"]
78
+ });
79
+ });
80
+ test("all parameters", () => {
81
+ const i = createIntegration()
82
+ .passAllQueryParameters();
83
+ const t = createTemplate(i);
84
+ expect(t).toEqual({
85
+ qs1: "querystring1",
86
+ qs2: "querystring2"
87
+ });
88
+ });
89
+ test("path parameter", () => {
90
+ const i = createIntegration()
91
+ .addPathParameter("p1");
92
+ const t = createTemplate(i);
93
+ expect(t).toEqual({
94
+ p1: "path1"
95
+ });
96
+ });
97
+ test("context parameter", () => {
98
+ const i = createIntegration()
99
+ .addContextParameter("c1");
100
+ const t = createTemplate(i);
101
+ expect(t).toEqual({
102
+ c1: "context1"
103
+ });
104
+ });
105
+ test("all parameters and header", () => {
106
+ const i = createIntegration()
107
+ .passAllQueryParameters()
108
+ .addHeaderParameter("h1");
109
+ const t = createTemplate(i);
110
+ expect(t).toEqual({
111
+ "h1": "header1",
112
+ qs1: "querystring1",
113
+ qs2: "querystring2"
114
+ });
115
+ });
116
+ test("all parameters & parameter - fail", () => {
117
+ expect(() => {
118
+ createIntegration()
119
+ .passAllQueryParameters()
120
+ .addQueryParameter("q1");
121
+ }).toThrow();
122
+ });
123
+ test("path parameters & pass all ", () => {
124
+ const i = createIntegration()
125
+ .addPathParameter("p1")
126
+ .passAllQueryParameters();
127
+ const t = createTemplate(i);
128
+ expect(t).toEqual({
129
+ p1: "path1",
130
+ qs1: "querystring1",
131
+ qs2: "querystring2"
132
+ });
133
+ });
134
+ });
135
+ //# sourceMappingURL=integration.test.js.map
@@ -1,6 +1,5 @@
1
1
  import { RESPONSE_DEFAULT_LAMBDA } from "../../../aws/infra/api/response.js";
2
2
  import etag from "etag";
3
- //const velocity = require("velocityjs");
4
3
  import velocity from "velocityjs";
5
4
  const TEST_BODY = "Hello world!";
6
5
  describe("response tests", () => {
@@ -183,7 +183,9 @@ export class AclBuilder {
183
183
  sampledRequestsEnabled: false,
184
184
  },
185
185
  rules,
186
- customResponseBodies: this._customResponseBodies,
186
+ ...(this._customResponseBodies && {
187
+ customResponseBodies: this._customResponseBodies,
188
+ }),
187
189
  });
188
190
  }
189
191
  }
@@ -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;
@@ -82,24 +80,35 @@ export class DigitrafficIntegration {
82
80
  }
83
81
  createRequestTemplates() {
84
82
  const parameterAssignments = [];
85
- if (this._passAllQueryParameters) {
86
- parameterAssignments.push(VELOCITY_ALL_PARAMS);
87
- }
88
83
  this.parameters.forEach((parameter) => {
89
84
  if (parameter.type === "context") {
90
- parameterAssignments.push(`"${parameter.name}":"$util.parseJson($context.${parameter.name}"`);
85
+ parameterAssignments.push(`#set($tmp = $paramMap.put('${parameter.name}', $util.escapeJavaScript($context['${parameter.name}'])))`);
91
86
  }
92
87
  else if (parameter.type === "multivaluequerystring") {
93
88
  // make multivaluequerystring values to array
94
- 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}'])))`);
95
90
  }
96
91
  else {
97
- 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}'])))`);
98
93
  }
99
94
  });
95
+ // parameters starting with _ will be handled as json, and will not be in quotes
96
+ // (for example multivalueparameters)
100
97
  return {
101
- [MediaType.APPLICATION_JSON]: `{
102
- ${parameterAssignments.length > 0 ? parameterAssignments.join(",\n ") + "\n" : ""}}`,
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
+ }`,
103
112
  };
104
113
  }
105
114
  createResponses() {
@@ -1,4 +1,4 @@
1
- import { type IntegrationResponse, MockIntegration, type Resource } from "aws-cdk-lib/aws-apigateway";
1
+ import { type IntegrationResponse, type MethodResponse, MockIntegration, type Resource } from "aws-cdk-lib/aws-apigateway";
2
2
  import { MediaType } from "../../types/mediatypes.js";
3
3
  /**
4
4
  * Static integration, that returns the given response with given mediaType from given resource.
@@ -11,8 +11,5 @@ export declare class DigitrafficStaticIntegration extends MockIntegration {
11
11
  constructor(resource: Resource, mediaType: MediaType, response: string, enableCors?: boolean, apiKeyRequired?: boolean, headers?: Record<string, string>);
12
12
  static json<K>(resource: Resource, response: K, enableCors?: boolean, apiKeyRequired?: boolean, headers?: Record<string, string>): DigitrafficStaticIntegration;
13
13
  static createIntegrationResponse(response: string, mediaType: MediaType, headers?: Record<string, string>): IntegrationResponse;
14
- static createMethodResponse(headers: Record<string, string>): {
15
- statusCode: string;
16
- responseParameters: Record<string, boolean>;
17
- };
14
+ static createMethodResponse(headers: Record<string, string>): MethodResponse;
18
15
  }
@@ -1,4 +1,4 @@
1
- import { type JsonSchema, JsonSchemaType, JsonSchemaVersion, type Model, type RequestValidator, type RestApi } from "aws-cdk-lib/aws-apigateway";
1
+ import { type JsonSchema, type Model, type RequestValidator, type RestApi } from "aws-cdk-lib/aws-apigateway";
2
2
  import type { ModelWithReference } from "../aws/types/model-with-reference.js";
3
3
  type ModelReferenceUrl = `https://apigateway.amazonaws.com/restapis/${string}/models/${string}`;
4
4
  /**
@@ -42,47 +42,10 @@ export declare function createArraySchema(model: Model, api: RestApi): JsonSchem
42
42
  * Creates a JSON Schema for a GeoJSON Feature. Can be used to generate OpenAPI descriptions.
43
43
  * @param modelReference Reference to a model object
44
44
  */
45
- export declare function featureSchema(modelReference: string): {
46
- schema: JsonSchemaVersion;
47
- type: JsonSchemaType;
48
- description: string;
49
- required: string[];
50
- properties: {
51
- type: {
52
- type: JsonSchemaType;
53
- description: string;
54
- enum: string[];
55
- };
56
- properties: {
57
- ref: string;
58
- };
59
- geometry: {
60
- type: JsonSchemaType;
61
- description: string;
62
- };
63
- };
64
- };
45
+ export declare function featureSchema(modelReference: string): JsonSchema;
65
46
  /**
66
47
  * Creates a JSON Schema for a GeoJSON Feature Collection. Can be used to generate OpenAPI descriptions.
67
48
  * @param modelReference Reference to a model object, in this case Features.
68
49
  */
69
- export declare function geojsonSchema(modelReference: string): {
70
- schema: JsonSchemaVersion;
71
- type: JsonSchemaType;
72
- description: string;
73
- required: string[];
74
- properties: {
75
- type: {
76
- type: JsonSchemaType;
77
- description: string;
78
- enum: string[];
79
- };
80
- features: {
81
- type: JsonSchemaType;
82
- items: {
83
- ref: string;
84
- };
85
- };
86
- };
87
- };
50
+ export declare function geojsonSchema(modelReference: string): JsonSchema;
88
51
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@digitraffic/common",
3
- "version": "2024.8.30-1",
3
+ "version": "2024.9.24-3",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "repository": {