Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix dependencies, tests, and clean code #35

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
dist
dist
yarn.lock
69 changes: 36 additions & 33 deletions lib/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { expect } from "chai";
import { get } from "lodash";
import createEvent from "./index";

describe("creating a new SNS event", () => {
Expand All @@ -11,9 +12,9 @@ describe("creating a new SNS event", () => {
},
},
],
} as any);
expect(event.Records[0].Sns.Message).to.equal("trigger-email");
expect(event.Records[0].Sns.Type).to.equal("Notification");
});
expect(get(event, "Records[0].Sns.Message")).to.equal("trigger-email");
expect(get(event, "Records[0].Sns.Type")).to.equal("Notification");
});
});

Expand All @@ -27,10 +28,10 @@ describe("createSqsEvent()", () => {
}),
},
],
} as any);
});

expect(event.Records[0].body).to.equal("{\"foo\":\"bar\"}");
expect(event.Records[0].eventSource).to.equal("aws:sqs");
expect(get(event, "Records[0].body")).to.equal('{"foo":"bar"}');
expect(get(event, "Records[0].eventSource")).to.equal("aws:sqs");
});
});

Expand All @@ -41,7 +42,7 @@ describe("createApigEvent()", () => {
first_name: "Sam",
last_name: "Smith",
}),
} as any);
});
const parsedBody = JSON.parse(event.body || "");

expect(parsedBody.first_name).to.equal("Sam");
Expand All @@ -61,13 +62,13 @@ describe("createWebsocketEvent()", () => {
connectedAt: 123,
connectionId: "abc123",
},
} as any);
});
const parsedBody = JSON.parse(event.body || "");

expect(parsedBody.first_name).to.equal("Sam");
expect(parsedBody.last_name).to.equal("Smith");
expect(event.requestContext.connectedAt).to.equal(123);
expect(event.requestContext.connectionId).to.equal("abc123");
expect(get(event, "requestContext.connectedAt")).to.equal(123);
expect(get(event, "requestContext.connectionId")).to.equal("abc123");
});
});

Expand All @@ -86,11 +87,11 @@ describe("createS3Event()", () => {
},
},
],
} as any);
});

expect(event.Records[0].s3.bucket.name).to.equal("my-bucket-name");
expect(event.Records[0].s3.object.key).to.equal("object-key");
expect(event.Records[0].eventName).to.equal("ObjectCreated:Put");
expect(get(event, "Records[0].s3.bucket.name")).to.equal("my-bucket-name");
expect(get(event, "Records[0].s3.object.key")).to.equal("object-key");
expect(get(event, "Records[0].eventName")).to.equal("ObjectCreated:Put");
});

it("should return S3 mocked event without side-effect", () => {
Expand All @@ -107,7 +108,7 @@ describe("createS3Event()", () => {
},
},
],
} as any);
});

const event2 = createEvent("aws:s3", {
Records: [
Expand All @@ -122,20 +123,20 @@ describe("createS3Event()", () => {
},
},
],
} as any);
});

expect(event.Records[0].s3.bucket.name).to.equal("my-bucket-name");
expect(event.Records[0].s3.object.key).to.equal("object-key");
expect(event2.Records[0].s3.object.key).to.equal("object-key-2");
expect(event.Records[0].eventName).to.equal("ObjectCreated:Put");
expect(get(event, "Records[0].s3.bucket.name")).to.equal("my-bucket-name");
expect(get(event, "Records[0].s3.object.key")).to.equal("object-key");
expect(get(event2, "Records[0].s3.object.key")).to.equal("object-key-2");
expect(get(event, "Records[0].eventName")).to.equal("ObjectCreated:Put");
});
});

describe("createScheduledEvent()", () => {
it("should return Scheduled mocked event", () => {
const event = createEvent("aws:scheduled", {
region: "us-west-2",
} as any);
});

expect(event.region).to.equal("us-west-2");
expect(event["detail-type"]).to.equal("Scheduled Event");
Expand All @@ -152,10 +153,12 @@ describe("createKinesisEvent()", () => {
},
},
],
} as any);
});

