diff --git a/functions/background/index.js b/functions/background/index.js index 46fe9e82cf..2d195d8d6b 100644 --- a/functions/background/index.js +++ b/functions/background/index.js @@ -36,6 +36,8 @@ exports.helloWorld = (event, callback) => { // [END functions_background_helloworld] // [START functions_background_promise] +const requestPromiseNative = require('request-promise-native'); + /** * Background Cloud Function that returns a Promise. Note that we don't pass * a "callback" argument to the function. @@ -45,9 +47,7 @@ exports.helloWorld = (event, callback) => { * @returns {Promise} */ exports.helloPromise = (event) => { - const request = require('request-promise'); - - return request({ + return requestPromiseNative({ uri: event.data.endpoint }); }; diff --git a/functions/background/package.json b/functions/background/package.json index 4a28cd2681..85feedf166 100644 --- a/functions/background/package.json +++ b/functions/background/package.json @@ -18,7 +18,7 @@ }, "dependencies": { "request": "2.83.0", - "request-promise": "4.2.2" + "request-promise-native": "^1.0.5" }, "devDependencies": { "@google-cloud/nodejs-repo-tools": "^3.0.0", diff --git a/functions/background/test/index.test.js b/functions/background/test/index.test.js index 6f7e73b9dd..a3654b4228 100644 --- a/functions/background/test/index.test.js +++ b/functions/background/test/index.test.js @@ -21,14 +21,14 @@ const test = require(`ava`); const tools = require(`@google-cloud/nodejs-repo-tools`); function getSample () { - const requestPromise = sinon.stub().returns(Promise.resolve(`test`)); + const requestPromiseNative = sinon.stub().returns(Promise.resolve(`test`)); return { program: proxyquire(`../`, { - 'request-promise': requestPromise + 'request-promise-native': requestPromiseNative }), mocks: { - requestPromise: requestPromise + requestPromiseNative: requestPromiseNative } }; } @@ -73,7 +73,7 @@ test.serial(`should make a promise request`, (t) => { return sample.program.helloPromise(event) .then((result) => { - t.deepEqual(sample.mocks.requestPromise.firstCall.args, [{ uri: `foo.com` }]); + t.deepEqual(sample.mocks.requestPromiseNative.firstCall.args, [{ uri: `foo.com` }]); t.is(result, `test`); }); }); diff --git a/functions/node8/index.js b/functions/node8/index.js index ece8dccbbf..47191eba9d 100644 --- a/functions/node8/index.js +++ b/functions/node8/index.js @@ -224,6 +224,40 @@ exports.helloAnalytics = (data, context) => { }; // [END functions_firebase_analytics_node8] +// [START functions_background_promise_node8] +const requestPromiseNative = require('request-promise-native'); + +/** + * Background Cloud Function that returns a Promise. Note that we don't pass + * a "callback" argument to the function. + * + * @param {object} data The Cloud Functions event data. + * @returns {Promise} + */ +exports.helloPromise = (data) => { + return requestPromiseNative({ + uri: data.endpoint + }); +}; +// [END functions_background_promise_node8] + +// [START functions_background_synchronous_node8] +/** + * Background Cloud Function that returns synchronously. Note that we don't pass + * a "callback" argument to the function. + * + * @param {object} data The Cloud Functions event data. + */ +exports.helloSynchronous = (data) => { + // This function returns synchronously + if (data.something === true) { + return 'Something is true!'; + } else { + throw new Error('Something was not true!'); + } +}; +// [END functions_background_synchronous_node8] + // [START functions_firebase_reactive_node8] const Firestore = require('@google-cloud/firestore'); diff --git a/functions/node8/package.json b/functions/node8/package.json index 071c641294..fc95af51b8 100644 --- a/functions/node8/package.json +++ b/functions/node8/package.json @@ -27,6 +27,8 @@ "uuid": "^3.3.2" }, "dependencies": { + "request": "^2.88.0", + "request-promise-native": "^1.0.5", "@google-cloud/firestore": "^0.18.0" } } diff --git a/functions/node8/test/index.test.js b/functions/node8/test/index.test.js index 219eac4738..92d4a69420 100644 --- a/functions/node8/test/index.test.js +++ b/functions/node8/test/index.test.js @@ -15,13 +15,15 @@ 'use strict'; -const sinon = require('sinon'); +const sinon = require(`sinon`); const uuid = require('uuid'); const test = require('ava'); const utils = require('@google-cloud/nodejs-repo-tools'); const proxyquire = require(`proxyquire`).noCallThru(); function getSample () { + const requestPromiseNative = sinon.stub().returns(Promise.resolve(`test`)); + const firestoreMock = { doc: sinon.stub().returnsThis(), set: sinon.stub() @@ -29,10 +31,12 @@ function getSample () { return { program: proxyquire(`../`, { + 'request-promise-native': requestPromiseNative, '@google-cloud/firestore': sinon.stub().returns(firestoreMock) }), mocks: { - firestore: firestoreMock + firestore: firestoreMock, + requestPromiseNative: requestPromiseNative } }; } @@ -155,6 +159,33 @@ test.serial('should monitor Analytics', t => { t.is(console.log.args[4][0], `Location: London, UK`); }); +test.serial(`should make a promise request`, (t) => { + const sample = getSample(); + const data = { + endpoint: `foo.com` + }; + + return sample.program.helloPromise(data) + .then((result) => { + t.deepEqual(sample.mocks.requestPromiseNative.firstCall.args, [{ uri: `foo.com` }]); + t.is(result, `test`); + }); +}); + +test.serial(`should return synchronously`, (t) => { + t.is(getSample().program.helloSynchronous({ + something: true + }), `Something is true!`); +}); + +test.serial(`should throw an error`, (t) => { + t.throws(() => { + getSample().program.helloSynchronous({ + something: false + }); + }, Error, `Something was not true!`); +}); + test(`should update data in response to Firestore events`, t => { const sample = getSample();