Skip to content

Commit 3f21cb8

Browse files
authored
fix: update API code to use Axios instances (#13029)
* fix: update API code to use Axios instance * docs: fix typo * fix: update all global axios imports to use Coder instance * fix: remove needless import * fix: update import order * refactor: rename coderAxiosInstance to axiosInstance * docs: update variable reference in FE contributing docs
1 parent dd27a8a commit 3f21cb8

File tree

8 files changed

+259
-177
lines changed

8 files changed

+259
-177
lines changed

docs/contributing/frontend.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ example below:
152152
export const getAgentListeningPorts = async (
153153
agentID: string,
154154
): Promise<TypesGen.ListeningPortsResponse> => {
155-
const response = await axios.get(
155+
const response = await axiosInstance.get(
156156
`/api/v2/workspaceagents/${agentID}/listening-ports`,
157157
);
158158
return response.data;

site/e2e/helpers.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { type BrowserContext, expect, type Page, test } from "@playwright/test";
2-
import axios from "axios";
32
import { type ChildProcess, exec, spawn } from "child_process";
43
import { randomUUID } from "crypto";
54
import express from "express";
65
import capitalize from "lodash/capitalize";
76
import path from "path";
87
import * as ssh from "ssh2";
98
import { Duplex } from "stream";
9+
import { axiosInstance } from "api/api";
1010
import type {
1111
WorkspaceBuildParameter,
1212
UpdateTemplateMeta,
@@ -398,7 +398,7 @@ export const waitUntilUrlIsNotResponding = async (url: string) => {
398398

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

site/e2e/reporter.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import type {
88
Reporter,
99
TestError,
1010
} from "@playwright/test/reporter";
11-
import axios from "axios";
1211
import * as fs from "fs/promises";
1312
import type { Writable } from "stream";
13+
import { axiosInstance } from "api/api";
1414
import { coderdPProfPort, enterpriseLicense } from "./constants";
1515

1616
class CoderReporter implements Reporter {
@@ -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 axiosInstance.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

+14-8
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 { axiosInstance } 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(axiosInstance, "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(axiosInstance.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+
axiosInstance.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+
axiosInstance.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+
axiosInstance.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+
axiosInstance.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+
axiosInstance.post = axiosMockPost;
116122

117123
try {
118124
await api.getApiKey();

0 commit comments

Comments
 (0)