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

Commit 23345af

Browse files
refactor: codebase cleanup (#90)
* chore: update node runtime version * refactor: leave typescript function only * chore: update circleci node version * refactor: remove unnecessary packages * docs: update readme * fix: change function type and return type under run-warm * fix: add context and event under test * fix: add line at hello handler Co-authored-by: peter bardawil <[email protected]>
1 parent b770c1b commit 23345af

20 files changed

+1006
-2036
lines changed

.babelrc

Lines changed: 0 additions & 12 deletions
This file was deleted.

.circleci/config.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ version: 2
22
jobs:
33
build:
44
docker:
5-
- image: circleci/node:10.16
5+
- image: circleci/node:12.13.1
66
steps:
77
- checkout
88
- run: yarn install
99
- run: yarn build
1010
test:
1111
docker:
12-
- image: circleci/node:10.16
12+
- image: circleci/node:12.13.1
1313
steps:
1414
- checkout
1515
- run: yarn install

.eslintrc.js

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,10 @@
11
module.exports = {
22
parser: '@typescript-eslint/parser',
3-
plugins: ['@typescript-eslint'],
4-
extends: ['prettier', 'airbnb'],
3+
extends: ['prettier', 'plugin:@typescript-eslint/recommended'],
54
parserOptions: {
65
ecmaVersion: 6,
76
sourceType: 'module',
87
},
98
env: { jest: true, browser: true, node: true },
10-
rules: {
11-
'no-console': 'warn',
12-
"import/extensions": [
13-
"error",
14-
"ignorePackages",
15-
{
16-
"js": "never",
17-
"ts": "never",
18-
}
19-
]
20-
},
21-
settings: {
22-
'import/resolver': {
23-
node: {
24-
paths: ['src'],
25-
extensions: ['.js', '.ts'],
26-
},
27-
},
28-
},
9+
rules: { 'no-console': 'warn' },
2910
};

.nvmrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v10.16.3
1+
v12.13.1

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ functions:
4444
method: get
4545
```
4646
47-
Ignoring the scheduling event, you can see here that we're setting up a function named `hello` with a handler at `src/hello.js` (the `.default` piece is just indicating that the function to run will be the default export from that file). The `http` event says that this function will run when an http event is triggered (on AWS, this happens via API Gateway).
47+
Ignoring the scheduling event, you can see here that we're setting up a function named `hello` with a handler at `src/hello.ts` (the `.default` piece is just indicating that the function to run will be the default export from that file). The `http` event says that this function will run when an http event is triggered (on AWS, this happens via API Gateway).
4848

4949
#### 2. Create your function
5050

51-
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/).
51+
This starter kit's Hello World function (which you will of course get rid of) can be found at [`./src/hello.ts`](./src/hello.ts). 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/).
5252

5353
---
5454

@@ -73,7 +73,7 @@ yarn serve
7373
### Test your functions with Jest
7474

7575
Jest is installed as the testrunner. To create a test, co-locate your test with the file it's testing
76-
as `<filename>.test.js` and then run/watch tests with:
76+
as `<filename>.test.ts` and then run/watch tests with:
7777

7878
```bash
7979
yarn test

fixtures/scheduled.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

jest.config.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
module.exports = {
22
roots: ['<rootDir>/src'],
3-
testRegex: '(.*\\.test\\.(tsx?|jsx?))$',
3+
testMatch: [
4+
'**/__tests__/**/*.+(ts|tsx|js)',
5+
'**/?(*.)+(spec|test).+(ts|tsx|js)',
6+
],
47
transform: {
5-
'^.+\\.tsx?$': 'ts-jest',
6-
'^.+\\.jsx?$': 'babel-jest',
8+
'^.+\\.(ts|tsx)$': 'ts-jest',
79
},
8-
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
910
};

package.json

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,12 @@
1818
"watch:hello": "sls invoke local --watch --function hello --path fixtures/event.json"
1919
},
2020
"devDependencies": {
21-
"@babel/core": "7.6.2",
22-
"@babel/preset-env": "7.6.2",
23-
"@babel/preset-typescript": "^7.6.0",
21+
"@types/aws-lambda": "^8.10.39",
2422
"@types/jest": "^25.1.0",
2523
"@typescript-eslint/eslint-plugin": "^3.0.0",
2624
"@typescript-eslint/parser": "^3.0.0",
27-
"aws-sdk": "^2.529.0",
28-
"babel-core": "^7.0.0-bridge.0",
29-
"babel-jest": "^24.9.0",
30-
"babel-loader": "^8.0.6",
3125
"eslint": "^6.3.0",
32-
"eslint-config-airbnb": "^18.0.1",
3326
"eslint-config-prettier": "^6.3.0",
34-
"eslint-import-resolver-typescript": "^2.0.0",
35-
"eslint-plugin-import": "^2.18.2",
36-
"eslint-plugin-jsx-a11y": "^6.2.3",
37-
"eslint-plugin-react": "^7.14.3",
38-
"eslint-plugin-react-hooks": "2.1.0",
3927
"husky": "^4.0.2",
4028
"jest": "^24.9.0",
4129
"lint-staged": "^10.0.3",
@@ -53,11 +41,6 @@
5341
"webpack": "^4.39.3",
5442
"webpack-node-externals": "^1.7.2"
5543
},
56-
"dependencies": {
57-
"@types/aws-lambda": "^8.10.31",
58-
"@types/node": "^13.1.0",
59-
"debug": "^4.1.1"
60-
},
6144
"husky": {
6245
"hooks": {
6346
"pre-commit": "lint-staged && yarn test"

serverless.yml

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ package:
2424

2525
provider:
2626
name: aws
27-
runtime: nodejs10.x
27+
runtime: nodejs12.x
2828
# you can overwrite defaults here
2929
# stage: dev
3030
# region: us-east-1
@@ -39,6 +39,7 @@ custom:
3939
webpackConfig: ./webpack.config.js
4040
includeModules: true
4141
packager: 'yarn' # Packager that will be used to package your external modules
42+
excludeFiles: src/**/*.test.[t|j]s # Exclude test files
4243
warmup:
4344
enabled: true
4445
events:
@@ -78,12 +79,6 @@ custom:
7879
# - exclude-me-dir/**
7980

8081
functions:
81-
hello-typescript:
82-
handler: src/hello-ts.default
83-
events:
84-
- http:
85-
path: hello-typescript
86-
method: get
8782
hello:
8883
handler: src/hello.default
8984
events:

src/__snapshots__/hello.test.js.snap renamed to src/__snapshots__/hello.test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
exports[`hello executes as expected 1`] = `
44
Object {
5-
"body": "{\\"message\\":\\"Go Serverless! Your function executed successfully!\\",\\"input\\":{}}",
5+
"body": "{\\"message\\":\\"Go Serverless! Your function executed successfully!\\",\\"input\\":{\\"key3\\":\\"value3\\",\\"key2\\":\\"value2\\",\\"key1\\":\\"value1\\"}}",
66
"statusCode": 200,
77
}
88
`;

src/hello-ts.ts

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/hello.test.js

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/hello.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import hello from './hello';
2+
import context from './test/utils/handler-helper';
3+
import event from './test/fixtures/event.json';
4+
5+
const callback = jest.fn();
6+
7+
describe('hello', () => {
8+
it('executes as expected', async () => {
9+
const response = await hello(event, context, callback);
10+
expect(response).toMatchSnapshot();
11+
});
12+
});

src/hello.js renamed to src/hello.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { successResponse, runWarm } from './utils';
2+
import { Response } from './utils/lambda-response';
23

3-
const hello = async (event) => {
4+
const hello = async (event: AWSLambda.APIGatewayEvent): Promise<Response> => {
45
// successResponse handles wrapping the response in an API Gateway friendly
5-
// format (see other responses, including CORS, in `./utils/lambda-response.js)
6+
// format (see other responses, including CORS, in `./utils/lambda-response.ts)
67
const response = successResponse({
78
message: 'Go Serverless! Your function executed successfully!',
89
input: event,
File renamed without changes.

src/test/utils/handler-helper.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const context = {
2+
awsRequestId: '',
3+
invokeid: '',
4+
logGroupName: '',
5+
logStreamName: '',
6+
functionVersion: '',
7+
isDefaultFunctionVersion: true,
8+
functionName: '',
9+
memoryLimitInMB: '0',
10+
succeed: jest.fn(),
11+
fail: jest.fn(),
12+
done: jest.fn(),
13+
getRemainingTimeInMillis: jest.fn(),
14+
callbackWaitsForEmptyEventLoop: true,
15+
invokedFunctionArn: '',
16+
};
17+
18+
export default context;

src/utils/lambda-response.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
1-
interface IJSON {
1+
interface JSON {
2+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
23
[key: string]: any;
34
}
45

5-
interface IResponseOptions {
6-
json: IJSON;
6+
interface ResponseOptions {
7+
json: JSON;
78
statusCode: number;
89
allowCORS?: boolean;
910
}
1011

11-
interface IResponse {
12+
export interface Response {
1213
statusCode: number;
1314
body: string;
1415
headers?: {
15-
[key: string]: any;
16+
[key: string]: string;
1617
};
1718
}
1819

1920
function lambdaResponse({
2021
json,
2122
statusCode,
2223
allowCORS = false,
23-
}: IResponseOptions) {
24-
const response: IResponse = {
24+
}: ResponseOptions): Response {
25+
const response: Response = {
2526
statusCode,
2627
body: JSON.stringify(json),
2728
};
@@ -35,29 +36,29 @@ function lambdaResponse({
3536
return response;
3637
}
3738

38-
export function errorResponse(json: IJSON) {
39+
export function errorResponse(json: JSON): Response {
3940
return lambdaResponse({
4041
json,
4142
statusCode: 500,
4243
});
4344
}
4445

45-
export function corsErrorResponse(json: IJSON) {
46+
export function corsErrorResponse(json: JSON): Response {
4647
return lambdaResponse({
4748
json,
4849
statusCode: 500,
4950
allowCORS: true,
5051
});
5152
}
5253

53-
export function successResponse(json: IJSON) {
54+
export function successResponse(json: JSON): Response {
5455
return lambdaResponse({
5556
json,
5657
statusCode: 200,
5758
});
5859
}
5960

60-
export function corsSuccessResponse(json: IJSON) {
61+
export function corsSuccessResponse(json: JSON): Response {
6162
return lambdaResponse({
6263
json,
6364
statusCode: 200,

src/utils/run-warm.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
const runWarm = (lambdaFunc: Function): AWSLambda.Handler => async (
1+
const runWarm = (lambdaFunc: AWSLambda.Handler): AWSLambda.Handler => (
22
event,
33
context,
4+
callback
45
) => {
56
// Detect the keep-alive ping from CloudWatch and exit early. This keeps our
67
// lambda function running hot.
78
if (event.source === 'serverless-plugin-warmup') {
8-
return 'pinged';
9+
return callback(null, 'pinged');
910
}
1011

11-
return lambdaFunc(event, context);
12+
return lambdaFunc(event, context, callback);
1213
};
1314

1415
export default runWarm;

webpack.config.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,6 @@ module.exports = {
1313
use: 'ts-loader',
1414
exclude: /node_modules/,
1515
},
16-
{
17-
test: /\.jsx?$/,
18-
include: __dirname,
19-
exclude: /node_modules/,
20-
use: {
21-
loader: 'babel-loader',
22-
},
23-
},
2416
],
2517
},
2618
resolve: {

0 commit comments

Comments
 (0)