Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

refactor: use fetch instead of axios

Brad Barbind7a51acb851c
diff --git a/apps/carbon/app/lib/autodesk/autodesk.server.ts b/apps/carbon/app/lib/autodesk/autodesk.server.ts
index fe3652cd5f..d128d5d21d 100644
--- a/apps/carbon/app/lib/autodesk/autodesk.server.ts
+++ b/apps/carbon/app/lib/autodesk/autodesk.server.ts
@@ -1,6 +1,4 @@
import { redis } from "@carbon/kv";
-import axios from "axios";
-
import { z } from "zod";
import {
AUTODESK_BUCKET_NAME,
@@ -77,13 +75,27 @@ export async function finalizeAutodeskUpload(
let response;
try {
- response = await axios(url, {
+ response = await fetch(url, {
method: autodeskAPI.finalizeAutodeskUpload.method,
- data: body,
+ body: JSON.stringify(body),
headers: {
Authorization: `Bearer ${token}`,
+ "Content-Type": "application/json",
},
});
+
+ if (!response.ok) {
+ throw new Error(`HTTP error! status: ${response.status}`);
+ }
+
+ const data = await response.json();
+
+ return {
+ data: {
+ urn: data.objectId as string,
+ },
+ error: null,
+ };
} catch (err) {
const message = (err as Error).message || "Something went wrong";
console.error(message, err);
@@ -94,13 +106,6 @@ export async function finalizeAutodeskUpload(
},
};
}
-
- return {
- data: {
- urn: response?.data?.objectId as string,
- },
- error: null,
- };
}
export async function getAutodeskSignedUrl(
@@ -123,6 +128,32 @@ export async function getAutodeskSignedUrl(
Authorization: `Bearer ${token}`,
},
});
+
+ if (!response.ok) {
+ throw new Error(`HTTP error! status: ${response.status}`);
+ }
+
+ const responseJson = await response.json();
+ const parsed = signedUrlSchema.safeParse(responseJson);
+
+ if (parsed.success === false) {
+ return {
+ data: null,
+ error: {
+ message: `Invalid response format: ${JSON.stringify(parsed.error)}`,
+ },
+ };
+ }
+
+ result = {
+ uploadKey: parsed.data.uploadKey,
+ url: parsed.data.urls?.[0] ?? "",
+ };
+
+ return {
+ data: result,
+ error: null,
+ };
} catch (err) {
const message = (err as Error).message || "Something went wrong";
console.error(message, err);
@@ -133,28 +164,6 @@ export async function getAutodeskSignedUrl(
},
};
}
-
- const reponseJson = await response?.json();
- const parsed = signedUrlSchema.safeParse(reponseJson);
-
- if (parsed.success === false) {
- return {
- data: null,
- error: {
- message: `Invalid response format: ${JSON.stringify(parsed.error)}`,
- },
- };
- }
-
- result = {
- uploadKey: parsed.data.uploadKey,
- url: parsed.data.urls?.[0] ?? "",
- };
-
- return {
- data: result,
- error: null,
- };
}
export async function getAutodeskToken(refresh = false, scope?: string) {
@@ -198,6 +207,11 @@ export async function getAutodeskToken(refresh = false, scope?: string) {
},
body: new URLSearchParams(postBody),
});
+
+ if (!response.ok) {
+ throw new Error(`HTTP error! status: ${response.status}`);
+ }
+
const result = (await response.json()) as AutodeskTokenResponse;
if (result?.access_token) {
redis.set("autodesk_token", result.access_token);
@@ -252,6 +266,10 @@ export async function getManifest(
},
});
+ if (!response.ok) {
+ throw new Error(`HTTP error! status: ${response.status}`);
+ }
+
const data = await response.json();
progress = data.progress;
@@ -368,6 +386,10 @@ export async function getTranslationStatus(urn: string, token: string) {
},
});
+ if (!response.ok) {
+ throw new Error(`HTTP error! status: ${response.status}`);
+ }
+
const data = await response.json();
return data.status;
@@ -407,13 +429,21 @@ export async function translateFile(urn: string, token: string) {
let response;
try {
- response = await axios(url, {
+ response = await fetch(url, {
method: autodeskAPI.translateFile.method,
headers: {
Authorization: `Bearer ${token}`,
+ "Content-Type": "application/json",
},
- data: body,
+ body: JSON.stringify(body),
});
+
+ if (!response.ok) {
+ throw new Error(`HTTP error! status: ${response.status}`);
+ }
+
+ const data = await response.json();
+ return { data: { urn: data?.urn, token }, error: null };
} catch (err) {
const message = (err as Error).message || "Something went wrong";
console.error(message, err);
@@ -424,24 +454,33 @@ export async function translateFile(urn: string, token: string) {
},
};
}
-
- const data = response?.data;
- return { data: { urn: data?.urn, token }, error: null };
}
export async function uploadToAutodesk(url: string, file: File, token: string) {
let response;
try {
- response = await axios(url, {
+ response = await fetch(url, {
method: "PUT",
- data: await file.arrayBuffer(),
+ body: await file.arrayBuffer(),
headers: {
"Content-Type": "application/octet-stream",
},
});
+
+ if (!response.ok) {
+ throw new Error(`HTTP error! status: ${response.status}`);
+ }
+
+ // For PUT requests, there might not be a JSON response
+ // So we return a success message instead of parsing the response
+ return {
+ data: { message: "File uploaded successfully" },
+ error: null,
+ };
} catch (err) {
const message = (err as Error).message || "Something went wrong";
+ console.error("Error uploading to Autodesk:", message);
return {
data: null,
@@ -450,9 +489,4 @@ export async function uploadToAutodesk(url: string, file: File, token: string) {
},
};
}
-
- return {
- data: response?.data,
- error: null,
- };
}
diff --git a/apps/carbon/app/lib/autodesk/autodesk.ts b/apps/carbon/app/lib/autodesk/autodesk.ts
index 3052f4161c..921df496ec 100644
--- a/apps/carbon/app/lib/autodesk/autodesk.ts
+++ b/apps/carbon/app/lib/autodesk/autodesk.ts
@@ -1,4 +1,3 @@
-import axios from "axios";
import { path } from "~/utils/path";
import type { AutodeskToken } from "./types";
@@ -76,12 +75,18 @@ const autodesk = {
_renewToken() {
this._setAndBroadcastIsRenewing(true);
- return axios.get(path.to.api.autodeskToken).then((response) => {
- const token = response.data;
- this.setAutodeskToken(token);
- this._setAndBroadcastIsRenewing(false);
- return token;
- });
+ return fetch(path.to.api.autodeskToken)
+ .then((response) => {
+ if (!response.ok) {
+ throw new Error("Network response was not ok");
+ }
+ return response.json();
+ })
+ .then((token) => {
+ this.setAutodeskToken(token);
+ this._setAndBroadcastIsRenewing(false);
+ return token;
+ });
},
_setAndBroadcastIsRenewing(value: boolean) {
diff --git a/apps/carbon/app/lib/exchange-rates.server.ts b/apps/carbon/app/lib/exchange-rates.server.ts
index 8c183c6330..f2ac896dd7 100644
--- a/apps/carbon/app/lib/exchange-rates.server.ts
+++ b/apps/carbon/app/lib/exchange-rates.server.ts
@@ -1,4 +1,3 @@
-import axios from "axios";
import type { CurrencyCode } from "~/modules/accounting";
type ExchangeClientOptions = {
@@ -49,9 +48,13 @@ export class ExchangeRatesClient {
}
async getExchangeRates(base?: CurrencyCode): Promise<Rates> {
- const { data } = await axios.get<ExchangeRatesResponse>(
- `${this.#apiUrl}?access_key=${this.#apiKey}`
- );
+ const response = await fetch(`${this.#apiUrl}?access_key=${this.#apiKey}`);
+
+ if (!response.ok) {
+ throw new Error(`HTTP error! status: ${response.status}`);
+ }
+
+ const data: ExchangeRatesResponse = await response.json();
if ("success" in data && data.success === true) {
const baseRate = data.rates[base ?? this.#baseCurrency];
diff --git a/apps/carbon/package.json b/apps/carbon/package.json
index 335ad92e77..2aaea9ebb0 100644
--- a/apps/carbon/package.json
+++ b/apps/carbon/package.json
@@ -44,7 +44,6 @@
"@visx/scale": "3.3.0",
"@visx/shape": "3.3.0",
"assert-never": "^1.2.1",
- "axios": "^1.6.5",
"clsx": "^2.1.0",
"cookie": "^0.6.0",
"d3-array": "^3.2.4",
diff --git a/package-lock.json b/package-lock.json
index 40cfbb4b49..89a25fbfa0 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -74,7 +74,6 @@
"@visx/scale": "3.3.0",
"@visx/shape": "3.3.0",
"assert-never": "^1.2.1",
- "axios": "^1.6.5",
"clsx": "^2.1.0",
"cookie": "^0.6.0",
"d3-array": "^3.2.4",