From bc74e1c791f9736858968c90c58d99dff70af594 Mon Sep 17 00:00:00 2001 From: Swain Molster Date: Wed, 29 Nov 2023 11:40:01 -0500 Subject: [PATCH] fix: add central type for base options of a handler --- src/dynamo-streams.ts | 47 +++++++++++++++++-------------------------- src/kinesis.ts | 28 ++++++++------------------ src/sqs.ts | 45 ++++++++++++++++------------------------- src/utils.ts | 18 +++++++++++++++++ 4 files changed, 61 insertions(+), 77 deletions(-) diff --git a/src/dynamo-streams.ts b/src/dynamo-streams.ts index 5809dc7..9187592 100644 --- a/src/dynamo-streams.ts +++ b/src/dynamo-streams.ts @@ -8,39 +8,28 @@ import { import { marshall, unmarshall } from '@aws-sdk/util-dynamodb'; import { BaseContext, + BaseHandlerConfig, processWithOrdering, withHealthCheckHandling, } from './utils'; -export type DynamoStreamHandlerConfig = { - /** - * A logger to use in the context. - */ - logger: LoggerInterface; - /** - * A listing of keys within a dynamo record's images to obfuscate in logging - * output. This will not perform a deep obfuscation of 'M' AttributeValue - * types and instead will simply obfuscate the entire value. - */ - loggerObfuscateImageKeys?: string[]; - /** - * A function for parsing images from the stream into your custom type. - * - * The `object` parameter is an _already unmarshalled_ version of the Dynamo - * record. - */ - parse: (object: unknown) => Entity; - /** - * Create a "context" for the lambda execution. (e.g. "data sources") - */ - createRunContext: (base: BaseContext) => Context | Promise; - /** - * The maximum concurrency for processing records. - * - * @default 5 - */ - concurrency?: number; -}; +export type DynamoStreamHandlerConfig = + BaseHandlerConfig & { + /** + * A function for parsing images from the stream into your custom type. + * + * The `object` parameter is an _already unmarshalled_ version of the Dynamo + * record. + */ + parse: (object: unknown) => Entity; + + /** + * A listing of keys within a dynamo record's images to obfuscate in logging + * output. This will not perform a deep obfuscation of 'M' AttributeValue + * types and instead will simply obfuscate the entire value. + */ + loggerObfuscateImageKeys?: string[]; + }; export type InsertAction = ( ctx: Context & BaseContext, diff --git a/src/kinesis.ts b/src/kinesis.ts index 648166d..864ea05 100644 --- a/src/kinesis.ts +++ b/src/kinesis.ts @@ -3,30 +3,18 @@ import { v4 as uuid } from 'uuid'; import { KinesisStreamEvent, Context as AWSContext } from 'aws-lambda'; import { BaseContext, + BaseHandlerConfig, processWithOrdering, withHealthCheckHandling, } from './utils'; -export type KinesisEventHandlerConfig = { - /** - * A logger to use in the context. - */ - logger: LoggerInterface; - /** - * A function for parsing the Kinesis event data into your custom type. - */ - parseEvent: (body: string) => Event; - /** - * Create a "context" for the lambda execution. (e.g. "data sources") - */ - createRunContext: (base: BaseContext) => Context | Promise; - /** - * The maximum concurrency for processing events. - * - * @default 5 - */ - concurrency?: number; -}; +export type KinesisEventHandlerConfig = + BaseHandlerConfig & { + /** + * A function for parsing the Kinesis event data into your custom type. + */ + parseEvent: (body: string) => Event; + }; export type KinesisEventAction = ( context: Context & BaseContext, diff --git a/src/sqs.ts b/src/sqs.ts index 57fbeeb..d1f3bc5 100644 --- a/src/sqs.ts +++ b/src/sqs.ts @@ -3,38 +3,27 @@ import { v4 as uuid } from 'uuid'; import { SQSEvent, Context as AWSContext } from 'aws-lambda'; import { BaseContext, + BaseHandlerConfig, processWithOrdering, withHealthCheckHandling, } from './utils'; -export type SQSMessageHandlerConfig = { - /** - * A logger to use in the context. - */ - logger: LoggerInterface; - /** - * A function for parsing SQS messages into your custom type. - */ - parseMessage: (body: string) => Message; - /** - * Create a "context" for the lambda execution. (e.g. "data sources") - */ - createRunContext: (base: BaseContext) => Context | Promise; - /** - * The maximum concurrency for processing messages. - * - * @default 5 - */ - concurrency?: number; - /** - * Whether or not to use SQS partial batch responses. If set to true, make - * sure to also turn on partial batch responses when configuring your event - * source mapping by specifying ReportBatchItemFailures for the - * FunctionResponseTypes action. For more details see: - * https://docs.aws.amazon.com/lambda/latest/dg/with-sqs.html#services-sqs-batchfailurereporting - */ - usePartialBatchResponses?: boolean; -}; +export type SQSMessageHandlerConfig = + BaseHandlerConfig & { + /** + * A function for parsing SQS messages into your custom type. + */ + parseMessage: (body: string) => Message; + + /** + * Whether or not to use SQS partial batch responses. If set to true, make + * sure to also turn on partial batch responses when configuring your event + * source mapping by specifying ReportBatchItemFailures for the + * FunctionResponseTypes action. For more details see: + * https://docs.aws.amazon.com/lambda/latest/dg/with-sqs.html#services-sqs-batchfailurereporting + */ + usePartialBatchResponses?: boolean; + }; export type SQSMessageAction = ( context: Context & BaseContext, diff --git a/src/utils.ts b/src/utils.ts index 733270a..595c902 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -9,6 +9,24 @@ export type BaseContext = { correlationId: string; }; +export type BaseHandlerConfig = { + /** + * A logger to use in the context. + */ + logger: LoggerInterface; + /** + * Create a "context" for the lambda execution. (e.g. "data sources") + */ + createRunContext: (base: BaseContext) => Context | Promise; + + /** + * The maximum concurrency for processing events. + * + * @default 5 + */ + concurrency?: number; +}; + export const withHealthCheckHandling = ( handler: (event: Event, context: Context) => Promise,