Skip to content

Commit

Permalink
docs: update cloudevent function docs to use declarative API (#381)
Browse files Browse the repository at this point in the history
This commit updates our README and docs/ to use declarative function
signatures for cloudevent functions.
  • Loading branch information
matthewrobertson committed Nov 5, 2021
1 parent 11d35b3 commit 81ba122
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
16 changes: 13 additions & 3 deletions README.md
Expand Up @@ -228,18 +228,28 @@ It will be passed as an argument to your function when it receives a request.
Note that your function must use the `cloudevent`-style function signature:

```js
exports.helloCloudEvents = (cloudevent) => {
const functions = require('@google-cloud/functions-framework');

functions.cloudEvent('helloCloudEvents', (cloudevent) => {
console.log(cloudevent.specversion);
console.log(cloudevent.type);
console.log(cloudevent.source);
console.log(cloudevent.subject);
console.log(cloudevent.id);
console.log(cloudevent.time);
console.log(cloudevent.datacontenttype);
};
});
```

To enable CloudEvents, set the signature type to `cloudevent`. By default, the HTTP signature will be used and automatic event unmarshalling will be disabled.
To enable the CloudEvent functions, you must list the Functions Framework as a dependency in your `package.json`:

```json
{
"dependencies": {
"@google-cloud/functions-framework": "~2.0.0-beta.1"
}
}
```

Learn how to use CloudEvents in this [guide](docs/cloudevents.md).

Expand Down
26 changes: 17 additions & 9 deletions docs/cloudevents.md
Expand Up @@ -7,27 +7,35 @@ This guide shows you how to use the Functions Framework for local testing with:

## Local Testing of CloudEvents

In your `package.json`, specify `--signature-type=cloudevent"` for the `functions-framework`:
Install `@google-cloud/functions-framework` and list it as a dependency in your `package.json`:

```sh
{
"scripts": {
"start": "functions-framework --target=helloCloudEvents --signature-type=cloudevent"
}
}
```
npm install --save @google-cloud/functions-framework
```

Create an `index.js` file:
Create an `index.js` file and declare your function:

```js
exports.helloCloudEvents = (cloudevent) => {
const functions = require('@google-cloud/functions-framework');

functions.cloudEvent('helloCloudEvents', (cloudevent) => {
console.log(cloudevent.specversion);
console.log(cloudevent.type);
console.log(cloudevent.source);
console.log(cloudevent.subject);
console.log(cloudevent.id);
console.log(cloudevent.time);
console.log(cloudevent.datacontenttype);
});
```

Add a `package.json` script to start the Functions Framework and pass the name of your function as the `target`.

```sh
{
"scripts": {
"start": "functions-framework --target=helloCloudEvents"
}
}
```

Expand Down

0 comments on commit 81ba122

Please sign in to comment.