Skip to content

Commit eab1c70

Browse files
committed
Update more samples.
1 parent 0d867a8 commit eab1c70

File tree

20 files changed

+415
-408
lines changed

20 files changed

+415
-408
lines changed

functions/background/index.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@
2020
* Background Cloud Function.
2121
*
2222
* @param {object} event The Cloud Functions event.
23-
* @param {object} event.payload The event payload.
23+
* @param {object} event.data The event data.
2424
* @param {function} The callback function.
2525
*/
2626
exports.helloWorld = function helloWorld (event, callback) {
27-
if (!event.payload.myMessage) {
27+
if (!event.data.myMessage) {
2828
// This is an error case, "myMessage" is required
2929
callback(new Error('No message defined!'));
3030
} else {
3131
// Everything is ok
32-
console.log(event.payload.myMessage);
32+
console.log(event.data.myMessage);
3333
callback();
3434
}
3535
};
@@ -41,14 +41,14 @@ exports.helloWorld = function helloWorld (event, callback) {
4141
* a "context" argument to the function.
4242
*
4343
* @param {object} event The Cloud Functions event.
44-
* @param {object} event.payload The event payload.
44+
* @param {object} event.data The event data.
4545
* @returns {Promise}
4646
*/
4747
exports.helloPromise = function helloPromise (event) {
4848
const request = require('request-promise');
4949

5050
return request({
51-
uri: event.payload.endpoint
51+
uri: event.data.endpoint
5252
});
5353
};
5454
// [END helloPromise]
@@ -59,11 +59,11 @@ exports.helloPromise = function helloPromise (event) {
5959
* a "context" argument to the function.
6060
*
6161
* @param {object} event The Cloud Functions event.
62-
* @param {object} event.payload The event payload.
62+
* @param {object} event.data The event data.
6363
*/
6464
exports.helloSynchronous = function helloSynchronous (event) {
6565
// This function returns synchronously
66-
if (event.payload.something === true) {
66+
if (event.data.something === true) {
6767
return 'Something is true!';
6868
} else {
6969
throw new Error('Something was not true!');

functions/background/test/index.test.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ function getSample () {
3333
describe(`functions:background`, () => {
3434
it(`should echo message`, () => {
3535
const event = {
36-
payload: {
36+
data: {
3737
myMessage: `hi`
3838
}
3939
};
@@ -43,7 +43,7 @@ describe(`functions:background`, () => {
4343
sample.program.helloWorld(event, callback);
4444

4545
assert.equal(console.log.callCount, 1);
46-
assert.deepEqual(console.log.firstCall.args, [event.payload.myMessage]);
46+
assert.deepEqual(console.log.firstCall.args, [event.data.myMessage]);
4747
assert.equal(callback.callCount, 1);
4848
assert.deepEqual(callback.firstCall.args, []);
4949
});
@@ -52,7 +52,7 @@ describe(`functions:background`, () => {
5252
const error = new Error(`No message defined!`);
5353
const callback = sinon.stub();
5454
const sample = getSample();
55-
sample.program.helloWorld({ payload: {} }, callback);
55+
sample.program.helloWorld({ data: {} }, callback);
5656

5757
assert.equal(callback.callCount, 1);
5858
assert.deepEqual(callback.firstCall.args, [error]);
@@ -61,7 +61,7 @@ describe(`functions:background`, () => {
6161
it(`should make a promise request`, () => {
6262
const sample = getSample();
6363
const event = {
64-
payload: {
64+
data: {
6565
endpoint: `foo.com`
6666
}
6767
};
@@ -75,7 +75,7 @@ describe(`functions:background`, () => {
7575

7676
it(`should return synchronously`, () => {
7777
assert.equal(getSample().program.helloSynchronous({
78-
payload: {
78+
data: {
7979
something: true
8080
}
8181
}), `Something is true!`);
@@ -84,7 +84,7 @@ describe(`functions:background`, () => {
8484
it(`should throw an error`, () => {
8585
assert.throws(() => {
8686
getSample().program.helloSynchronous({
87-
payload: {
87+
data: {
8888
something: false
8989
}
9090
});

functions/datastore/README.md

+8-8
Original file line numberDiff line numberDiff line change
@@ -21,31 +21,31 @@ Functions for your project.
2121

2222
1. Create a Cloud Storage Bucket to stage our deployment:
2323

24-
gsutil mb gs://[YOUR_BUCKET_NAME]
24+
gsutil mb gs://YOUR_BUCKET_NAME
2525

26-
* Replace `[YOUR_BUCKET_NAME]` with the name of your Cloud Storage Bucket.
26+
* Replace `YOUR_BUCKET_NAME` with the name of your Cloud Storage Bucket.
2727

2828
1. Ensure the Cloud Datastore API is enabled:
2929

3030
[Click here to enable the Cloud Datastore API](https://console.cloud.google.com/flows/enableapi?apiid=datastore.googleapis.com&redirect=https://github.com/GoogleCloudPlatform/nodejs-docs-samples/tree/master/functions/datastore)
3131

3232
1. Deploy the "get" function with an HTTP trigger:
3333

34-
gcloud alpha functions deploy get --stage-bucket [YOUR_BUCKET_NAME] --trigger-http
34+
gcloud alpha functions deploy get --stage-bucket YOUR_BUCKET_NAME --trigger-http
3535

36-
* Replace `[YOUR_BUCKET_NAME]` with the name of your Cloud Storage Bucket.
36+
* Replace `YOUR_BUCKET_NAME` with the name of your Cloud Storage Bucket.
3737

3838
1. Deploy the "set" function with an HTTP trigger:
3939

40-
gcloud alpha functions deploy set --stage-bucket [YOUR_BUCKET_NAME] --trigger-http
40+
gcloud alpha functions deploy set --stage-bucket YOUR_BUCKET_NAME --trigger-http
4141

42-
* Replace `[YOUR_BUCKET_NAME]` with the name of your Cloud Storage Bucket.
42+
* Replace `YOUR_BUCKET_NAME` with the name of your Cloud Storage Bucket.
4343

4444
1. Deploy the "del" function with an HTTP trigger:
4545

46-
gcloud alpha functions deploy del --stage-bucket [YOUR_BUCKET_NAME] --trigger-http
46+
gcloud alpha functions deploy del --stage-bucket YOUR_BUCKET_NAME --trigger-http
4747

48-
* Replace `[YOUR_BUCKET_NAME]` with the name of your Cloud Storage Bucket.
48+
* Replace `YOUR_BUCKET_NAME` with the name of your Cloud Storage Bucket.
4949

5050
1. Call the "set" function to create a new entity:
5151

functions/errorreporting/index.js

+47-42
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,29 @@
1-
// Copyright 2016, Google, Inc.
2-
// Licensed under the Apache License, Version 2.0 (the "License");
3-
// you may not use this file except in compliance with the License.
4-
// You may obtain a copy of the License at
5-
//
6-
// http://www.apache.org/licenses/LICENSE-2.0
7-
//
8-
// Unless required by applicable law or agreed to in writing, software
9-
// distributed under the License is distributed on an "AS IS" BASIS,
10-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11-
// See the License for the specific language governing permissions and
12-
// limitations under the License.
1+
/**
2+
* Copyright 2016, Google, Inc.
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
1315

1416
'use strict';
1517

1618
// [START setup]
17-
var Logging = require('@google-cloud/logging');
19+
const Logging = require('@google-cloud/logging');
1820

19-
// Instantiate a logging client
20-
var logging = Logging();
21+
// Instantiates a client
22+
const logging = Logging();
2123
// [END setup]
2224

2325
// [START reportDetailedError]
24-
var reportDetailedError = require('./report');
26+
const reportDetailedError = require('./report');
2527
// [END reportDetailedError]
2628

2729
// [START helloSimpleErrorReport]
@@ -36,22 +38,22 @@ function reportError (err, callback) {
3638
// This is the name of the StackDriver log stream that will receive the log
3739
// entry. This name can be any valid log stream name, but must contain "err"
3840
// in order for the error to be picked up by StackDriver Error Reporting.
39-
var logName = 'errors';
40-
var log = logging.log(logName);
41+
const logName = 'errors';
42+
const log = logging.log(logName);
4143

4244
// https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/MonitoredResource
43-
var monitoredResource = {
45+
const monitoredResource = {
4446
type: 'cloud_function',
4547
labels: {
4648
function_name: process.env.FUNCTION_NAME
4749
}
4850
};
4951

5052
// https://cloud.google.com/error-reporting/reference/rest/v1beta1/ErrorEvent
51-
var errorEvent = {
53+
const errorEvent = {
5254
message: err.stack,
5355
serviceContext: {
54-
service: 'cloud_function:' + process.env.FUNCTION_NAME,
56+
service: `cloud_function:${process.env.FUNCTION_NAME}`,
5557
version: require('./package.json').version || 'unknown'
5658
}
5759
};
@@ -65,23 +67,23 @@ function reportError (err, callback) {
6567
/**
6668
* HTTP Cloud Function.
6769
*
68-
* @param {Object} req Cloud Function request object.
69-
* @param {Object} res Cloud Function response object.
70+
* @param {object} req Cloud Function request context.
71+
* @param {object} res Cloud Function response context.
7072
*/
7173
exports.helloSimpleError = function helloSimpleError (req, res) {
7274
try {
7375
if (req.method !== 'GET') {
74-
var error = new Error('Only GET requests are accepted!');
76+
const error = new Error('Only GET requests are accepted!');
7577
error.code = 405;
7678
throw error;
7779
}
7880
// All is good, respond to the HTTP request
79-
return res.send('Hello World!');
81+
res.send('Hello World!');
8082
} catch (err) {
8183
// Report the error
82-
return reportError(err, function () {
84+
reportError(err, () => {
8385
// Now respond to the HTTP request
84-
return res.status(error.code || 500).send(err.message);
86+
res.status(err.code || 500).send(err.message);
8587
});
8688
}
8789
};
@@ -91,25 +93,27 @@ exports.helloSimpleError = function helloSimpleError (req, res) {
9193
/**
9294
* HTTP Cloud Function.
9395
*
94-
* @param {Object} req Cloud Function request object.
95-
* @param {Object} res Cloud Function response object.
96+
* @param {object} req Cloud Function request context.
97+
* @param {object} req.body The request body.
98+
* @param {string} req.body.message Message provided in the request.
99+
* @param {object} res Cloud Function response context.
96100
*/
97101
exports.helloHttpError = function helloHttpError (req, res) {
98102
try {
99103
if (req.method !== 'POST' && req.method !== 'GET') {
100-
var error = new Error('Only POST and GET requests are accepted!');
104+
const error = new Error('Only POST and GET requests are accepted!');
101105
error.code = 405;
102106
throw error;
103107
}
104108
// All is good, respond to the HTTP request
105-
return res.send('Hello ' + (req.body.message || 'World') + '!');
109+
res.send(`Hello ${req.body.message || 'World'}!`);
106110
} catch (err) {
107111
// Set the response status code before reporting the error
108112
res.status(err.code || 500);
109113
// Report the error
110-
return reportDetailedError(err, req, res, function () {
114+
reportDetailedError(err, req, res, () => {
111115
// Now respond to the HTTP request
112-
return res.send(err.message);
116+
res.send(err);
113117
});
114118
}
115119
};
@@ -119,22 +123,23 @@ exports.helloHttpError = function helloHttpError (req, res) {
119123
/**
120124
* Background Cloud Function.
121125
*
122-
* @param {Object} context Cloud Function context object.
123-
* @param {Object} data Request data, provided by a trigger.
124-
* @param {string} data.message Message, provided by the trigger.
126+
* @param {object} event The Cloud Functions event.
127+
* @param {object} event.data The event data.
128+
* @param {string} event.data.message Message, provided by the trigger.
129+
* @param {function} The callback function.
125130
*/
126-
exports.helloBackgroundError = function helloBackgroundError (context, data) {
131+
exports.helloBackgroundError = function helloBackgroundError (event, callback) {
127132
try {
128-
if (!data.message) {
133+
if (!event.data.message) {
129134
throw new Error('"message" is required!');
130135
}
131136
// All is good, respond with a message
132-
return context.success('Hello World!');
137+
callback(null, 'Hello World!');
133138
} catch (err) {
134139
// Report the error
135-
return reportDetailedError(err, function () {
136-
// Now finish mark the execution failure
137-
return context.failure(err.message);
140+
reportDetailedError(err, () => {
141+
// Now finish and mark the execution as a failure
142+
callback(err);
138143
});
139144
}
140145
};

functions/errorreporting/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
"test": "mocha -R spec -t 120000 --require intelli-espower-loader ../../test/_setup.js test/*.test.js"
1010
},
1111
"dependencies": {
12-
"@google-cloud/logging": "^0.1.1"
12+
"@google-cloud/logging": "^0.5.0"
1313
},
1414
"devDependencies": {
15-
"mocha": "^3.0.2"
15+
"mocha": "^3.1.2"
1616
}
1717
}

0 commit comments

Comments
 (0)