Skip to content

Commit

Permalink
Fix CORS issue with HTTP APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
t-richard committed Oct 10, 2021
1 parent e093eb5 commit 0923fa8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/constructs/aws/Upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,20 @@ export class Upload extends AwsConstruct {
httpApiId: Fn.ref(this.provider.naming.getHttpApiLogicalId()),
});

const lambdaProxyIntegration = new LambdaProxyIntegration({
handler: this.function,
});

this.route = new HttpRoute(this, "Route", {
httpApi: this.httpApi,
integration: new LambdaProxyIntegration({
handler: this.function,
}),
integration: lambdaProxyIntegration,
routeKey: HttpRouteKey.with("/upload-url", HttpMethod.POST),
});
this.route = new HttpRoute(this, "CORSRoute", {
httpApi: this.httpApi,
integration: lambdaProxyIntegration,
routeKey: HttpRouteKey.with("/upload-url", HttpMethod.OPTIONS),
});
}

if (resolvedConfiguration.apiGateway === "rest") {
Expand Down Expand Up @@ -153,6 +160,7 @@ const crypto = require("crypto");
const s3 = new AWS.S3();
exports.handler = async (event) => {
if (event.requestContext?.http?.method === 'OPTIONS') return "";
const body = JSON.parse(event.body);
const fileName = \`tmp/\${crypto.randomBytes(5).toString('hex')}-\${body.fileName}\`;
Expand Down
2 changes: 2 additions & 0 deletions test/unit/upload.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ describe("upload", () => {
"uploadRouteuploadRoute2545F0B8PermissionCB079AC2",
"uploadRouteHttpIntegration02104492e88c1940a1c8d0dbac532c8091C83E5A",
httpApiRoute,
"uploadCORSRouteuploadCORSRouteA2C80313PermissionEA9DCB1F",
"uploadCORSRouteC21947AF",
]);
});
it("should create all required resources with REST API", async () => {
Expand Down

0 comments on commit 0923fa8

Please sign in to comment.