Skip to content

Commit 9c15971

Browse files
stew-rAce Nassri
authored and
Ace Nassri
committed
Make docstrings consistent; add aliasing to make different event types clear (#692)
* Make docstrings consistent; add aliasing * Fix spacing issues, add `context` parameter to helloBackground
1 parent e79ee37 commit 9c15971

File tree

1 file changed

+29
-24
lines changed

1 file changed

+29
-24
lines changed

functions/node8/index.js

+29-24
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
* Background Cloud Function that only executes within a certain time
1919
* period after the triggering event to avoid infinite retry loops.
2020
*
21-
* @param {object} event The Cloud Functions event payload.
22-
* @param {function} context Information about the event.
21+
* @param {object} data The event payload.
22+
* @param {object} context The event metadata.
2323
*/
2424
exports.avoidInfiniteRetries = (data, context) => {
2525
const eventAge = Date.now() - Date.parse(context.timestamp);
@@ -41,8 +41,8 @@ exports.avoidInfiniteRetries = (data, context) => {
4141
* Background Cloud Function that demonstrates
4242
* how to toggle retries using a promise
4343
*
44-
* @param {object} data The Cloud Functions event payload.
45-
* @param {function} context The Cloud Functions metadata.
44+
* @param {object} data The event payload.
45+
* @param {object} context The event metadata.
4646
*/
4747
exports.retryPromise = (data, context) => {
4848
const tryAgain = !!data.retry;
@@ -59,9 +59,10 @@ exports.retryPromise = (data, context) => {
5959
/**
6060
* Background Cloud Function.
6161
*
62-
* @param {object} data Data passed to the Cloud Function.
62+
* @param {object} data The event payload.
63+
* @param {object} context The event metadata.
6364
*/
64-
exports.helloBackground = (data) => {
65+
exports.helloBackground = (data, context) => {
6566
return `Hello ${data.name || 'World'}!`;
6667
};
6768
// [END functions_helloworld_background_node8]
@@ -72,11 +73,12 @@ exports.helloBackground = (data) => {
7273
* This function is exported by index.js, and executed when
7374
* the trigger topic receives a message.
7475
*
75-
* @param {object} data The Cloud Functions event payload.
76-
* @param {object} context The Cloud Functions metadata.
76+
* @param {object} data The event payload.
77+
* @param {object} context The event metadata.
7778
*/
78-
exports.helloPubSub = (data) => {
79-
const name = data.data ? Buffer.from(data.data, 'base64').toString() : 'World';
79+
exports.helloPubSub = (data, context) => {
80+
const pubSubMessage = data;
81+
const name = pubSubMessage.data ? Buffer.from(pubSubMessage.data, 'base64').toString() : 'World';
8082

8183
console.log(`Hello, ${name}!`);
8284
};
@@ -86,17 +88,19 @@ exports.helloPubSub = (data) => {
8688
/**
8789
* Background Cloud Function to be triggered by Cloud Storage.
8890
*
89-
* @param {object} data The Cloud Functions event payload.
91+
* @param {object} data The event payload.
92+
* @param {object} context The event metadata.
9093
*/
91-
exports.helloGCS = (data) => {
92-
if (data.resourceState === 'not_exists') {
93-
console.log(`File ${data.name} deleted.`);
94-
} else if (data.metageneration === '1') {
94+
exports.helloGCS = (data, context) => {
95+
const file = data;
96+
if (file.resourceState === 'not_exists') {
97+
console.log(`File ${file.name} deleted.`);
98+
} else if (file.metageneration === '1') {
9599
// metageneration attribute is updated on metadata changes.
96100
// on create value is 1
97-
console.log(`File ${data.name} uploaded.`);
101+
console.log(`File ${file.name} uploaded.`);
98102
} else {
99-
console.log(`File ${data.name} metadata updated.`);
103+
console.log(`File ${file.name} metadata updated.`);
100104
}
101105
};
102106
// [END functions_helloworld_storage_node8]
@@ -105,16 +109,17 @@ exports.helloGCS = (data) => {
105109
/**
106110
* Generic background Cloud Function to be triggered by Cloud Storage.
107111
*
108-
* @param {object} data The Cloud Functions event payload.
109-
* @param {object} context The Cloud Functions event metadata.
112+
* @param {object} data The event payload.
113+
* @param {object} context The event metadata.
110114
*/
111115
exports.helloGCSGeneric = (data, context) => {
116+
const file = data;
112117
console.log(` Event ${context.eventId}`);
113118
console.log(` Event Type: ${context.eventType}`);
114-
console.log(` Bucket: ${data.bucket}`);
115-
console.log(` File: ${data.name}`);
116-
console.log(` Metageneration: ${data.metageneration}`);
117-
console.log(` Created: ${data.timeCreated}`);
118-
console.log(` Updated: ${data.updated}`);
119+
console.log(` Bucket: ${file.bucket}`);
120+
console.log(` File: ${file.name}`);
121+
console.log(` Metageneration: ${file.metageneration}`);
122+
console.log(` Created: ${file.timeCreated}`);
123+
console.log(` Updated: ${file.updated}`);
119124
};
120125
// [END functions_helloworld_storage_generic_node8]

0 commit comments

Comments
 (0)