Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 58 additions & 32 deletions platform/src/components/aws/apigatewayv2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<apigatewayv2.Api>;
private apigDomain?: Output<apigatewayv2.DomainName>;
private apiMapping?: Output<apigatewayv2.ApiMapping>;
private logGroup: cloudwatch.LogGroup;
Expand All @@ -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();
Expand Down Expand Up @@ -757,25 +756,6 @@ export class ApiGatewayV2 extends Component implements Link.Linkable {
});
}

function normalizeCors() {
return output(args.cors).apply((cors) => {
if (cors === false) return {};
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't just returning undefined here do the trick?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you did this because typescript complains about pulumi, we can either do @ts-ignore or unwrap cors from Input in the type definition

i lean towards the latter

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simply returning undefined doesn't work, because it needs to be returned from inside the .apply(), since the input argument type is Input<boolean | Apiv2args...>. But returning undefined from inside the apply() results in a return type of Output<Apiv2CorsConfiguration | undefined>, which is not assignable to Input<Apiv2CorsConfiguration> | undefined.

What are the consequences of changing the cors argument type to boolean | Prettify<ApiGatewayV2CorsArgs>? The individual fields on ApiGatewayV2CorsArgs are still Input, so I am guessing it's fine? Still allows someone to pass in an Input for the specific fields?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would break on the very very unlikely scenario that someone is passing an output to cors

we should do the // @ts-ignore instead to be safe


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;
Expand Down Expand Up @@ -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() {
Expand Down
Loading