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

gcf-decorators

v0.0.3

Published

Create a decorator-based framework for google cloud functions

Downloads

2

Readme

Decorators for Google Cloud Functions

Google Cloud Functions is an amazing product that lowers the threshold to develop and deploy code. However, it's still a little raw and lacks many basic validations. Very quickly, developers find themselves repeating the same set of validations across functions, and to that end, this library is meant to aid in making code DRY and clean, allowing developers to focus on core business logic.

Method Decorators

The library currently only supports using typescript method decorators, which means functions should be in classes instead of in a single file or declared as functions.

NOTE: Incorrect

exports function helloHttp = (req, res) => {
  res.send(`Hello World!`);
};

Instead, use classes.

class HelloApi {
  static async helloHttp(req, res) {
    res.send(`Hello World!`);
  }
}

Cors

Google's recommendation for handling CORS can be a bit gnarly, especially if the majority of cloud functions need to be cors enabled. With this library, we simply apply the @cors decorator to the methods that are API's:

class HelloWorldApi {
  @cors() // substitute you own domains here, default is '*'
  static async helloWorld(req, res) {
    return res.status(200).send('Hello world')
  }
}

// In index.ts, do the following:
export const helloWorldApi = {
  helloWorld: HelloWorldApi.helloWorld,
}

Http Methods

Similarly for HTTP methods, Google's official recommendation is extremely verbose. Instead, with this library, we can automatically enforce the correct HTTP method:

class HelloWorldApi {
  @post() // anything other than a GET will automatically return a 404, with empty json
  static async helloWorld(req, res) {
    // Do cool POST things
    return res.status(200).send('Hello world')
  }
}

// In index.ts, do the following:
export const helloWorldApi = {
  helloWorld: HelloWorldApi.helloWorld,
}

More powerfully, by coupling this library with tsc-parsers, we can enforce POST and GET parameters of even complex types.

class HelloWorldApi {
  @post({
    message: StringValidator, // Any non-string auto rejected with 400
    count: IntValidator,      // Any non-integer auto rejected with 400
  })
  static async helloWorld(req, res) {
    // Do cool POST things
    return res.status(200).send('Hello world')
  }
}

// In index.ts, do the following:
export const helloWorldApi = {
  helloWorld: HelloWorldApi.helloWorld,
}

Authorization

Currently, this library only supports Basic Authentication.

class HelloWorldApi {
  @basicauth(process.env.SUPER_SECRET_PASSWORD)
  @post() // anything other than a GET will automatically return a 404, with empty json
  static async helloWorld(req, res) {
    // Do cool POST things
    return res.status(200).send('Hello world')
  }
}

// In index.ts, do the following:
export const helloWorldApi = {
  helloWorld: HelloWorldApi.helloWorld,
}