@digitraffic/common 2024.8.30-1 → 2024.9.24-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.
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,103 @@
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
+ JSON.parse(response);
13
+ return template;
14
+ }
15
+ function createResponseFromTemplate(template) {
16
+ const compile = new velocity.Compile(velocity.parse(template));
17
+ return compile.render({
18
+ input: {
19
+ path: () => ({
20
+ body: "",
21
+ }),
22
+ },
23
+ util: {
24
+ base64Decode: (data) => Buffer.from(data, "base64").toString(),
25
+ },
26
+ context: {
27
+ responseOverride: {
28
+ c1: "value"
29
+ }
30
+ }
31
+ });
32
+ }
33
+ function createIntegration() {
34
+ const app = new App();
35
+ const stack = new Stack(app);
36
+ const f = new Function(stack, "id", {
37
+ runtime: Runtime.NODEJS_20_X,
38
+ code: Code.fromInline("placeholder"),
39
+ handler: "handler",
40
+ });
41
+ return new DigitrafficIntegration(f);
42
+ }
43
+ function expectAssignmentInTemplate(t, name) {
44
+ expect(t).toContain(`"${name}":`);
45
+ }
46
+ test("no parameters", () => {
47
+ const i = createIntegration();
48
+ const t = createTemplate(i);
49
+ expect(JSON.parse(t)).toEqual({});
50
+ });
51
+ test("query parameter", () => {
52
+ const i = createIntegration()
53
+ .addQueryParameter("q1");
54
+ const t = createTemplate(i);
55
+ expectAssignmentInTemplate(t, "q1");
56
+ });
57
+ test("multivaluequery parameter", () => {
58
+ const i = createIntegration()
59
+ .addMultiValueQueryParameter("m1");
60
+ const t = createTemplate(i);
61
+ expectAssignmentInTemplate(t, "m1");
62
+ });
63
+ test("all parameters", () => {
64
+ const i = createIntegration()
65
+ .passAllQueryParameters();
66
+ const t = createTemplate(i);
67
+ expectAssignmentInTemplate(t, "$paramName");
68
+ });
69
+ test("path parameter", () => {
70
+ const i = createIntegration()
71
+ .addPathParameter("p1");
72
+ const t = createTemplate(i);
73
+ expectAssignmentInTemplate(t, "p1");
74
+ });
75
+ test("context parameter", () => {
76
+ const i = createIntegration()
77
+ .addContextParameter("c1");
78
+ const t = createTemplate(i);
79
+ expectAssignmentInTemplate(t, "c1");
80
+ });
81
+ test("all parameters and header", () => {
82
+ const i = createIntegration()
83
+ .passAllQueryParameters()
84
+ .addHeaderParameter("h1");
85
+ const t = createTemplate(i);
86
+ expectAssignmentInTemplate(t, "$paramName");
87
+ expectAssignmentInTemplate(t, "h1");
88
+ });
89
+ test("all parameters & parameter - fail", () => {
90
+ expect(() => {
91
+ createIntegration()
92
+ .passAllQueryParameters()
93
+ .addQueryParameter("q1");
94
+ }).toThrow();
95
+ });
96
+ test("path parameters & pass all ", () => {
97
+ const i = createIntegration()
98
+ .addPathParameter("p")
99
+ .passAllQueryParameters();
100
+ createTemplate(i);
101
+ });
102
+ });
103
+ //# 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
  }
@@ -2,9 +2,9 @@ import { LambdaIntegration, PassthroughBehavior } from "aws-cdk-lib/aws-apigatew
2
2
  import { MediaType } from "../../types/mediatypes.js";
3
3
  import { DigitrafficIntegrationResponse } from "../../runtime/digitraffic-integration-response.js";
4
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
5
+ #foreach($paramName in $params.keySet())
6
+ "$paramName":"$util.escapeJavaScript($params.get($paramName))" #if($foreach.hasNext),#end
7
+ #end
8
8
  `;
9
9
  export class DigitrafficIntegration {
10
10
  lambda;
@@ -82,12 +82,9 @@ export class DigitrafficIntegration {
82
82
  }
83
83
  createRequestTemplates() {
84
84
  const parameterAssignments = [];
85
- if (this._passAllQueryParameters) {
86
- parameterAssignments.push(VELOCITY_ALL_PARAMS);
87
- }
88
85
  this.parameters.forEach((parameter) => {
89
86
  if (parameter.type === "context") {
90
- parameterAssignments.push(`"${parameter.name}":"$util.parseJson($context.${parameter.name}"`);
87
+ parameterAssignments.push(`"${parameter.name}":"$util.parseJson($context.${parameter.name})"`);
91
88
  }
92
89
  else if (parameter.type === "multivaluequerystring") {
93
90
  // make multivaluequerystring values to array
@@ -97,9 +94,11 @@ export class DigitrafficIntegration {
97
94
  parameterAssignments.push(`"${parameter.name}":"$util.escapeJavaScript($input.params('${parameter.name}'))"`);
98
95
  }
99
96
  });
97
+ const templateString = (this._passAllQueryParameters ? VELOCITY_ALL_PARAMS : "")
98
+ + (parameterAssignments.length > 0 ? parameterAssignments.join(",\n ") + "\n" : "");
100
99
  return {
101
100
  [MediaType.APPLICATION_JSON]: `{
102
- ${parameterAssignments.length > 0 ? parameterAssignments.join(",\n ") + "\n" : ""}}`,
101
+ ${templateString}}`,
103
102
  };
104
103
  }
105
104
  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-1",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "repository": {