Created
March 3, 2018 17:35
-
-
Save lucasklaassen/e05ac99d42f5c9b0b1d4c85164f8fa6d to your computer and use it in GitHub Desktop.
Serverless lambda function which uses middleware to handle warmup, validation, JSON Parsing and error handling.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
import ExampleObject from './../objects/ExampleObject'; | |
const schema = { | |
"properties": { | |
"primaryID": { | |
"type": "string", | |
"format": "uuid" | |
} | |
}, | |
"required": ["primaryID"] | |
}; | |
const middy = require('middy'); | |
const { | |
jsonBodyParser, | |
validator, | |
warmup, | |
httpErrorHandler | |
} = require('middy/middlewares'); | |
// Our Business Logic | |
const fetchEndpoint = async(event, context, callback) => { | |
let exampleObject = new ExampleObject(event); | |
return exampleObject.fetch(); | |
}; | |
const fetchHandler = middy(fetchEndpoint) | |
.use(warmup()) | |
.use(jsonBodyParser()) | |
.use(validator({ | |
inputSchema: schema | |
})) | |
.use(httpErrorHandler()); | |
module.exports.fetch = fetchHandler; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment