Skip to content

Instantly share code, notes, and snippets.

@lucasklaassen
Created March 3, 2018 17:35
Show Gist options
  • Save lucasklaassen/e05ac99d42f5c9b0b1d4c85164f8fa6d to your computer and use it in GitHub Desktop.
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.
'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