Skip to content

Commit

Permalink
Update live-network for axios version 1.7.3
Browse files Browse the repository at this point in the history
  • Loading branch information
lambertkevin committed Aug 19, 2024
1 parent 441d751 commit 0067813
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
17 changes: 12 additions & 5 deletions libs/live-network/src/network.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import axios from "axios";
import axios, { AxiosHeaders } from "axios";
import { getEnv, setEnv } from "@ledgerhq/live-env";
import network, { requestInterceptor, responseInterceptor } from "./network";
import * as logs from "@ledgerhq/logs";
Expand Down Expand Up @@ -26,6 +26,7 @@ describe("network", () => {
baseURL: "baseURL",
url: "url",
data: "data",
headers: new AxiosHeaders(),
};
const req = requestInterceptor(request);
expect(req).toEqual(request);
Expand All @@ -38,6 +39,7 @@ describe("network", () => {
baseURL: "baseURL",
url: "url",
data: "data",
headers: new AxiosHeaders(),
};
const req = requestInterceptor(request);
expect(req).toEqual({
Expand All @@ -55,6 +57,7 @@ describe("network", () => {
baseURL: "baseURL",
url: "url",
data: "data",
headers: new AxiosHeaders(),
};
requestInterceptor(request);

Expand All @@ -69,11 +72,12 @@ describe("network", () => {
baseURL: "baseURL",
url: "url",
data: "data",
headers: new AxiosHeaders(),
},
data: "data",
status: 200,
statusText: "OK",
headers: {},
headers: new AxiosHeaders(),
};
const res = responseInterceptor(response);
expect(res).toEqual(response);
Expand All @@ -87,11 +91,12 @@ describe("network", () => {
baseURL: "baseURL",
url: "url",
data: "data",
headers: new AxiosHeaders(),
},
headers: new AxiosHeaders(),
data: "data",
status: 200,
statusText: "OK",
headers: {},
};
const res = responseInterceptor(response);
expect(res).toEqual(response);
Expand All @@ -107,11 +112,12 @@ describe("network", () => {
baseURL: "baseURL",
url: "url",
data: "data",
headers: new AxiosHeaders(),
},
headers: new AxiosHeaders(),
data: "data",
status: 200,
statusText: "OK",
headers: {},
};
responseInterceptor(response);

Expand All @@ -124,11 +130,12 @@ describe("network", () => {
baseURL: "baseURL",
url: "url",
data: "data",
headers: new AxiosHeaders(),
},
data: "data",
status: 500,
statusText: "Error",
headers: {},
headers: new AxiosHeaders(),
};

try {
Expand Down
28 changes: 13 additions & 15 deletions libs/live-network/src/network.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,43 @@
import axios, {
AxiosPromise,
AxiosResponse,
type AxiosPromise,
type AxiosResponse,
type InternalAxiosRequestConfig,
type AxiosError,
type AxiosRequestConfig,
type Method,
} from "axios";
import { LedgerAPI4xx, LedgerAPI5xx, NetworkDown } from "@ledgerhq/errors";
import { changes, getEnv } from "@ledgerhq/live-env";
import { retry } from "@ledgerhq/live-promise";
import { log } from "@ledgerhq/logs";

type Metadata = { startTime: number };
type ExtendedXHRConfig = AxiosRequestConfig & { metadata?: Metadata };
type ExtendedXHRConfig = InternalAxiosRequestConfig & { metadata?: Metadata };

export const requestInterceptor = (request: AxiosRequestConfig): ExtendedXHRConfig => {
export const requestInterceptor = (request: ExtendedXHRConfig): ExtendedXHRConfig => {
if (!getEnv("ENABLE_NETWORK_LOGS")) {
return request;
}

const { baseURL, url, method = "", data } = request;
log("network", `${method} ${baseURL || ""}${url}`, { data });

const req: ExtendedXHRConfig = request;

req.metadata = {
request.metadata = {
startTime: Date.now(),
};

return req;
return request;
};

type InterceptedResponse = {
config: ExtendedXHRConfig;
} & AxiosResponse<any>;
} & AxiosResponse;

export const responseInterceptor = (response: InterceptedResponse): InterceptedResponse => {
export const responseInterceptor = (response: AxiosResponse): AxiosResponse => {
if (!getEnv("ENABLE_NETWORK_LOGS")) {
return response;
}

const { baseURL, url, method = "", metadata } = response.config;
const { baseURL, url, method, metadata } = response.config as ExtendedXHRConfig;
const { startTime = 0 } = metadata || {};

log(
Expand All @@ -55,12 +53,12 @@ export const responseInterceptor = (response: InterceptedResponse): InterceptedR

type InterceptedError = {
response?: InterceptedResponse;
} & AxiosError<any>;
} & AxiosError;

export const errorInterceptor = (error: InterceptedError): InterceptedError => {
const config = error?.response?.config;
if (!config) throw error;
const { baseURL, url, method = "", metadata } = config;
const { baseURL, url, method = "", metadata } = config as ExtendedXHRConfig;
const { startTime = 0 } = metadata || {};

const duration = `${(Date.now() - startTime).toFixed(0)}ms`;
Expand Down Expand Up @@ -117,7 +115,7 @@ if (NETWORK_USE_HTTPS_KEEP_ALIVE) {
axios.defaults.httpsAgent = new https.Agent({ keepAlive: true });
}

const makeError = (msg: string, status: number, url: string | undefined, method: Method | "") => {
const makeError = (msg: string, status: number, url: string | undefined, method: string) => {
const obj = {
status,
url,
Expand Down

0 comments on commit 0067813

Please sign in to comment.