@@ -19,6 +19,12 @@ import {
19
19
ClientContextEnv ,
20
20
ApiGatewayEventValidity ,
21
21
EventBridgeEvent ,
22
+ SqsEvent ,
23
+ SqsRecord ,
24
+ SqsRecordAttributes ,
25
+ SqsMessageAttribute ,
26
+ SqsBatchResponse ,
27
+ SqsBatchItemFailure ,
22
28
} from "./glambda.mjs" ;
23
29
24
30
export function toApiGatewayProxyEventV2 ( event ) {
@@ -225,3 +231,67 @@ export function toEventBridgeEvent(event) {
225
231
event . detail ,
226
232
) ;
227
233
}
234
+
235
+ export function toSqsEvent ( event ) {
236
+ return new SqsEvent ( List . fromArray ( event . Records . map ( toSqsRecord ) ) ) ;
237
+ }
238
+
239
+ function toSqsRecord ( record ) {
240
+ return new SqsRecord (
241
+ record . messageId ,
242
+ record . receiptHandle ,
243
+ record . body ,
244
+ toSqsRecordAttributes ( record . attributes ) ,
245
+ toMessageAttributes ( record . messageAttributes ) ,
246
+ record . md5OfBody ,
247
+ record . eventSource ,
248
+ record . eventSourceARN ,
249
+ record . awsRegion ,
250
+ ) ;
251
+ }
252
+
253
+ function toSqsRecordAttributes ( attrs ) {
254
+ return new SqsRecordAttributes (
255
+ maybe ( attrs . AWSTraceHeader ) ,
256
+ attrs . ApproximateReceiveCount ,
257
+ attrs . SentTimestamp ,
258
+ attrs . SenderId ,
259
+ attrs . ApproximateFirstReceiveTimestamp ,
260
+ maybe ( attrs . SequenceNumber ) ,
261
+ maybe ( attrs . MessageGroupId ) ,
262
+ maybe ( attrs . MessageDeduplicationId ) ,
263
+ maybe ( attrs . DeadLetterQueueSourceArn ) ,
264
+ ) ;
265
+ }
266
+
267
+ function toMessageAttributes ( attrs ) {
268
+ let entries = Object . entries ( attrs ) . map ( ( [ key , value ] ) => {
269
+ let v = toSqsMessageAttribute ( value ) ;
270
+ return [ key , v ] ;
271
+ } ) ;
272
+ return $dict . from_list ( List . fromArray ( entries ) ) ;
273
+ }
274
+
275
+ function toSqsMessageAttribute ( attr ) {
276
+ return new SqsMessageAttribute (
277
+ maybe ( attr . stringValue ) ,
278
+ maybe ( attr . binaryValue ) ,
279
+ maybe ( attr . stringListValue ) ,
280
+ maybe ( attr . binaryListValue ) ,
281
+ attr . dataType ,
282
+ ) ;
283
+ }
284
+
285
+ export function fromSqsBatchResponse ( response ) {
286
+ return {
287
+ batchItemFailures : response . batch_item_failures
288
+ . toArray ( )
289
+ . map ( fromSqsBatchItemFailure ) ,
290
+ } ;
291
+ }
292
+
293
+ function fromSqsBatchItemFailure ( failure ) {
294
+ return {
295
+ itemIdentifier : failure . item_identifier ,
296
+ } ;
297
+ }
0 commit comments