postlight/serverless-typescript-starter

`serverless webpack serve` does not work for POST

Closed this issue · 2 comments

This may or may not be an issue, but it's not clear how to pass the event object when running a local dev server via yarn serve or serverless webpack serve.

The function below attempts to send an email via sendgrid:

const sg = require('sendgrid')(process.env.SENDGRID_API_KEY);
const helper = require('sendgrid').mail;

const personalization = new helper.Personalization();
const mail = new helper.Mail();

const hello = (event, context, callback) => {

  const content = new helper.Content('text/plain', event.message);
  const emailFrom = new helper.Email(event.email);

  mail.setFrom(emailFrom);
  mail.setSubject(event.subject);
  mail.addContent(content);

  const email = new helper.Email('dillon@honestfox.com.au', 'Dillon Bailey');
  personalization.addTo(email);

  mail.addPersonalization(personalization);

  const request = sg.emptyRequest({
    method: 'POST',
    path: '/v3/mail/send',
    body: mail.toJSON(),
  });

  // With promise
  return sg.API(request)
    .then(response => {
      callback(null,  {
        statusCode: response.statusCode,
        body: response,
      });
    })
    .catch(error => {
      // error is an instance of SendGridError
      // The full response is attached to error.response
      callback(error.response,  {
        statusCode: error.response.statusCode,
        body: error.response,
      });
  });
};

export default hello;

The event object this is expecting is something like:

{
  "message": "Test message",
	"email": "dillon@honestfox.com.au",
	"subject": "Test Subject"
}

This all works fine when posting that object to the actual API or even when running yarn watch:hello but when running yarn serve and using Postman to POST to http://localhost:8000/hello the response is that message, email and subject are all missing which means the event object is not passed to the function.

Thoughts?

Hi @dillonbailey, somehow I missed this issue (sorry!) — are you still having this problem?

Hey @adampash I do know that since I got it working with the API I don't know if I carried on with local testing.

From memory it had something to do with using Lambda Proxy - I think it's safest to close this off for now and if I revisit in the future will post an update 👍