@bifravst/http-api-mock 1.2.0 → 1.2.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.
@@ -2,7 +2,7 @@ import {
2
2
  DeleteItemCommand,
3
3
  DynamoDBClient,
4
4
  PutItemCommand,
5
- QueryCommand,
5
+ ScanCommand,
6
6
  } from '@aws-sdk/client-dynamodb'
7
7
  import { marshall, unmarshall } from '@aws-sdk/util-dynamodb'
8
8
  import type {
@@ -14,6 +14,7 @@ import { URLSearchParams } from 'url'
14
14
  import { checkMatchingQueryParams } from './checkMatchingQueryParams.js'
15
15
  import { splitMockResponse } from './splitMockResponse.js'
16
16
  import { sortQueryString } from '../../src/sortQueryString.js'
17
+ import { URLSearchParamsToObject } from '../../src/URLSearchParamsToObject.js'
17
18
 
18
19
  const db = new DynamoDBClient({})
19
20
 
@@ -37,44 +38,17 @@ export const handler = async (
37
38
  await db.send(
38
39
  new PutItemCommand({
39
40
  TableName: process.env.REQUESTS_TABLE_NAME,
40
- Item: {
41
- methodPathQuery: {
42
- S: `${event.httpMethod} ${pathWithQuery}`,
43
- },
44
- timestamp: {
45
- S: new Date().toISOString(),
46
- },
47
- requestId: {
48
- S: context.awsRequestId,
49
- },
50
- method: {
51
- S: event.httpMethod,
52
- },
53
- path: {
54
- S: pathWithQuery,
55
- },
56
- resource: { S: path },
57
- query:
58
- query === undefined
59
- ? { NULL: true }
60
- : {
61
- M: marshall(
62
- [...query.entries()].reduce(
63
- (o, [k, v]) => ({ ...o, [k]: v }),
64
- {},
65
- ),
66
- ),
67
- },
68
- body: {
69
- S: event.body ?? '{}',
70
- },
71
- headers: {
72
- S: JSON.stringify(event.headers),
73
- },
74
- ttl: {
75
- N: `${Math.round(Date.now() / 1000) + 5 * 60}`,
76
- },
77
- },
41
+ Item: marshall({
42
+ methodPathQuery: `${event.httpMethod} ${pathWithQuery}`,
43
+ timestamp: new Date().toISOString(),
44
+ requestId: context.awsRequestId,
45
+ method: event.httpMethod,
46
+ path,
47
+ query: query === undefined ? null : URLSearchParamsToObject(query),
48
+ body: event.body ?? '{}',
49
+ headers: JSON.stringify(event.headers),
50
+ ttl: Math.round(Date.now() / 1000) + 5 * 60,
51
+ }),
78
52
  }),
79
53
  )
80
54
 
@@ -82,17 +56,16 @@ export const handler = async (
82
56
  console.debug(
83
57
  `Checking if response exists for ${event.httpMethod} ${pathWithQuery}...`,
84
58
  )
85
- // Query using httpMethod and path only
59
+ // Scan using httpMethod and path only so query strings can be partially matched
86
60
  const { Items } = await db.send(
87
- new QueryCommand({
61
+ new ScanCommand({
88
62
  TableName: process.env.RESPONSES_TABLE_NAME,
89
- KeyConditionExpression: 'methodPathQuery = :methodPathQuery',
63
+ FilterExpression: 'begins_with(methodPathQuery, :methodPath)',
90
64
  ExpressionAttributeValues: {
91
- [':methodPathQuery']: {
92
- S: `${event.httpMethod} ${pathWithQuery}`,
65
+ [':methodPath']: {
66
+ S: `${event.httpMethod} ${path}`,
93
67
  },
94
68
  },
95
- ScanIndexForward: false,
96
69
  }),
97
70
  )
98
71
  console.debug(`Found response items: ${Items?.length}`)
@@ -1,9 +1,10 @@
1
- import { DeleteItemCommand, DynamoDBClient, PutItemCommand, QueryCommand, } from '@aws-sdk/client-dynamodb';
1
+ import { DeleteItemCommand, DynamoDBClient, PutItemCommand, ScanCommand, } from '@aws-sdk/client-dynamodb';
2
2
  import { marshall, unmarshall } from '@aws-sdk/util-dynamodb';
3
3
  import { URLSearchParams } from 'url';
4
4
  import { checkMatchingQueryParams } from './checkMatchingQueryParams.js';
5
5
  import { splitMockResponse } from './splitMockResponse.js';
6
6
  import { sortQueryString } from '../../src/sortQueryString.js';
7
+ import { URLSearchParamsToObject } from '../../src/URLSearchParamsToObject.js';
7
8
  const db = new DynamoDBClient({});
8
9
  export const handler = async (event, context) => {
9
10
  console.log(JSON.stringify({ event }));
@@ -15,51 +16,29 @@ export const handler = async (event, context) => {
15
16
  const pathWithQuery = sortQueryString(`${path}${query !== undefined ? `?${query.toString()}` : ''}`);
16
17
  await db.send(new PutItemCommand({
17
18
  TableName: process.env.REQUESTS_TABLE_NAME,
18
- Item: {
19
- methodPathQuery: {
20
- S: `${event.httpMethod} ${pathWithQuery}`,
21
- },
22
- timestamp: {
23
- S: new Date().toISOString(),
24
- },
25
- requestId: {
26
- S: context.awsRequestId,
27
- },
28
- method: {
29
- S: event.httpMethod,
30
- },
31
- path: {
32
- S: pathWithQuery,
33
- },
34
- resource: { S: path },
35
- query: query === undefined
36
- ? { NULL: true }
37
- : {
38
- M: marshall([...query.entries()].reduce((o, [k, v]) => ({ ...o, [k]: v }), {})),
39
- },
40
- body: {
41
- S: event.body ?? '{}',
42
- },
43
- headers: {
44
- S: JSON.stringify(event.headers),
45
- },
46
- ttl: {
47
- N: `${Math.round(Date.now() / 1000) + 5 * 60}`,
48
- },
49
- },
19
+ Item: marshall({
20
+ methodPathQuery: `${event.httpMethod} ${pathWithQuery}`,
21
+ timestamp: new Date().toISOString(),
22
+ requestId: context.awsRequestId,
23
+ method: event.httpMethod,
24
+ path,
25
+ query: query === undefined ? null : URLSearchParamsToObject(query),
26
+ body: event.body ?? '{}',
27
+ headers: JSON.stringify(event.headers),
28
+ ttl: Math.round(Date.now() / 1000) + 5 * 60,
29
+ }),
50
30
  }));
51
31
  // Check if response exists
52
32
  console.debug(`Checking if response exists for ${event.httpMethod} ${pathWithQuery}...`);
53
- // Query using httpMethod and path only
54
- const { Items } = await db.send(new QueryCommand({
33
+ // Scan using httpMethod and path only so query strings can be partially matched
34
+ const { Items } = await db.send(new ScanCommand({
55
35
  TableName: process.env.RESPONSES_TABLE_NAME,
56
- KeyConditionExpression: 'methodPathQuery = :methodPathQuery',
36
+ FilterExpression: 'begins_with(methodPathQuery, :methodPath)',
57
37
  ExpressionAttributeValues: {
58
- [':methodPathQuery']: {
59
- S: `${event.httpMethod} ${pathWithQuery}`,
38
+ [':methodPath']: {
39
+ S: `${event.httpMethod} ${path}`,
60
40
  },
61
41
  },
62
- ScanIndexForward: false,
63
42
  }));
64
43
  console.debug(`Found response items: ${Items?.length}`);
65
44
  let res;
@@ -6,7 +6,6 @@ export type Request = {
6
6
  ttl: string;
7
7
  headers: Record<string, string>;
8
8
  method: string;
9
- resource: string;
10
9
  requestId: string;
11
10
  body: string;
12
11
  methodPathQuery: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bifravst/http-api-mock",
3
- "version": "1.2.0",
3
+ "version": "1.2.2",
4
4
  "description": "Helper functions for AWS lambdas written in TypeScript.",
5
5
  "exports": {
6
6
  "./*": {
@@ -83,7 +83,7 @@
83
83
  ],
84
84
  "prettier": "@bifravst/prettier-config",
85
85
  "dependencies": {
86
- "@aws-sdk/client-cloudformation": "^3.554.0",
86
+ "@aws-sdk/client-cloudformation": "^3.555.0",
87
87
  "@aws-sdk/client-dynamodb": "^3.554.0",
88
88
  "@aws-sdk/client-sts": "^3.554.0",
89
89
  "@aws-sdk/util-dynamodb": "^3.554.0",
package/src/requests.ts CHANGED
@@ -8,7 +8,6 @@ export type Request = {
8
8
  ttl: string //e.g. 1712322374
9
9
  headers: Record<string, string> //e.g. '{"Accept":"*/*","Accept-Encoding":"br, gzip, deflate","Accept-Language":"*","CloudFront-Forwarded-Proto":"https","CloudFront-Is-Desktop-Viewer":"true","CloudFront-Is-Mobile-Viewer":"false","CloudFront-Is-SmartTV-Viewer":"false","CloudFront-Is-Tablet-Viewer":"false","CloudFront-Viewer-ASN":"2116","CloudFront-Viewer-Country":"NO","Host":"idj1fffo0k.execute-api.eu-west-1.amazonaws.com","sec-fetch-mode":"cors","User-Agent":"node","Via":"1.1 b053873243f91b1bb6dc406ce0c67db4.cloudfront.net (CloudFront)","X-Amz-Cf-Id":"_vJIGo6Z89QxDzoqOZL4G0PQqPFWGesVXVan4ND934_Urqn2ifSOsQ==","X-Amzn-Trace-Id":"Root=1-660ff61a-25e1219a7f153e1b0c768358","X-Forwarded-For":"194.19.86.146, 130.176.182.18","X-Forwarded-Port":"443","X-Forwarded-Proto":"https"}'
10
10
  method: string //e.g.'GET'
11
- resource: string //e.g. '555c3960-2092-438b-b2b0-f28eebd1f5bb'
12
11
  requestId: string //e.g.'f34b042b-e9a2-4089-97a2-241516d40d64'
13
12
  body: string //e.g.'{}'
14
13
  methodPathQuery: string //e.g.'GET 555c3960-2092-438b-b2b0-f28eebd1f5bb'