I am really new to AWS SQS and middy middleware. I have one SQS lambda function which job is update one of my dynamo DB table. I found middy middleware for SQS event from one article. I am having difficulties to understand how this middleware works. My typescript consistently throws me the error when I am trying to use this middleware to my SQS lambda handler.

it says:

Type ‘PromiseSettledResult[]’ is not assignable to type ‘PromiseSettledResult[]’. Type ‘PromiseSettledResult’ is not assignable to type ‘PromiseSettledResult’. Type ‘PromiseFulfilledResult’ is not assignable to type ‘PromiseSettledResult’. Type ‘PromiseFulfilledResult’ is not assignable to type ‘PromiseFulfilledResult’. Type ‘void’ is not assignable to type ‘SQSRecord’.ts(2322)

This is the middleware

import middy from '@middy/core'
import sqsPartialBatchFailure from '@middy/sqs-partial-batch-failure'
import ssm from '@middy/ssm'
import { DEFAULT_MASKED_LOG_FIELD_NAMES } from './constants'
import { middyLogMask } from './logging/shared-logger'
import { Context, Handler, SQSEvent, SQSRecord } from 'aws-lambda'

import { middySqsErrorLogger } from './middy-sqs-error-logger'
import { middyXRayEgressHttp } from './middy-x-ray-egress-http'
import { middyCorrelationIds } from './wrapper/middy-correlation-ids'

/**
 * Returns handler with default set of middlewares for SQS handlers
 * - middyLogMask (configurable: maskedLogFieldNames)
 * - middyXRayEgressHttp
 * - middyCorrelationIds
 * - sqsPartialBatchFailure deletes successfully processed messages from SQS
 * - ssm, if ssmParams is provided in config, injects SSM params into Context
 * - middySqsErrorLogger Logs rejected SQS messages with correlation ID
 *
 * @param handlerFn lambda function
 * @param config configuration object
 * @returns middyfied handler
 */
export const middySqsHandlerWrapper = (
  handlerFn: Handler<SQSEvent, PromiseSettledResult<SQSRecord>[]>,
  {
    ssmParams,
    maskedLogFieldNames = [],
  }: { ssmParams?: Record<string, string>; maskedLogFieldNames?: string[] },
): middy.Middy<SQSEvent, PromiseSettledResult<SQSRecord>[], Context> => {
  return middy(handlerFn).use([
    middyLogMask({
      maskedLogFieldNames: [...DEFAULT_MASKED_LOG_FIELD_NAMES, ...maskedLogFieldNames],
    }),
    middyXRayEgressHttp(),
    middyCorrelationIds(),
    sqsPartialBatchFailure(),
    ...(ssmParams
      ? [
          ssm({
            cache: true,
            setToContext: true,
            names: ssmParams,
          }),
        ]
      : []),
    middySqsErrorLogger(),
  ])
}

This is the SQS lambda handler where I am using above middleware

import { middySqsHandlerWrapper } from '/middy-sqs-handler-wrapper'
import { PaymentStreamSnsMessage } from '/types/sns'
import { Log } from './shared-logger'
import { SQSEvent, SQSRecord } from 'aws-lambda'

import {
  TaggedOrderStateId,
  tagOrderStateId,
  untagId,
  updateInputWithTimestampRefresh,
} from '../data-sources/picking-ddb-utils'
import { findOrderStateNode } from '../data-sources/picking-state'
import { updateDocument } from '../data-sources/service/dynamo'
import { PaymentStatus } from '../generated/graphql'
import { OrderStateNode } from '../types/picking-state'

const doSomething = async (
  o: PaymentStreamSnsMessage,
  orders: PaymentStreamSnsMessage[],
): Promise<void> => {
  const existingOrders = await findOrderStateNode(tagOrderStateId(o.orderId)) // find the order, is it exsit or not
  if (existingOrders === undefined) {
    Log.info('Payment order could not be found from the picking table', {
      order: o,
    })
  }

  const existingOrderId = existingOrders?.pk as TaggedOrderStateId

  const updatedOrder = orders.find((r) => r.orderId === untagId(existingOrderId))

  if (!updatedOrder) return
  const paymentStatus: PaymentStatus = updatedOrder.paymentStatus
  const newUpdatedOrder = updateInputWithTimestampRefresh<OrderStateNode>(
    { pk: existingOrderId, sk: existingOrderId },
    { paymentStatus },
  )

  try {
    await updateDocument(newUpdatedOrder)
  } catch (e) {
    Log.warn('Updating initialized state failed', { orderId: newUpdatedOrder.Key }, e)
  }
}

const parseOrdersFromSqs = (event: SQSEvent): PaymentStreamSnsMessage[] => {
  try {
    return event.Records.flatMap((r) => JSON.parse(r.body) as unknown) as PaymentStreamSnsMessage[]
  } catch (error) {
    Log.error('New order from SNS failed at parsing orders', { event }, error)
    return []
  }
}

export const handlerFn = async (event: SQSEvent): Promise<PromiseSettledResult<SQSRecord>[]> => {
  const orders = parseOrdersFromSqs(event)

  const promise = orders.map(async (o) => {
    await doSomething(o, orders)
  })

  return await Promise.allSettled(promise)
}

export const handler = middySqsHandlerWrapper(handlerFn, {})