├── .gitignore ├── LICENSE ├── README.md ├── event.json ├── handler.ts ├── karma.conf.js ├── models.ts ├── package.json ├── serverless.yml ├── tests.ts ├── tsconfig.json ├── tslint.json ├── typings.json └── webpack.config.js /.gitignore: -------------------------------------------------------------------------------- 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 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 18 | .grunt 19 | 20 | # node-waf configuration 21 | .lock-wscript 22 | 23 | # Compiled binary addons (http://nodejs.org/api/addons.html) 24 | build/Release 25 | 26 | # Dependency directory 27 | node_modules 28 | 29 | # Optional npm cache directory 30 | .npm 31 | 32 | # Optional REPL history 33 | .node_repl_history 34 | 35 | typings 36 | .webpack -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Greg Shackles 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: -------------------------------------------------------------------------------- 1 | # serverless-typescript-demo 2 | 3 | This is a very basic project demonstrating how to build a Serverless application using TypeScript, including linting and tests. You can refer to [this blog post](https://gregshackles.com/getting-started-with-serverless-and-typescript/) as a walkthrough of the code here. 4 | -------------------------------------------------------------------------------- /event.json: -------------------------------------------------------------------------------- 1 | { 2 | "method": "GET", 3 | "headers": { 4 | "host": "localhost:8000", 5 | "user-agent": "curl/7.49.1", 6 | "accept": "*/*" 7 | }, 8 | "body": {}, 9 | "path": {}, 10 | "query": { 11 | "foo": "bar" 12 | } 13 | } -------------------------------------------------------------------------------- /handler.ts: -------------------------------------------------------------------------------- 1 | import { ICallback, IEventPayload } from './models'; 2 | 3 | export function hello(event: IEventPayload, context, callback: ICallback) { 4 | callback(undefined, { 5 | message: `Method: ${event.method}, Param: ${event.query.foo}`, 6 | event: event 7 | }); 8 | } -------------------------------------------------------------------------------- /karma.conf.js: -------------------------------------------------------------------------------- 1 | var webpackConfig = require('./webpack.config'); 2 | 3 | module.exports = function(config) { 4 | config.set({ 5 | basePath: '', 6 | frameworks: ['mocha', 'chai'], 7 | files: ['tests.ts'], 8 | preprocessors: { 9 | 'tests.ts': ['webpack'] 10 | }, 11 | webpack: { 12 | module: webpackConfig.module, 13 | resolve: webpackConfig.resolve 14 | }, 15 | reporters: ['progress'], 16 | colors: true, 17 | logLevel: config.LOG_INFO, 18 | browsers: ['PhantomJS'], 19 | singleRun: true 20 | }); 21 | } -------------------------------------------------------------------------------- /models.ts: -------------------------------------------------------------------------------- 1 | export interface IResponsePayload { 2 | message: string; 3 | event: any; 4 | } 5 | 6 | export interface IQueryParameters { 7 | foo: string; 8 | } 9 | 10 | export interface IEventPayload { 11 | method: string; 12 | query: IQueryParameters; 13 | } 14 | 15 | export interface ICallback { 16 | (error: any, result: IResponsePayload): void; 17 | } -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "serverless-typescript-demo", 3 | "version": "1.0.0", 4 | "description": "Basic demo mixing Serverless and TypeScript", 5 | "main": "handler.ts", 6 | "scripts": { 7 | "lint": "tslint *.ts", 8 | "test": "karma start" 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "git+https://github.com/gshackles/serverless-typescript-demo.git" 13 | }, 14 | "author": "Greg Shackles", 15 | "license": "MIT", 16 | "bugs": { 17 | "url": "https://github.com/gshackles/serverless-typescript-demo/issues" 18 | }, 19 | "homepage": "https://github.com/gshackles/serverless-typescript-demo#readme", 20 | "devDependencies": { 21 | "chai": "^3.5.0", 22 | "karma": "^1.3.0", 23 | "karma-chai": "^0.1.0", 24 | "karma-mocha": "^1.2.0", 25 | "karma-phantomjs-launcher": "^1.0.2", 26 | "karma-typescript-preprocessor2": "^1.2.1", 27 | "karma-webpack": "^1.8.0", 28 | "mocha": "^3.1.2", 29 | "phantomjs-prebuilt": "^2.1.13", 30 | "serverless-webpack": "^1.0.0-rc.2", 31 | "ts-loader": "^0.9.4", 32 | "tslint": "^3.15.1", 33 | "tslint-config-olo": "^0.1.0", 34 | "typescript": "^2.0.3", 35 | "webpack": "^1.13.2" 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /serverless.yml: -------------------------------------------------------------------------------- 1 | service: serverless-typescript-demo 2 | 3 | provider: 4 | name: aws 5 | runtime: nodejs4.3 6 | 7 | plugins: 8 | - serverless-webpack 9 | 10 | functions: 11 | hello: 12 | handler: handler.hello 13 | 14 | events: 15 | - http: 16 | path: hello 17 | method: get -------------------------------------------------------------------------------- /tests.ts: -------------------------------------------------------------------------------- 1 | import { hello } from './handler'; 2 | import * as chai from 'chai'; 3 | const expect = chai.expect; 4 | 5 | describe('hello function', () => { 6 | it('processes the query string', done => { 7 | const requestEvent = { 8 | method: 'GET', 9 | query: { 10 | foo: 'bar' 11 | } 12 | }; 13 | 14 | hello(requestEvent, {}, (err, result) => { 15 | expect(err).to.be.undefined; 16 | expect(result.event).to.equal(requestEvent); 17 | expect(result.message).to.equal('Method: GET, Param: bar'); 18 | 19 | done(); 20 | }); 21 | }); 22 | }); -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "es5", 4 | "module": "commonjs" 5 | }, 6 | "exclude": [ 7 | "node_modules" 8 | ] 9 | } -------------------------------------------------------------------------------- /tslint.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "tslint-config-olo" 3 | } -------------------------------------------------------------------------------- /typings.json: -------------------------------------------------------------------------------- 1 | { 2 | "globalDependencies": { 3 | "mocha": "registry:dt/mocha#2.2.5+20160720003353" 4 | }, 5 | "dependencies": { 6 | "chai": "registry:npm/chai#3.5.0+20160723033700" 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /webpack.config.js: -------------------------------------------------------------------------------- 1 | var path = require('path'); 2 | 3 | module.exports = { 4 | entry: './handler.ts', 5 | target: 'node', 6 | module: { 7 | loaders: [ 8 | { test: /\.ts(x?)$/, loader: 'ts-loader' } 9 | ] 10 | }, 11 | resolve: { 12 | extensions: ['.ts', '.js', '.tsx', '.jsx', ''] 13 | }, 14 | output: { 15 | libraryTarget: 'commonjs', 16 | path: path.join(__dirname, '.webpack'), 17 | filename: 'handler.js' 18 | }, 19 | }; --------------------------------------------------------------------------------