@bifravst/http-api-mock 2.0.123 → 2.0.125
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.
- package/cdk/resources/HttpApiMock.ts +20 -4
- package/cdk/resources/http-api-mock-lambda.ts +2 -2
- package/dist/cdk/resources/HttpApiMock.d.ts +2 -2
- package/dist/cdk/resources/HttpApiMock.js +18 -2
- package/dist/cdk/resources/http-api-mock-lambda.js +2 -2
- package/dist/src/responses.js +2 -0
- package/package.json +3 -2
- package/src/responses.ts +2 -0
|
@@ -17,8 +17,8 @@ import type { Construct } from 'constructs'
|
|
|
17
17
|
|
|
18
18
|
export class HttpApiMock extends Resource {
|
|
19
19
|
public readonly api: ApiGateway.RestApi
|
|
20
|
-
public readonly requestsTable: DynamoDB.
|
|
21
|
-
public readonly responsesTable: DynamoDB.
|
|
20
|
+
public readonly requestsTable: DynamoDB.Table
|
|
21
|
+
public readonly responsesTable: DynamoDB.Table
|
|
22
22
|
|
|
23
23
|
public constructor(
|
|
24
24
|
parent: Construct,
|
|
@@ -38,7 +38,7 @@ export class HttpApiMock extends Resource {
|
|
|
38
38
|
this.requestsTable = new DynamoDB.Table(this, 'requests', {
|
|
39
39
|
billingMode: DynamoDB.BillingMode.PAY_PER_REQUEST,
|
|
40
40
|
partitionKey: {
|
|
41
|
-
name: '
|
|
41
|
+
name: 'requestId',
|
|
42
42
|
type: DynamoDB.AttributeType.STRING,
|
|
43
43
|
},
|
|
44
44
|
sortKey: {
|
|
@@ -49,12 +49,20 @@ export class HttpApiMock extends Resource {
|
|
|
49
49
|
removalPolicy: RemovalPolicy.DESTROY,
|
|
50
50
|
timeToLiveAttribute: 'ttl',
|
|
51
51
|
})
|
|
52
|
+
this.requestsTable.addGlobalSecondaryIndex({
|
|
53
|
+
indexName: 'methodPathQuery',
|
|
54
|
+
partitionKey: {
|
|
55
|
+
name: 'methodPathQuery',
|
|
56
|
+
type: DynamoDB.AttributeType.STRING,
|
|
57
|
+
},
|
|
58
|
+
projectionType: DynamoDB.ProjectionType.ALL,
|
|
59
|
+
})
|
|
52
60
|
|
|
53
61
|
// This table will store optional responses to be sent
|
|
54
62
|
this.responsesTable = new DynamoDB.Table(this, 'responses', {
|
|
55
63
|
billingMode: DynamoDB.BillingMode.PAY_PER_REQUEST,
|
|
56
64
|
partitionKey: {
|
|
57
|
-
name: '
|
|
65
|
+
name: 'responseId',
|
|
58
66
|
type: DynamoDB.AttributeType.STRING,
|
|
59
67
|
},
|
|
60
68
|
sortKey: {
|
|
@@ -65,6 +73,14 @@ export class HttpApiMock extends Resource {
|
|
|
65
73
|
removalPolicy: RemovalPolicy.DESTROY,
|
|
66
74
|
timeToLiveAttribute: 'ttl',
|
|
67
75
|
})
|
|
76
|
+
this.responsesTable.addGlobalSecondaryIndex({
|
|
77
|
+
indexName: 'methodPathQuery',
|
|
78
|
+
partitionKey: {
|
|
79
|
+
name: 'methodPathQuery',
|
|
80
|
+
type: DynamoDB.AttributeType.STRING,
|
|
81
|
+
},
|
|
82
|
+
projectionType: DynamoDB.ProjectionType.ALL,
|
|
83
|
+
})
|
|
68
84
|
|
|
69
85
|
// This lambda will publish all requests made to the API Gateway in the queue
|
|
70
86
|
const lambda = new Lambda.Function(this, 'Lambda', {
|
|
@@ -90,7 +90,7 @@ export const handler = async (
|
|
|
90
90
|
console.debug(`Matched response`, JSON.stringify({ response: objItem }))
|
|
91
91
|
|
|
92
92
|
if (
|
|
93
|
-
objItem?.
|
|
93
|
+
objItem?.requestId !== undefined &&
|
|
94
94
|
objItem?.timestamp !== undefined &&
|
|
95
95
|
objItem?.keep !== true
|
|
96
96
|
) {
|
|
@@ -98,7 +98,7 @@ export const handler = async (
|
|
|
98
98
|
new DeleteItemCommand({
|
|
99
99
|
TableName: process.env.RESPONSES_TABLE_NAME,
|
|
100
100
|
Key: marshall({
|
|
101
|
-
|
|
101
|
+
requestId: objItem.requestId,
|
|
102
102
|
timestamp: objItem.timestamp,
|
|
103
103
|
}),
|
|
104
104
|
}),
|
|
@@ -3,8 +3,8 @@ import { aws_apigateway as ApiGateway, aws_dynamodb as DynamoDB, aws_lambda as L
|
|
|
3
3
|
import type { Construct } from 'constructs';
|
|
4
4
|
export declare class HttpApiMock extends Resource {
|
|
5
5
|
readonly api: ApiGateway.RestApi;
|
|
6
|
-
readonly requestsTable: DynamoDB.
|
|
7
|
-
readonly responsesTable: DynamoDB.
|
|
6
|
+
readonly requestsTable: DynamoDB.Table;
|
|
7
|
+
readonly responsesTable: DynamoDB.Table;
|
|
8
8
|
constructor(parent: Construct, { lambdaSources, layers, }: {
|
|
9
9
|
lambdaSources: {
|
|
10
10
|
httpApiMock: PackedLambda;
|
|
@@ -10,7 +10,7 @@ export class HttpApiMock extends Resource {
|
|
|
10
10
|
this.requestsTable = new DynamoDB.Table(this, 'requests', {
|
|
11
11
|
billingMode: DynamoDB.BillingMode.PAY_PER_REQUEST,
|
|
12
12
|
partitionKey: {
|
|
13
|
-
name: '
|
|
13
|
+
name: 'requestId',
|
|
14
14
|
type: DynamoDB.AttributeType.STRING,
|
|
15
15
|
},
|
|
16
16
|
sortKey: {
|
|
@@ -21,11 +21,19 @@ export class HttpApiMock extends Resource {
|
|
|
21
21
|
removalPolicy: RemovalPolicy.DESTROY,
|
|
22
22
|
timeToLiveAttribute: 'ttl',
|
|
23
23
|
});
|
|
24
|
+
this.requestsTable.addGlobalSecondaryIndex({
|
|
25
|
+
indexName: 'methodPathQuery',
|
|
26
|
+
partitionKey: {
|
|
27
|
+
name: 'methodPathQuery',
|
|
28
|
+
type: DynamoDB.AttributeType.STRING,
|
|
29
|
+
},
|
|
30
|
+
projectionType: DynamoDB.ProjectionType.ALL,
|
|
31
|
+
});
|
|
24
32
|
// This table will store optional responses to be sent
|
|
25
33
|
this.responsesTable = new DynamoDB.Table(this, 'responses', {
|
|
26
34
|
billingMode: DynamoDB.BillingMode.PAY_PER_REQUEST,
|
|
27
35
|
partitionKey: {
|
|
28
|
-
name: '
|
|
36
|
+
name: 'responseId',
|
|
29
37
|
type: DynamoDB.AttributeType.STRING,
|
|
30
38
|
},
|
|
31
39
|
sortKey: {
|
|
@@ -36,6 +44,14 @@ export class HttpApiMock extends Resource {
|
|
|
36
44
|
removalPolicy: RemovalPolicy.DESTROY,
|
|
37
45
|
timeToLiveAttribute: 'ttl',
|
|
38
46
|
});
|
|
47
|
+
this.responsesTable.addGlobalSecondaryIndex({
|
|
48
|
+
indexName: 'methodPathQuery',
|
|
49
|
+
partitionKey: {
|
|
50
|
+
name: 'methodPathQuery',
|
|
51
|
+
type: DynamoDB.AttributeType.STRING,
|
|
52
|
+
},
|
|
53
|
+
projectionType: DynamoDB.ProjectionType.ALL,
|
|
54
|
+
});
|
|
39
55
|
// This lambda will publish all requests made to the API Gateway in the queue
|
|
40
56
|
const lambda = new Lambda.Function(this, 'Lambda', {
|
|
41
57
|
description: 'Mocks a HTTP API and stores all requests in SQS for inspection, and optionally replies with enqued responses',
|
|
@@ -53,13 +53,13 @@ export const handler = async (event, context) => {
|
|
|
53
53
|
if (matchedQueryParams === false)
|
|
54
54
|
continue;
|
|
55
55
|
console.debug(`Matched response`, JSON.stringify({ response: objItem }));
|
|
56
|
-
if (objItem?.
|
|
56
|
+
if (objItem?.requestId !== undefined &&
|
|
57
57
|
objItem?.timestamp !== undefined &&
|
|
58
58
|
objItem?.keep !== true) {
|
|
59
59
|
await db.send(new DeleteItemCommand({
|
|
60
60
|
TableName: process.env.RESPONSES_TABLE_NAME,
|
|
61
61
|
Key: marshall({
|
|
62
|
-
|
|
62
|
+
requestId: objItem.requestId,
|
|
63
63
|
timestamp: objItem.timestamp,
|
|
64
64
|
}),
|
|
65
65
|
}));
|
package/dist/src/responses.js
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import { PutItemCommand } from '@aws-sdk/client-dynamodb';
|
|
2
2
|
import { marshall } from '@aws-sdk/util-dynamodb';
|
|
3
|
+
import { randomUUID } from 'node:crypto';
|
|
3
4
|
import { sortQuery } from './sortQueryString.js';
|
|
4
5
|
export const registerResponse = async (db, responsesTable, response) => {
|
|
5
6
|
await db.send(new PutItemCommand({
|
|
6
7
|
TableName: responsesTable,
|
|
7
8
|
Item: marshall({
|
|
9
|
+
responseId: randomUUID(),
|
|
8
10
|
methodPathQuery: `${response.method} ${response.path}${response.queryParams !== undefined ? `?${sortQuery(response.queryParams)}` : ``}`,
|
|
9
11
|
timestamp: new Date().toISOString(),
|
|
10
12
|
statusCode: response.statusCode,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bifravst/http-api-mock",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.125",
|
|
4
4
|
"description": "Helper functions for AWS lambdas written in TypeScript.",
|
|
5
5
|
"exports": {
|
|
6
6
|
"./*": {
|
|
@@ -93,11 +93,12 @@
|
|
|
93
93
|
"tsx": "^4.19.1"
|
|
94
94
|
},
|
|
95
95
|
"devDependencies": {
|
|
96
|
-
"@bifravst/eslint-config-typescript": "6.1.
|
|
96
|
+
"@bifravst/eslint-config-typescript": "6.1.16",
|
|
97
97
|
"@bifravst/prettier-config": "1.1.0",
|
|
98
98
|
"@commitlint/config-conventional": "19.5.0",
|
|
99
99
|
"@types/aws-lambda": "8.10.145",
|
|
100
100
|
"@types/node": "20.16.5",
|
|
101
|
+
"commitlint": "19.5.0",
|
|
101
102
|
"globstar": "1.0.0",
|
|
102
103
|
"husky": "9.1.6"
|
|
103
104
|
}
|
package/src/responses.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { PutItemCommand, type DynamoDBClient } from '@aws-sdk/client-dynamodb'
|
|
2
2
|
import { marshall } from '@aws-sdk/util-dynamodb'
|
|
3
|
+
import { randomUUID } from 'node:crypto'
|
|
3
4
|
import { sortQuery } from './sortQueryString.js'
|
|
4
5
|
|
|
5
6
|
export type Response = {
|
|
@@ -30,6 +31,7 @@ export const registerResponse = async (
|
|
|
30
31
|
TableName: responsesTable,
|
|
31
32
|
Item: marshall(
|
|
32
33
|
{
|
|
34
|
+
responseId: randomUUID(),
|
|
33
35
|
methodPathQuery: `${response.method} ${response.path}${response.queryParams !== undefined ? `?${sortQuery(response.queryParams)}` : ``}`,
|
|
34
36
|
timestamp: new Date().toISOString(),
|
|
35
37
|
statusCode: response.statusCode,
|