Skip to content

Commit 6464351

Browse files
authored
Merge pull request #2 from bennettsf/testing-branch
Adding API.MD, lambda options and types
2 parents 71052e0 + f2afc4d commit 6464351

File tree

4 files changed

+64
-9
lines changed

4 files changed

+64
-9
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/rest-api-construct/API.md

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

7+
import { Construct } from 'constructs';
8+
9+
// @public
10+
export class AmplifyConstruct extends Construct {
11+
constructor(scope: Construct, id: string, props?: ConstructCognitoProps);
12+
}
13+
14+
// @public (undocumented)
15+
export type ConstructCognitoProps = {
16+
includeQueue?: boolean;
17+
};
18+
719
// (No @packageDocumentation comment for this package)
820

921
```

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

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import { Construct } from 'constructs';
22
import * as lambda from 'aws-cdk-lib/aws-lambda';
33
import * as apigateway from 'aws-cdk-lib/aws-apigateway';
4-
import { RestApiConstructProps } from './types.js';
4+
import {
5+
ExistingDirectory,
6+
ExistingLambda,
7+
NewFromCode,
8+
NewFromTemplate,
9+
RestApiConstructProps,
10+
} from './types.js';
511

612
/**
713
* Rest API construct for Amplify Backend
@@ -14,12 +20,35 @@ export class RestApiConstruct extends Construct {
1420
constructor(scope: Construct, id: string, props: RestApiConstructProps) {
1521
super(scope, id);
1622

17-
// Create a new Lambda function for the API Gateway
18-
const handler = new lambda.Function(this, 'handler', {
19-
runtime: lambda.Runtime.NODEJS_18_X,
20-
handler: 'index.handler',
21-
code: lambda.Code.fromAsset(props.lambdaEntry),
22-
});
23+
let code: lambda.AssetCode | lambda.InlineCode = lambda.Code.fromInline('');
24+
const src = props.lambdaEntry.source;
25+
if ('path' in src) {
26+
const lamb = src as ExistingDirectory;
27+
code = lambda.Code.fromAsset(lamb.path);
28+
} else if ('code' in src) {
29+
const lamb = src as NewFromCode;
30+
code = lambda.Code.fromInline(lamb.code);
31+
} else if ('template' in src) {
32+
//TODO: Implement use of templates (which ones to support, and how - cli version is complex). The available templates depend on the runtime
33+
const lamb = src as NewFromTemplate;
34+
if (lamb.template === 'Hello World') {
35+
code = lambda.Code.fromInline(
36+
"function handler() {console.log('Hello World!');}",
37+
);
38+
}
39+
}
40+
41+
let handler: lambda.IFunction;
42+
if ('id' in src) {
43+
const lamb = src as ExistingLambda;
44+
handler = lambda.Function.fromFunctionName(this, lamb.id, lamb.name);
45+
} else {
46+
handler = new lambda.Function(this, 'handler', {
47+
runtime: props.lambdaEntry.runtime,
48+
handler: 'index.handler',
49+
code: code,
50+
});
51+
}
2352

2453
// Create a new API Gateway REST API with the specified name
2554
this.api = new apigateway.RestApi(this, 'RestApi', {

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,22 @@
1+
import * as lamb from 'aws-cdk-lib/aws-lambda';
2+
3+
//defines 4 potential sources for Lambda function
4+
export type ExistingDirectory = { path: string };
5+
export type ExistingLambda = { id: string; name: string };
6+
export type NewFromCode = { code: string };
7+
export type NewFromTemplate = { template: string };
8+
9+
//adds runtime to the source
10+
type LambdaSource = {
11+
runtime: lamb.Runtime;
12+
source: ExistingDirectory | ExistingLambda | NewFromCode | NewFromTemplate;
13+
};
14+
115
export type RestApiConstructProps = {
216
apiName: string;
317
path: string;
418
routes: HttpMethod[];
5-
lambdaEntry: string;
19+
lambdaEntry: LambdaSource;
620
};
721

822
export type HttpMethod =

0 commit comments

Comments
 (0)