From 216ef26380acd15ebe8eb8029a13c8c30e75249f Mon Sep 17 00:00:00 2001 From: Franziska Hinkelmann Date: Wed, 28 Oct 2020 10:05:13 +0100 Subject: [PATCH] chore: use node-fetch instead of axios See b/171857357. --- appengine/analytics/app.js | 4 ++-- appengine/analytics/package.json | 4 ++-- functions/security/index.js | 10 +++++----- functions/security/package.json | 4 ++-- functions/security/test/index.test.js | 16 +++++++--------- functions/tips/index.js | 4 ++-- functions/tips/package.json | 4 ++-- 7 files changed, 22 insertions(+), 24 deletions(-) diff --git a/appengine/analytics/app.js b/appengine/analytics/app.js index 9f0bc4f26d..1db961bf39 100644 --- a/appengine/analytics/app.js +++ b/appengine/analytics/app.js @@ -16,7 +16,7 @@ // [START gae_flex_analytics_track_event] const express = require('express'); -const axios = require('axios'); +const fetch = require('node-fetch'); const app = express(); app.enable('trust proxy'); @@ -46,7 +46,7 @@ const trackEvent = (category, action, label, value) => { ev: value, }; - return axios.get('http://www.google-analytics.com/debug/collect', { params: data }); + return fetch('http://www.google-analytics.com/debug/collect', { params: data }); }; app.get('/', async (req, res, next) => { diff --git a/appengine/analytics/package.json b/appengine/analytics/package.json index 43954dd608..2b04244fe3 100644 --- a/appengine/analytics/package.json +++ b/appengine/analytics/package.json @@ -18,8 +18,8 @@ "test": "npm run system-test" }, "dependencies": { - "axios": "^0.21.0", - "express": "^4.16.4" + "express": "^4.16.4", + "node-fetch": "^2.6.1" }, "devDependencies": { "mocha": "^8.0.0", diff --git a/functions/security/index.js b/functions/security/index.js index 228595923c..603832f383 100644 --- a/functions/security/index.js +++ b/functions/security/index.js @@ -15,7 +15,7 @@ 'use strict'; // [START functions_bearer_token] -const {get} = require('axios'); +const fetch = require('node-fetch'); // TODO(developer): set these values const REGION = 'us-central1'; @@ -31,19 +31,19 @@ const tokenUrl = metadataServerURL + functionURL; exports.callingFunction = async (req, res) => { // Fetch the token - const tokenResponse = await get(tokenUrl, { + const tokenResponse = await fetch(tokenUrl, { headers: { 'Metadata-Flavor': 'Google', }, }); - const token = tokenResponse.data; + const token = await tokenResponse.text(); // Provide the token in the request to the receiving function try { - const functionResponse = await get(functionURL, { + const functionResponse = await fetch(functionURL, { headers: {Authorization: `bearer ${token}`}, }); - res.status(200).send(functionResponse.data); + res.status(200).send(await functionResponse.text()); } catch (err) { console.error(err); res.status(500).send('An error occurred! See logs for more details.'); diff --git a/functions/security/package.json b/functions/security/package.json index 77ee0abd2d..b240bfb411 100644 --- a/functions/security/package.json +++ b/functions/security/package.json @@ -15,9 +15,9 @@ "test": "mocha test/*.test.js" }, "dependencies": { - "axios": "^0.21.0", "eslint-plugin-node": "^11.1.0", - "mocha": "^8.0.0" + "mocha": "^8.0.0", + "node-fetch": "^2.6.1" }, "devDependencies": { "assert": "^2.0.0", diff --git a/functions/security/test/index.test.js b/functions/security/test/index.test.js index 0b70787aa3..8a47579c8f 100644 --- a/functions/security/test/index.test.js +++ b/functions/security/test/index.test.js @@ -19,14 +19,12 @@ const sinon = require('sinon'); const assert = require('assert'); const getSample = () => { - const getMock = sinon + const fetchMock = sinon .stub() .onFirstCall() - .resolves({data: 'some-token'}) + .resolves({text: () => 'some-token'}) .onSecondCall() - .resolves({data: 'function-response'}); - - const axiosMock = {get: getMock}; + .resolves({text: () => 'function-response'}); const resMock = {}; resMock.status = sinon.stub().returns(resMock); @@ -34,11 +32,11 @@ const getSample = () => { return { sample: proxyquire('../', { - axios: axiosMock, + "node-fetch": fetchMock, }), mocks: { res: resMock, - axios: axiosMock, + fetch: fetchMock, }, }; }; @@ -49,8 +47,8 @@ describe('functions_bearer_token', () => { await sample.callingFunction(null, mocks.res); - assert(mocks.axios.get.calledTwice); - assert.deepEqual(mocks.axios.get.firstCall.args[1], { + assert(mocks.fetch.calledTwice); + assert.deepEqual(mocks.fetch.firstCall.args[1], { headers: {'Metadata-Flavor': 'Google'}, }); diff --git a/functions/tips/index.js b/functions/tips/index.js index d7e3d08160..eb43a54fde 100644 --- a/functions/tips/index.js +++ b/functions/tips/index.js @@ -74,7 +74,7 @@ exports.lazyGlobals = (req, res) => { // [END functions_tips_lazy_globals] // [START functions_tips_connection_pooling] -const axios = require('axios'); +const fetch = require('node-fetch'); const http = require('http'); const https = require('https'); @@ -90,7 +90,7 @@ const httpsAgent = new https.Agent({keepAlive: true}); */ exports.connectionPooling = async (req, res) => { try { - const {data} = await axios.get('/', {httpAgent, httpsAgent}); + const {data} = await fetch('/', {httpAgent, httpsAgent}); res.status(200).send(`Data: ${data}`); } catch (err) { res.status(500).send(`Error: ${err.message}`); diff --git a/functions/tips/package.json b/functions/tips/package.json index 0bf32e83ae..f935f0f266 100644 --- a/functions/tips/package.json +++ b/functions/tips/package.json @@ -16,10 +16,10 @@ }, "dependencies": { "@google-cloud/pubsub": "^2.0.0", - "axios": "^0.21.0" + "mocha": "^8.0.0" }, "devDependencies": { - "mocha": "^8.0.0", + "node-fetch": "^2.6.1", "sinon": "^9.0.0" } }