import type { Context, DynamoDBBatchResponse, DynamoDBRecord, DynamoDBStreamEvent } from 'aws-lambda'; import Transaction, { TransactionConfig } from './Transaction.js'; import Response, { ResponseErrorType } from '../API/Response.js'; /** * Interface representing a DynamoDB record with marshalled data. * Extends the DynamoDBRecord interface. * @property {object} Keys - The keys of the record. * @property {object} OldImage - The old image of the record. * @property {object} NewImage - The new image of the record. */ export interface DynamoDBMarshalledRecord extends DynamoDBRecord { marshalled: { Keys?: object; OldImage?: object; NewImage?: object; }; } /** * Defines a type for executing a transaction on DynamoDB. * @param {Transaction} transaction - The transaction to execute. * @param {DynamoDBMarshalledRecord} recordContent - The content of the DynamoDB record. * @returns A promise that resolves to a response or a DynamoDB batch response. */ export type DynamoTransactionExecution = (transaction: Transaction, recordContent: DynamoDBMarshalledRecord) => Promise | DynamoDBBatchResponse | null>; /** * Represents a DynamoDB transaction handler that processes events from a DynamoDB stream. * @template ResponseInnerType - The inner type of the response. */ export default class DynamoTransaction { /** * A boolean flag indicating whether failures are allowed. */ private readonly allowFailure; /** * Readonly property that holds the transaction configuration. */ private readonly config; /** * The context object that provides information about the current execution context. * @type {Context} */ private readonly context; /** * Represents an event from a DynamoDB stream. * @type {DynamoDBStreamEvent} */ private readonly event; /** * Constructor for a TransactionHandler object. * @param {DynamoDBStreamEvent} event - The DynamoDB stream event that triggered the transaction. * @param {Context} context - The AWS Lambda context object. * @param {TransactionConfig} [config] - Optional configuration for the transaction. * @param {boolean} [allowFailure] - Flag to indicate whether to allow transaction failure. * @returns None */ constructor(event: DynamoDBStreamEvent, context: Context, config?: TransactionConfig, allowFailure?: boolean); /** * Processes the event execution and returns a response based on the outcome. * @param {DynamoTransactionExecution} execution - The execution object to process. * @returns {Promise | null | DynamoDBBatchResponse>} A promise that resolves to a response object or null. * @throws {Error} If the response code is not within the success range and failure is not allowed. */ processEvent(execution: DynamoTransactionExecution): Promise | null | DynamoDBBatchResponse>; /** * Processes a raw event by executing a transaction on each record in the event. * @param {DynamoTransactionExecution} execution - The transaction execution function. * @returns {Promise | null | DynamoDBBatchResponse>} A promise that resolves to a response object, null, or a DynamoDB batch response. */ private processRawEvent; }