Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,13 @@ module.exports = {
"no-console": "error",
"no-loss-of-precision": "error",
"no-prototype-builtins": 0,
"no-restricted-globals": [
"error",
{
name: "fetch",
message: "Please use 'fetch' from '@lodestar/api' instead.",
},
],
"no-restricted-imports": [
"error",
{
Expand Down
1 change: 1 addition & 0 deletions packages/api/src/utils/client/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
async function wrappedFetch(url: string | URL, init?: RequestInit): Promise<Response> {
try {
// eslint-disable-next-line no-restricted-globals

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a comment here which explains why disabling this rule is important here. I believe that's the only place we need to use native fetch.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a comment but obviously the function can't call itself, so I thought it is kinda self explanatory here

return await fetch(url, init);
} catch (e) {
throw new FetchError(url, e);
Expand Down
10 changes: 5 additions & 5 deletions packages/api/test/unit/client/httpClientFallback.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {describe, it, beforeEach, afterEach, expect, vi} from "vitest";
import {HttpClient} from "../../../src/utils/client/index.js";
import {HttpClient, fetch} from "../../../src/utils/client/index.js";

describe("httpClient fallback", () => {
const testRoute = {url: "/test-route", method: "GET" as const};
const DEBUG_LOGS = Boolean(process.env.DEBUG);

// Using fetchSub instead of actually setting up servers because there are some strange
// race conditions, where the server stub doesn't count the call in time before the test is over.
const fetchStub = vi.fn();
const fetchStub = vi.fn<Parameters<typeof fetch>, ReturnType<typeof fetch>>();

let httpClient: HttpClient;

Expand All @@ -20,7 +20,7 @@ describe("httpClient fallback", () => {
const serverErrors = new Map<number, boolean>();

// With baseURLs above find the server index associated with that URL
function getServerIndex(url: URL): number {
function getServerIndex(url: URL | string): number {
const i = baseUrls.findIndex((baseUrl) => url.toString().startsWith(baseUrl));
if (i < 0) {
throw Error(`fetch called with unknown url ${url.toString()}`);
Expand All @@ -33,7 +33,7 @@ describe("httpClient fallback", () => {
httpClient = new HttpClient({
baseUrl: "",
urls: baseUrls.map((baseUrl) => ({baseUrl})),
fetch: fetchStub as typeof fetch,
fetch: fetchStub,
});

fetchStub.mockImplementation(async (url) => {
Expand All @@ -57,7 +57,7 @@ describe("httpClient fallback", () => {
const callCounts: number[] = [];
for (let i = 0; i < serverCount; i++) callCounts[i] = 0;
for (const call of fetchStub.mock.calls) {
callCounts[getServerIndex(call)]++;
callCounts[getServerIndex(call[0])]++;
}

expect(callCounts.join(",")).toBe(expectedCallCounts.join(","));
Expand Down