@bifravst/http-api-mock 1.3.1 → 1.3.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.
|
@@ -69,11 +69,13 @@ export const handler = async (
|
|
|
69
69
|
}),
|
|
70
70
|
)
|
|
71
71
|
console.debug(`Found response items: ${Items?.length}`)
|
|
72
|
+
// use newest response first
|
|
73
|
+
const itemsByTimestampDesc = (Items ?? [])
|
|
74
|
+
.map((Item) => unmarshall(Item))
|
|
75
|
+
.sort((a, b) => b.timestamp.localeCompare(a.timestamp))
|
|
72
76
|
|
|
73
77
|
let res: APIGatewayProxyResult | undefined
|
|
74
|
-
for (const
|
|
75
|
-
// use newest response first
|
|
76
|
-
const objItem = unmarshall(Item)
|
|
78
|
+
for (const objItem of itemsByTimestampDesc) {
|
|
77
79
|
const hasExpectedQueryParams = 'queryParams' in objItem
|
|
78
80
|
const matchedQueryParams = hasExpectedQueryParams
|
|
79
81
|
? checkMatchingQueryParams(
|
|
@@ -84,27 +86,27 @@ export const handler = async (
|
|
|
84
86
|
if (matchedQueryParams === false) continue
|
|
85
87
|
|
|
86
88
|
if (
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
89
|
+
objItem?.methodPathQuery !== undefined &&
|
|
90
|
+
objItem?.timestamp !== undefined &&
|
|
91
|
+
objItem?.keep !== true
|
|
90
92
|
) {
|
|
91
93
|
await db.send(
|
|
92
94
|
new DeleteItemCommand({
|
|
93
95
|
TableName: process.env.RESPONSES_TABLE_NAME,
|
|
94
|
-
Key: {
|
|
95
|
-
methodPathQuery:
|
|
96
|
-
timestamp:
|
|
97
|
-
},
|
|
96
|
+
Key: marshall({
|
|
97
|
+
methodPathQuery: objItem.methodPathQuery,
|
|
98
|
+
timestamp: objItem.timestamp,
|
|
99
|
+
}),
|
|
98
100
|
}),
|
|
99
101
|
)
|
|
100
102
|
}
|
|
101
103
|
|
|
102
|
-
const { body, headers } = splitMockResponse(
|
|
104
|
+
const { body, headers } = splitMockResponse(objItem.body ?? '')
|
|
103
105
|
|
|
104
106
|
// Send as binary, if mock response is HEX encoded. See https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-payload-encodings.html
|
|
105
107
|
const isBinary = /^[0-9a-f]+$/.test(body)
|
|
106
108
|
res = {
|
|
107
|
-
statusCode:
|
|
109
|
+
statusCode: objItem.statusCode ?? 200,
|
|
108
110
|
headers: isBinary
|
|
109
111
|
? {
|
|
110
112
|
...headers,
|
|
@@ -41,32 +41,34 @@ export const handler = async (event, context) => {
|
|
|
41
41
|
},
|
|
42
42
|
}));
|
|
43
43
|
console.debug(`Found response items: ${Items?.length}`);
|
|
44
|
+
// use newest response first
|
|
45
|
+
const itemsByTimestampDesc = (Items ?? [])
|
|
46
|
+
.map((Item) => unmarshall(Item))
|
|
47
|
+
.sort((a, b) => b.timestamp.localeCompare(a.timestamp));
|
|
44
48
|
let res;
|
|
45
|
-
for (const
|
|
46
|
-
// use newest response first
|
|
47
|
-
const objItem = unmarshall(Item);
|
|
49
|
+
for (const objItem of itemsByTimestampDesc) {
|
|
48
50
|
const hasExpectedQueryParams = 'queryParams' in objItem;
|
|
49
51
|
const matchedQueryParams = hasExpectedQueryParams
|
|
50
52
|
? checkMatchingQueryParams(event.queryStringParameters, objItem.queryParams)
|
|
51
53
|
: true;
|
|
52
54
|
if (matchedQueryParams === false)
|
|
53
55
|
continue;
|
|
54
|
-
if (
|
|
55
|
-
|
|
56
|
-
|
|
56
|
+
if (objItem?.methodPathQuery !== undefined &&
|
|
57
|
+
objItem?.timestamp !== undefined &&
|
|
58
|
+
objItem?.keep !== true) {
|
|
57
59
|
await db.send(new DeleteItemCommand({
|
|
58
60
|
TableName: process.env.RESPONSES_TABLE_NAME,
|
|
59
|
-
Key: {
|
|
60
|
-
methodPathQuery:
|
|
61
|
-
timestamp:
|
|
62
|
-
},
|
|
61
|
+
Key: marshall({
|
|
62
|
+
methodPathQuery: objItem.methodPathQuery,
|
|
63
|
+
timestamp: objItem.timestamp,
|
|
64
|
+
}),
|
|
63
65
|
}));
|
|
64
66
|
}
|
|
65
|
-
const { body, headers } = splitMockResponse(
|
|
67
|
+
const { body, headers } = splitMockResponse(objItem.body ?? '');
|
|
66
68
|
// Send as binary, if mock response is HEX encoded. See https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-payload-encodings.html
|
|
67
69
|
const isBinary = /^[0-9a-f]+$/.test(body);
|
|
68
70
|
res = {
|
|
69
|
-
statusCode:
|
|
71
|
+
statusCode: objItem.statusCode ?? 200,
|
|
70
72
|
headers: isBinary
|
|
71
73
|
? {
|
|
72
74
|
...headers,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bifravst/http-api-mock",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.3",
|
|
4
4
|
"description": "Helper functions for AWS lambdas written in TypeScript.",
|
|
5
5
|
"exports": {
|
|
6
6
|
"./*": {
|
|
@@ -87,7 +87,7 @@
|
|
|
87
87
|
"@aws-sdk/client-dynamodb": "^3.556.0",
|
|
88
88
|
"@aws-sdk/client-sts": "^3.556.0",
|
|
89
89
|
"@aws-sdk/util-dynamodb": "^3.556.0",
|
|
90
|
-
"@bifravst/aws-cdk-lambda-helpers": "^1.4.
|
|
90
|
+
"@bifravst/aws-cdk-lambda-helpers": "^1.4.2",
|
|
91
91
|
"@bifravst/run": "^1.2.0",
|
|
92
92
|
"@nordicsemiconductor/cloudformation-helpers": "^9.0.3",
|
|
93
93
|
"@nordicsemiconductor/from-env": "^3.0.1",
|