@digitraffic/common 2024.3.22-1 → 2024.3.22-2
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,26 @@
|
|
1
|
+
import { DigitrafficStaticIntegration } from "../../aws/infra/api/static-integration.mjs";
|
2
|
+
import { MediaType } from "../../aws/types/mediatypes.mjs";
|
3
|
+
describe("response tests", () => {
|
4
|
+
it("createIntegrationResponse works", () => {
|
5
|
+
const integrationResponse = DigitrafficStaticIntegration.createIntegrationResponse("FakeResource", MediaType.APPLICATION_JSON, { "test-header": "test-value" });
|
6
|
+
expect(integrationResponse).toEqual({
|
7
|
+
responseParameters: {
|
8
|
+
"method.response.header.test-header": "'test-value'"
|
9
|
+
},
|
10
|
+
responseTemplates: {
|
11
|
+
"application/json": "FakeResource"
|
12
|
+
},
|
13
|
+
statusCode: "200"
|
14
|
+
});
|
15
|
+
});
|
16
|
+
it("createMethodResponse works", () => {
|
17
|
+
const methodResponse = DigitrafficStaticIntegration.createMethodResponse({ "test-header": "test-value" });
|
18
|
+
expect(methodResponse).toEqual({
|
19
|
+
responseParameters: {
|
20
|
+
"method.response.header.test-header": true
|
21
|
+
},
|
22
|
+
statusCode: "200"
|
23
|
+
});
|
24
|
+
});
|
25
|
+
});
|
26
|
+
//# sourceMappingURL=static-integration.test.mjs.map
|
@@ -8,8 +8,17 @@ import { MediaType } from "../../types/mediatypes.mjs";
|
|
8
8
|
* @param response
|
9
9
|
*/
|
10
10
|
export declare class DigitrafficStaticIntegration extends MockIntegration {
|
11
|
-
constructor(resource: Resource, mediaType: MediaType, response: string, enableCors?: boolean, apiKeyRequired?: boolean);
|
12
|
-
static json<K>(resource: Resource, response: K, enableCors?: boolean, apiKeyRequired?: boolean): DigitrafficStaticIntegration;
|
13
|
-
|
14
|
-
|
11
|
+
constructor(resource: Resource, mediaType: MediaType, response: string, enableCors?: boolean, apiKeyRequired?: boolean, headers?: Record<string, string>);
|
12
|
+
static json<K>(resource: Resource, response: K, enableCors?: boolean, apiKeyRequired?: boolean, headers?: Record<string, string>): DigitrafficStaticIntegration;
|
13
|
+
static createIntegrationResponse(response: string, mediaType: MediaType, headers?: Record<string, string>): {
|
14
|
+
statusCode: string;
|
15
|
+
responseTemplates: {
|
16
|
+
[x: string]: string;
|
17
|
+
};
|
18
|
+
responseParameters: Record<string, string>;
|
19
|
+
};
|
20
|
+
static createMethodResponse(headers: Record<string, string>): {
|
21
|
+
statusCode: string;
|
22
|
+
responseParameters: Record<string, boolean>;
|
23
|
+
};
|
15
24
|
}
|
@@ -1,12 +1,8 @@
|
|
1
|
-
import { MockIntegration, PassthroughBehavior, Resource
|
1
|
+
import { MockIntegration, PassthroughBehavior, Resource } from "aws-cdk-lib/aws-apigateway";
|
2
2
|
import { MediaType } from "../../types/mediatypes.mjs";
|
3
|
-
import { RESPONSE_CORS_INTEGRATION } from "./responses.mjs";
|
4
3
|
const INTEGRATION_RESPONSE_200 = `{
|
5
4
|
"statusCode": 200
|
6
5
|
}`;
|
7
|
-
const METHOD_RESPONSE_200 = {
|
8
|
-
statusCode: "200",
|
9
|
-
};
|
10
6
|
/**
|
11
7
|
* Static integration, that returns the given response with given mediaType from given resource.
|
12
8
|
*
|
@@ -15,8 +11,11 @@ const METHOD_RESPONSE_200 = {
|
|
15
11
|
* @param response
|
16
12
|
*/
|
17
13
|
export class DigitrafficStaticIntegration extends MockIntegration {
|
18
|
-
constructor(resource, mediaType, response, enableCors = true, apiKeyRequired = true) {
|
19
|
-
|
14
|
+
constructor(resource, mediaType, response, enableCors = true, apiKeyRequired = true, headers = {}) {
|
15
|
+
if (enableCors) {
|
16
|
+
headers = { ...headers, "Access-Control-Allow-Origin": "*" };
|
17
|
+
}
|
18
|
+
const integrationResponse = DigitrafficStaticIntegration.createIntegrationResponse(response, mediaType, headers);
|
20
19
|
super({
|
21
20
|
passthroughBehavior: PassthroughBehavior.WHEN_NO_TEMPLATES,
|
22
21
|
requestTemplates: {
|
@@ -28,39 +27,44 @@ export class DigitrafficStaticIntegration extends MockIntegration {
|
|
28
27
|
resource.addMethod(httpMethod, this, {
|
29
28
|
apiKeyRequired,
|
30
29
|
methodResponses: [
|
31
|
-
DigitrafficStaticIntegration.createMethodResponse(
|
30
|
+
DigitrafficStaticIntegration.createMethodResponse(headers),
|
32
31
|
],
|
33
32
|
});
|
34
33
|
});
|
35
34
|
}
|
36
|
-
static json(resource, response, enableCors = true, apiKeyRequired = true) {
|
37
|
-
return new DigitrafficStaticIntegration(resource, MediaType.APPLICATION_JSON, JSON.stringify(response), enableCors, apiKeyRequired);
|
35
|
+
static json(resource, response, enableCors = true, apiKeyRequired = true, headers = {}) {
|
36
|
+
return new DigitrafficStaticIntegration(resource, MediaType.APPLICATION_JSON, JSON.stringify(response), enableCors, apiKeyRequired, headers);
|
38
37
|
}
|
39
|
-
static createIntegrationResponse(response, mediaType,
|
40
|
-
const
|
38
|
+
static createIntegrationResponse(response, mediaType, headers = {}) {
|
39
|
+
const params = mapRecord(headers, (entry) => ["method.response.header." + entry[0], `'${entry[1]}'`]);
|
40
|
+
return {
|
41
41
|
statusCode: "200",
|
42
42
|
responseTemplates: {
|
43
43
|
[mediaType]: response,
|
44
44
|
},
|
45
|
+
responseParameters: params
|
45
46
|
};
|
46
|
-
return enableCors
|
47
|
-
? { ...integrationResponse, ...RESPONSE_CORS_INTEGRATION }
|
48
|
-
: integrationResponse;
|
49
47
|
}
|
50
|
-
static createMethodResponse(
|
51
|
-
|
52
|
-
|
53
|
-
|
48
|
+
static createMethodResponse(headers) {
|
49
|
+
const allowedHeaders = Object.keys(headers);
|
50
|
+
const entries = Object.fromEntries(allowedHeaders.map((key) => [key, true]));
|
51
|
+
return {
|
52
|
+
statusCode: "200",
|
53
|
+
responseParameters: prefixKeys("method.response.header.", entries)
|
54
|
+
};
|
54
55
|
}
|
55
56
|
}
|
56
|
-
function
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
57
|
+
function mapRecord(obj, func) {
|
58
|
+
const mappedEntries = Object.entries(obj).map((entry) => func(entry));
|
59
|
+
return Object.fromEntries(mappedEntries);
|
60
|
+
}
|
61
|
+
/**
|
62
|
+
* Create a new Record with prefix added to each of the keys.
|
63
|
+
*
|
64
|
+
* @param prefix
|
65
|
+
* @param obj
|
66
|
+
*/
|
67
|
+
function prefixKeys(prefix, obj) {
|
68
|
+
return mapRecord(obj, (entry) => [prefix + entry[0], entry[1]]);
|
65
69
|
}
|
66
70
|
//# sourceMappingURL=static-integration.mjs.map
|