@beesolve/sqs-handler 0.1.2 → 0.1.4
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/dist/cdk.js +1 -0
- package/dist/index.js +11 -11
- package/package.json +1 -1
package/dist/cdk.js
CHANGED
package/dist/index.js
CHANGED
|
@@ -11,17 +11,17 @@ 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
|
-
|
|
15
|
-
|
|
14
|
+
fn: v.picklist(Object.keys(props.functions)),
|
|
15
|
+
args: v.array(v.any())
|
|
16
16
|
})), body);
|
|
17
17
|
if (!result.success)
|
|
18
18
|
throw new Error(`Wrong message format: ${JSON.stringify(v.flatten(result.issues), null, 2)}`);
|
|
19
|
-
const {
|
|
19
|
+
const { fn, args } = result.output;
|
|
20
20
|
console.info({
|
|
21
|
-
function:
|
|
22
|
-
arguments:
|
|
21
|
+
function: fn,
|
|
22
|
+
arguments: args
|
|
23
23
|
});
|
|
24
|
-
await props.functions[
|
|
24
|
+
await props.functions[fn]?.(...args.map((args2) => decodeFromStringifiable(args2)));
|
|
25
25
|
} catch (error) {
|
|
26
26
|
console.error(error);
|
|
27
27
|
batchItemFailures.push({ itemIdentifier: messageId });
|
|
@@ -31,20 +31,20 @@ function createSqsHandlers(props) {
|
|
|
31
31
|
};
|
|
32
32
|
const functions = Object.entries(props.functions).reduce((result, [functionName]) => ({
|
|
33
33
|
...result,
|
|
34
|
-
[functionName](...args) {
|
|
34
|
+
async[functionName](...args) {
|
|
35
35
|
const originalFunction = props.functions[functionName];
|
|
36
36
|
if (originalFunction == null)
|
|
37
37
|
throw Error(`Cannot invoke "${functionName}". Make sure the function is defined.`);
|
|
38
38
|
const functionArgs = args.slice(0, originalFunction.length);
|
|
39
39
|
const fifoOptions = args[originalFunction.length];
|
|
40
40
|
if (props.localInvocation) {
|
|
41
|
-
originalFunction(...functionArgs);
|
|
41
|
+
await originalFunction(...functionArgs);
|
|
42
42
|
} else {
|
|
43
|
-
props.sqsClient.send(new SendMessageCommand({
|
|
43
|
+
await props.sqsClient.send(new SendMessageCommand({
|
|
44
44
|
QueueUrl: props.queueUrl,
|
|
45
45
|
MessageBody: JSON.stringify({
|
|
46
|
-
|
|
47
|
-
|
|
46
|
+
fn: functionName,
|
|
47
|
+
args: functionArgs.map((args2) => encodeToStringifiable(args2))
|
|
48
48
|
}),
|
|
49
49
|
MessageDeduplicationId: fifoOptions?.deduplicationId,
|
|
50
50
|
MessageGroupId: fifoOptions?.groupId
|