From d7995fb987de83dd3d68814fcfef011fb4a7ad30 Mon Sep 17 00:00:00 2001 From: Flavio Stutz Date: Thu, 11 Jan 2024 18:57:00 +0100 Subject: [PATCH] fix: eventType for Cloudwatch doesnt work --- Makefile | 5 +++-- package.json | 2 +- pnpm-lock.yaml | 2 +- src/lambda/lambda-base.test.ts | 13 ++++++++++++- src/lambda/lambda-base.ts | 4 ++-- src/lambda/types.ts | 4 ++-- tsconfig.json | 2 +- 7 files changed, 22 insertions(+), 10 deletions(-) diff --git a/Makefile b/Makefile index 654aa88..c66d5ec 100644 --- a/Makefile +++ b/Makefile @@ -2,11 +2,12 @@ SHELL := /bin/bash build: install rm -rf dist - pnpm exec esbuild src/index.ts --bundle --platform=node --minify --outfile=dist/index.js + @# TODO add --minify later + pnpm exec esbuild src/index.ts --bundle --platform=node --outfile=dist/index.js pnpm exec tsc --emitDeclarationOnly --outDir dist lint: - pnpm exec eslint . --ext .ts + pnpm exec eslint ./src --ext .ts pnpm exec tsc -noEmit --skipLibCheck pnpm audit diff --git a/package.json b/package.json index 919849f..63f553d 100644 --- a/package.json +++ b/package.json @@ -35,7 +35,7 @@ "@stoplight/spectral-cli": "^6.11.0", "@types/npm-which": "^3.0.3", "@types/tmp": "^0.2.6", - "aws-cdk-lib": "^2.118.0", + "aws-cdk-lib": "^2.117.0", "constructs": "^10.3.0", "fs": "0.0.1-security", "npm-which": "^3.0.1", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 8668845..8cc20bb 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -21,7 +21,7 @@ dependencies: specifier: ^0.2.6 version: 0.2.6 aws-cdk-lib: - specifier: ^2.118.0 + specifier: ^2.117.0 version: 2.118.0(constructs@10.3.0) constructs: specifier: ^10.3.0 diff --git a/src/lambda/lambda-base.test.ts b/src/lambda/lambda-base.test.ts index 918abae..63ee80a 100644 --- a/src/lambda/lambda-base.test.ts +++ b/src/lambda/lambda-base.test.ts @@ -126,7 +126,7 @@ describe('lambda-base', () => { const f = (): void => { // eslint-disable-next-line no-new - new BaseNodeJsFunction(stack, 'test-lambda', { + new BaseNodeJsFunction(stack, 'test-lambda1', { stage: 'dev', eventType: EventType.Http, baseCodePath: 'src/lambda/__tests__', @@ -142,6 +142,17 @@ describe('lambda-base', () => { expect(func.nodeJsFunction.isBoundToVpc).toBe(false); + const f2 = (): void => { + // eslint-disable-next-line no-new + new BaseNodeJsFunction(stack, 'test-lambda', { + stage: 'dev', + baseCodePath: 'src/lambda/__tests__', + }); + }; + expect(f2).toThrow('eventType is required if entry is not defined'); + + expect(func.nodeJsFunction.isBoundToVpc).toBe(false); + // execute synth and test results const template = Template.fromStack(stack); // console.log(JSON.stringify(template.toJSON(), null, 2)); diff --git a/src/lambda/lambda-base.ts b/src/lambda/lambda-base.ts index 41d5a24..123b44f 100644 --- a/src/lambda/lambda-base.ts +++ b/src/lambda/lambda-base.ts @@ -16,7 +16,7 @@ import { ServicePrincipal } from 'aws-cdk-lib/aws-iam'; import { vpcFromConfig } from '../utils'; -import { EventType, BaseNodeJsProps, LambdaConfig } from './types'; +import { BaseNodeJsProps, LambdaConfig } from './types'; // CDK L2 constructs // Docs: https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_lambda_nodejs.NodejsFunction.html#entry @@ -95,7 +95,7 @@ export const getPropsWithDefaults = ( if (!props.eventType) { throw new Error('eventType is required if entry is not defined'); } - const eventTypeStr = EventType[props.eventType].toLowerCase(); + const eventTypeStr = props.eventType.toLowerCase(); entry = `${props.baseCodePath || 'handlers'}/${eventTypeStr}/${id}/index.ts`; } diff --git a/src/lambda/types.ts b/src/lambda/types.ts index 0a28fa5..be77d33 100644 --- a/src/lambda/types.ts +++ b/src/lambda/types.ts @@ -36,8 +36,8 @@ export type LambdaConfig = NodejsFunctionProps & { }; export enum EventType { - Cloudwatch, - Http, + Cloudwatch = 'cloudwatch', + Http = 'http', } export type BaseNodeJsProps = LambdaConfig & { diff --git a/tsconfig.json b/tsconfig.json index 49993af..34160ee 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -4,6 +4,6 @@ "sourceMap": true, "declaration": true }, - "include": ["**/*"], + "include": ["src/**/*", ".eslintrc.js", "jest.config.js"], "exclude": ["node_modules", "**/*.spec.ts"] }