Skip to content

Commit 2b38e6f

Browse files
author
Brendan McKee
committed
fix: address PR feedback
1 parent ce74a94 commit 2b38e6f

File tree

6 files changed

+41
-35
lines changed

6 files changed

+41
-35
lines changed

src/api.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { aws_apigateway } from "aws-cdk-lib";
22
import { Construct } from "constructs";
3+
import { isParameterDecl } from ".";
34
import { FunctionDecl, isFunctionDecl } from "./declaration";
45
import { isErr } from "./error";
56
import {
@@ -100,6 +101,11 @@ export abstract class BaseApiIntegration {
100101
* inference.
101102
*/
102103
export class ApiIntegrations {
104+
/**
105+
* Identify subclasses as API integrations to the Functionless plugin
106+
*/
107+
public static readonly FunctionlessType = "ApiIntegrations";
108+
103109
/**
104110
* Create a {@link MockApiIntegration}.
105111
*/
@@ -309,7 +315,7 @@ export class AwsApiIntegration<
309315
// TODO: resource is not the right scope, prevents adding 2 methods to the resource
310316
// because of the IAM roles created
311317
// should `this` be a Method?
312-
const apiGwIntegration = integration!.apiGWVtl.makeIntegration(
318+
const apiGwIntegration = integration!.apiGWVtl.createIntegration(
313319
resource,
314320
requestTemplate,
315321
integrationResponses
@@ -415,12 +421,12 @@ export function toVTL(
415421
}
416422
})
417423
);
418-
} else if (isArgument(node)) {
419-
return inner(node);
424+
} else if (isArgument(node) && node.expr) {
425+
return inner(node.expr);
420426
} else if (isPropAccessExpr(node)) {
421427
// ignore the function param name, we'll replace it with the VTL
422428
// mapping template inputs
423-
const path = pathFromFunctionParameter(node)?.slice(1);
429+
const [_, ...path] = pathFromFunctionParameter(node) ?? [];
424430

425431
if (path) {
426432
if (location === "response") {
@@ -516,7 +522,7 @@ function validateFunctionDecl(a: any): FunctionDecl {
516522
function isFunctionParameter(node: FunctionlessNode): node is Identifier {
517523
if (!isIdentifier(node)) return false;
518524
const ref = node.lookup();
519-
return ref?.kind === "ParameterDecl" && ref.parent?.kind === "FunctionDecl";
525+
return isParameterDecl(ref) && isFunctionDecl(ref.parent);
520526
}
521527

522528
/**
@@ -551,7 +557,7 @@ export interface ApiGatewayVtlIntegration {
551557
/**
552558
* Construct an API GW integration.
553559
*/
554-
makeIntegration: (
560+
createIntegration: (
555561
scope: Construct,
556562
requestTemplate: string,
557563
responses: aws_apigateway.IntegrationResponse[]

src/checker.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as ts from "typescript";
22
import * as tsserver from "typescript/lib/tsserverlibrary";
3-
import { BaseApiIntegration } from "./api";
3+
import { ApiIntegrations, BaseApiIntegration } from "./api";
44
import { AppsyncResolver } from "./appsync";
55
import { EventBus, EventBusRule } from "./event-bridge";
66
import { EventBusTransform } from "./event-bridge/transform";
@@ -225,16 +225,17 @@ export function makeFunctionlessChecker(
225225
function isApiIntegrationsStaticMethod(
226226
node: ts.Node
227227
): node is ApiIntegrationsStaticMethodInterface {
228-
const x =
228+
return (
229229
ts.isCallExpression(node) &&
230230
ts.isPropertyAccessExpression(node.expression) &&
231231
(node.expression.name.text === "mock" ||
232232
node.expression.name.text == "aws") &&
233233
ts.isIdentifier(node.expression.expression) &&
234-
// TODO: is this enough? should we grab the type and make sure it
235-
// has FunctionlessKind?
236-
node.expression.expression.text === "ApiIntegrations";
237-
return x;
234+
isFunctionlessClassOfKind(
235+
node.expression.expression,
236+
ApiIntegrations.FunctionlessType
237+
)
238+
);
238239
}
239240

240241
function isApiIntegration(node: ts.Node): node is ApiIntegrationInterface {

src/compile.ts

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -582,26 +582,6 @@ export function compile(
582582
);
583583
}
584584

585-
function visitApiIntegrationMapperProp(
586-
prop: ts.PropertyAssignment
587-
): ts.PropertyAssignment {
588-
const { initializer } = prop;
589-
if (
590-
!ts.isFunctionExpression(initializer) &&
591-
!ts.isArrowFunction(initializer)
592-
) {
593-
throw new Error(
594-
`Expected mapping property of an ApiIntegration to be a function. Found ${initializer.getText()}.`
595-
);
596-
}
597-
598-
return ts.factory.updatePropertyAssignment(
599-
prop,
600-
prop.name,
601-
errorBoundary(() => toFunction("FunctionDecl", initializer))
602-
);
603-
}
604-
605585
function visitApiIntegrationResponsesProp(
606586
prop: ts.PropertyAssignment
607587
): ts.PropertyAssignment {
@@ -622,6 +602,25 @@ export function compile(
622602
);
623603
}
624604

605+
function visitApiIntegrationMapperProp(
606+
prop: ts.PropertyAssignment
607+
): ts.PropertyAssignment {
608+
const { initializer } = prop;
609+
const toFunc = errorBoundary(() => {
610+
if (
611+
!ts.isFunctionExpression(initializer) &&
612+
!ts.isArrowFunction(initializer)
613+
) {
614+
throw new Error(
615+
`Expected mapping property of an ApiIntegration to be a function. Found ${initializer.getText()}.`
616+
);
617+
}
618+
return toFunction("FunctionDecl", initializer);
619+
});
620+
621+
return ts.factory.updatePropertyAssignment(prop, prop.name, toFunc);
622+
}
623+
625624
function toExpr(
626625
node: ts.Node | undefined,
627626
scope: ts.Node

src/function.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ abstract class FunctionBase<P, O>
131131
this.apiGWVtl = {
132132
prepareRequest: (obj) => obj,
133133

134-
makeIntegration: (_scope, requestTemplate, integrationResponses) => {
134+
createIntegration: (_scope, requestTemplate, integrationResponses) => {
135135
return new aws_apigateway.LambdaIntegration(this.resource, {
136136
proxy: false,
137137
passthroughBehavior: aws_apigateway.PassthroughBehavior.NEVER,

src/step-function.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ abstract class BaseStepFunction<
519519
};
520520
},
521521

522-
makeIntegration: (scope, requestTemplate, integrationResponses) => {
522+
createIntegration: (scope, requestTemplate, integrationResponses) => {
523523
const credentialsRole = new aws_iam.Role(
524524
scope,
525525
"ApiGatewayIntegrationRole",

src/table.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ export class Table<
316316
};
317317
},
318318

319-
makeIntegration: (api, template, integrationResponses) => {
319+
createIntegration: (api, template, integrationResponses) => {
320320
const credentialsRole = new aws_iam.Role(
321321
api,
322322
"ApiGatewayIntegrationRole",

0 commit comments

Comments
 (0)