Skip to content

fix: update API code to use Axios instances #13029

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion docs/contributing/frontend.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ example below:
export const getAgentListeningPorts = async (
agentID: string,
): Promise<TypesGen.ListeningPortsResponse> => {
const response = await axios.get(
const response = await axiosInstance.get(
`/api/v2/workspaceagents/${agentID}/listening-ports`,
);
return response.data;
Expand Down
4 changes: 2 additions & 2 deletions site/e2e/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { type BrowserContext, expect, type Page, test } from "@playwright/test";
import axios from "axios";
import { type ChildProcess, exec, spawn } from "child_process";
import { randomUUID } from "crypto";
import express from "express";
import capitalize from "lodash/capitalize";
import path from "path";
import * as ssh from "ssh2";
import { Duplex } from "stream";
import { axiosInstance } from "api/api";
import type {
WorkspaceBuildParameter,
UpdateTemplateMeta,
Expand Down Expand Up @@ -398,7 +398,7 @@ export const waitUntilUrlIsNotResponding = async (url: string) => {

while (retries < maxRetries) {
try {
await axios.get(url);
await axiosInstance.get(url);
} catch (error) {
return;
}
Expand Down
5 changes: 3 additions & 2 deletions site/e2e/reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import type {
Reporter,
TestError,
} from "@playwright/test/reporter";
import axios from "axios";
import * as fs from "fs/promises";
import type { Writable } from "stream";
import { axiosInstance } from "api/api";
import { coderdPProfPort, enterpriseLicense } from "./constants";

class CoderReporter implements Reporter {
Expand Down Expand Up @@ -136,9 +136,10 @@ class CoderReporter implements Reporter {
const logLines = (chunk: string): string[] => chunk.trimEnd().split("\n");

const exportDebugPprof = async (outputFile: string) => {
const response = await axios.get(
const response = await axiosInstance.get(
`http://127.0.0.1:${coderdPProfPort}/debug/pprof/goroutine?debug=1`,
);

if (response.status !== 200) {
throw new Error(`Error: Received status code ${response.status}`);
}
Expand Down
22 changes: 14 additions & 8 deletions site/src/api/api.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import axios from "axios";
import {
MockTemplate,
MockTemplateVersionParameter1,
Expand All @@ -8,6 +7,7 @@ import {
MockWorkspaceBuildParameter1,
} from "testHelpers/entities";
import * as api from "./api";
import { axiosInstance } from "./api";
import type * as TypesGen from "./typesGenerated";

describe("api.ts", () => {
Expand All @@ -17,13 +17,16 @@ describe("api.ts", () => {
const loginResponse: TypesGen.LoginWithPasswordResponse = {
session_token: "abc_123_test",
};
jest.spyOn(axios, "post").mockResolvedValueOnce({ data: loginResponse });

jest
.spyOn(axiosInstance, "post")
.mockResolvedValueOnce({ data: loginResponse });

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

// then
expect(axios.post).toHaveBeenCalled();
expect(axiosInstance.post).toHaveBeenCalled();
expect(result).toStrictEqual(loginResponse);
});

Expand All @@ -38,7 +41,7 @@ describe("api.ts", () => {
const axiosMockPost = jest.fn().mockImplementationOnce(() => {
return Promise.reject(expectedError);
});
axios.post = axiosMockPost;
axiosInstance.post = axiosMockPost;

try {
await api.login("test", "123");
Expand All @@ -54,7 +57,7 @@ describe("api.ts", () => {
const axiosMockPost = jest.fn().mockImplementationOnce(() => {
return Promise.resolve();
});
axios.post = axiosMockPost;
axiosInstance.post = axiosMockPost;

// when
await api.logout();
Expand All @@ -73,7 +76,8 @@ describe("api.ts", () => {
const axiosMockPost = jest.fn().mockImplementationOnce(() => {
return Promise.reject(expectedError);
});
axios.post = axiosMockPost;

axiosInstance.post = axiosMockPost;

try {
await api.logout();
Expand All @@ -92,7 +96,8 @@ describe("api.ts", () => {
const axiosMockPost = jest.fn().mockImplementationOnce(() => {
return Promise.resolve({ data: apiKeyResponse });
});
axios.post = axiosMockPost;

axiosInstance.post = axiosMockPost;

// when
const result = await api.getApiKey();
Expand All @@ -112,7 +117,8 @@ describe("api.ts", () => {
const axiosMockPost = jest.fn().mockImplementationOnce(() => {
return Promise.reject(expectedError);
});
axios.post = axiosMockPost;

axiosInstance.post = axiosMockPost;

try {
await api.getApiKey();
Expand Down
Loading