0.2.0 • Published 7 years ago

google-function-resource v0.2.0

Weekly downloads
1
License
MIT
Repository
github
Last release
7 years ago

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