Repository: motdotla/node-lambda
Pull request #770 (2024-12-15 01:57:24)

sdk-v3: Use sdk-v3 with _updateEventSources()
GH-641


Patch:
diff --git a/lib/main.js b/lib/main.js
index 8752968..adc9246 100644
--- a/lib/main.js
+++ b/lib/main.js
@@ -29,9 +29,12 @@ const { createNamespace } = require('continuation-local-storage')
  */
 const {
   LambdaClient,
+  CreateEventSourceMappingCommand,
   CreateFunctionCommand,
-  ListEventSourceMappingsCommand,
+  DeleteEventSourceMappingCommand,
   GetFunctionCommand,
+  ListEventSourceMappingsCommand,
+  UpdateEventSourceMappingCommand,
   UpdateFunctionCodeCommand,
   UpdateFunctionConfigurationCommand
 } = require('@aws-sdk/client-lambda')
@@ -840,30 +843,16 @@ they may not work as expected in the Lambda environment.
       switch (updateEventSource.type) {
         case 'create':
           delete updateEventSource.type
-          return new Promise((resolve, reject) => {
-            lambda.createEventSourceMapping(updateEventSource, (err, data) => {
-              if (err) return reject(err)
-              resolve(data)
-            })
-          })
+          return lambda.send(new CreateEventSourceMappingCommand(updateEventSource))
         case 'update':
           delete updateEventSource.type
-          return new Promise((resolve, reject) => {
-            lambda.updateEventSourceMapping(updateEventSource, (err, data) => {
-              if (err) return reject(err)
-              resolve(data)
-            })
-          })
+          return lambda.send(new UpdateEventSourceMappingCommand(updateEventSource))
         case 'delete':
           delete updateEventSource.type
-          return new Promise((resolve, reject) => {
-            lambda.deleteEventSourceMapping(updateEventSource, (err, data) => {
-              if (err) return reject(err)
-              resolve(data)
-            })
-          })
+          return lambda.send(new DeleteEventSourceMappingCommand(updateEventSource))
+        default:
+          return Promise.resolve()
       }
-      return Promise.resolve()
     }))
   }
 
@@ -1033,7 +1022,7 @@ they may not work as expected in the Lambda environment.
               ])
             }),
             this._updateEventSources(
-              lambda,
+              lambdaClient,
               params.FunctionName,
               existingEventSourceList,
               eventSourceList.EventSourceMappings
@@ -1056,7 +1045,7 @@ they may not work as expected in the Lambda environment.
 
           return Promise.all([
             this._updateEventSources(
-              lambda,
+              lambdaClient,
               params.FunctionName,
               [],
               eventSourceList.EventSourceMappings


Test patch:
diff --git a/test/main.js b/test/main.js
index 4f60a85..ae29500 100644
--- a/test/main.js
+++ b/test/main.js
@@ -19,9 +19,12 @@ awsMock.setSDK(path.resolve('node_modules/aws-sdk'))
 const { mockClient } = require('aws-sdk-client-mock')
 const {
   LambdaClient,
+  CreateEventSourceMappingCommand,
   CreateFunctionCommand,
-  ListEventSourceMappingsCommand,
+  DeleteEventSourceMappingCommand,
   GetFunctionCommand,
+  ListEventSourceMappingsCommand,
+  UpdateEventSourceMappingCommand,
   UpdateFunctionCodeCommand,
   UpdateFunctionConfigurationCommand
 } = require('@aws-sdk/client-lambda')
@@ -172,11 +175,14 @@ describe('lib/main', function () {
 
     // for sdk v3
     mockLambdaClient.reset()
+    mockLambdaClient.on(CreateEventSourceMappingCommand).resolves(lambdaMockSettings.createEventSourceMapping)
     mockLambdaClient.on(CreateFunctionCommand).resolves(lambdaMockSettings.createFunction)
+    mockLambdaClient.on(DeleteEventSourceMappingCommand).resolves(lambdaMockSettings.deleteEventSourceMapping)
     mockLambdaClient.on(GetFunctionCommand).resolves(lambdaMockSettings.getFunction)
+    mockLambdaClient.on(ListEventSourceMappingsCommand).resolves(lambdaMockSettings.listEventSourceMappings)
+    mockLambdaClient.on(UpdateEventSourceMappingCommand).resolves(lambdaMockSettings.updateEventSourceMapping)
     mockLambdaClient.on(UpdateFunctionCodeCommand).resolves(lambdaMockSettings.updateFunctionCode)
     mockLambdaClient.on(UpdateFunctionConfigurationCommand).resolves(lambdaMockSettings.updateFunctionConfiguration)
-    mockLambdaClient.on(ListEventSourceMappingsCommand).resolves(lambdaMockSettings.listEventSourceMappings)
   })
   after(() => {
     _awsRestore()
@@ -1424,7 +1430,7 @@ describe('lib/main', function () {
       program.eventSourceFile = ''
       const eventSourceList = lambda._eventSourceList(program)
       return lambda._updateEventSources(
-        awsLambda,
+        lambdaClient,
         '',
         [],
         eventSourceList.EventSourceMappings
@@ -1437,7 +1443,7 @@ describe('lib/main', function () {
       program.eventSourceFile = 'event_sources.json'
       const eventSourceList = lambda._eventSourceList(program)
       return lambda._updateEventSources(
-        awsLambda,
+        lambdaClient,
         'functionName',
         [],
         eventSourceList.EventSourceMappings
@@ -1448,7 +1454,7 @@ describe('lib/main', function () {
 
     it('simple test with mock (In case of deletion)', () => {
       return lambda._updateEventSources(
-        awsLambda,
+        lambdaClient,
         'functionName',
         lambdaMockSettings.listEventSourceMappings.EventSourceMappings,
         {}
@@ -1461,7 +1467,7 @@ describe('lib/main', function () {
       program.eventSourceFile = 'event_sources.json'
       const eventSourceList = lambda._eventSourceList(program)
       return lambda._updateEventSources(
-        awsLambda,
+        lambdaClient,
         'functionName',
         lambdaMockSettings.listEventSourceMappings.EventSourceMappings,
         eventSourceList.EventSourceMappings