@superrb/netlify-function-helpers

1.1.0 • Public • Published

Netlify Function Helpers

A library of helpers to use when writing Netlify functions.

withCors

A wrapper around the netlify function callback to handle CORS and parse posted data.

Usage

const { withCors, ValidationError } = require('@superrb/netlify-function-helpers')

exports.handler = withCors((data) => {
  // Throw `ValidationError` to respond with an error message and a 400 status code
  if (!data) {
    throw new ValidationError('Missing data')
  }

  // Throw any other error to respond with the error message and a 500 status code
  try {
    doSomething()
  } catch (err) {
    throw new Error('Something went wrong')
  }

  // Return an object, which will be sent to the client as JSON
  return {
    success: true
  }
})

Readme

Keywords

none

Package Sidebar

Install

npm i @superrb/netlify-function-helpers

Weekly Downloads

1

Version

1.1.0

License

MIT

Unpacked Size

4.7 kB

Total Files

15

Last publish

Collaborators

  • molovo
  • grh_smith