google-function-resource
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 = 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 { tasks}
Add the library to package.json:
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.
Request Body | Response (201) |
---|---|
|
|
List Resources
GET /resources
Returns a list of resources with pagination. Default page size is 20.
Request Query Parameters | Response (200) |
---|---|
|
Body:
Headers:
|
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.
Request URI | Response (200) |
---|---|
|
|
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.
Request Body | Response (200) |
---|---|
|
|
Destroy Resource
DELETE /resources/:id
Removes a resource.
Request Body | Response (204) |
---|---|
(empty) | (empty) |
Configuration
Settings can be customized upon requiring the library, and have the following defaults:
const tasks = // 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: GstoredefaultValuesNOW 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 { tasks}
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