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

create-response-object

v1.3.0

Published

A very, very tiny object creator for lambda-proxy resopnses.

Downloads

19

Readme

create-response-object

NPM Module. A very, very tiny object creator for lambda-proxy responses. Primarily for 200 and 400 responses with an optional message. Also supports 301 and 302 responses with redirect URI.

Install

npm install --save create-response-object

Usage

let params = {
  code: '200',
  message: 'Hello world!'
};

createResponseObject(params);

Parameter Options

  • code: (optional but recommended) The HTTP status code to return. Defaults to '200'.
    {string} '200', '301', '302', '400', etc
  • uri: (required only for 301|302 codes) The redirect uri.
  • message: (optional) The response body's message.
    {string} 'Hello World!'
  • cors: (optional) Adds CORS headers to the response.
    • allowHeaders (optional) List of allowed headers separated by comma.
      {string} Defaults to 'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'
    • allowOrigin (optional) Origin to include in the CORS response.
      {string} Defaults to '*'
    • allowMethods (optional) List of allowed headers seperated by comma.
      {string} Defaults to '*'

Examples

Import or require the module:

import createResponseObject from 'create-response-object';           // ES Modulees import  
const createResponseObject = require('create-response-object');      // CommonJS import  

Return a plain 400 response:

exports.handler = async (event, context) => {
  return await createResponseObject({
    code: '400',
    message: 'Something went wrong!'
  });
};

Will output:

{
    "statusCode": "400",
    "body": "Something went wrong!",
    "headers": {
        "Content-Type": "application/json"
    }
}

Return a 200 response with CORS headers:

exports.handler = async (event, context) => {
  return await createResponseObject({
    code: '200',
    message: 'Hello World!'
    cors: {
      allowHeaders: 'Content-Type',
      allowOrigin: 'http://localhost:3000',
      allowMethods: 'OPTIONS,GET'
    }
  });
};

Will output:

{
    "statusCode": "200",
    "body": "Hello world!",
    "headers": {
        "Access-Control-Allow-Headers": "Content-Type",
        "Access-Control-Allow-Methods": "OPTIONS,GET",
        "Access-Control-Allow-Origin": "http://localhost:3000",
        "Content-Type": "application/json"
    }
}

Return a 301 response with redirect:

exports.handler = async (event, context) => {
  return await createResponseObject({
    code: '301',
    uri: 'https://www.npmjs.com/package/create-response-object'
  });
};

Will output:

{
    "statusCode": "301",
    "body": "Hello World!",
    "headers": {
        "Location": "https://www.npmjs.com/package/create-response-object"
    }
}

Credit

This function, as small as it is, draws inspiration from jroberson.