├── .github └── FUNDING.yml ├── .gitignore ├── LICENSE ├── README.md ├── index.js ├── middleware.js ├── package-lock.json └── package.json /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: compwright 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | 8 | # Runtime data 9 | pids 10 | *.pid 11 | *.seed 12 | *.pid.lock 13 | 14 | # Directory for instrumented libs generated by jscoverage/JSCover 15 | lib-cov 16 | 17 | # Coverage directory used by tools like istanbul 18 | coverage 19 | 20 | # nyc test coverage 21 | .nyc_output 22 | 23 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 24 | .grunt 25 | 26 | # Bower dependency directory (https://bower.io/) 27 | bower_components 28 | 29 | # node-waf configuration 30 | .lock-wscript 31 | 32 | # Compiled binary addons (https://nodejs.org/api/addons.html) 33 | build/Release 34 | 35 | # Dependency directories 36 | node_modules/ 37 | jspm_packages/ 38 | 39 | # TypeScript v1 declaration files 40 | typings/ 41 | 42 | # Optional npm cache directory 43 | .npm 44 | 45 | # Optional eslint cache 46 | .eslintcache 47 | 48 | # Optional REPL history 49 | .node_repl_history 50 | 51 | # Output of 'npm pack' 52 | *.tgz 53 | 54 | # Yarn Integrity file 55 | .yarn-integrity 56 | 57 | # dotenv environment variables file 58 | .env 59 | 60 | # next.js build output 61 | .next 62 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Jonathon Hill 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. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # aws-serverless-koa 2 | 3 | This library enables you to utilize AWS Lambda and Amazon API Gateway to respond to web and API requests using the Koa.js application framework 4 | 5 | ## Installation 6 | 7 | ```bash 8 | $ npm install --save aws-serverless-koa 9 | ``` 10 | 11 | ## Usage 12 | 13 | ```javascript 14 | const Koa = require('koa'); 15 | const serverless = require('aws-serverless-koa'); 16 | 17 | const app = new Koa(); 18 | 19 | app.use(async ctx => { 20 | ctx.body = 'Hello, world!'; 21 | }); 22 | 23 | module.exports.handler = serverless(app); 24 | ``` 25 | 26 | ## Middleware 27 | 28 | This package includes middleware to easily get the event object Lambda receives from API Gateway (based on `aws-serverless-express/middleware`): 29 | 30 | ``` 31 | const awsServerlessKoaMiddleware = require('aws-serverless-koa/middleware'); 32 | app.use(awsServerlessKoaMiddleware()); 33 | app.use(ctx => { 34 | ctx.body = ctx.apiGateway.event 35 | }); 36 | ``` 37 | 38 | ## License 39 | 40 | MIT license 41 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const { createServer, proxy } = require('aws-serverless-express'); 4 | 5 | module.exports = app => { 6 | const server = createServer(app.callback()); 7 | return (event, ctx) => { proxy(server, event, ctx); }; 8 | }; 9 | -------------------------------------------------------------------------------- /middleware.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = (options = {}) => async function apiGatewayEventParser (ctx, next) { 4 | const reqPropKey = options.reqPropKey || 'apiGateway'; 5 | const deleteHeaders = options.deleteHeaders === undefined ? true : options.deleteHeaders; 6 | 7 | if (!ctx.headers['x-apigateway-event'] || !ctx.headers['x-apigateway-context']) { 8 | throw new Error('Missing x-apigateway-event or x-apigateway-context header(s)'); 9 | } 10 | 11 | ctx.request[reqPropKey] = { 12 | event: JSON.parse(decodeURIComponent(ctx.headers['x-apigateway-event'])), 13 | context: JSON.parse(decodeURIComponent(ctx.headers['x-apigateway-context'])) 14 | }; 15 | 16 | if (deleteHeaders) { 17 | delete ctx.headers['x-apigateway-event']; 18 | delete ctx.headers['x-apigateway-context']; 19 | } 20 | 21 | await next(); 22 | }; 23 | -------------------------------------------------------------------------------- /package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "aws-serverless-koa", 3 | "version": "1.0.2", 4 | "lockfileVersion": 1, 5 | "requires": true, 6 | "dependencies": { 7 | "aws-serverless-express": { 8 | "version": "3.3.8", 9 | "resolved": "https://registry.npmjs.org/aws-serverless-express/-/aws-serverless-express-3.3.8.tgz", 10 | "integrity": "sha512-2TQdF5EhxnAtGeEi+wSi2M3xCfpiemuImnpU7HKih3onH0izJ/G2tkM+gwcGHZEsW/gLWFl/JjQAYGa3fILfvw==", 11 | "requires": { 12 | "binary-case": "^1.0.0", 13 | "type-is": "^1.6.16" 14 | } 15 | }, 16 | "binary-case": { 17 | "version": "1.1.4", 18 | "resolved": "https://registry.npmjs.org/binary-case/-/binary-case-1.1.4.tgz", 19 | "integrity": "sha512-9Kq8m6NZTAgy05Ryuh7U3Qc4/ujLQU1AZ5vMw4cr3igTdi5itZC6kCNrRr2X8NzPiDn2oUIFTfa71DKMnue/Zg==" 20 | }, 21 | "media-typer": { 22 | "version": "0.3.0", 23 | "resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz", 24 | "integrity": "sha1-hxDXrwqmJvj/+hzgAWhUUmMlV0g=" 25 | }, 26 | "mime-db": { 27 | "version": "1.44.0", 28 | "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.44.0.tgz", 29 | "integrity": "sha512-/NOTfLrsPBVeH7YtFPgsVWveuL+4SjjYxaQ1xtM1KMFj7HdxlBlxeyNLzhyJVx7r4rZGJAZ/6lkKCitSc/Nmpg==" 30 | }, 31 | "mime-types": { 32 | "version": "2.1.27", 33 | "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.27.tgz", 34 | "integrity": "sha512-JIhqnCasI9yD+SsmkquHBxTSEuZdQX5BuQnS2Vc7puQQQ+8yiP5AY5uWhpdv4YL4VM5c6iliiYWPgJ/nJQLp7w==", 35 | "requires": { 36 | "mime-db": "1.44.0" 37 | } 38 | }, 39 | "type-is": { 40 | "version": "1.6.18", 41 | "resolved": "https://registry.npmjs.org/type-is/-/type-is-1.6.18.tgz", 42 | "integrity": "sha512-TkRKr9sUTxEH8MdfuCSP7VizJyzRNMjj2J2do2Jr3Kym598JVdEksuzPQCnlFPW4ky9Q+iA+ma9BGm06XQBy8g==", 43 | "requires": { 44 | "media-typer": "0.3.0", 45 | "mime-types": "~2.1.24" 46 | } 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "aws-serverless-koa", 3 | "version": "1.0.2", 4 | "description": "This library enables you to utilize AWS Lambda and Amazon API Gateway to respond to web and API requests using the Koa.js application framework", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/compwright/aws-serverless-koa.git" 12 | }, 13 | "keywords": [ 14 | "koa", 15 | "serverless", 16 | "aws", 17 | "lambda", 18 | "api", 19 | "gateway", 20 | "middleware", 21 | "event" 22 | ], 23 | "author": "Jonathon Hill ", 24 | "license": "MIT", 25 | "bugs": { 26 | "url": "https://github.com/compwright/aws-serverless-koa/issues" 27 | }, 28 | "homepage": "https://github.com/compwright/aws-serverless-koa#readme", 29 | "dependencies": { 30 | "aws-serverless-express": "^3.3.8" 31 | } 32 | } 33 | --------------------------------------------------------------------------------