|
| 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). |
0 commit comments