From b289d6241751c890791be95900afca648a1abe11 Mon Sep 17 00:00:00 2001 From: Wajeeh Zantout Date: Fri, 20 Sep 2019 15:44:16 +0300 Subject: [PATCH 1/3] chore: use async/await instead of callbacks --- .prettierrc | 4 ++-- src/__snapshots__/hello.test.js.snap | 19 +++---------------- src/hello-ts.ts | 8 ++------ src/hello.js | 7 ++----- src/hello.test.js | 8 +++----- src/types/index.ts | 0 src/types/lambda.ts | 15 --------------- src/utils/run-warm.ts | 9 ++++----- 8 files changed, 16 insertions(+), 54 deletions(-) delete mode 100644 src/types/index.ts delete mode 100644 src/types/lambda.ts diff --git a/.prettierrc b/.prettierrc index c72b091..c1a6f66 100644 --- a/.prettierrc +++ b/.prettierrc @@ -1,4 +1,4 @@ { - singleQuote: true, - trailingComma: "es5" + "singleQuote": true, + "trailingComma": "es5" } diff --git a/src/__snapshots__/hello.test.js.snap b/src/__snapshots__/hello.test.js.snap index d595cb6..d94b174 100644 --- a/src/__snapshots__/hello.test.js.snap +++ b/src/__snapshots__/hello.test.js.snap @@ -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, } `; diff --git a/src/hello-ts.ts b/src/hello-ts.ts index ff28857..2ebe00a 100644 --- a/src/hello-ts.ts +++ b/src/hello-ts.ts @@ -1,10 +1,6 @@ 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({ @@ -12,7 +8,7 @@ const helloTs: AWSLambda.Handler = ( input: event, }); - callback(null, response); + return 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 }); diff --git a/src/hello.js b/src/hello.js index 1e5aca2..7f0efa0 100644 --- a/src/hello.js +++ b/src/hello.js @@ -1,6 +1,6 @@ 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({ @@ -8,10 +8,7 @@ const hello = (event, context, callback) => { 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 diff --git a/src/hello.test.js b/src/hello.test.js index 5531af6..0936955 100644 --- a/src/hello.test.js +++ b/src/hello.test.js @@ -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(); }); }); diff --git a/src/types/index.ts b/src/types/index.ts deleted file mode 100644 index e69de29..0000000 diff --git a/src/types/lambda.ts b/src/types/lambda.ts deleted file mode 100644 index df35154..0000000 --- a/src/types/lambda.ts +++ /dev/null @@ -1,15 +0,0 @@ -export interface IEvent { - source: string; -} - -interface IJSON { - [key: string]: any; -} - -export type ICallback = (param1: any | null, response: IJSON | string) => void; - -export type ILambdaFunc = ( - event: IEvent, - context: {}, - callback: ICallback -) => void | ILambdaFunc; diff --git a/src/utils/run-warm.ts b/src/utils/run-warm.ts index 1c7a100..c220949 100644 --- a/src/utils/run-warm.ts +++ b/src/utils/run-warm.ts @@ -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; From 6ad65d67012714e4be44c406308bc08f5c0e7bc5 Mon Sep 17 00:00:00 2001 From: Wajeeh Zantout Date: Fri, 20 Sep 2019 16:14:01 +0300 Subject: [PATCH 2/3] docs: update readme file --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b0b16bd..8617f03 100644 --- a/README.md +++ b/README.md @@ -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/). --- From de9c0d84fe90aa0c711921057050e086a3399cdf Mon Sep 17 00:00:00 2001 From: Wajeeh Zantout Date: Mon, 23 Sep 2019 10:14:53 +0300 Subject: [PATCH 3/3] refactor: remove old comment --- src/hello-ts.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/hello-ts.ts b/src/hello-ts.ts index 2ebe00a..f4a3e57 100644 --- a/src/hello-ts.ts +++ b/src/hello-ts.ts @@ -9,9 +9,6 @@ const helloTs: Function = async (event: AWSLambda.APIGatewayEvent) => { }); return 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 }); }; // runWarm function handles pings from the scheduler so you don't