Skip to content

Commit c229875

Browse files
committed
fix: update all global axios imports to use Coder instance
1 parent 7faaf2e commit c229875

File tree

6 files changed

+29
-20
lines changed

6 files changed

+29
-20
lines changed

site/e2e/helpers.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { type BrowserContext, expect, type Page, test } from "@playwright/test";
2-
import axios from "axios";
2+
import { coderAxiosInstance } from "api/api";
33
import { type ChildProcess, exec, spawn } from "child_process";
44
import { randomUUID } from "crypto";
55
import express from "express";
@@ -398,7 +398,7 @@ export const waitUntilUrlIsNotResponding = async (url: string) => {
398398

399399
while (retries < maxRetries) {
400400
try {
401-
await axios.get(url);
401+
await coderAxiosInstance.get(url);
402402
} catch (error) {
403403
return;
404404
}

site/e2e/reporter.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type {
88
Reporter,
99
TestError,
1010
} from "@playwright/test/reporter";
11-
import axios from "axios";
11+
import { coderAxiosInstance } from "api/api";
1212
import * as fs from "fs/promises";
1313
import type { Writable } from "stream";
1414
import { coderdPProfPort, enterpriseLicense } from "./constants";
@@ -136,9 +136,10 @@ class CoderReporter implements Reporter {
136136
const logLines = (chunk: string): string[] => chunk.trimEnd().split("\n");
137137

138138
const exportDebugPprof = async (outputFile: string) => {
139-
const response = await axios.get(
139+
const response = await coderAxiosInstance.get(
140140
`http://127.0.0.1:${coderdPProfPort}/debug/pprof/goroutine?debug=1`,
141141
);
142+
142143
if (response.status !== 200) {
143144
throw new Error(`Error: Received status code ${response.status}`);
144145
}

site/src/api/api.test.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import axios from "axios";
21
import {
32
MockTemplate,
43
MockTemplateVersionParameter1,
@@ -8,6 +7,7 @@ import {
87
MockWorkspaceBuildParameter1,
98
} from "testHelpers/entities";
109
import * as api from "./api";
10+
import { coderAxiosInstance } from "./api";
1111
import type * as TypesGen from "./typesGenerated";
1212

1313
describe("api.ts", () => {
@@ -17,13 +17,16 @@ describe("api.ts", () => {
1717
const loginResponse: TypesGen.LoginWithPasswordResponse = {
1818
session_token: "abc_123_test",
1919
};
20-
jest.spyOn(axios, "post").mockResolvedValueOnce({ data: loginResponse });
20+
21+
jest
22+
.spyOn(coderAxiosInstance, "post")
23+
.mockResolvedValueOnce({ data: loginResponse });
2124

2225
// when
2326
const result = await api.login("test", "123");
2427

2528
// then
26-
expect(axios.post).toHaveBeenCalled();
29+
expect(coderAxiosInstance.post).toHaveBeenCalled();
2730
expect(result).toStrictEqual(loginResponse);
2831
});
2932

@@ -38,7 +41,7 @@ describe("api.ts", () => {
3841
const axiosMockPost = jest.fn().mockImplementationOnce(() => {
3942
return Promise.reject(expectedError);
4043
});
41-
axios.post = axiosMockPost;
44+
coderAxiosInstance.post = axiosMockPost;
4245

4346
try {
4447
await api.login("test", "123");
@@ -54,7 +57,7 @@ describe("api.ts", () => {
5457
const axiosMockPost = jest.fn().mockImplementationOnce(() => {
5558
return Promise.resolve();
5659
});
57-
axios.post = axiosMockPost;
60+
coderAxiosInstance.post = axiosMockPost;
5861

5962
// when
6063
await api.logout();
@@ -73,7 +76,8 @@ describe("api.ts", () => {
7376
const axiosMockPost = jest.fn().mockImplementationOnce(() => {
7477
return Promise.reject(expectedError);
7578
});
76-
axios.post = axiosMockPost;
79+
80+
coderAxiosInstance.post = axiosMockPost;
7781

7882
try {
7983
await api.logout();
@@ -92,7 +96,8 @@ describe("api.ts", () => {
9296
const axiosMockPost = jest.fn().mockImplementationOnce(() => {
9397
return Promise.resolve({ data: apiKeyResponse });
9498
});
95-
axios.post = axiosMockPost;
99+
100+
coderAxiosInstance.post = axiosMockPost;
96101

97102
// when
98103
const result = await api.getApiKey();
@@ -112,7 +117,8 @@ describe("api.ts", () => {
112117
const axiosMockPost = jest.fn().mockImplementationOnce(() => {
113118
return Promise.reject(expectedError);
114119
});
115-
axios.post = axiosMockPost;
120+
121+
coderAxiosInstance.post = axiosMockPost;
116122

117123
try {
118124
await api.getApiKey();

site/src/api/errors.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import axios, { type AxiosError, type AxiosResponse } from "axios";
1+
import { type AxiosError, type AxiosResponse, isAxiosError } from "axios";
2+
import { coderAxiosInstance } from "./api";
23

34
const Language = {
45
errorsByCode: {
@@ -25,7 +26,7 @@ export type ApiError = AxiosError<ApiErrorResponse> & {
2526

2627
export const isApiError = (err: unknown): err is ApiError => {
2728
return (
28-
axios.isAxiosError(err) &&
29+
isAxiosError(err) &&
2930
err.response !== undefined &&
3031
isApiErrorResponse(err.response.data)
3132
);

site/src/contexts/auth/RequireAuth.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import axios from "axios";
1+
import { coderAxiosInstance } from "api/api";
22
import { type FC, useEffect } from "react";
33
import { Outlet, Navigate, useLocation } from "react-router-dom";
44
import { isApiError } from "api/errors";
@@ -22,7 +22,7 @@ export const RequireAuth: FC = () => {
2222
return;
2323
}
2424

25-
const interceptorHandle = axios.interceptors.response.use(
25+
const interceptorHandle = coderAxiosInstance.interceptors.response.use(
2626
(okResponse) => okResponse,
2727
(error: unknown) => {
2828
// 401 Unauthorized
@@ -32,13 +32,14 @@ export const RequireAuth: FC = () => {
3232
signOut();
3333
}
3434

35-
// Otherwise, pass the response through so that it can be displayed in the UI
35+
// Otherwise, pass the response through so that it can be displayed in
36+
// the UI
3637
return Promise.reject(error);
3738
},
3839
);
3940

4041
return () => {
41-
axios.interceptors.response.eject(interceptorHandle);
42+
coderAxiosInstance.interceptors.response.eject(interceptorHandle);
4243
};
4344
}, [isLoading, isSigningOut, isSignedIn, signOut]);
4445

site/src/contexts/useProxyLatency.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import PerformanceObserver from "@fastly/performance-observer-polyfill";
2-
import axios from "axios";
2+
import { coderAxiosInstance } from "api/api";
33
import { useEffect, useReducer, useState } from "react";
44
import type { Region } from "api/typesGenerated";
55
import { generateRandomString } from "utils/random";
@@ -198,7 +198,7 @@ export const useProxyLatency = (
198198
observer.observe({ entryTypes: ["resource"] });
199199

200200
const proxyRequests = Object.keys(proxyChecks).map((latencyURL) => {
201-
return axios.get(latencyURL, {
201+
return coderAxiosInstance.get(latencyURL, {
202202
withCredentials: false,
203203
// Must add a custom header to make the request not a "simple request".
204204
// We want to force a preflight request.

0 commit comments

Comments
 (0)