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

serverless-endpoint

v0.1.0

Published

Dependency-less express like wrapper for serverless functions.

Downloads

11

Readme

serverless-endpoint

Dependency-less express like wrapper for serverless functions.

Build Status npm version Coverage Status

Support

✔️️ AWS Api Gateway | ✔️ Google Cloud Functions | ❌ Azure functions | ❌ IBM OpenWhisk

Install

npm install --save serverless-endpoint

usage

Basic Example

// handler

const endpoint = require('serverless-endpoint');

function getHelloWorld(req, res) {

  res.send(200, { message: 'Hello World!' })
}

module.exports.handler = endpoint(getHelloWorld)

Path Parameter Example

// endpoint /hello/{value}

// handler

const endpoint = require('serverless-endpoint');

function getHelloWorld(req, res) {

  res.send(200, { message: `Hello world! ${req.params.value}` })
}

module.exports.handler = endpoint(getHelloWorld)

Query Parameter Example

// endpoint /hello/?timestamp=true

// handler

const endpoint = require('serverless-endpoint');

function getHelloWorld(req, res) {

  const timestamp = req.query.timestamp ? new Date() : ''

  res.send(200, { message: `Hello World! ${timestamp}` })
}

module.exports.handler = endpoint(getHelloWorld)

Body Example

// endpoint /hello/, { data: 'lorem' }

// handler

const endpoint = require('serverless-endpoint');

function getHelloWorld(req, res) {

  res.send(200, { message: `Hello World! ${req.body.data}` })
}

module.exports.handler = endpoint(getHelloWorld)

Cors Response Example

// endpoint /hello

// handler

const endpoint = require('serverless-endpoint');

function getHelloWorld(req, res) {

  res.header({ "Access-Control-Allow-Origin" : "*" })
    .send(200, { message: `Hello World!` })
}

module.exports.handler = endpoint(getHelloWorld)

Cors through Config Response Example

const endpoint = require('serverless-endpoint');

// Config Options
const opts = {
  headers: { "Access-Control-Allow-Origin": "*" }
}

// endpoint /hello
function getHelloWorld(req, res) {

  res.send(200, { message: `Hello World!` })
}

// endpoint /ping
function ping(req, res) {

  res.header({ "Additional-Header": "example" })
    .send(200, { message: `Ping!` })
  // returns  
  // {
  //   statusCode: 200,
  //   body: message,
  //   headers: {
  //     "Access-Control-Allow-Origin": "*",
  //     "Additional-Header": "example"
  //   }
  // }
}

module.exports.getHelloWorld = endpoint(getHelloWorld, opts)
module.exports.ping = endpoint(ping, opts)

Api

endpoint(cloudFunctionHandler, options)

Higher Order Function that abstracts the different cloud function parameters into a single express-like api. Is configurable by options parameter.

options

Properties

| Name | Type | Description | | --- | --- | --- | | headers | Object | default headers to be sent with res.send |

req : Object

Properties

| Name | Type | Description | | --- | --- | --- | | body | Object | http body object sent by request | | method | string | Http method - GET, PUT, POST, DELETE, etc.. | | path | string | A cleaned url string | | resource | string | base resource of url | | headers | Object | header object containing all header information | | params | Object | parameters object from url path - /resource/{id} = { id: <value> } | | query | Object | query parameters object from url - /resource?sort=asc = { sort: 'asc' } | | id | string | AWS Only string id of the request: AWS.event.requestContext.requestId | | apiId | string | AWS Only string apiId: AWS.event.requestContext.apiId | | stage | string | AWS Only api stage from url - /dev/resource = 'dev' | | identity | Object | AWS Only identity of user: event.requestContext.identity | | authorizer | Object | AWS Only object returned from custom authorizer: event.requestContext.authorizer | | header | function | value for the header key - header(headerKey) | | get | function | value for the header key - get(headerKey) | getOriginalRequest | function | AWS Onlyreturns the arguments provided to the http function |

res : Object

Properties

| Name | Type | Description | | --- | --- | --- | | send | function | Sends the HTTP response. | | error | function | AWS Only Returns an error to api gateway. | | header | function | Set header key to value, or pass an object of header fields. | | set | function | Alias for header | | getHeader | function | Get value for header key. | | get | function | Alias for getHeader |

res.send(statusCode, body)

Formats statusCode, body to be sent as a HTTP response back to api consumer (Api Gateway, Google Endpoint). The body parameter can be a a String, an object, or an Array.

Kind: public function Returns: Object - response HTTP response object formatted for Api Gateway.

| Param | Type | Description | | --- | --- | --- | | statusCode | number | Http Response code | | body | string | Object | Array | Response body

res.error(error) AWS Only

returns error to api gateway

Kind: public function Returns: Object - Error to be handed to ApiGateway

| Param | Type | Description | | --- | --- | --- | | err | Object | Caught javascript error |

res.headers(key [, value])

Set header key to value, or pass an object of header fields. Examples:

res.header('Foo', ['bar', 'bot']);
res.header('Content-Type', 'application/json');
res.header({ 'Content-Type': 'text/html', 'X-API-Key': 'buildkey' });

// chaining
res.header({ 'Content-Type': 'text/html' })
  .send(200, html);

Aliased as res.set().

Kind: public function Returns: Res - Returns the same Res for chaining

res.getHeader(key)

Get value for header key. Examples:

res.header('Foo', 'bar');
res.getHeader('Foo');
// Returns 'bar'

Aliased as res.get().

Kind: public function Returns: string - Returns value for header key.