Skip to content
Open
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
4 changes: 4 additions & 0 deletions lib/events/aws/api-gateway-event-template.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
"accountId": "123456789012",
"resourceId": "us4z18",
"stage": "test",
"authorizer": {},
"protocol": "",
"requestId": "41b45ea3-70b5-11e6-b7bd-69b5aaebc7d9",
"identity": {
"accessKey": "",
Expand All @@ -38,8 +40,10 @@
"accountId": "",
"cognitoIdentityId": "",
"caller": "",
"clientCert": null,
"apiKey": "",
"sourceIp": "192.168.100.1",
"principalOrgId": "o-hefegcwbcibwue",
"cognitoAuthenticationType": "",
"cognitoAuthenticationProvider": "",
"userArn": "",
Expand Down
20 changes: 20 additions & 0 deletions lib/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,26 @@ describe("createApigEvent()", () => {
});
});

it("should return APIG mocked event with custom authorizer", () => {
const event = createEvent("aws:apiGateway", {
requestContext: {
authorizer: {
user_info: JSON.stringify({
id: 1234,
name: "Sam Smith",
}),
},
},
} as any);

expect(!!event.requestContext.authorizer).to.be.true;

const authorizer = event.requestContext.authorizer || {};
const parsedAuthorizer = JSON.parse(authorizer.user_info || "");
expect(parsedAuthorizer.id).to.eql(1234);
expect(parsedAuthorizer.name).to.eql("Sam Smith");
});

describe("createWebsocketEvent()", () => {
it("should return websocket mocked event", () => {
const event = createEvent("aws:websocket", {
Expand Down
5 changes: 3 additions & 2 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,11 @@ export const dictionary = {
"aws:cognitoUserPool": cognitoUserPoolEventTemplate as CognitoUserPoolEvent,
"aws:websocket": apiGatewayTemplate as APIGatewayEvent, // Websockets are included in APIG typedef: https://github.com/DefinitelyTyped/DefinitelyTyped/pull/32855/files
};

// https://typeofnan.dev/creating-your-own-deeppartial-type-in-typescript/
type DeepPartial<T> = Partial<{ [P in keyof T]: DeepPartial<T[P]> }>;
export default function createEvent<T extends keyof typeof dictionary, B>(
eventType: T,
body: typeof dictionary[T],
body: DeepPartial<typeof dictionary[T]>,
): typeof dictionary[T] {
const event = dictionary[eventType];
let generatedEvent = {};
Expand Down
Loading