diff --git a/platform/src/components/aws/apigatewayv2.ts b/platform/src/components/aws/apigatewayv2.ts index 4951a93268..39407bf4b6 100644 --- a/platform/src/components/aws/apigatewayv2.ts +++ b/platform/src/components/aws/apigatewayv2.ts @@ -675,7 +675,7 @@ export class ApiGatewayV2 extends Component implements Link.Linkable { private constructorName: string; private constructorArgs: ApiGatewayV2Args; private constructorOpts: ComponentResourceOptions; - private api: apigatewayv2.Api; + private api: Output; private apigDomain?: Output; private apiMapping?: Output; private logGroup: cloudwatch.LogGroup; @@ -692,7 +692,6 @@ export class ApiGatewayV2 extends Component implements Link.Linkable { const accessLog = normalizeAccessLog(); const domain = normalizeDomain(); - const cors = normalizeCors(); const vpc = normalizeVpc(); const vpcLink = createVpcLink(); @@ -757,25 +756,6 @@ export class ApiGatewayV2 extends Component implements Link.Linkable { }); } - function normalizeCors() { - return output(args.cors).apply((cors) => { - if (cors === false) return {}; - - const defaultCors: types.input.apigatewayv2.ApiCorsConfiguration = { - allowHeaders: ["*"], - allowMethods: ["*"], - allowOrigins: ["*"], - }; - return cors === true || cors === undefined - ? defaultCors - : { - ...defaultCors, - ...cors, - maxAge: cors.maxAge && toSeconds(cors.maxAge), - }; - }); - } - function normalizeVpc() { // "vpc" is undefined if (!args.vpc) return; @@ -809,17 +789,63 @@ export class ApiGatewayV2 extends Component implements Link.Linkable { } function createApi() { - return new apigatewayv2.Api( - ...transform( - args.transform?.api, - `${name}Api`, - { - protocolType: "HTTP", - corsConfiguration: cors, - }, - { parent }, - ), - ); + return output(args.cors).apply((cors) => { + const defaultCors: types.input.apigatewayv2.ApiCorsConfiguration = { + allowHeaders: ["*"], + allowMethods: ["*"], + allowOrigins: ["*"], + }; + + if (cors === false) { + return output(undefined).apply( + () => + new apigatewayv2.Api( + ...transform( + args.transform?.api, + `${name}Api`, + { protocolType: "HTTP" }, + { parent }, + ), + ), + ); + } + + if (cors === true || cors === undefined) { + return output(undefined).apply( + () => + new apigatewayv2.Api( + ...transform( + args.transform?.api, + `${name}Api`, + { + protocolType: "HTTP", + corsConfiguration: defaultCors, + }, + { parent }, + ), + ), + ); + } + + const { maxAge: maxAgeInput, ...corsRest } = cors; + return output(maxAgeInput).apply((maxAge) => + new apigatewayv2.Api( + ...transform( + args.transform?.api, + `${name}Api`, + { + protocolType: "HTTP", + corsConfiguration: { + ...defaultCors, + ...corsRest, + maxAge: maxAge && toSeconds(maxAge), + }, + }, + { parent }, + ), + ), + ); + }); } function createLogGroup() {