@beesolve/sqs-handler 0.1.3 → 0.1.5

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.
Files changed (2) hide show
  1. package/dist/index.js +14 -14
  2. package/package.json +2 -2
package/dist/index.js CHANGED
@@ -11,17 +11,14 @@ function createSqsHandlers(props) {
11
11
  for (const { body, messageId } of event.Records) {
12
12
  try {
13
13
  const result = v.safeParse(v.pipe(v.string(), v.parseJson(), v.object({
14
- fName: v.picklist(Object.keys(props.functions)),
15
- fArgs: v.array(v.any())
14
+ fn: v.picklist(Object.keys(props.functions)),
15
+ args: v.array(v.any())
16
16
  })), body);
17
17
  if (!result.success)
18
- throw new Error(`Wrong message format: ${JSON.stringify(v.flatten(result.issues), null, 2)}`);
19
- const { fName, fArgs } = result.output;
20
- console.info({
21
- function: fName,
22
- arguments: JSON.stringify(fArgs, null, 2)
23
- });
24
- await props.functions[fName]?.(...fArgs.map((args) => decodeFromStringifiable(args)));
18
+ throw new Error(`Wrong message format: ${prettyPrint(v.flatten(result.issues))}`);
19
+ const { fn, args } = result.output;
20
+ console.info(prettyPrint({ function: fn, arguments: args }));
21
+ await props.functions[fn]?.(...args.map((args2) => decodeFromStringifiable(args2)));
25
22
  } catch (error) {
26
23
  console.error(error);
27
24
  batchItemFailures.push({ itemIdentifier: messageId });
@@ -31,20 +28,20 @@ function createSqsHandlers(props) {
31
28
  };
32
29
  const functions = Object.entries(props.functions).reduce((result, [functionName]) => ({
33
30
  ...result,
34
- [functionName](...args) {
31
+ async[functionName](...args) {
35
32
  const originalFunction = props.functions[functionName];
36
33
  if (originalFunction == null)
37
34
  throw Error(`Cannot invoke "${functionName}". Make sure the function is defined.`);
38
35
  const functionArgs = args.slice(0, originalFunction.length);
39
36
  const fifoOptions = args[originalFunction.length];
40
37
  if (props.localInvocation) {
41
- originalFunction(...functionArgs);
38
+ await originalFunction(...functionArgs);
42
39
  } else {
43
- props.sqsClient.send(new SendMessageCommand({
40
+ await props.sqsClient.send(new SendMessageCommand({
44
41
  QueueUrl: props.queueUrl,
45
42
  MessageBody: JSON.stringify({
46
- fName: functionName,
47
- fArgs: functionArgs.map((args2) => encodeToStringifiable(args2))
43
+ fn: functionName,
44
+ args: functionArgs.map((args2) => encodeToStringifiable(args2))
48
45
  }),
49
46
  MessageDeduplicationId: fifoOptions?.deduplicationId,
50
47
  MessageGroupId: fifoOptions?.groupId
@@ -54,6 +51,9 @@ function createSqsHandlers(props) {
54
51
  }), {});
55
52
  return [handler, functions];
56
53
  }
54
+ function prettyPrint(value) {
55
+ return JSON.stringify(value, null, 2);
56
+ }
57
57
  export {
58
58
  createSqsHandlers
59
59
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@beesolve/sqs-handler",
3
3
  "description": "",
4
- "version": "0.1.3",
4
+ "version": "0.1.5",
5
5
  "type": "module",
6
6
  "files": [
7
7
  "dist"
@@ -45,7 +45,7 @@
45
45
  "constructs": "^10.4.5",
46
46
  "@aws-sdk/client-sqs": "^3.978.0",
47
47
  "valibot": "^1.2.0",
48
- "@beesolve/helpers": "0.1.1",
48
+ "@beesolve/helpers": "0.1.2",
49
49
  "@beesolve/cdk-email-alarms": "0.1.1",
50
50
  "@beesolve/cdk-constructs": "0.1.1"
51
51
  },