Skip to content

Commit

Permalink
fix(platform-serverless): add support of REQUEST type lambda authorizer
Browse files Browse the repository at this point in the history
  • Loading branch information
Romakita committed Aug 7, 2024
1 parent cce5442 commit 92a90d9
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export class PlatformServerlessHandler {
}

private async flush($ctx: ServerlessContext<ServerlessEvent>) {
const body: unknown = $ctx.isHttpEvent() ? await this.makeHttpResponse($ctx) : $ctx.response.getBody();
const body: unknown = $ctx.isHttpEvent() && !$ctx.isAuthorizerEvent() ? await this.makeHttpResponse($ctx) : $ctx.response.getBody();

await this.injector.emit("$onResponse", $ctx);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ describe("ServerlessContext", () => {
expect(ctx.request.event).toEqual(ctx.response.event);
expect(ctx.request.response).toEqual(ctx.response);
expect(ctx.response.request).toEqual(ctx.request);
expect(ctx.isHttpEvent()).toBeTruthy();
expect(ctx.isAuthorizerEvent()).toBeFalsy();
});
it("should push callback with event headers", () => {
const ctx = createServerlessContext({
Expand Down Expand Up @@ -79,5 +81,25 @@ describe("ServerlessContext", () => {
expect(ctx.request.response).toEqual(ctx.response);
expect(ctx.response.request).toEqual(ctx.request);
});
it("should accept auth event (request)", () => {
const ctx = createServerlessContext({
event: {
type: "REQUEST",
httpMethod: "GET"
} as never,
endpoint: {} as any
});
expect(ctx.isHttpEvent()).toBeTruthy();
expect(ctx.isAuthorizerEvent()).toBeTruthy();
});
it("should accept auth event (token)", () => {
const ctx = createServerlessContext({
event: {
type: "TOKEN"
} as never,
endpoint: {} as any
});
expect(ctx.isAuthorizerEvent()).toBeTruthy();
});
});
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {DIContext, DIContextOptions} from "@tsed/di";
import {JsonEntityStore} from "@tsed/schema";
import {type APIGatewayProxyEvent, Context} from "aws-lambda";
import {type APIGatewayProxyEvent, APIGatewayTokenAuthorizerEvent, Context} from "aws-lambda";
import {ServerlessRequest} from "./ServerlessRequest.js";
import {ServerlessResponse} from "./ServerlessResponse.js";
import type {ServerlessResponseStream} from "./ServerlessResponseStream";
Expand Down Expand Up @@ -44,6 +44,10 @@ export class ServerlessContext<Event extends object = APIGatewayProxyEvent> exte
return "httpMethod" in this.event;
}

isAuthorizerEvent() {
return "type" in this.event && ["TOKEN", "REQUEST"].includes(this.event.type as string);
}

async destroy() {
await super.destroy();
this.response.destroy();
Expand Down

0 comments on commit 92a90d9

Please sign in to comment.