Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New samples for Cloud Functions docs #132

Merged
merged 23 commits into from
Jun 23, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Added promise sample.
  • Loading branch information
jmdobry committed Jun 22, 2016
commit 79ae03384417566caca49ee675834eb8cf63805a
File renamed without changes.
49 changes: 49 additions & 0 deletions functions/background/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2016, Google, Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';

// [START helloworld]
/**
* Background Cloud Function.

Choose a reason for hiding this comment

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

What's a background function?

Copy link
Member Author

Choose a reason for hiding this comment

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

Any function that's triggered, without the "triggerer" waiting for a response. This will be thoroughly documented in the docs.

*
* @param {Object} context Cloud Function context.
* @param {Object} data Request data, provided by a trigger.

Choose a reason for hiding this comment

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

Document that data expects a message property.

Copy link
Member Author

Choose a reason for hiding this comment

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

done

*/
exports.helloworld = function helloworld (context, data) {
if (data.message === undefined) {
// This is an error case, "message" is required
context.failure('No message defined!');
} else {
// Everything is ok
console.log(data.message);
context.success();
}
};
// [END helloworld]

// [START helloPromise]
var request = require('request-promise');

/**
* Background Cloud Function that returns a Promise.
*
* @param {Object} data Request data, provided by a trigger.
* @returns {Promise}
*/
exports.helloPromise = function helloPromise (data) {
return request({
uri: data.endpoint
});
};
// [END helloPromise]
15 changes: 15 additions & 0 deletions functions/background/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "nodejs-docs-samples-functions",
"description": "Node.js samples found on https://cloud.google.com",
"version": "0.0.1",
"private": true,
"license": "Apache Version 2.0",
"author": "Google Inc.",
"repository": {
"type": "git",
"url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git"
},
"dependencies": {
"request-promise": "^3.0.0"
}
}
29 changes: 0 additions & 29 deletions functions/message/index.js

This file was deleted.

11 changes: 4 additions & 7 deletions functions/pubsub/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var pubsub = gcloud.pubsub();
* @param {string} data.topic Topic name on which to publish.
* @param {string} data.message Message to publish.
*/
function publish (context, data) {
exports.publish = function publish (context, data) {
try {
if (!data.topic) {
throw new Error('Topic not provided. Make sure you have a ' +
Expand Down Expand Up @@ -63,7 +63,7 @@ function publish (context, data) {
console.error(err);
return context.failure(err.message);
}
}
};

/**
* Triggered from a message on a Pub/Sub topic.
Expand All @@ -74,13 +74,10 @@ function publish (context, data) {
* @param {Object} data Request data, in this case an object provided by the Pub/Sub trigger.
* @param {Object} data.message Message that was published via Pub/Sub.
*/
function subscribe (context, data) {
exports.subscribe = function subscribe (context, data) {
// We're just going to log the message to prove that it worked!
console.log(data.message);

// Don't forget to call success!
context.success();
}

exports.publish = publish;
exports.subscribe = subscribe;
};