Skip to content

Commit cef99a7

Browse files
authored
Merge pull request #5 from bennettsf/lambda-handling
Lambda handling, API.md updates
2 parents e9c2a81 + c9af6b7 commit cef99a7

File tree

6 files changed

+107
-3
lines changed

6 files changed

+107
-3
lines changed

packages/rest-api-construct/API.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,55 @@
44
55
```ts
66

7+
import * as apiGateway from 'aws-cdk-lib/aws-apigateway';
8+
import { Construct } from 'constructs';
9+
import * as lamb from 'aws-cdk-lib/aws-lambda';
10+
11+
// @public (undocumented)
12+
export type ExistingDirectory = {
13+
path: string;
14+
};
15+
16+
// @public (undocumented)
17+
export type ExistingLambda = {
18+
id: string;
19+
name: string;
20+
};
21+
22+
// @public (undocumented)
23+
export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD' | 'OPTIONS';
24+
25+
// @public (undocumented)
26+
export type LambdaSource = {
27+
runtime: lamb.Runtime;
28+
source: ExistingDirectory | ExistingLambda | NewFromCode;
29+
};
30+
31+
// @public (undocumented)
32+
export type NewFromCode = {
33+
code: string;
34+
};
35+
36+
// @public
37+
export class RestApiConstruct extends Construct {
38+
constructor(scope: Construct, id: string, props: RestApiConstructProps);
39+
// (undocumented)
40+
readonly api: apiGateway.RestApi;
41+
}
42+
43+
// @public (undocumented)
44+
export type RestApiConstructProps = {
45+
apiName: string;
46+
apiProps: RestApiPathConfig[];
47+
};
48+
49+
// @public (undocumented)
50+
export type RestApiPathConfig = {
51+
path: string;
52+
routes: HttpMethod[];
53+
lambdaEntry: LambdaSource;
54+
};
55+
756
// (No @packageDocumentation comment for this package)
857

958
```

packages/rest-api-construct/src/construct.test.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,54 @@ import { App, Stack } from 'aws-cdk-lib';
44
import { Template } from 'aws-cdk-lib/assertions';
55
import * as lambda from 'aws-cdk-lib/aws-lambda';
66

7+
void describe('RestApiConstruct Lambda Handling', () => {
8+
void it('loads an existing local lambda function if the directory is specified', () => {
9+
const app = new App();
10+
const stack = new Stack(app);
11+
new RestApiConstruct(stack, 'RestApiLambdaTest', {
12+
apiName: 'RestApiLambdaTest',
13+
apiProps: [
14+
{
15+
path: 'items',
16+
routes: ['GET'],
17+
lambdaEntry: {
18+
runtime: lambda.Runtime.NODEJS_18_X,
19+
source: { path: 'packages/rest-api-construct/lib/test-assets' },
20+
},
21+
},
22+
],
23+
});
24+
const template = Template.fromStack(stack);
25+
template.hasResourceProperties('AWS::Lambda::Function', {
26+
Handler: 'index.handler',
27+
});
28+
});
29+
30+
void it('creates a new lambda function if the code is specified', () => {
31+
const app = new App();
32+
const stack = new Stack(app);
33+
new RestApiConstruct(stack, 'RestApiNewLambdaTest', {
34+
apiName: 'RestApiNewLambda',
35+
apiProps: [
36+
{
37+
path: 'items',
38+
routes: ['GET'],
39+
lambdaEntry: {
40+
runtime: lambda.Runtime.NODEJS_22_X,
41+
source: {
42+
code: "export const handler = () => {return 'Hello World! This is a new lambda function.';};",
43+
},
44+
},
45+
},
46+
],
47+
});
48+
const template = Template.fromStack(stack);
49+
template.hasResourceProperties('AWS::Lambda::Function', {
50+
Handler: 'index.handler',
51+
});
52+
});
53+
});
54+
755
void describe('RestApiConstruct', () => {
856
void it('creates a queue if specified', () => {
957
const app = new App();

packages/rest-api-construct/src/construct.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ export class RestApiConstruct extends Construct {
3030
const source = lambdaEntry.source;
3131

3232
// Determine Lambda code source - either ExistingDirectory, NewFromCode, or ExistingLambda (function already exists in aws and does not need to be constructed)
33-
let code: lambda.AssetCode | lambda.InlineCode =
34-
lambda.Code.fromInline('');
33+
let code!: lambda.AssetCode | lambda.InlineCode;
3534
if ('path' in source) {
3635
const src = source as ExistingDirectory;
3736
code = lambda.Code.fromAsset(src.path);
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
export * from './construct.js';
2+
export * from './types.js';
3+
export { RestApiPathConfig, LambdaSource } from './types.js';
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/**
2+
* Hello world lambda used for testing providing a file for lambda for a rest api path
3+
*/
4+
export const handler = () => {
5+
return 'Hello World! This is a lambda function.';
6+
};

packages/rest-api-construct/src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export type ExistingLambda = { id: string; name: string };
66
export type NewFromCode = { code: string };
77

88
//adds runtime to the source
9-
type LambdaSource = {
9+
export type LambdaSource = {
1010
runtime: lamb.Runtime;
1111
source: ExistingDirectory | ExistingLambda | NewFromCode;
1212
};

0 commit comments

Comments
 (0)