Skip to content
Merged
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions .changeset/happy-moons-invent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@plutolang/pluto-infra": patch
"@plutolang/pluto": patch
---

feat(sdk): add CORS support for AWS ApiGateway
33 changes: 27 additions & 6 deletions packages/pluto-infra/src/aws/router.apigateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,31 +26,52 @@ export class ApiGatewayRouter
{
public readonly id: string;

public _url: pulumi.Output<string>;
public readonly _url: pulumi.Output<string>;

private apiGateway: Api;
private routes: Route[];
private readonly apiGateway: Api;
private readonly routes: Route[];

public outputs?: pulumi.Output<any>;
public readonly outputs?: pulumi.Output<any>;

constructor(name: string, opts?: RouterOptions) {
super("pluto:router:aws/ApiGateway", name, opts);
this.id = genResourceId(Router.fqn, name);

const enableCORS = opts?.cors ?? true;
this.apiGateway = new aws.apigatewayv2.Api(
genAwsResourceName(this.id, "api"),
{
name: genAwsResourceName(this.id, "api"),
protocolType: "HTTP",
corsConfiguration: enableCORS
? {
allowMethods: ["*"],
allowOrigins: ["*"],
allowHeaders: ["*"],
}
: undefined,
},
{ parent: this }
);

this.routes = [];

if (enableCORS) {
// If CORS is enabled, create a default route to handle preflight requests.
const route = new aws.apigatewayv2.Route(
genAwsResourceName(this.id, "default-route"),
{
apiId: this.apiGateway.id,
routeKey: "$default",
},
{ parent: this }
);
this.routes.push(route);
}

const region = currentAwsRegion();
this._url = pulumi.interpolate`https://${this.apiGateway.id}.execute-api.${region}.amazonaws.com/${DEFAULT_STAGE_NAME}`;
this.outputs = this._url;

this.routes = [];
}

public url(): string {
Expand Down
8 changes: 7 additions & 1 deletion packages/pluto/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@ export interface RequestHandler extends FnResource {
* The options for instantiating an infrastructure implementation class or a client implementation
* class.
*/
export interface RouterOptions {}
export interface RouterOptions {
/**
* Whether to enable CORS for the router. If true, the router will respond to preflight requests
* and include the appropriate CORS headers in responses. @default true
*/
cors?: boolean;
}

/**
* Define the access methods for Router that operate during runtime.
Expand Down