Skip to content

Commit

Permalink
Move fetch to @lodestar/api package
Browse files Browse the repository at this point in the history
  • Loading branch information
nflaig committed Jul 28, 2023
1 parent 8ae400b commit ee7ad2a
Show file tree
Hide file tree
Showing 14 changed files with 25 additions and 14 deletions.
3 changes: 3 additions & 0 deletions packages/api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ export {
HttpError,
ApiError,
Metrics,
FetchError,
isFetchError,
fetch,
} from "./utils/client/index.js";
export * from "./utils/routes.js";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/**
* Wrapper around native fetch to improve error handling
* Native fetch with transparent and consistent error handling
*
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/fetch)
*/
async function wrappedFetch(url: string | URL, init?: RequestInit): Promise<Response> {
try {
Expand All @@ -11,6 +13,10 @@ async function wrappedFetch(url: string | URL, init?: RequestInit): Promise<Resp

export {wrappedFetch as fetch};

export function isFetchError(e: unknown): e is FetchError {
return e instanceof FetchError;
}

export type FetchErrorType = "failed" | "input" | "aborted" | "unknown";

type FetchErrorCause = NativeFetchFailedError["cause"] | NativeFetchInputError["cause"];
Expand Down
5 changes: 3 additions & 2 deletions packages/api/src/utils/client/httpClient.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {ErrorAborted, Logger, TimeoutError, fetch, FetchError} from "@lodestar/utils";
import {ErrorAborted, Logger, TimeoutError} from "@lodestar/utils";
import {ReqGeneric, RouteDef} from "../index.js";
import {ApiClientResponse, ApiClientSuccessResponse} from "../../interfaces.js";
import {fetch, isFetchError} from "./fetch.js";
import {stringifyQuery, urlJoin} from "./format.js";
import {Metrics} from "./metrics.js";
import {HttpStatusCode} from "./httpStatusCode.js";
Expand Down Expand Up @@ -322,7 +323,7 @@ export class HttpClient implements IHttpClient {
}

function isAbortedError(e: Error): boolean {
return e instanceof FetchError && e.type === "aborted";
return isFetchError(e) && e.type === "aborted";
}

function getErrorMessage(errBody: string): string {
Expand Down
1 change: 1 addition & 0 deletions packages/api/src/utils/client/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./client.js";
export * from "./httpClient.js";
export * from "./fetch.js";
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import "../setup.js";
import crypto from "node:crypto";
import http from "node:http";
import {expect} from "chai";
import {FetchError, FetchErrorType, fetch} from "../../src/fetch.js";
import {FetchError, FetchErrorType, fetch} from "../../../src/utils/client/fetch.js";

describe("FetchError", function () {
const port = 37421;
Expand Down
2 changes: 1 addition & 1 deletion packages/api/test/utils/fetchOpenApiSpec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs from "node:fs";
import path from "node:path";
import {fetch} from "@lodestar/utils";
import {fetch} from "@lodestar/api";
import {OpenApiFile, OpenApiJson} from "./parseOpenApiSpec.js";

/* eslint-disable no-console */
Expand Down
3 changes: 2 additions & 1 deletion packages/beacon-node/src/eth1/provider/jsonRpcHttpClient.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {ErrorAborted, TimeoutError, retry, fetch} from "@lodestar/utils";
import {fetch} from "@lodestar/api";
import {ErrorAborted, TimeoutError, retry} from "@lodestar/utils";
import {IGauge, IHistogram} from "../../metrics/interface.js";
import {IJson, RpcPayload} from "../interface.js";
import {encodeJwtToken} from "./jwt.js";
Expand Down
2 changes: 1 addition & 1 deletion packages/beacon-node/src/execution/engine/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {isFetchError} from "@lodestar/utils";
import {isFetchError} from "@lodestar/api";
import {IJson, RpcPayload} from "../../eth1/interface.js";
import {IJsonRpcHttpClient} from "../../eth1/provider/jsonRpcHttpClient.js";
import {ExecutePayloadStatus, ExecutionEngineState} from "./interface.js";
Expand Down
3 changes: 2 additions & 1 deletion packages/beacon-node/src/monitoring/service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {Registry} from "prom-client";
import {ErrorAborted, Logger, TimeoutError, fetch} from "@lodestar/utils";
import {fetch} from "@lodestar/api";
import {ErrorAborted, Logger, TimeoutError} from "@lodestar/utils";
import {RegistryMetricCreator} from "../metrics/index.js";
import {HistogramExtra} from "../metrics/utils/histogram.js";
import {defaultMonitoringOptions, MonitoringOptions} from "./options.js";
Expand Down
2 changes: 1 addition & 1 deletion packages/beacon-node/test/e2e/api/impl/config.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {fetch} from "@lodestar/utils";
import {fetch} from "@lodestar/api";
import {ForkName, activePreset} from "@lodestar/params";
import {chainConfig} from "@lodestar/config/default";
import {ethereumConsensusSpecsTests} from "../../../spec/specTestVersioning.js";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import "mocha";
import crypto from "node:crypto";
import http from "node:http";
import {expect} from "chai";
import {FetchError} from "@lodestar/utils";
import {FetchError} from "@lodestar/api";
import {JsonRpcHttpClient} from "../../../src/eth1/provider/jsonRpcHttpClient.js";
import {getGoerliRpcUrl} from "../../testParams.js";
import {RpcPayload} from "../../../src/eth1/interface.js";
Expand Down
2 changes: 1 addition & 1 deletion packages/beacon-node/test/unit/metrics/server/http.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {fetch} from "@lodestar/utils";
import {fetch} from "@lodestar/api";
import {getHttpMetricsServer, HttpMetricsServer} from "../../../../src/metrics/index.js";
import {testLogger} from "../../../utils/logger.js";
import {createMetricsTest} from "../utils.js";
Expand Down
1 change: 0 additions & 1 deletion packages/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ export * from "./assert.js";
export * from "./bytes.js";
export * from "./err.js";
export * from "./errors.js";
export * from "./fetch.js";
export * from "./format.js";
export * from "./logger.js";
export * from "./map.js";
Expand Down
2 changes: 1 addition & 1 deletion packages/validator/src/util/externalSignerClient.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {ContainerType, toHexString, ValueOf} from "@chainsafe/ssz";
import {fetch} from "@lodestar/api";
import {phase0, altair, capella} from "@lodestar/types";
import {ForkSeq} from "@lodestar/params";
import {fetch} from "@lodestar/utils";
import {ValidatorRegistrationV1} from "@lodestar/types/bellatrix";
import {BeaconConfig} from "@lodestar/config";
import {computeEpochAtSlot, blindedOrFullBlockToHeader} from "@lodestar/state-transition";
Expand Down

0 comments on commit ee7ad2a

Please sign in to comment.