Repository: swarmion/swarmion
Pull request #320 (2022-10-26 13:14:00)

feat(serverless-helpers): add getCdkhandlerPath
We currently have a `getHandlerPath` that works for the Serverless Framework. However the CDK requires the file to have the `ts` extension and not the handler name.

This PR adds this feature.

It may be used as such:
```
const myLambda = new NodejsFunction(this, 'Lambda', {
  entry: getCdkHandlerPath(__dirname),
  handler: 'main',
  runtime: Runtime.NODEJS_16_X,
  architecture: Architecture.ARM_64,
  awsSdkConnectionReuse: true,
  bundling: sharedEsbuildConfig,
  environment: {
    ...
  },
});
```


Patch:
diff --git a/.all-contributorsrc b/.all-contributorsrc
index e0ec8538..f192927a 100644
--- a/.all-contributorsrc
+++ b/.all-contributorsrc
@@ -183,8 +183,7 @@
       "avatar_url": "https://avatars.githubusercontent.com/u/16920060?v=4",
       "profile": "https://github.com/Yoctoboy",
       "contributions": [
-        "doc",
-        "code"
+        "doc"
       ]
     },
     {
diff --git a/README.md b/README.md
index 9b3f6ee8..f6e9bb09 100644
--- a/README.md
+++ b/README.md
@@ -54,7 +54,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
       <td align="center"><a href="https://github.com/Paulmolin"><img src="https://avatars.githubusercontent.com/u/5166068?v=4?s=100" width="100px;" alt="Paul Molin"/><br /><sub><b>Paul Molin</b></sub></a><br /><a href="https://github.com/swarmion/swarmion/commits?author=Paulmolin" title="Code">💻</a></td>
       <td align="center"><a href="https://github.com/Thomasprelot"><img src="https://avatars.githubusercontent.com/u/56639487?v=4?s=100" width="100px;" alt="Thomas Prelot"/><br /><sub><b>Thomas Prelot</b></sub></a><br /><a href="https://github.com/swarmion/swarmion/commits?author=Thomasprelot" title="Code">💻</a></td>
       <td align="center"><a href="https://github.com/art049"><img src="https://avatars.githubusercontent.com/u/15641577?v=4?s=100" width="100px;" alt="Arthur Pastel"/><br /><sub><b>Arthur Pastel</b></sub></a><br /><a href="https://github.com/swarmion/swarmion/commits?author=art049" title="Documentation">📖</a></td>
-      <td align="center"><a href="https://github.com/Yoctoboy"><img src="https://avatars.githubusercontent.com/u/16920060?v=4?s=100" width="100px;" alt="Alexis Reymann"/><br /><sub><b>Alexis Reymann</b></sub></a><br /><a href="https://github.com/swarmion/swarmion/commits?author=Yoctoboy" title="Documentation">📖</a> <a href="https://github.com/swarmion/swarmion/commits?author=Yoctoboy" title="Code">💻</a></td>
+      <td align="center"><a href="https://github.com/Yoctoboy"><img src="https://avatars.githubusercontent.com/u/16920060?v=4?s=100" width="100px;" alt="Alexis Reymann"/><br /><sub><b>Alexis Reymann</b></sub></a><br /><a href="https://github.com/swarmion/swarmion/commits?author=Yoctoboy" title="Documentation">📖</a></td>
       <td align="center"><a href="https://github.com/bchangal"><img src="https://avatars.githubusercontent.com/u/10755384?v=4?s=100" width="100px;" alt="Carton"/><br /><sub><b>Carton</b></sub></a><br /><a href="https://github.com/swarmion/swarmion/commits?author=bchangal" title="Code">💻</a></td>
     </tr>
   </tbody>
diff --git a/examples/swarmion-full-stack/frontend/app/src/theme/muiTheme.ts b/examples/swarmion-full-stack/frontend/app/src/theme/muiTheme.ts
index e100a0d5..315abf42 100644
--- a/examples/swarmion-full-stack/frontend/app/src/theme/muiTheme.ts
+++ b/examples/swarmion-full-stack/frontend/app/src/theme/muiTheme.ts
@@ -1,5 +1,5 @@
 import '@mui/lab/themeAugmentation';
-import { createTheme, ThemeOptions } from '@mui/material';
+import { createTheme, ThemeOptions } from '@mui/material/styles';
 
 import { UNIT } from './helpers';
 import { MuiButtonOverrides } from './overrides';
diff --git a/packages/serverless-helpers/src/helpers/applyHttpMiddlewares.ts b/packages/serverless-helpers/src/helpers/applyHttpMiddlewares.ts
index 64210ffa..80574c04 100644
--- a/packages/serverless-helpers/src/helpers/applyHttpMiddlewares.ts
+++ b/packages/serverless-helpers/src/helpers/applyHttpMiddlewares.ts
@@ -10,7 +10,7 @@ interface Options {
   outputSchema?: JSONSchema;
 }
 
-const applyHttpMiddlewares = <Event, Result>(
+export const applyHttpMiddlewares = <Event, Result>(
   handler: Handler<Event, Result>,
   { inputSchema, outputSchema }: Options,
 ): middy.MiddyfiedHandler<Event, Result> => {
@@ -23,5 +23,3 @@ const applyHttpMiddlewares = <Event, Result>(
 
   return middyfiedHandler;
 };
-
-export default applyHttpMiddlewares;
diff --git a/packages/serverless-helpers/src/helpers/getCdkHandlerPath.ts b/packages/serverless-helpers/src/helpers/getCdkHandlerPath.ts
new file mode 100644
index 00000000..66b27305
--- /dev/null
+++ b/packages/serverless-helpers/src/helpers/getCdkHandlerPath.ts
@@ -0,0 +1,6 @@
+/** Helper to be used in CDK config.ts files to retrieve the path of the handler. */
+export const getCdkHandlerPath = (directoryPath: string): string => {
+  const processRunLocation = process.cwd();
+
+  return directoryPath.replace(processRunLocation + '/', '') + '/handler.ts';
+};
diff --git a/packages/serverless-helpers/src/helpers/getEnvVariable.ts b/packages/serverless-helpers/src/helpers/getEnvVariable.ts
index f4311e69..57102bd9 100644
--- a/packages/serverless-helpers/src/helpers/getEnvVariable.ts
+++ b/packages/serverless-helpers/src/helpers/getEnvVariable.ts
@@ -1,9 +1,7 @@
-const getEnvVariable = (name: string): string => {
+export const getEnvVariable = (name: string): string => {
   const variable = process.env[name];
   if (variable === undefined)
     throw new Error(`Environment variable not found: ${name}`);
 
   return variable;
 };
-
-export default getEnvVariable;
diff --git a/packages/serverless-helpers/src/helpers/getHandlerPath.ts b/packages/serverless-helpers/src/helpers/getHandlerPath.ts
index 9344e093..454040d2 100644
--- a/packages/serverless-helpers/src/helpers/getHandlerPath.ts
+++ b/packages/serverless-helpers/src/helpers/getHandlerPath.ts
@@ -1,8 +1,6 @@
 /** Helper to be used in config.ts files to retrieve the path of the handler. */
-const getHandlerPath = (directoryPath: string): string => {
+export const getHandlerPath = (directoryPath: string): string => {
   const processRunLocation = process.cwd();
 
   return directoryPath.replace(processRunLocation + '/', '') + '/handler.main';
 };
-
-export default getHandlerPath;
diff --git a/packages/serverless-helpers/src/helpers/index.ts b/packages/serverless-helpers/src/helpers/index.ts
index df4f9f49..afe28101 100644
--- a/packages/serverless-helpers/src/helpers/index.ts
+++ b/packages/serverless-helpers/src/helpers/index.ts
@@ -1,5 +1,6 @@
-export { default as applyHttpMiddlewares } from './applyHttpMiddlewares';
-export { default as getEnvVariable } from './getEnvVariable';
-export { default as getHandlerPath } from './getHandlerPath';
-export { default as testFunctionNames } from './testFunctionNames';
-export { default as mergeStageParams } from './mergeStageParams';
+export { applyHttpMiddlewares } from './applyHttpMiddlewares';
+export { getEnvVariable } from './getEnvVariable';
+export { getHandlerPath } from './getHandlerPath';
+export { testFunctionNames } from './testFunctionNames';
+export { mergeStageParams } from './mergeStageParams';
+export { getCdkHandlerPath } from './getCdkHandlerPath';
diff --git a/packages/serverless-helpers/src/helpers/mergeStageParams.ts b/packages/serverless-helpers/src/helpers/mergeStageParams.ts
index 3b535860..4cc9e6ff 100644
--- a/packages/serverless-helpers/src/helpers/mergeStageParams.ts
+++ b/packages/serverless-helpers/src/helpers/mergeStageParams.ts
@@ -1,4 +1,4 @@
-const mergeStageParams = <
+export const mergeStageParams = <
   Stage extends string,
   CommonParamKeys extends string,
   ServiceParamKeys extends string,
@@ -16,5 +16,3 @@ const mergeStageParams = <
     {} as Record<Stage, Record<CommonParamKeys & ServiceParamKeys, unknown>>,
   );
 };
-
-export default mergeStageParams;


Test patch:
diff --git a/packages/serverless-helpers/src/helpers/__tests__/mergeStageParams.test.ts b/packages/serverless-helpers/src/helpers/__tests__/mergeStageParams.test.ts
index 29893c2c..34627c04 100644
--- a/packages/serverless-helpers/src/helpers/__tests__/mergeStageParams.test.ts
+++ b/packages/serverless-helpers/src/helpers/__tests__/mergeStageParams.test.ts
@@ -1,4 +1,4 @@
-import mergeStageParams from '../mergeStageParams';
+import { mergeStageParams } from '../mergeStageParams';
 
 describe('mergeStageParams', () => {
   it('should merge single stage params', () => {
diff --git a/packages/serverless-helpers/src/helpers/testFunctionNames.ts b/packages/serverless-helpers/src/helpers/testFunctionNames.ts
index 841670d8..2dbc1ed4 100644
--- a/packages/serverless-helpers/src/helpers/testFunctionNames.ts
+++ b/packages/serverless-helpers/src/helpers/testFunctionNames.ts
@@ -17,7 +17,7 @@ const getFullFunctionName = (config: AWS, functionName: string): string =>
  *
  * @param config serverless configuration object
  */
-const testFunctionNames = (config: AWS): void => {
+export const testFunctionNames = (config: AWS): void => {
   const functionNames = getFunctionNames(config);
   if (functionNames.length === 0) {
     it('has no functions declared', () => {
@@ -38,5 +38,3 @@ const testFunctionNames = (config: AWS): void => {
     },
   );
 };
-
-export default testFunctionNames;