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

google-function-resource

v0.2.0

Published

Resource management library for Google HTTP Functions

Downloads

8

Readme

google-function-resource

Build Status Test Coverage Code Climate npm version

A simple resource management system for Google Cloud HTTP Functions.

It stores data in Google Datastore using gstore-node.

Usage

For a resource named "Task" for example, create a new Google HTTP Function to manage your resource with the following code:

const tasks = require('google-function-resource')({
  name: 'Task',
  schema: {
    title: {
      type: 'string',
      required: true
    },
    description: {
      type: 'string'
    },
    createdOn: {
      type: 'datetime',
      write: false,
      excludeFromIndexes: true
    },
    modifiedOn: {
      type: 'datetime',
      write: false,
      excludeFromIndexes: true
    }
  }
})

exports.handleRequest = function (req, res) {
  tasks.manage(req, res)
}

Add the library to package.json:

{
  "name": "your-function",
  "version": "0.0.1",
  "dependencies": {
    "google-function-resource": "0.2.0"
  }
}

Finally, make sure the entry point is correct. In the example above, it should be handleRequest.

Then, assuming you named your function "tasks", the following endpoints will be served by your function:

  • POST /tasks
  • GET /tasks
  • GET /tasks/:id
  • PUT|PATCH /tasks/:id
  • DELETE /tasks/:id
  • OPTIONS /tasks

Read more about how each endpoint works in the next section.

Actions

For the next sections, keep in mind that the resource endpoint is determined by the name of your function. So when this document says:

  • POST /resources

And your function is named "tasks", then the correct endpoint will be:

  • POST /tasks

Create Resource

  • POST /resources

This endpoint creates a new resource.

{
  "title": "My New Task",
  "description": "description of my new task"
}
{
  "id": "12345",
  "title": "My New Task",
  "description": "description of my new task",
  "createdOn": "..."
}

List Resources

  • GET /resources

Returns a list of resources with pagination. Default page size is 20.

  • /resources
    • first 20 resources
  • /resources?limit=10
    • first 10 resources
  • /resources?start=NextPageKey000123
    • first 20 resources
    • starting from key "NextPageKey000123"

Body:

[
  {"id": "1", ...},
  {"id": "2", ...},
  ...
]

Headers:

  • X-Page-Size: 20
  • X-Next-Page-Cursor: "NextPageKey000123"

The X-Next-Page-Cursor header will be absent if there are no more entries to fetch.

(Filters and sorting are not yet supported.)

Show Resource

  • GET /resources/:id

Returns data of a single resource.

/resources/12345

{
  "id": "12345",
  "title": "My Task",
  "description": "description of task",
  "createdOn": "..."
}

Update Resource

  • PUT /resources/:id
  • PATCH /resources/:id

Updates data of a single resource.

Both PUT and PATCH methods behave the same way, and partial data can be provided.

{
  "title": "My edited Task"
}
{
  "id": "12345",
  "title": "My edited Task",
  // ... rest of fields
}

Destroy Resource

  • DELETE /resources/:id

Removes a resource.

Configuration

Settings can be customized upon requiring the library, and have the following defaults:

const tasks = require('google-function-resource')({

  // Resource name. Must be set!
  // It will be used as the entity name in Datastore.
  // Recommended format: singular, capitalized. Ex: "Task"
  name: null,

  // Datastore settings.
  datastore: {
    namespace: undefined,

    // Default page size when listing resources.
    limit: 20
  },

  // You must provide the full schema.
  // This is just a working placeholder.
  schema: {
    name: {
      type: 'string',
      excludeFromIndexes: true
    },
    createdOn: {
      type: 'datetime',
      write: false,
      default: Gstore.defaultValues.NOW,
      excludeFromIndexes: true
    },
    modifiedOn: {
      type: 'datetime',
      write: false,
      excludeFromIndexes: true
    }
  },

  // Customize CORS headers here.
  cors: {
    'Access-Control-Allow-Methods': 'GET, POST, PUT, PATCH, DELETE',
    'Access-Control-Allow-Headers': 'origin, content-type, accept',

    // Better secure your API by allowing only specific domains.
    'Access-Control-Allow-Origin': '*',

    // Make sure you keep the exposed headers below
    //   or pagination may fail on the client side.
    'Access-Control-Expose-Headers': 'x-next-page-cursor, x-page-size'
  }

})

exports.handleRequest = function (req, res) {
  tasks.manage(req, res)
}

If you want to customize Schema validators with your own functions, take a look at the Gstore Schema documentation.

TODO/Wishlist

  • Google reCAPTCHA support on resource creation and update.
  • Support other data stores (like MySQL).

License

MIT