Skip to content
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

Switch to using Axios for requests #2958

Merged
merged 2 commits into from
Jun 29, 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
53 changes: 53 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@
"@fontsource/open-sans": "^4.5.10",
"@octokit/rest": "^19.0.13",
"@react-nano/use-event-source": "^0.12.0",
"axios": "^1.7.2",
"bezier-js": "^6.1.4",
"chakra-ui-markdown-renderer": "^4.1.0",
"chokidar": "^3.5.3",
Expand Down
31 changes: 23 additions & 8 deletions src/common/Backend.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import axios, { AxiosRequestConfig } from 'axios';
import {
BackendJsonNode,
Category,
Expand Down Expand Up @@ -155,24 +156,38 @@ export class Backend {
}

private async fetchJson<T>(path: string, method: 'POST' | 'GET', json?: unknown): Promise<T> {
const options: RequestInit = {
const options: AxiosRequestConfig = {
method,
cache: 'no-cache',
url: `${this.url}${path}`,
};
const { signal } = this.abortController;
if (json !== undefined) {
options.body = JSON.stringify(json);
options.data = json;
options.headers = {
'Content-Type': 'application/json',
};
options.signal = signal;
}
const resp = await fetch(`${this.url}${path}`, options);
const result = (await resp.json()) as T;
if (ServerError.isJson(result)) {
throw ServerError.fromJson(result);

try {
const resp = await axios.request<T>(options);
return resp.data;
} catch (error) {
if (axios.isAxiosError(error)) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const responseData = error.response?.data;
if (ServerError.isJson(responseData)) {
throw ServerError.fromJson(responseData);
}
if (error.response?.data) {
return error.response.data as T;
}
}
if (ServerError.isJson(error)) {
throw ServerError.fromJson(error);
}
throw error;
}
return result;
}

/**
Expand Down
Loading