@beesolve/sqs-handler 0.1.6 → 0.1.8

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/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  ## Installation
4
4
 
5
- Install the latest version of package and its peer dependencies.
5
+ Install the latest version of package:
6
6
 
7
7
  ```bash
8
8
  npm i @beesolve/sqs-handler
@@ -10,4 +10,38 @@ npm i @beesolve/sqs-handler
10
10
 
11
11
  ## Usage
12
12
 
13
- todo
13
+ Create `tasks.ts` file in your server application, which defines your handlers.
14
+
15
+ ```ts
16
+ import { createSqsHandlers } from "@beesolve/sqs-handler";
17
+ import { SQSClient } from "@aws-sdk/client-sqs";
18
+
19
+ export const [handler, tasks] = createSqsHandlers({
20
+ fifo: false, // you can create either fifo or standard queues
21
+ sqsClient: new SQSClient(), // provide SQS client
22
+ queueUrl: process.env.BEESOLVE_TASKS_QUEUE_URL!, // BEESOLVE_TASKS_QUEUE_URL env variable will be automatically injected to your Lambda function
23
+ functions: {
24
+ // here goes your functions
25
+ test: async (payload: any) => {
26
+ console.log({ payload });
27
+ },
28
+ },
29
+ });
30
+ ```
31
+
32
+ Create `SqsHandler` instance with CDK.
33
+
34
+ ```ts
35
+ import { SqsHandler } from "@beesolve/sqs-handler/cdk";
36
+
37
+ const sqsHandler = new SqsHandler(stack, "SqsHandler", {
38
+ entry: resolve(`src/lib/server/tasks.ts`), // path to your `tasks.ts` file
39
+ handlerProps: {
40
+ memorySize: 1024,
41
+ timeout: Duration.seconds(5),
42
+ },
43
+ alarms,
44
+ });
45
+
46
+ sqsHandler.grantAccess(handler);
47
+ ```
package/dist/cdk.d.ts CHANGED
@@ -8,10 +8,18 @@ interface SqsHandlerProps {
8
8
  */
9
9
  readonly entry: string;
10
10
  /**
11
- * @default false
11
+ * When you provide instance of EmailAlarms alarm for your SQS queues and Lambda handler will be set up automatically.
12
+ */
13
+ readonly alarms?: EmailAlarms;
14
+ /**
15
+ * You can change default memorySize and timeout here.
16
+ *
17
+ * @default
18
+ * {
19
+ * memorySize: 1024,
20
+ * timeout: Duration.seconds(30)
21
+ * }
12
22
  */
13
- readonly isProd?: boolean;
14
- readonly alarms: EmailAlarms;
15
23
  readonly handlerProps?: Pick<NodejsFunctionProps, "memorySize" | "timeout">;
16
24
  }
17
25
  declare class SqsHandler extends Construct {
package/dist/cdk.js CHANGED
@@ -1,7 +1,9 @@
1
1
  // packages/sqs-handler/cdk.ts
2
- import { SqsWithDlq } from "@beesolve/cdk-constructs";
2
+ import {
3
+ nodejsFunctionDefaultConfig,
4
+ SqsWithDlq
5
+ } from "@beesolve/cdk-constructs";
3
6
  import { Duration } from "aws-cdk-lib";
4
- import { Architecture, Runtime } from "aws-cdk-lib/aws-lambda";
5
7
  import {
6
8
  NodejsFunction
7
9
  } from "aws-cdk-lib/aws-lambda-nodejs";
@@ -14,29 +16,23 @@ class SqsHandler extends Construct {
14
16
  handler;
15
17
  constructor(scope, id, props) {
16
18
  super(scope, id);
17
- const { isProd = false, handlerProps = {} } = props;
19
+ const { handlerProps = {} } = props;
18
20
  this.handler = new NodejsFunction(this, "QueueHandler", {
19
- description: "Tasks queue handler",
21
+ description: `${id} queue handler`,
20
22
  entry: resolve(__dirname, props.entry),
21
23
  handler: "handler",
22
- bundling: {
23
- minify: isProd,
24
- sourceMap: isProd,
25
- sourcesContent: false,
26
- target: "es2022"
27
- },
28
24
  memorySize: 1024,
29
25
  timeout: Duration.seconds(30),
30
- runtime: Runtime.NODEJS_24_X,
31
- architecture: Architecture.ARM_64,
32
- depsLockFilePath: resolve(`${__dirname}/../../bun.lock`),
26
+ bundling: nodejsFunctionDefaultConfig.bundling,
27
+ ...nodejsFunctionDefaultConfig.runtime,
33
28
  ...handlerProps
34
29
  });
35
30
  this.queue = SqsWithDlq.asLambdaInput({
36
31
  lambda: this.handler
37
32
  });
38
33
  this.handler.addEnvironment("BEESOLVE_TASKS_QUEUE_URL", this.queue.queue.queueUrl);
39
- props.alarms.reportSqsErrors(this.queue);
34
+ props.alarms?.reportSqsErrors(this.queue);
35
+ props.alarms?.reportLambdaErrors(this.handler);
40
36
  }
41
37
  grantAccess = (grantee) => {
42
38
  this.queue.queue.grantSendMessages(grantee);
package/dist/index.js CHANGED
@@ -15,9 +15,9 @@ function createSqsHandlers(props) {
15
15
  args: v.array(v.any())
16
16
  })), body);
17
17
  if (!result.success)
18
- throw new Error(`Wrong message format: ${prettyPrint(v.flatten(result.issues))}`);
18
+ throw new Error(`Wrong message format: ${JSON.stringify(v.flatten(result.issues))}`);
19
19
  const { fn, args } = result.output;
20
- console.info(prettyPrint({ function: fn, arguments: args }));
20
+ console.info(JSON.stringify({ function: fn, arguments: args }));
21
21
  await props.functions[fn]?.(...args.map((args2) => decodeFromStringifiable(args2)));
22
22
  } catch (error) {
23
23
  console.error(error);
@@ -35,7 +35,7 @@ function createSqsHandlers(props) {
35
35
  const functionArgs = args.slice(0, originalFunction.length);
36
36
  const fifoOptions = args[originalFunction.length];
37
37
  if (props.localInvocation) {
38
- await originalFunction(...functionArgs);
38
+ originalFunction(...functionArgs);
39
39
  } else {
40
40
  await props.sqsClient.send(new SendMessageCommand({
41
41
  QueueUrl: props.queueUrl,
@@ -51,9 +51,6 @@ function createSqsHandlers(props) {
51
51
  }), {});
52
52
  return [handler, functions];
53
53
  }
54
- function prettyPrint(value) {
55
- return JSON.stringify(value, null, 2);
56
- }
57
54
  export {
58
55
  createSqsHandlers
59
56
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@beesolve/sqs-handler",
3
3
  "description": "",
4
- "version": "0.1.6",
4
+ "version": "0.1.8",
5
5
  "type": "module",
6
6
  "files": [
7
7
  "dist"
@@ -45,9 +45,9 @@
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.3",
48
+ "@beesolve/helpers": "0.1.4",
49
49
  "@beesolve/cdk-email-alarms": "0.1.1",
50
- "@beesolve/cdk-constructs": "0.1.1"
50
+ "@beesolve/cdk-constructs": "0.1.2"
51
51
  },
52
52
  "devDependencies": {
53
53
  "@types/aws-lambda": "^8.10.160"