From d10a372c12c90111cc924ae56cc7231952967f24 Mon Sep 17 00:00:00 2001 From: DanielTQuach Date: Mon, 9 Jun 2025 00:44:02 -0700 Subject: [PATCH] 2DPrinting remove axios --- src/APIFunctions/2DPrinting.js | 74 +++++++++++++++++++--------------- 1 file changed, 42 insertions(+), 32 deletions(-) diff --git a/src/APIFunctions/2DPrinting.js b/src/APIFunctions/2DPrinting.js index 6fdf07d03..de570e43d 100644 --- a/src/APIFunctions/2DPrinting.js +++ b/src/APIFunctions/2DPrinting.js @@ -1,4 +1,3 @@ -import axios from 'axios'; import { PrintApiResponse, ApiResponse @@ -22,14 +21,17 @@ export const range = (start, end) => { export async function healthCheck() { let status = new ApiResponse(); const url = new URL('/api/Printer/healthCheck', BASE_API_URL); - await axios.get(url.href) - .then(res => { - status.responseData = res.data; - }) - .catch(err => { - status.responseData = err; + try { + const res = await fetch(url.href); + if (res.ok) { + status.responseData = await res.json(); + } else { status.error = true; - }); + } + } catch (err) { + status.responseData = err; + status.error = true; + } return status; } @@ -78,18 +80,22 @@ export function parseRange(pages, maxPages) { export async function printPage(data, token) { let status = new ApiResponse(); const url = new URL('/api/Printer/sendPrintRequest', BASE_API_URL); - await axios.post(url.href, data, { - headers: { - 'Content-Type': 'multipart/form-data', - 'Authorization': `Bearer ${token}` - } - }) - .then(response => { - status.responseData = response.data.message; - }) - .catch(() => { - status.error = true; + try { + const res = await fetch(url.href, { + method: 'POST', + headers: { + 'Authorization': `Bearer ${token}` + }, + body: data }); + if (res.ok) { + const response = await res.json(); + status.responseData = response.message; + } + } catch (err) { + status.responseData = err; + status.error = true; + } return status; } @@ -105,20 +111,24 @@ export async function printPage(data, token) { export async function getPagesPrinted(email, token) { let status = new PrintApiResponse(); const url = new URL('/api/user/getPagesPrintedCount', BASE_API_URL); - await axios - .post(url.href, { - email - }, { + try { + const res = await fetch(url.href, { + method: 'POST', headers: { - 'Authorization': `Bearer ${token}` - } - } - ) - .then(res => { - status.pagesUsed = res.data; - }) - .catch(() => { - status.error = true; + 'Authorization': `Bearer ${token}`, + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ email }) }); + if (res.ok) { + const response = await res.json(); + status.pagesUsed = response.pagesUsed; + } else { + status.error = true; + } + } catch (err) { + status.responseData = err; + status.error = true; + } return status; }