@bifravst/http-api-mock 2.0.130 → 2.1.0

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.
@@ -34,21 +34,16 @@ export const handler = async (
34
34
  `${path}${query !== undefined ? `?${query.toString()}` : ''}`,
35
35
  )
36
36
 
37
- await db.send(
38
- new PutItemCommand({
39
- TableName: process.env.REQUESTS_TABLE_NAME,
40
- Item: marshall({
41
- methodPathQuery: `${event.httpMethod} ${pathWithQuery}`,
42
- timestamp: new Date().toISOString(),
43
- requestId: context.awsRequestId,
44
- method: event.httpMethod,
45
- path,
46
- query: query === undefined ? null : Object.fromEntries(query),
47
- body: event.body ?? '{}',
48
- headers: JSON.stringify(event.headers),
49
- }),
50
- }),
51
- )
37
+ const request = {
38
+ methodPathQuery: `${event.httpMethod} ${pathWithQuery}`,
39
+ timestamp: new Date().toISOString(),
40
+ requestId: context.awsRequestId,
41
+ method: event.httpMethod,
42
+ path,
43
+ query: query === undefined ? null : Object.fromEntries(query),
44
+ body: event.body ?? '{}',
45
+ headers: JSON.stringify(event.headers),
46
+ }
52
47
 
53
48
  // Check if response exists
54
49
  console.debug(
@@ -74,7 +69,10 @@ export const handler = async (
74
69
  .map((Item) => unmarshall(Item))
75
70
  .sort((a, b) => b.timestamp.localeCompare(a.timestamp))
76
71
 
77
- let res: APIGatewayProxyResult | undefined
72
+ let res: APIGatewayProxyResult = {
73
+ statusCode: 404,
74
+ body: 'No responses found',
75
+ }
78
76
  for (const objItem of itemsByTimestampDesc) {
79
77
  const hasExpectedQueryParams =
80
78
  'queryParams' in objItem || query !== undefined
@@ -121,15 +119,29 @@ export const handler = async (
121
119
  : body,
122
120
  isBase64Encoded: isBinary,
123
121
  }
124
- console.debug(`Return response`, JSON.stringify({ response: res }))
125
122
 
126
123
  break
127
124
  }
128
125
 
129
- if (res !== undefined) {
130
- return res
131
- }
126
+ console.debug(`Return response`, JSON.stringify({ response: res }))
127
+
128
+ await db.send(
129
+ new PutItemCommand({
130
+ TableName: process.env.REQUESTS_TABLE_NAME,
131
+ Item: marshall(
132
+ {
133
+ ...request,
134
+ responseStatusCode: res.statusCode,
135
+ responseHeaders: res.headers,
136
+ responseBody: res.body,
137
+ responseIsBase64Encoded: res.isBase64Encoded,
138
+ },
139
+ {
140
+ removeUndefinedValues: true,
141
+ },
142
+ ),
143
+ }),
144
+ )
132
145
 
133
- console.debug('No responses found')
134
- return { statusCode: 404, body: '' }
146
+ return res
135
147
  }
@@ -13,19 +13,16 @@ export const handler = async (event, context) => {
13
13
  : undefined;
14
14
  const path = event.path.replace(/^\//, '');
15
15
  const pathWithQuery = sortQueryString(`${path}${query !== undefined ? `?${query.toString()}` : ''}`);
16
- await db.send(new PutItemCommand({
17
- TableName: process.env.REQUESTS_TABLE_NAME,
18
- Item: marshall({
19
- methodPathQuery: `${event.httpMethod} ${pathWithQuery}`,
20
- timestamp: new Date().toISOString(),
21
- requestId: context.awsRequestId,
22
- method: event.httpMethod,
23
- path,
24
- query: query === undefined ? null : Object.fromEntries(query),
25
- body: event.body ?? '{}',
26
- headers: JSON.stringify(event.headers),
27
- }),
28
- }));
16
+ const request = {
17
+ methodPathQuery: `${event.httpMethod} ${pathWithQuery}`,
18
+ timestamp: new Date().toISOString(),
19
+ requestId: context.awsRequestId,
20
+ method: event.httpMethod,
21
+ path,
22
+ query: query === undefined ? null : Object.fromEntries(query),
23
+ body: event.body ?? '{}',
24
+ headers: JSON.stringify(event.headers),
25
+ };
29
26
  // Check if response exists
30
27
  console.debug(`Checking if response exists for ${event.httpMethod} ${pathWithQuery}...`);
31
28
  // Scan using httpMethod and path only so query strings can be partially matched
@@ -43,7 +40,10 @@ export const handler = async (event, context) => {
43
40
  const itemsByTimestampDesc = (Items ?? [])
44
41
  .map((Item) => unmarshall(Item))
45
42
  .sort((a, b) => b.timestamp.localeCompare(a.timestamp));
46
- let res;
43
+ let res = {
44
+ statusCode: 404,
45
+ body: 'No responses found',
46
+ };
47
47
  for (const objItem of itemsByTimestampDesc) {
48
48
  const hasExpectedQueryParams = 'queryParams' in objItem || query !== undefined;
49
49
  const matchedQueryParams = hasExpectedQueryParams
@@ -79,12 +79,20 @@ export const handler = async (event, context) => {
79
79
  : body,
80
80
  isBase64Encoded: isBinary,
81
81
  };
82
- console.debug(`Return response`, JSON.stringify({ response: res }));
83
82
  break;
84
83
  }
85
- if (res !== undefined) {
86
- return res;
87
- }
88
- console.debug('No responses found');
89
- return { statusCode: 404, body: '' };
84
+ console.debug(`Return response`, JSON.stringify({ response: res }));
85
+ await db.send(new PutItemCommand({
86
+ TableName: process.env.REQUESTS_TABLE_NAME,
87
+ Item: marshall({
88
+ ...request,
89
+ responseStatusCode: res.statusCode,
90
+ responseHeaders: res.headers,
91
+ responseBody: res.body,
92
+ responseIsBase64Encoded: res.isBase64Encoded,
93
+ }, {
94
+ removeUndefinedValues: true,
95
+ }),
96
+ }));
97
+ return res;
90
98
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bifravst/http-api-mock",
3
- "version": "2.0.130",
3
+ "version": "2.1.0",
4
4
  "description": "Helper functions for AWS lambdas written in TypeScript.",
5
5
  "exports": {
6
6
  "./*": {