Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions run/events-pubsub/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

// [START run_events_pubsub_handler]
const express = require('express');
const {toMessagePublishedEvent} = require('@google/events/cloud/pubsub/v1/MessagePublishedData');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit - I don't imagine someone can do something like this?

const {toMessagePublishedEvent} = require('@google/events/cloud/pubsub/v1').MessagePublishedData;

(I ask because this format makes reverse engineering/"exploring" the "type definition universe" a bit easier, via Node's built-in debugging tools.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, we can do that. Going to hold off for right now though.

const app = express();

app.use(express.json());
Expand All @@ -20,9 +21,10 @@ app.post('/', (req, res) => {
console.log(`Bad Request: ${errorMessage}`);
return;
}
const pubSubMessage = req.body.message;
const name = pubSubMessage.data
? Buffer.from(pubSubMessage.data, 'base64').toString().trim()
// Cast to MessagePublishedEvent for IDE autocompletion
const pubSubMessage = toMessagePublishedEvent(req.body);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: would message be too non-descript here? (It's shorter 🙂 )

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be non-descript.

I'm not going to write data.message.data.

(Flashbacks to data.data.data)

const name = pubSubMessage.message && pubSubMessage.message.data
? Buffer.from(pubSubMessage.message.data, 'base64').toString().trim()
: 'World';

const result = `Hello, ${name}! ID: ${req.get('ce-id') || ''}`;
Expand Down
1 change: 1 addition & 0 deletions run/events-pubsub/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"system-test": "test/runner.sh mocha test/system.test.js --timeout=10000"
},
"dependencies": {
"@google/events": "^1.4.2",
"express": "^4.16.4"
},
"devDependencies": {
Expand Down