Skip to content

Commit a1e9ab0

Browse files
authored
[CHORE] use builtin fetch (#624)
* [CHORE] use builtin fetch * [] fix other usages * [] remove dependency * [] remove dependency
1 parent 1107ece commit a1e9ab0

5 files changed

Lines changed: 11 additions & 14 deletions

File tree

package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
"devDependencies": {
2121
"@types/jest": "^27.4.0",
2222
"@types/mock-fs": "4.13.0",
23-
"@types/node-fetch": "^2.5.10",
2423
"@types/serverless": "3.12.27",
2524
"jest": "^29.7.0",
2625
"jest-environment-node": "^30.2.0",
@@ -59,7 +58,6 @@
5958
},
6059
"dependencies": {
6160
"@datadog/datadog-ci": "3.21.4",
62-
"node-fetch": "^2.6.1",
6361
"simple-git": "^3.16.0"
6462
},
6563
"peerDependencies": {

src/monitor-api-requests.spec.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import fetch from "node-fetch";
21
import {
32
createMonitor,
43
updateMonitor,
@@ -10,7 +9,9 @@ import {
109
} from "./monitor-api-requests";
1110
import { MonitorParams, handleMonitorsApiResponse } from "./monitors";
1211

13-
jest.mock("node-fetch");
12+
beforeAll(() => {
13+
global.fetch = jest.fn();
14+
});
1415

1516
const monitorParams: MonitorParams = {
1617
tags: [

src/monitor-api-requests.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import fetch, { Response } from "node-fetch";
21
import * as Serverless from "serverless";
32
import { MonitorParams, ServerlessMonitor, replaceCriticalThreshold } from "./monitors";
43

@@ -44,7 +43,7 @@ export async function createMonitor(
4443
monitorsApiKey: string,
4544
monitorsAppKey: string,
4645
): Promise<Response> {
47-
const response: Response = await fetch(`https://api.${site}/api/v1/monitor`, {
46+
const response = await fetch(`https://api.${site}/api/v1/monitor`, {
4847
method: "POST",
4948
headers: {
5049
"DD-API-KEY": monitorsApiKey,
@@ -63,7 +62,7 @@ export async function updateMonitor(
6362
monitorsApiKey: string,
6463
monitorsAppKey: string,
6564
): Promise<Response> {
66-
const response: Response = await fetch(`https://api.${site}/api/v1/monitor/${monitorId}`, {
65+
const response = await fetch(`https://api.${site}/api/v1/monitor/${monitorId}`, {
6766
method: "PUT",
6867
headers: {
6968
"DD-API-KEY": monitorsApiKey,
@@ -82,7 +81,7 @@ export async function deleteMonitor(
8281
monitorsApiKey: string,
8382
monitorsAppKey: string,
8483
): Promise<Response> {
85-
const response: Response = await fetch(`https://api.${site}/api/v1/monitor/${monitorId}`, {
84+
const response = await fetch(`https://api.${site}/api/v1/monitor/${monitorId}`, {
8685
method: "DELETE",
8786
headers: {
8887
"DD-API-KEY": monitorsApiKey,
@@ -105,7 +104,7 @@ export async function searchMonitors(
105104
let pageCount = 1;
106105
do {
107106
const query = `tag:"${queryTag}"`;
108-
const response: Response = await fetch(`https://api.${site}/api/v1/monitor/search?query=${query}&page=${page}`, {
107+
const response = await fetch(`https://api.${site}/api/v1/monitor/search?query=${query}&page=${page}`, {
109108
method: "GET",
110109
headers: {
111110
"DD-API-KEY": monitorsApiKey,
@@ -118,7 +117,7 @@ export async function searchMonitors(
118117
throw new Error(`Can't fetch monitors. Status code: ${response.status}. Message: ${response.statusText}`);
119118
}
120119

121-
const json = await response.json();
120+
const json: any = await response.json(); // TODO: use proper type/parsing
122121
monitors = monitors.concat(json.monitors);
123122
pageCount = json.metadata.page_count;
124123
page += 1;
@@ -178,7 +177,7 @@ export async function getRecommendedMonitors(
178177
const recommendedMonitors: { [key: string]: ServerlessMonitor } = {};
179178
// Setting a count of 50 in the hope that all can be fetched at once. The default is 10 per page.
180179
const endpoint = `https://api.${site}/api/v2/monitor/recommended?count=50&start=0&search=tag%3A%22product%3Aserverless%22%20AND%20tag%3A%22integration%3Aamazon-lambda%22`;
181-
const response: Response = await fetch(endpoint, {
180+
const response = await fetch(endpoint, {
182181
method: "GET",
183182
headers: {
184183
"DD-API-KEY": monitorsApiKey,
@@ -190,7 +189,7 @@ export async function getRecommendedMonitors(
190189
throw new Error(`Can't fetch monitor params. Status code: ${response.status}. Message: ${response.statusText}`);
191190
}
192191

193-
const json = await response.json();
192+
const json: any = await response.json(); // TODO: use proper type/parsing
194193
const recommendedMonitorsData = json.data;
195194
recommendedMonitorsData.forEach((recommendedMonitorParam: RecommendedMonitorParams) => {
196195
const recommendedMonitorId = parseRecommendedMonitorServerlessId(recommendedMonitorParam);

src/monitors.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
getRecommendedMonitors,
77
TemplateVariable,
88
} from "./monitor-api-requests";
9-
import { Response } from "node-fetch";
109

1110
export interface MonitorParams {
1211
[key: string]: any;

yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5679,7 +5679,7 @@ node-domexception@^1.0.0:
56795679
resolved "https://registry.yarnpkg.com/node-domexception/-/node-domexception-1.0.0.tgz#6888db46a1f71c0b76b3f7555016b63fe64766e5"
56805680
integrity sha512-/jKZoMpw0F8GRwl4/eLROPA3cfcXtLApP0QzLmUT/HuPCZWyB7IY9ZrMeKw2O/nFIqPQB3PVM9aYm0F312AXDQ==
56815681

5682-
node-fetch@^2.6.1, node-fetch@^2.6.11, node-fetch@^2.6.9, node-fetch@^2.7.0:
5682+
node-fetch@^2.6.11, node-fetch@^2.6.9, node-fetch@^2.7.0:
56835683
version "2.7.0"
56845684
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.7.0.tgz#d0f0fa6e3e2dc1d27efcd8ad99d550bda94d187d"
56855685
integrity sha512-c4FRfUm/dbcWZ7U+1Wq0AwCyFL+3nt2bEw05wfxSz+DWpWsitgmSgYmy2dQdWyKC1694ELPqMs/YzUSNozLt8A==

0 commit comments

Comments
 (0)