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

Commit 36d43f6

Browse files
authored
feat: typescript support (#42)
* feat: typescript support You can use ts or js. Linting and build for both * adjustments * update README * update jest to handle typescript
1 parent e4d99d7 commit 36d43f6

18 files changed

+516
-183
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
src/**/*.ts

.eslintrc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,17 @@
1515
"jasmine": true,
1616
"xit": true,
1717
"jest": true
18+
},
19+
"settings": {
20+
"plugins": [
21+
"import"
22+
],
23+
"rules": {
24+
"import/no-unresolved": "error"
25+
},
26+
"import/resolver": {
27+
"typescript": {},
28+
}
1829
}
1930
}
2031

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ node_modules
33
.webpack
44
.serverless
55
.env*
6+
.build

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
[![Greenkeeper badge](https://badges.greenkeeper.io/postlight/serverless-babel-starter.svg)](https://greenkeeper.io/)
33
[![CircleCI](https://circleci.com/gh/postlight/serverless-babel-starter/tree/master.svg?style=svg)](https://circleci.com/gh/postlight/serverless-babel-starter/tree/master)
44

5-
Postlight's Modern Serverless Starter Kit adds a light layer on top of the Serverless framework, giving you the latest in modern JavaScript (ES6 via Webpack + Babel, testing with Jest, linting with ESLint, and formatting with Prettier), the ease and power of Serverless, and a few handy helpers (like functions for handling warm functions and response helpers).
5+
Postlight's Modern Serverless Starter Kit adds a light layer on top of the Serverless framework, giving you the latest in modern JavaScript (ES6 via Webpack + Babel, TypeScript (but only if you want it), testing with Jest, linting with ESLint, and formatting with Prettier), the ease and power of Serverless, and a few handy helpers (like functions for handling warm functions and response helpers).
66

77
Once installed, you can create and deploy functions with the latest ES6 features in minutes, with linting and formatting baked in.
88

jest.config.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module.exports = {
2+
"roots": [
3+
"<rootDir>/src"
4+
],
5+
"testRegex": "(.*\\.test\\.(tsx?|jsx?))$",
6+
"transform": {
7+
"^.+\\.tsx?$": "ts-jest",
8+
"^.+\\.jsx?$": "babel-jest"
9+
},
10+
"moduleFileExtensions": [
11+
"ts",
12+
"tsx",
13+
"js",
14+
"jsx",
15+
"json",
16+
"node"
17+
],
18+
}

package.json

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88
"deploy": "export NODE_ENV=dev && yarn deploy:env",
99
"deploy:production": "export NODE_ENV=production && yarn deploy:env",
1010
"deploy:stage": "export NODE_ENV=stage && yarn deploy:env",
11-
"lint": "eslint ./src",
11+
"lint": "tslint -c tslint.json --fix './src/**/*.ts' && eslint ./src --fix",
1212
"precommit": "lint-staged",
1313
"serve": "serverless offline start",
14+
"serve:watch": "nodemon -e js,ts,jsx,tsx -x serverless offline start",
1415
"tail:hello": "serverless logs --function hello --tail",
1516
"test": "jest",
1617
"test:watch": "jest --watch",
@@ -21,29 +22,40 @@
2122
"devDependencies": {
2223
"@babel/core": "7.2.2",
2324
"@babel/preset-env": "7.2.3",
25+
"@types/jest": "^23.3.10",
2426
"babel-core": "^7.0.0-bridge.0",
2527
"babel-jest": "^23.4.2",
2628
"babel-loader": "^8.0.0",
2729
"eslint": "^5.4.0",
2830
"eslint-config-airbnb": "^17.1.0",
2931
"eslint-config-prettier": "^3.0.1",
32+
"eslint-import-resolver-typescript": "^1.1.1",
3033
"eslint-plugin-import": "^2.14.0",
3134
"eslint-plugin-jsx-a11y": "^6.1.1",
3235
"eslint-plugin-react": "^7.11.0",
3336
"husky": "^1.0.0",
3437
"jest": "^23.5.0",
3538
"lint-staged": "^8.0.0",
39+
"nodemon": "^1.18.9",
3640
"prettier": "^1.14.2",
3741
"serverless": "^1.32.0",
3842
"serverless-offline": "^3.25.10",
3943
"serverless-webpack": "^5.2.0",
44+
"ts-jest": "^23.10.5",
45+
"ts-loader": "^5.3.1",
46+
"tslint": "^5.11.0",
47+
"tslint-config-prettier": "^1.17.0",
48+
"tslint-react": "^3.6.0",
49+
"typescript": "^3.2.1",
4050
"webpack": "^4.17.1",
4151
"webpack-node-externals": "^1.7.2"
4252
},
43-
"dependencies": {},
53+
"dependencies": {
54+
"@types/aws-lambda": "^8.10.15"
55+
},
4456
"lint-staged": {
4557
"src/**/*.js": [
46-
"yarn lint -- --fix",
58+
"yarn lint",
4759
"prettier --write",
4860
"git add"
4961
]

serverless.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,16 @@ custom:
6363
# - exclude-me-dir/**
6464

6565
functions:
66+
hello-typescript:
67+
handler: src/hello-ts.default
68+
events:
69+
- http:
70+
path: hello-typescript
71+
method: get
72+
# Ping every 5 minutes to avoid cold starts
73+
- schedule:
74+
rate: rate(5 minutes)
75+
enabled: true
6676
hello:
6777
handler: src/hello.default
6878
events:

src/hello-ts.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { successResponse, runWarm } from './utils';
2+
3+
const helloTs: AWSLambda.Handler = (
4+
event: AWSLambda.APIGatewayEvent,
5+
_context,
6+
callback
7+
) => {
8+
// successResponse handles wrapping the response in an API Gateway friendly
9+
// format (see other responses, including CORS, in `./utils/lambda-response.js)
10+
const response = successResponse({
11+
message: 'Go Serverless! Your function executed successfully!',
12+
input: event,
13+
});
14+
15+
callback(null, response);
16+
17+
// Use this code if you don't use the http event with the LAMBDA-PROXY integration
18+
// callback(null, { message: 'Go Serverless v1.0! Your function executed successfully!', event });
19+
};
20+
21+
// runWarm function handles pings from the scheduler so you don't
22+
// have to put that boilerplate in your function.
23+
export default runWarm(helloTs);

src/types/index.ts

Whitespace-only changes.

src/types/lambda.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export interface IEvent {
2+
source: string;
3+
}
4+
5+
interface IJSON {
6+
[key: string]: any;
7+
}
8+
9+
export type ICallback = (param1: any | null, response: IJSON | string) => void;
10+
11+
export type ILambdaFunc = (
12+
event: IEvent,
13+
context: {},
14+
callback: ICallback
15+
) => void | ILambdaFunc;

0 commit comments

Comments
 (0)