Skip to content

Commit

Permalink
refactor: add constants file and revert from axios to fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
Lombardoc4 committed Jan 23, 2024
1 parent 50afa96 commit 40ccc1c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 27 deletions.
62 changes: 35 additions & 27 deletions src/app/lib/utils.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { faker } from '@faker-js/faker';

Check warning on line 1 in src/app/lib/utils.tsx

View workflow job for this annotation

GitHub Actions / ⬣ ESLint, ʦ TypeScript, 💅 Prettier

Run autofix to sort these imports!
import axios from 'axios';

Check warning on line 2 in src/app/lib/utils.tsx

View workflow job for this annotation

GitHub Actions / ⬣ ESLint, ʦ TypeScript, 💅 Prettier

'axios' is defined but never used
import { serverUrl } from '../../constants';

export const positionEnding = (position: number | string) => {
// Convert to int
Expand Down Expand Up @@ -118,12 +119,11 @@ const dataConfig: DataConfigSchema = {
},
};

const serverURL = 'http://127.0.0.1:8081';
export const fetchAPI = async (
endpoint: string,
statusCheck: boolean = false,
) => {
const server = statusCheck || document.body.classList.contains('server');
const useServer = statusCheck || document.body.classList.contains('server');
// Headers for statusCheck so
const options = statusCheck ? { headers: { cache: 'no-store' } } : {};

Expand All @@ -134,32 +134,40 @@ export const fetchAPI = async (
] || false;

// If we are not using the server return the dummy data
if (!server) {
if (!useServer) {
return dummy;
} else {
// Fetch from server
const data = await axios(`${serverURL}/${endpoint}`, options)
.then(
(res) => {
// Response is not successful
if (res.statusText !== 'OK') {
throw new Error('Not 2xx response', { cause: res });
}
// Sucess - parse data
return res.data;
},
// Catch initial fetch error
(err) => {
throw new Error('Server not connecting', { cause: err });
},
)
// Return parsed data
.then((data) => data)
// Catch errors from above
.catch(() => {
}

// Fetch from server
const data = await fetch(`${serverUrl}/${endpoint}`, { ...options })
.then(
(res) => {
// Response is not successful
if (!res.ok) {
throw new Error('Not 2xx response', { cause: res });
}

// Success parse data
return res.json();
},
// Catch initial fetch error
(err) => {
throw new Error('Server not connecting', { cause: err });
},
)
// Return parsed data
.then((data) => data)
// Catch errors from above
.catch((err) => {
if (err === 'Server not connecting')
return dummy;
if (err.status === 404)
return dummy;
});

return data;
}
return dummy;
});

// console.log('data', data)

return data;
};
3 changes: 3 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const serverUrl = 'http://127.0.0.1:8081';

export { serverUrl };

0 comments on commit 40ccc1c

Please sign in to comment.