Skip to content

Commit

Permalink
Merge pull request #65 from lifeomic/base-options
Browse files Browse the repository at this point in the history
fix: add central type for base options of a handler
  • Loading branch information
swain authored Nov 29, 2023
2 parents 3bb56a5 + bc74e1c commit 2f46b52
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 77 deletions.
47 changes: 18 additions & 29 deletions src/dynamo-streams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,28 @@ import {
import { marshall, unmarshall } from '@aws-sdk/util-dynamodb';
import {
BaseContext,
BaseHandlerConfig,
processWithOrdering,
withHealthCheckHandling,
} from './utils';

export type DynamoStreamHandlerConfig<Entity, Context> = {
/**
* 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<Context>;
/**
* The maximum concurrency for processing records.
*
* @default 5
*/
concurrency?: number;
};
export type DynamoStreamHandlerConfig<Entity, Context> =
BaseHandlerConfig<Context> & {
/**
* 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<Entity, Context> = (
ctx: Context & BaseContext,
Expand Down
28 changes: 8 additions & 20 deletions src/kinesis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Event, Context> = {
/**
* 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<Context>;
/**
* The maximum concurrency for processing events.
*
* @default 5
*/
concurrency?: number;
};
export type KinesisEventHandlerConfig<Event, Context> =
BaseHandlerConfig<Context> & {
/**
* A function for parsing the Kinesis event data into your custom type.
*/
parseEvent: (body: string) => Event;
};

export type KinesisEventAction<Event, Context> = (
context: Context & BaseContext,
Expand Down
45 changes: 17 additions & 28 deletions src/sqs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Message, Context> = {
/**
* 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<Context>;
/**
* 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<Message, Context> =
BaseHandlerConfig<Context> & {
/**
* 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<Message, Context> = (
context: Context & BaseContext,
Expand Down
18 changes: 18 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,24 @@ export type BaseContext = {
correlationId: string;
};

export type BaseHandlerConfig<Context> = {
/**
* 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<Context>;

/**
* The maximum concurrency for processing events.
*
* @default 5
*/
concurrency?: number;
};

export const withHealthCheckHandling =
<Event, HandlerResponse>(
handler: (event: Event, context: Context) => Promise<HandlerResponse>,
Expand Down

0 comments on commit 2f46b52

Please sign in to comment.