Skip to content

Commit

Permalink
Switch to using Axios for requests (#2958)
Browse files Browse the repository at this point in the history
* Switch to using Axios for requests

* TS
  • Loading branch information
joeyballentine authored Jun 29, 2024
1 parent 3acfb0f commit afe35c3
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 8 deletions.
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

0 comments on commit afe35c3

Please sign in to comment.