expect(
Buffer.from(event.Records[0].kinesis.data, "base64").toString("ascii"),
Buffer.from(get(event, "Records[0].kinesis.data"), "base64").toString(
"ascii"
)
).to.equal("kinesis test");
});
});
Expand All @@ -164,8 +167,8 @@ describe("createCloudWatchEvent()", () => {
it("should return a valid event", () => {
const event = createEvent("aws:cloudWatch", {
"detail-type": "Something has been deleted.",
"region": "us-east-1",
} as any);
region: "us-east-1",
});
expect(event["detail-type"]).to.equal("Something has been deleted.");
expect(event.region).to.equal("us-east-1");
});
Expand All @@ -179,7 +182,7 @@ describe("createCloudWatchLogEvent()", () => {
},
}) as any;
expect(event.awslogs.data).to.equal(
"Some gzipped, then base64 encoded data",
"Some gzipped, then base64 encoded data"
);
});
});
Expand All @@ -197,9 +200,9 @@ describe("createAlexaSkillEvent()", () => {
},
},
},
} as any);
expect(event.request.type).to.equal("CanFulfillIntentRequest");
expect(event.context.System.device.deviceId).to.equal("myDevice");
});
expect(get(event, "request.type")).to.equal("CanFulfillIntentRequest");
expect(get(event, "context.System.device.deviceId")).to.equal("myDevice");
});
});

Expand All @@ -209,8 +212,8 @@ describe("createAlexaSmartHomeEvent()", () => {
payload: {
switchControlAction: "TURN_OFF",
},
} as any);
expect(event.payload.switchControlAction).to.equal("TURN_OFF");
});
expect(get(event, "payload.switchControlAction")).to.equal("TURN_OFF");
});
});

Expand All @@ -222,7 +225,7 @@ describe("createIotEvent()", () => {
be: "anything I want",
},
},
} as any);
});
expect(event.this.can.be).to.equal("anything I want");
});
});
Expand All @@ -231,7 +234,7 @@ describe("createCognitoPoolEvent()", () => {
it("should return a valid event", () => {
const event = createEvent("aws:cognitoUserPool", {
userName: "notAJ",
} as any);
});
expect(event.userName).to.eql("notAJ");
});
});
6 changes: 3 additions & 3 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { merge, cloneDeep, Dictionary } from "lodash";
import { merge, cloneDeep } from "lodash";
import {
APIGatewayEvent,
ScheduledEvent,
Expand Down Expand Up @@ -57,8 +57,8 @@ export const dictionary = {

export default function createEvent<T extends keyof typeof dictionary, B>(
eventType: T,
body: typeof dictionary[T]
): typeof dictionary[T] {
body: (typeof dictionary)[T]
): (typeof dictionary)[T] {
const event = dictionary[eventType];
let generatedEvent = {};
if (event) {
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
"author": "Serverless, Inc.",
"license": "Apache-2.0",
"devDependencies": {
"@types/aws-lambda": "^8.10.115",
"@types/chai": "^4.3.5",
"@types/aws-lambda": "8.10.24",
Copy link
Contributor

Choose a reason for hiding this comment

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

Why dependencies are downgraded?

Are those packages not respecting semver?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

typescript doesn't at the least. You were getting a build error because the versions are out of sync with each other.

If we think we need to have the newer version then we should make a v2 that uses latest packages and node

Copy link
Contributor

@medikoo medikoo May 18, 2023

Choose a reason for hiding this comment

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

I simply followed semever and updated packages in version range that should not introduce breaking changes.

Majority of packages on npm follow semver, so it was pretty natural move, that normally works. It's kind of surprising it isn't here.

Therefore if you're sure that some of the bumped packages do not follow semver please fix them to a specific version, but rest should be safe with ^ versioning

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@medikoo I have packages updated where it will build with the newest.

"@types/chai": "~4.1.7",
"@types/lodash": "4.14.123",
"@types/mocha": "^5.2.7",
"@types/node": "^11.15.54",
"aws-lambda": "^0.1.2",
Expand All @@ -26,10 +27,9 @@
"ts-mocha": "^6.0.0",
"ts-node": "^8.10.2",
"tslint": "^5.20.1",
"typescript": "^3.9.10"
"typescript": "~3.4.5"
},
"dependencies": {
"@types/lodash": "^4.14.194",
"lodash": "^4.17.21"
"lodash": "^4.17.11"
}
}
6 changes: 2 additions & 4 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
"esModuleInterop": true,
"resolveJsonModule": true
},
"typeRoots": [
"node_modules/@types",
"./types"
]
"exclude": ["node_modules", "dist"],
"include": ["lib"]
}