Skip to content

Commit ac8a5ea

Browse files
committed
Initial commit
0 parents  commit ac8a5ea

10 files changed

+15911
-0
lines changed

.gitignore

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
6+
# Runtime data
7+
pids
8+
*.pid
9+
*.seed
10+
11+
# Directory for instrumented libs generated by jscoverage/JSCover
12+
lib-cov
13+
14+
# Coverage directory used by tools like istanbul
15+
coverage
16+
17+
# nyc test coverage
18+
.nyc_output
19+
20+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
21+
.grunt
22+
23+
# node-waf configuration
24+
.lock-wscript
25+
26+
# Compiled binary addons (http://nodejs.org/api/addons.html)
27+
build/Release
28+
29+
# Dependency directories
30+
node_modules
31+
jspm_packages
32+
33+
# Optional npm cache directory
34+
.npm
35+
36+
# Optional REPL history
37+
.node_repl_history
38+
39+
# vim
40+
.*.sw*
41+
Session.vim
42+
43+
# Serverless
44+
.webpack
45+
.serverless
46+
47+
# env
48+
env.yml
49+
.env
50+
51+
# Jetbrains IDEs
52+
.idea

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017 Anomaly Innovations
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+135
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
# Serverless TypeScript Starter
2+
3+
A Serverless starter that adds TypeScript, serverless-offline, linting, environment variables, and unit test support.
4+
5+
This starter uses the [serverless-bundle](https://github.com/AnomalyInnovations/serverless-bundle) plugin and the [serverless-offline](https://github.com/dherault/serverless-offline) plugin. It supports:
6+
7+
- **Generating optimized Lambda packages with Webpack**
8+
- **Using TypeScript in your handler functions and tests**
9+
- **Run API Gateway locally**
10+
- Use `serverless offline start`
11+
- **Support for unit tests**
12+
- Run `npm test` to run your tests
13+
- **Sourcemaps for proper error messages**
14+
- Error message show the correct line numbers
15+
- Works in production with CloudWatch
16+
- **Lint your code with ESLint**
17+
- **Add environment variables for your stages**
18+
- **No need to manage Webpack or Babel configs**
19+
20+
---
21+
22+
### Demo
23+
24+
A demo version of this service is hosted on AWS - [`https://ylsml6v6r6.execute-api.us-east-1.amazonaws.com/dev/hello`](https://ylsml6v6r6.execute-api.us-east-1.amazonaws.com/dev/hello)
25+
26+
And here is the ES6 source behind it
27+
28+
``` javascript
29+
export async function hello(
30+
event: APIGatewayEvent,
31+
context: Context
32+
): Promise<APIGatewayProxyResult> {
33+
return {
34+
statusCode: 200,
35+
body: JSON.stringify({
36+
message: "Go Serverless v2.0! Your function executed successfully!",
37+
context,
38+
event,
39+
}),
40+
};
41+
}
42+
```
43+
44+
### Requirements
45+
46+
- [Install the Serverless Framework](https://serverless.com/framework/docs/providers/aws/guide/installation/)
47+
- [Configure your AWS CLI](https://serverless.com/framework/docs/providers/aws/guide/credentials/)
48+
49+
### Installation
50+
51+
To create a new Serverless project.
52+
53+
``` bash
54+
$ serverless install --url https://github.com/AnomalyInnovations/serverless-typescript-starter --name my-project
55+
```
56+
57+
Enter the new directory
58+
59+
``` bash
60+
$ cd my-project
61+
```
62+
63+
Install the Node.js packages
64+
65+
``` bash
66+
$ npm install
67+
```
68+
69+
### Usage
70+
71+
To run a function on your local
72+
73+
``` bash
74+
$ serverless invoke local --function hello
75+
```
76+
77+
To simulate API Gateway locally using [serverless-offline](https://github.com/dherault/serverless-offline)
78+
79+
``` bash
80+
$ serverless offline start
81+
```
82+
83+
Deploy your project
84+
85+
``` bash
86+
$ serverless deploy
87+
```
88+
89+
Deploy a single function
90+
91+
``` bash
92+
$ serverless deploy function --function hello
93+
```
94+
95+
#### Running Tests
96+
97+
Run your tests using
98+
99+
``` bash
100+
$ npm test
101+
```
102+
103+
We use Jest to run our tests. You can read more about setting up your tests [here](https://facebook.github.io/jest/docs/en/getting-started.html#content).
104+
105+
#### Environment Variables
106+
107+
To add environment variables to your project
108+
109+
1. Rename `env.example` to `.env`.
110+
2. Add environment variables for your local stage to `.env`.
111+
3. Uncomment `environment:` block in the `serverless.yml` and reference the environment variable as `${env:MY_ENV_VAR}`. Where `MY_ENV_VAR` is added to your `.env` file.
112+
4. Make sure to not commit your `.env`.
113+
114+
#### Linting
115+
116+
We use [ESLint](https://eslint.org) to lint your code via [serverless-bundle](https://github.com/AnomalyInnovations/serverless-bundle).
117+
118+
You can turn this off by adding the following to your `serverless.yml`.
119+
120+
``` yaml
121+
custom:
122+
bundle:
123+
linting: false
124+
```
125+
126+
To [override the default config](https://eslint.org/docs/user-guide/configuring), add a `.eslintrc.json` file. To ignore ESLint for specific files, add it to a `.eslintignore` file.
127+
128+
### Support
129+
130+
- Open a [new issue](https://github.com/AnomalyInnovations/serverless-typescript-starter/issues/new) if you've found a bug or have some suggestions.
131+
- Or submit a pull request!
132+
133+
---
134+
135+
This repo is maintained by [Anomaly Innovations](https://anoma.ly); makers of [Seed](https://seed.run) and [Serverless Stack](https://serverless-stack.com).

env.example

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# HOW TO USE:
2+
#
3+
# 1 Add environment variables for local development.
4+
# 2 Rename this file to .env and uncomment it's usage
5+
# in the serverless.yml.
6+
# 3 Make sure to not commit this file.
7+
8+
SAMPLE_ENV_VAR=i-am-an-environment-variable

handler.ts

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { Context, APIGatewayEvent, APIGatewayProxyResult } from "aws-lambda";
2+
3+
export async function hello(
4+
event: APIGatewayEvent,
5+
context: Context
6+
): Promise<APIGatewayProxyResult> {
7+
return {
8+
statusCode: 200,
9+
body: JSON.stringify({
10+
message: "Go Serverless v2.0! Your function executed successfully!",
11+
context,
12+
event,
13+
}),
14+
};
15+
}

0 commit comments

Comments
 (0)