File tree Expand file tree Collapse file tree 4 files changed +58
-0
lines changed Expand file tree Collapse file tree 4 files changed +58
-0
lines changed Original file line number Diff line number Diff line change 3838 " typescript"
3939 ],
4040 "devDependencies" : {
41+ "@aws-cdk/assert" : " ^1.70.0" ,
4142 "@aws-cdk/aws-lambda" : " ^1.70.0" ,
4243 "@aws-cdk/core" : " ^1.70.0" ,
4344 "@commitlint/cli" : " ^11.0.0" ,
Original file line number Diff line number Diff line change 11import * as lambda from '@aws-cdk/aws-lambda' ;
2+ import * as cdk from '@aws-cdk/core' ;
3+
4+ import { nodeMajorVersion } from './utils' ;
5+
6+ export interface NodejsFunctionProps extends lambda . FunctionOptions {
7+ /**
8+ * The name of the exported handler in the entry file.
9+ *
10+ * @default "handler"
11+ */
12+ readonly handler ?: string ;
13+ /**
14+ * The runtime environment. Only runtimes of the Node.js family are
15+ * supported.
16+ *
17+ * @default - `NODEJS_12_X` if `process.versions.node` >= '12.0.0',
18+ * `NODEJS_10_X` otherwise.
19+ */
20+ readonly runtime ?: lambda . Runtime ;
21+ }
222
323export class NodejsFunction extends lambda . Function {
24+ constructor ( scope : cdk . Construct , id : string , props : NodejsFunctionProps = { } ) {
25+ if ( props . runtime && props . runtime . family !== lambda . RuntimeFamily . NODEJS ) {
26+ throw new Error ( 'Only `NODEJS` runtimes are supported.' ) ;
27+ }
28+
29+ const handler = props . handler ?? 'handler' ;
30+ const defaultRunTime = nodeMajorVersion ( ) >= 12
31+ ? lambda . Runtime . NODEJS_12_X
32+ : lambda . Runtime . NODEJS_10_X ;
33+ const runtime = props . runtime ?? defaultRunTime ;
434
35+ super ( scope , id , {
36+ ...props ,
37+ runtime,
38+ code : lambda . Code . fromInline ( 'TODO' ) ,
39+ handler : `index.${ handler } ` ,
40+ } ) ;
41+ }
542}
Original file line number Diff line number Diff line change 1+ /**
2+ * Returns the major version of node installation
3+ */
4+ export function nodeMajorVersion ( ) : number {
5+ return parseInt ( process . versions . node . split ( '.' ) [ 0 ] , 10 ) ;
6+ }
Original file line number Diff line number Diff line change 1+ import '@aws-cdk/assert/jest' ;
2+ import { Stack } from '@aws-cdk/core' ;
3+ import { NodejsFunction } from '../src' ;
4+
5+ describe ( 'NodejsFunction tests' , ( ) => {
6+ it ( 'Should not class constructor be thrown' , async ( ) => {
7+ const stack = new Stack ( ) ;
8+ const factory = ( ) => new NodejsFunction ( stack , 'lambda-function' , { } ) ;
9+ expect ( factory ) . not . toThrow ( ) ;
10+ expect ( stack ) . toHaveResource ( 'AWS::Lambda::Function' , {
11+ Handler : 'index.handler' ,
12+ } ) ;
13+ } ) ;
14+ } ) ;
You can’t perform that action at this time.
0 commit comments