From 9c8c3a4438385290aacae2f75ee49e72609c4ec9 Mon Sep 17 00:00:00 2001 From: George Gunderson Date: Thu, 15 Apr 2021 11:13:27 +1000 Subject: [PATCH] feat: sqs handler --- packages/sls-aws/package.json | 2 +- packages/sls-aws/src/index.ts | 1 + packages/sls-aws/src/sqs/index.ts | 17 +++++++++++++++++ packages/sls-aws/src/sqs/json-sqs-message.ts | 16 ++++++++++++++++ packages/sls-aws/src/sqs/json-sqs-messages.ts | 13 +++++++++++++ 5 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 packages/sls-aws/src/sqs/index.ts create mode 100644 packages/sls-aws/src/sqs/json-sqs-message.ts create mode 100644 packages/sls-aws/src/sqs/json-sqs-messages.ts diff --git a/packages/sls-aws/package.json b/packages/sls-aws/package.json index 7d95296..c6cb46b 100644 --- a/packages/sls-aws/package.json +++ b/packages/sls-aws/package.json @@ -1,6 +1,6 @@ { "name": "@cabiri-io/sls-aws", - "version": "0.9.0", + "version": "0.10.0", "author": "Cabiri ", "license": "UNLICENSED", "repository": { diff --git a/packages/sls-aws/src/index.ts b/packages/sls-aws/src/index.ts index 51847c3..cd51776 100644 --- a/packages/sls-aws/src/index.ts +++ b/packages/sls-aws/src/index.ts @@ -1,3 +1,4 @@ export { snsMessage, snsMessages } from './sns' +export { sqsMessage, sqsMessages } from './sqs' export * from './api-gateway' export * from './cognito' diff --git a/packages/sls-aws/src/sqs/index.ts b/packages/sls-aws/src/sqs/index.ts new file mode 100644 index 0000000..1fa2b3d --- /dev/null +++ b/packages/sls-aws/src/sqs/index.ts @@ -0,0 +1,17 @@ +import { EnvironmentConfig, Handler, SlsEnvironment, environment } from '@cabiri-io/sls-env' +import type { Context, SQSEvent } from 'aws-lambda' +import { response } from '../reponse/response-or-error' +import { jsonSQSMessage } from './json-sqs-message' +import { jsonSQSMessages } from './json-sqs-messages' + +export type SQSHandler = Handler> + +export const sqsMessage = ( + config?: EnvironmentConfig +): SlsEnvironment => + environment(config).payload(jsonSQSMessage).successHandler(response) + +export const sqsMessages = ( + config?: EnvironmentConfig +): SlsEnvironment> => + environment>(config).payload(jsonSQSMessages).successHandler(response) diff --git a/packages/sls-aws/src/sqs/json-sqs-message.ts b/packages/sls-aws/src/sqs/json-sqs-message.ts new file mode 100644 index 0000000..436dbee --- /dev/null +++ b/packages/sls-aws/src/sqs/json-sqs-message.ts @@ -0,0 +1,16 @@ +import type { Context, SQSEvent } from 'aws-lambda' + +import { PayloadError, PayloadParseError } from '../error/parse' + +const jsonSQSMessage = (event: SQSEvent, _context: Context): T => { + if (event.Records.length === 1) { + try { + return (JSON.parse(event.Records[0].body) as unknown) as T + } catch (err) { + throw new PayloadParseError('failed to process sqs message to json', { originalError: err }) + } + } + throw new PayloadError('event contains more than 1 record') +} + +export { jsonSQSMessage } diff --git a/packages/sls-aws/src/sqs/json-sqs-messages.ts b/packages/sls-aws/src/sqs/json-sqs-messages.ts new file mode 100644 index 0000000..ecfff4a --- /dev/null +++ b/packages/sls-aws/src/sqs/json-sqs-messages.ts @@ -0,0 +1,13 @@ +import type { Context, SQSEvent } from 'aws-lambda' + +import { PayloadParseError } from '../error/parse' + +const jsonSQSMessages = (event: SQSEvent, _context: Context): Array => { + try { + return event.Records.map(r => r.body).map(m => JSON.parse(m)) + } catch (err) { + throw new PayloadParseError('failed to process sqs message to json', { originalError: err }) + } +} + +export { jsonSQSMessages }