npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@mooncake-dev/lambda-res-handler

v1.1.3

Published

AWS Lambda APIG proxy integration helper to send an HTTP response.

Downloads

8

Readme

Lambda res handler

AWS Lambda APIG proxy integration helper to send an HTTP response.

Installation

npm i @mooncake-dev/lambda-res-handler

API

The module exposes a function createResHandler that returns an resHandlers Object with helper methods to send HTTP responses. It can be configured with default headers, that will be sent every time the response handler helper is called.

createResHandler(defaultHeaders)

Parameters:

| Name | Required | Type | Description | | -------------- | -------- | ------ | -------------------------------------------- | | defaultHeaders | No | Object | Contains default headers as key-value pairs. |

Returns:

Object resHandlers.

This Object contains the following methods:

| Name | Description | | ---- | ------------------------------------------------------------------- | | json | Stringifies response payload as JSON. | | html | Responds with html content and sets the proper content type headers |

resHandlers.json(code, body, headers)

Parameters:

| Name | Required | Type | Description | | ------- | -------- | --------------- | ----------------------------------------------------------------------------- | | code | No | Number | A valid HTTP status code, which defaults to 200. | | body | No | Object or Array | The payload to be returned as JSON. | | headers | No | Object | Any headers to send (in addition to the defaultHeaders) as key-value pairs. |

Returns:

Object.

This is an AWS Lambda conformant HTTP response Object:

{
  headers: {},
  statusCode: 200,
  body: {}
}

Usage

const createResHandler = require('@mooncake-dev/lambda-res-handler');
const sendRes = createResHandler();
sendRes.json(201, { hello: 'world' });

Examples

'use strict';

const createResHandler = require('@mooncake-dev/lambda-res-handler');

const defaultHeaders = {
  'Access-Control-Allow-Origin': '*'
};

const sendRes = createResHandler(defaultHeaders);

/**
 * Lambda APIG proxy integration.
 *
 * @param {Object} event - HTTP input
 *
 * @return {Object} HTTP output
 */
module.exports.sendData = async event => {
  try {
    const data = [1, 2, 3];
    return sendRes.json(200, data);
  } catch (err) {
    console.log('error: ', err); // eslint-disable-line no-console

    const statusCode = err.statusCode || 500;
    const data = {
      error: err.message,
      details: err.details
    };

    return sendRes.json(statusCode, data, { 'extra-header': 'kaput' });
  }
};

resHandlers.html(code, body, headers)

Parameters:

| Name | Required | Type | Description | | ------- | -------- | ------ | ----------------------------------------------------------------------------- | | code | No | Number | A valid HTTP status code, which defaults to 200. | | body | No | String | The payload to be returned as HTML. | | headers | No | Object | Any headers to send (in addition to the defaultHeaders) as key-value pairs. |

NOTE: Content-Type header is always set as text/html. For working with this function you need to set the "Content-Type" API Gateway response headers to "text/html" as well, for the specific endpoint. Additionally you have to set a response template with value "$input.path('$')".

Returns:

Object.

This is an AWS Lambda conformant HTTP response Object:

{
  headers: {},
  statusCode: 200,
  body: '<html><head><meta charset="utf-8" /></head><body><div>Hello World!</div></body></html>'
}

Usage

const createResHandler = require('@mooncake-dev/lambda-res-handler');
const sendRes = createResHandler();
sendRes.html(
  200,
  '<html><head><meta charset="utf-8" /></head><body><div>Hello World!</div></body></html>'
);

Examples

'use strict';

const createResHandler = require('@mooncake-dev/lambda-res-handler');

const sendRes = createResHandler();

/**
 * Lambda APIG proxy integration.
 *
 * @param {Object} event - HTTP input
 *
 * @return {Object} HTTP output
 */
module.exports.sendData = async event => {
  try {
    const message = 'Hello World';
    const html = `<html><head><meta charset="utf-8" /></head><body><div>${message}</div></body></html>`;
    return sendRes.html(200, html, { 'x-extra-header': true });
  } catch (err) {
    console.log('error: ', err); // eslint-disable-line no-console

    const statusCode = err.statusCode || 500;

    const html = `<html><head><meta charset="utf-8" /></head><body><div>${
      err.message
    }</div></body></html>`;

    return sendRes.json(statusCode, html, { 'extra-header': 'kaput' });
  }
};

Publish

For now we publish manually using:

npm publish --access public

Make sure:

  • You increment the npm version after you make code changes with npm version.
  • You're logged in.

More information can be found here.