Skip to content

Commit

Permalink
replace axios with native fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
mgwalker committed Nov 20, 2024
1 parent 565ed15 commit a04cd9e
Show file tree
Hide file tree
Showing 19 changed files with 184 additions and 229 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
"dependencies": {
"@18f/us-federal-holidays": "^4.0.0",
"@slack/bolt": "^4.1.0",
"axios": "^1.7.7",
"cfenv": "^1.2.3",
"cheerio": "^1.0.0-rc.12",
"csv-parse": "^5.0.4",
Expand Down
5 changes: 2 additions & 3 deletions src/scripts/define.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
// @bot define contracting officer

const { directMention } = require("@slack/bolt");
const axios = require("axios");
const he = require("he");
const yaml = require("js-yaml");
const {
Expand Down Expand Up @@ -114,9 +113,9 @@ module.exports = (app) => {

// Cache the glossary for 1 minute
const glossary = await cache("glossary get", 60, async () => {
const { data } = await axios.get(
const data = await fetch(
"https://raw.githubusercontent.com/18F/the-glossary/main/glossary.yml",
);
).then((r) => r.text());

return yaml.load(data, { json: true }).entries;
});
Expand Down
5 changes: 2 additions & 3 deletions src/scripts/define.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const {
axios,
getApp,
utils: { cache },
} = require("../utils/test");
Expand Down Expand Up @@ -228,8 +227,8 @@ describe("glossary", () => {

const fetcher = cache.mock.calls[0].pop();

axios.get.mockResolvedValue({
data: `
fetch.mockResolvedValue({
text: async () => `
entries:
term 1: ATO
term 2:
Expand Down
28 changes: 10 additions & 18 deletions src/scripts/dot-gov.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
*/

const { directMention } = require("@slack/bolt");
const axios = require("axios");
const https = require("https");

/* eslint-disable import/no-unresolved */
Expand Down Expand Up @@ -186,15 +185,13 @@ const selectDomainsAtRandom = (domainsArr, numToSelect) => {
* Returns a human-readable response status.
*/
const checkDomainStatus = async (domainObj, timeout = 2000) =>
axios
.head(`https://${domainObj[DATA_FIELDS.NAME]}`, {
timeout,
httpsAgent: new https.Agent({
rejectUnauthorized: false,
}),
})
fetch(`https://${domainObj[DATA_FIELDS.NAME]}`, {
method: "HEAD",
agent: new https.Agent({ rejectUnauthorized: false }),

Check failure

Code scanning / CodeQL

Disabling certificate validation High

Disabling certificate validation is strongly discouraged.
signal: AbortSignal.timeout(timeout),
})
.then((response) => {
if (response && response.status) {
if (response?.ok && response.status) {
return response.statusText;
}
return "Unknown Status";
Expand All @@ -203,7 +200,7 @@ const checkDomainStatus = async (domainObj, timeout = 2000) =>
if (error.response) {
return error.response.statusText;
}
if (error.code === "CERT_HAS_EXPIRED") {
if (error.cause?.code === "CERT_HAS_EXPIRED") {
return "Cert Expired";
}
return "Unknown Status";
Expand Down Expand Up @@ -240,14 +237,9 @@ module.exports = (app) => {
module.exports.dotGov = async ({ entity, searchTerm, say, thread }) => {
// fetch the domain list
const domains = await cache("dotgov domains", 1440, async () =>
axios
.get(CISA_DOTGOV_DATA_URL)
.then((response) => {
if (response && response.data) {
return parse(response.data, { columns: true });
}
return [];
})
fetch(CISA_DOTGOV_DATA_URL)
.then((response) => response.text())
.then((data) => parse(data, { columns: true }))
.catch(() => []),
);

Expand Down
45 changes: 20 additions & 25 deletions src/scripts/dot-gov.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const {
axios,
getApp,
utils: { cache },
} = require("../utils/test");
Expand Down Expand Up @@ -99,10 +98,16 @@ describe("dot-gov domains", () => {
username: ".Gov",
};

const fetchResponse = {
json: jest.fn(),
text: jest.fn(),
};

beforeAll(() => {});

beforeEach(() => {
jest.resetAllMocks();
fetch.mockResolvedValue(fetchResponse);
});

afterAll(() => {});
Expand Down Expand Up @@ -138,37 +143,38 @@ describe("dot-gov domains", () => {
});

describe("gets domains from github if the cache is expired or whatever", () => {
let fetch;
let fetcher;

beforeEach(async () => {
cache.mockResolvedValue([]);
await handler(message);
fetch = cache.mock.calls[0][2];
fetcher = cache.mock.calls[0][2];
});

it("if the API throws an error", async () => {
axios.get.mockRejectedValue("error");
const out = await fetch();
fetch.mockRejectedValue("error");
const out = await fetcher();

expect(out).toEqual([]);
});

it("if the API returns a malformed object", async () => {
axios.get.mockResolvedValue({ data: { data_is_missing: [] } });
const out = await fetch();
fetchResponse.json.mockResolvedValue({ data: { data_is_missing: [] } });
const out = await fetcher();

expect(out).toEqual([]);
});

it("if the API doesn't return any domains", async () => {
axios.get.mockResolvedValue({ data: { data: [] } });
const out = await fetch();
fetchResponse.json.mockResolvedValue({ data: { data: [] } });
const out = await fetcher();

expect(out).toEqual([]);
});

it("if the API does return some domains", async () => {
axios.get.mockResolvedValue({
data: [
fetchResponse.text.mockResolvedValue(
[
"Domain Name,Domain Type,Agency,Organization,City,State,Security Contact Email",
"ALBANYCA.GOV,City,Non-Federal Agency,City of Albany,Albany,CA,(blank)",
"BELLEPLAINEIOWA.GOV,City,Non-Federal Agency,City of Belle Plaine,Belle Plaine,IA,(blank)",
Expand All @@ -180,18 +186,14 @@ describe("dot-gov domains", () => {
"VIVOTE.GOV,State,Non-Federal Agency,Election System of the Virgin Islands,St Croix,VI,(blank)",
"CHICKASAW-NSN.GOV,Tribal,Non-Federal Agency,the Chickasaw Nation,Ada,OK,(blank)",
].join("\n"),
});
const out = await fetch();
);
const out = await fetcher();

expect(out).toEqual(mockCache);
});
});

describe("responds with a domain", () => {
beforeEach(() => {
axios.head.mockImplementation(() => Promise.resolve("Ok"));
});

it("unless there aren't any domains", async () => {
cache.mockResolvedValue([]);
await handler(message);
Expand All @@ -216,7 +218,7 @@ describe("dot-gov domains", () => {
});

it("even if checking the status of the domain fails", async () => {
axios.head.mockImplementation(() => Promise.reject(new Error()));
fetch.mockRejectedValue(new Error());
cache.mockResolvedValue(mockCache);
await handler(message);

Expand Down Expand Up @@ -281,10 +283,6 @@ describe("dot-gov domains", () => {
const message = { message: { thread_ts: "thread id" }, say: jest.fn() };

describe("responds with results", () => {
beforeEach(() => {
axios.head.mockImplementation(() => Promise.resolve("Ok"));
});

it("if there aren't any results", async () => {
cache.mockResolvedValue(mockCache);
message.context = {
Expand Down Expand Up @@ -341,9 +339,6 @@ describe("dot-gov domains", () => {
});
});
describe("filters correctly by entity", () => {
beforeEach(() => {
axios.head.mockImplementation(() => Promise.resolve("Ok"));
});
it("if entity is executive", async () => {
cache.mockResolvedValue(mockCache);
message.context = {
Expand Down
3 changes: 1 addition & 2 deletions src/scripts/handbook.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const axios = require("axios");
const {
helpMessage,
slack: { postEphemeralResponse },
Expand Down Expand Up @@ -54,7 +53,7 @@ module.exports = (app) => {
const url = `${baseUrl}${encodeURIComponent(searchString)}`;

try {
const { data } = await axios.get(url);
const data = await fetch(url).then((r) => r.json());
const results = data.results.slice(0, 3);

if (results.length === 0) {
Expand Down
50 changes: 25 additions & 25 deletions src/scripts/handbook.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const {
axios,
getApp,
utils: {
slack: { postEphemeralResponse },
Expand All @@ -10,8 +9,13 @@ const handbook = require("./handbook");
describe("TTS Handbook search", () => {
const app = getApp();

const fetchResponse = {
json: jest.fn(),
};

beforeEach(() => {
jest.resetAllMocks();
fetch.mockResolvedValue(fetchResponse);
});

it("subscribes to the right message", () => {
Expand All @@ -38,7 +42,7 @@ describe("TTS Handbook search", () => {
});

it("gracefully responds if there is an error", async () => {
axios.get.mockRejectedValue();
fetch.mockRejectedValue();
await handler(message);

expect(postEphemeralResponse).toHaveBeenCalledWith(message, {
Expand All @@ -52,7 +56,7 @@ describe("TTS Handbook search", () => {
message.context.matches[2] = "”some ’search‘ goes here“";
await handler(message);

expect(axios.get).toHaveBeenCalledWith(
expect(fetch).toHaveBeenCalledWith(
"https://search.usa.gov/search/?utf8=no&affiliate=tts-handbook&format=json&query=%22some%20'search'%20goes%20here%22",
);
});
Expand All @@ -64,7 +68,7 @@ describe("TTS Handbook search", () => {
});

it("and there are no search results", async () => {
axios.get.mockResolvedValue({ data: { results: [] } });
fetchResponse.json.mockResolvedValue({ results: [] });
await handler(message);

expect(message.say).toHaveBeenCalledWith({
Expand All @@ -76,16 +80,14 @@ describe("TTS Handbook search", () => {
});

it("and there are some results", async () => {
axios.get.mockResolvedValue({
data: {
results: [
{ body: "this is result #1", link: "link1", title: "result 1" },
{ body: "this is result #2", link: "link2", title: "result 2" },
{ body: "this is result #3", link: "link3", title: "result 3" },
{ body: "this is result #4", link: "link4", title: "result 4" },
{ body: "this is result #5", link: "link5", title: "result 5" },
],
},
fetchResponse.json.mockResolvedValue({
results: [
{ body: "this is result #1", link: "link1", title: "result 1" },
{ body: "this is result #2", link: "link2", title: "result 2" },
{ body: "this is result #3", link: "link3", title: "result 3" },
{ body: "this is result #4", link: "link4", title: "result 4" },
{ body: "this is result #5", link: "link5", title: "result 5" },
],
});
await handler(message);

Expand Down Expand Up @@ -148,7 +150,7 @@ describe("TTS Handbook search", () => {
});

it("and there are no search results", async () => {
axios.get.mockResolvedValue({ data: { results: [] } });
fetchResponse.json.mockResolvedValue({ results: [] });
await handler(message);

expect(message.say).toHaveBeenCalledWith({
Expand All @@ -160,16 +162,14 @@ describe("TTS Handbook search", () => {
});

it("and there are some results", async () => {
axios.get.mockResolvedValue({
data: {
results: [
{ body: "this is result #1", link: "link1", title: "result 1" },
{ body: "this is result #2", link: "link2", title: "result 2" },
{ body: "this is result #3", link: "link3", title: "result 3" },
{ body: "this is result #4", link: "link4", title: "result 4" },
{ body: "this is result #5", link: "link5", title: "result 5" },
],
},
fetchResponse.json.mockResolvedValue({
results: [
{ body: "this is result #1", link: "link1", title: "result 1" },
{ body: "this is result #2", link: "link2", title: "result 2" },
{ body: "this is result #3", link: "link3", title: "result 3" },
{ body: "this is result #4", link: "link4", title: "result 4" },
{ body: "this is result #5", link: "link5", title: "result 5" },
],
});
await handler(message);

Expand Down
9 changes: 3 additions & 6 deletions src/scripts/opm_status.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
// lauraggit

const { directMention } = require("@slack/bolt");
const axios = require("axios");
const {
helpMessage,
stats: { incrementStats },
Expand All @@ -38,12 +37,10 @@ module.exports = (robot) => {
incrementStats("OPM status");

try {
const { data, status } = await axios.get(
const data = await fetch(
"https://www.opm.gov/json/operatingstatus.json",
);
if (status !== 200) {
throw new Error("Invalid status");
}
).then((r) => r.json());

say({
icon_emoji: icons[data.Icon],
text: `${data.StatusSummary} for ${data.AppliesTo}. (<${data.Url}|Read more>)`,
Expand Down
Loading

0 comments on commit a04cd9e

Please sign in to comment.