From 7e6bcf63ecbc1cf08b41883c00505b4801b5f659 Mon Sep 17 00:00:00 2001
From: Alice Sheather <alice@beforeyoubid.com.au>
Date: Fri, 16 Aug 2024 17:13:36 +1000
Subject: [PATCH] feat: added `lambdaRegion` option to config options this will
 let the consumer decide which region to invoke on the target function if
 trying to make a network request across regions, the user must previously
 pass the entire Lambda class in just to configure this one variable

---
 src/adapters/lambda-invocation.ts |  6 ++++++
 src/types.ts                      |  6 +++++-
 test/lambda-invocation.test.ts    | 21 +++++++++++++++++++++
 3 files changed, 32 insertions(+), 1 deletion(-)

diff --git a/src/adapters/lambda-invocation.ts b/src/adapters/lambda-invocation.ts
index 05e188b..f7903c9 100644
--- a/src/adapters/lambda-invocation.ts
+++ b/src/adapters/lambda-invocation.ts
@@ -13,9 +13,15 @@ import { AbortController } from '@aws-sdk/abort-controller';
 const lambdaInvocationAdapter: AlphaAdapter = async (config) => {
   const LambdaClass = config.Lambda || Lambda;
   const lambdaOptions: LambdaClientConfig = {
+    region: config.lambdaRegion,
     endpoint: config.lambdaEndpoint || process.env.LAMBDA_ENDPOINT,
   };
 
+  if (config.lambdaRegion) {
+    // Allow the region to be controlled via config
+    lambdaOptions.region = config.lambdaRegion;
+  }
+
   if (config.timeout) {
     // Set some low level HTTP client timeout options
     // so that the system level resources will be
diff --git a/src/types.ts b/src/types.ts
index 7c94db8..040e5a7 100644
--- a/src/types.ts
+++ b/src/types.ts
@@ -1,5 +1,5 @@
 import type { AxiosPromise, AxiosRequestConfig, AxiosResponse, InternalAxiosRequestConfig } from 'axios';
-import type { Lambda } from '@aws-sdk/client-lambda';
+import type { Lambda, LambdaClientConfig } from '@aws-sdk/client-lambda';
 import type { Context, Handler } from 'aws-lambda';
 import { SignatureV4CryptoInit, SignatureV4Init } from '@aws-sdk/signature-v4';
 
@@ -26,6 +26,10 @@ export interface AlphaOptions<D = any> extends AxiosRequestConfig<D> {
    * (Optional) The AWS endpoint to use when invoking the target Lambda function.
    */
   lambdaEndpoint?: string;
+  /**
+   * (Optional) The AWS region to use when invoking the target Lambda function.
+   */
+  lambdaRegion?: LambdaClientConfig['region'];
   Lambda?: typeof Lambda;
 }
 
diff --git a/test/lambda-invocation.test.ts b/test/lambda-invocation.test.ts
index 44f9a3e..7339946 100644
--- a/test/lambda-invocation.test.ts
+++ b/test/lambda-invocation.test.ts
@@ -524,3 +524,24 @@ test('lambdaEndpoint config option is provided to the Lambda client', async () =
 
   expect(FakeLambda).toHaveBeenCalledWith({ endpoint: 'http://test-endpoint' });
 });
+
+test('lambdaRegion config option is provided to the Lambda client', async () => {
+  const alpha = new Alpha('lambda://test-function', {
+    lambdaRegion: 'ap-southeast-2',
+    Lambda: FakeLambda,
+  });
+  createResponse(mockLambda, {
+    StatusCode: 200,
+    Payload: {
+      body: 'test',
+      statusCode: 200,
+    },
+  });
+
+  const response = await alpha.get('/test');
+
+  expect(response.data).toBe('test');
+  expect(response.status).toBe(200);
+
+  expect(FakeLambda).toHaveBeenCalledWith({ region: 'ap-southeast-2' });
+});