|
1 | 1 | import { Construct } from 'constructs';
|
2 | 2 | import * as lambda from 'aws-cdk-lib/aws-lambda';
|
3 |
| -import * as apigateway from 'aws-cdk-lib/aws-apigateway'; |
| 3 | +import * as apiGateway from 'aws-cdk-lib/aws-apigateway'; |
4 | 4 | import {
|
5 | 5 | ExistingDirectory,
|
6 | 6 | ExistingLambda,
|
7 | 7 | NewFromCode,
|
8 |
| - NewFromTemplate, |
| 8 | + // NewFromTemplate, |
9 | 9 | RestApiConstructProps,
|
10 | 10 | } from './types.js';
|
11 | 11 |
|
12 | 12 | /**
|
13 | 13 | * Rest API construct for Amplify Backend
|
14 | 14 | */
|
15 | 15 | export class RestApiConstruct extends Construct {
|
16 |
| - public readonly api: apigateway.RestApi; |
| 16 | + public readonly api: apiGateway.RestApi; |
17 | 17 | /**
|
18 | 18 | * Create a new RestApiConstruct
|
19 | 19 | */
|
20 | 20 | constructor(scope: Construct, id: string, props: RestApiConstructProps) {
|
21 | 21 | super(scope, id);
|
22 | 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 |
| - } |
52 |
| - |
53 | 23 | // Create a new API Gateway REST API with the specified name
|
54 |
| - this.api = new apigateway.RestApi(this, 'RestApi', { |
| 24 | + this.api = new apiGateway.RestApi(this, 'RestApi', { |
55 | 25 | restApiName: props.apiName,
|
56 | 26 | });
|
57 | 27 |
|
58 |
| - // Create a resource for the specified path |
59 |
| - const resource = this.addNestedResource(this.api.root, props.path); |
| 28 | + // Iterate over each path configuration |
| 29 | + for (const [index, pathConfig] of Object.entries(props.apiProps)) { |
| 30 | + const { path, routes, lambdaEntry } = pathConfig; |
| 31 | + const source = lambdaEntry.source; |
60 | 32 |
|
61 |
| - // Add methods to the resource for each HTTP method specified in props.routes |
62 |
| - for (const method of props.routes) { |
63 |
| - resource.addMethod(method, new apigateway.LambdaIntegration(handler)); |
| 33 | + // Determine Lambda code source |
| 34 | + let code: lambda.AssetCode | lambda.InlineCode = |
| 35 | + lambda.Code.fromInline(''); |
| 36 | + if ('path' in source) { |
| 37 | + const src = source as ExistingDirectory; |
| 38 | + code = lambda.Code.fromAsset(src.path); |
| 39 | + } else if ('code' in source) { |
| 40 | + const src = source as NewFromCode; |
| 41 | + code = lambda.Code.fromInline(src.code); |
| 42 | + } else if ('template' in source) { |
| 43 | + // const src = source as NewFromTemplate; |
| 44 | + // NOTE: You may expand supported templates later |
| 45 | + code = lambda.Code.fromInline( |
| 46 | + "exports.handler = () => { console.log('Hello World'); };", |
| 47 | + ); |
| 48 | + } else { |
| 49 | + // fallback to dummy if none matched — should never happen if typing is correct |
| 50 | + code = lambda.Code.fromInline('exports.handler = () => {};'); |
| 51 | + } |
| 52 | + |
| 53 | + // Create or reference Lambda function |
| 54 | + let handler: lambda.IFunction; |
| 55 | + if ('id' in source) { |
| 56 | + const src = source as ExistingLambda; |
| 57 | + handler = lambda.Function.fromFunctionName(this, src.id, src.name); |
| 58 | + } else { |
| 59 | + handler = new lambda.Function(this, `LambdaHandler-${index}`, { |
| 60 | + runtime: lambdaEntry.runtime, |
| 61 | + handler: 'index.handler', |
| 62 | + code, |
| 63 | + }); |
| 64 | + } |
| 65 | + |
| 66 | + // Add resource and methods for this route |
| 67 | + const resource = this.addNestedResource(this.api.root, path); |
| 68 | + for (const method of routes) { |
| 69 | + resource.addMethod(method, new apiGateway.LambdaIntegration(handler)); |
| 70 | + } |
64 | 71 | }
|
65 | 72 | }
|
66 | 73 |
|
67 | 74 | /**
|
68 | 75 | * Adds nested resources to the API based on the provided path.
|
69 | 76 | */
|
70 | 77 | private addNestedResource(
|
71 |
| - root: apigateway.IResource, |
| 78 | + root: apiGateway.IResource, |
72 | 79 | path: string,
|
73 |
| - ): apigateway.IResource { |
| 80 | + ): apiGateway.IResource { |
74 | 81 | return path.split('/').reduce((resource, part) => {
|
75 | 82 | return resource.getResource(part) ?? resource.addResource(part);
|
76 | 83 | }, root);
|
|
0 commit comments