Skip to content
This repository was archived by the owner on Jul 11, 2024. It is now read-only.

chore: use async syntax instead of callbacks #80

Merged
merged 3 commits into from
Sep 23, 2019
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
4 changes: 2 additions & 2 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
singleQuote: true,
trailingComma: "es5"
"singleQuote": true,
"trailingComma": "es5"
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Ignoring the scheduling event, you can see here that we're setting up a function

#### 2. Create your function

This starter kit's Hello World function (which you will of course get rid of) can be found at [`./src/hello.js`](./src/hello.js). There you can see a basic function that's intended to work in conjunction with API Gateway (i.e., it is web-accessible). Like most Serverless functions, the `hello` function accepts an event, context, and callback. When your function is completed, you execute the callback with your response. (This is all basic Serverless; if you've never used it, be sure to read through [their docs](https://serverless.com/framework/docs/).
This starter kit's Hello World function (which you will of course get rid of) can be found at [`./src/hello.js`](./src/hello.js). There you can see a basic function that's intended to work in conjunction with API Gateway (i.e., it is web-accessible). Like most Serverless functions, the `hello` function is asynchronous and accepts an event & context. (This is all basic Serverless; if you've never used it, be sure to read through [their docs](https://serverless.com/framework/docs/).

---

Expand Down
19 changes: 3 additions & 16 deletions src/__snapshots__/hello.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,21 +1,8 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`hello executes as expected 1`] = `
[MockFunction] {
"calls": Array [
Array [
null,
Object {
"body": "{\\"message\\":\\"Go Serverless! Your function executed successfully!\\",\\"input\\":{}}",
"statusCode": 200,
},
],
],
"results": Array [
Object {
"type": "return",
"value": undefined,
},
],
Object {
"body": "{\\"message\\":\\"Go Serverless! Your function executed successfully!\\",\\"input\\":{}}",
"statusCode": 200,
}
`;
11 changes: 2 additions & 9 deletions src/hello-ts.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
import { successResponse, runWarm } from './utils';

const helloTs: AWSLambda.Handler = (
event: AWSLambda.APIGatewayEvent,
_context,
callback
) => {
const helloTs: Function = async (event: AWSLambda.APIGatewayEvent) => {
// successResponse handles wrapping the response in an API Gateway friendly
// format (see other responses, including CORS, in `./utils/lambda-response.js)
const response = successResponse({
message: 'Go Serverless! Your function executed successfully!',
input: event,
});

callback(null, response);

// Use this code if you don't use the http event with the LAMBDA-PROXY integration
// callback(null, { message: 'Go Serverless v1.0! Your function executed successfully!', event });
return response;
};

// runWarm function handles pings from the scheduler so you don't
Expand Down
7 changes: 2 additions & 5 deletions src/hello.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import { successResponse, runWarm } from './utils';

const hello = (event, context, callback) => {
const hello = async event => {
// successResponse handles wrapping the response in an API Gateway friendly
// format (see other responses, including CORS, in `./utils/lambda-response.js)
const response = successResponse({
message: 'Go Serverless! Your function executed successfully!',
input: event,
});

callback(null, response);

// Use this code if you don't use the http event with the LAMBDA-PROXY integration
// callback(null, { message: 'Go Serverless v1.0! Your function executed successfully!', event });
return response;
};

// runWarm function handles pings from the scheduler so you don't
Expand Down
8 changes: 3 additions & 5 deletions src/hello.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import hello from './hello';

describe('hello', () => {
it('executes as expected', () => {
const cb = jest.fn();
hello({}, {}, cb);
expect(cb).toBeCalled();
expect(cb).toMatchSnapshot();
it('executes as expected', async () => {
const response = await hello({});
expect(response).toMatchSnapshot();
});
});
Empty file removed src/types/index.ts
Empty file.
15 changes: 0 additions & 15 deletions src/types/lambda.ts

This file was deleted.

9 changes: 4 additions & 5 deletions src/utils/run-warm.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
const runWarm = (lambdaFunc: AWSLambda.Handler): AWSLambda.Handler => (
const runWarm = (lambdaFunc: Function): AWSLambda.Handler => async (
event,
context,
callback
context
) => {
// Detect the keep-alive ping from CloudWatch and exit early. This keeps our
// lambda function running hot.
if (event.source === 'serverless-plugin-warmup') {
return callback(null, 'pinged');
return 'pinged';
}

return lambdaFunc(event, context, callback);
return lambdaFunc(event, context);
};

export default runWarm;