diegotremper / aws-serverless-restful-wrapper

An API Gateway event wrapper for AWS Lambda functions for REST APIs
MIT License
1 stars 0 forks source link

aws-serverless-restful-wrapper


NOTE This package is no longer maintained and was moved to @cloudifyjs/restful.

An API Gateway event wrapper for AWS Lambda functions for REST APIs

CircleCI npm npm codecov dependencies Status devDependencies Status Known Vulnerabilities Greenkeeper badge

Install

$ npm install aws-serverless-restful-wrapper

Features

Usage

Fetch a single document

GET /todos/{id}

const restful = require('aws-serverless-restful-wrapper')

module.exports.get = restful.document({
  target: async (path, query, headers) => {
    console.log('Returning todo')
    return {
      id: '123',
      text: 'My task',
      checked: true
    }
  }
})

API Gateway Response:

HTTP 200
Content-Type: 'application/json'

{
  "id": "123",
  "text": "My task",
  "checked": true
}

Fetch a collection

const restful = require('aws-serverless-restful-wrapper')

module.exports.get = restful.collection({
  target: async (path, query, headers) => {
    console.log('Returning todos')
    return [
      {
        id: '1',
        text: 'My task 1',
        checked: true
      },
      {
        id: '2',
        text: 'My task 2',
        checked: true
      }
    ]
  }
})

API Gateway Response:

HTTP 200
Content-Type: 'application/json'

[
  {
    "id": "1",
    "text": "My task 1",
    "checked": true
  },
  {
    "id": "2",
    "text": "My task 2",
    "checked": true
  }
]

Using Joi validation

const Joi = require('@hapi/joi')
const restful = require('aws-serverless-restful-wrapper')

module.exports.get = restful.document({
  validators: {
    path: Joi.object({
      id: Joi.string().required()
    })
  },
  target: async (path, query, headers) => {
    console.log('Returning todo')
    return {
      text: 'My task',
      checked: true
    }
  }
})

Contributing

License

This source code is licensed under the MIT license found in the LICENSE.txt file.