Skip to content

Commit

Permalink
chore: use node-fetch instead of axios
Browse files Browse the repository at this point in the history
See b/171857357.
  • Loading branch information
fhinkel committed Oct 28, 2020
1 parent 92fce56 commit 216ef26
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 24 deletions.
4 changes: 2 additions & 2 deletions appengine/analytics/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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) => {
Expand Down
4 changes: 2 additions & 2 deletions appengine/analytics/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
10 changes: 5 additions & 5 deletions functions/security/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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.');
Expand Down
4 changes: 2 additions & 2 deletions functions/security/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
16 changes: 7 additions & 9 deletions functions/security/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,24 @@ 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);
resMock.send = sinon.stub().returns(resMock);

return {
sample: proxyquire('../', {
axios: axiosMock,
"node-fetch": fetchMock,
}),
mocks: {
res: resMock,
axios: axiosMock,
fetch: fetchMock,
},
};
};
Expand All @@ -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'},
});

Expand Down
4 changes: 2 additions & 2 deletions functions/tips/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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}`);
Expand Down
4 changes: 2 additions & 2 deletions functions/tips/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}

0 comments on commit 216ef26

Please sign in to comment.