This repo serves as the starting point for all Serverless API projects. The boilerplate creates a solid foundation for any AWS Lambda-based API project using Koa (in Typescript) with testing, ORM, infrastructure orchestration, and CI/CD already set up.
- Node ~8.1.2 syntax support (through webpack), which is required by Lambda
- Serverless Framework to deploy Lambda (with 15 min execution support)
- Eslint as linter
- Language: Typescript v3.5 with ECMA2018 support
- Dev Runtime: Node Dubnium (see ES2018 support)
- Framework: KoaJS v2 - API only implementation
- Linter: ESLint with Typescript Support
- Git Hooks: Provided by husky 🐶 and lint-staged 🚫💩
- Logger: Winston
- Test Framework: Jest & Supertest
- Security Headers: HelmetJS
- Serverless Framework: Serverless
- Copy
.env.example
to.env
- Copy
serverless.env.example.yml
toserverless.env.example.yml
- Run
yarn
- Run
yarn watch
- Wait a sec for the server to spin up ...
- In a browser or Postman, go to
http://localhost:3000
to see Hello World!
Unit Testing is accomplished through the Jest(https://jestjs.io/) framework. In addition, API requests are mocked using the Supertest wrapper around the Koa API.
Run all tests with yarn test
.
Tests are broken down into the blocks/types:
- Application Level Tests - These are integration tests that test the entire API server as it would run in Lambda (without the Lambda wrapper, triggered by an external API request. These test how all the components fit together including, but not limited to, the router, middleware, controllers, models, and DB connections.
Common uses include: ensuring model consistency across multiple controllers, middleware consistency across endpoints, and role/user restrictions across endpoints and controllers - Router Level Tests - Router tests ensure the correct controllers are mounted at the correct endpoints. This is critical as external services depend on the stability of the endpoints.
- Controller Level Test - Controller tests mount individual controllers (and individual functions) in vanilla Koa servers to confirm granular level functionality. This decreases the rigor of application-level & router-level testing required.
See https://serverless.com/blog/serverless-api-gateway-domain/ for additional details.
To set up your function to use enterprise serverless:
- Register at https://dashboard.serverless.com
- Run
yarn sls login
to connect your local installation to Serverless Enterprise - Uncomment and populate:
tenant: MyTenantID app: MyAppID org: MyOrgID
Bryan Vaz (Github: @bryanvaz)
- https://github.com/javieraviles/node-typescript-koa-rest for inspiration and baseline reference koa typescript implementation