Skip to content

Commit 2e4d84e

Browse files
Co-authored-by: evan <evanuxd@gmail.com>
1 parent 8e546b0 commit 2e4d84e

1 file changed

Lines changed: 52 additions & 44 deletions

File tree

src/APIFunctions/Cleezy.js

Lines changed: 52 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import axios from 'axios';
1+
22
import { ApiResponse } from './ApiResponses';
33

44
import { BASE_API_URL } from '../Enums';
@@ -9,26 +9,28 @@ export async function getAllUrls({
99
}) {
1010
let status = new ApiResponse();
1111
const url = new URL('/api/Cleezy/list', BASE_API_URL);
12-
await axios
13-
.get(
14-
url.href, {
15-
params: {
16-
page,
17-
...(search !== undefined && { search }),
18-
sortColumn,
19-
sortOrder
20-
}, headers: {
21-
'Authorization': `Bearer ${token}`
22-
}
23-
},
24-
)
25-
.then(res => {
26-
status.responseData = res.data;
27-
})
28-
.catch(err => {
29-
status.responseData = err;
30-
status.error = true;
12+
if (page) url.searchParams.set('page', page);
13+
if (search !== null) url.searchParams.set('search', search);
14+
if (sortColumn) url.searchParams.set('sortColumn', sortColumn);
15+
if (sortOrder) url.searchParams.set('sortOrder', sortOrder);
16+
try {
17+
const res = await fetch(url.href, {
18+
method: 'GET',
19+
headers: {
20+
'Authorization': `Bearer ${token}`,
21+
'Content-Type': 'application/json'
22+
}
3123
});
24+
if (res.ok) {
25+
const result = await res.json();
26+
status.responseData = result;
27+
} else {
28+
status.error = true;
29+
}
30+
} catch(err) {
31+
status.error = true;
32+
status.responseData = err;
33+
}
3234
return status;
3335
}
3436

@@ -37,18 +39,21 @@ export async function createUrl(url, alias = null, token) {
3739
const urlToAdd = { url, alias };
3840
try {
3941
const url = new URL('/api/Cleezy/createUrl', BASE_API_URL);
40-
const response = await axios
41-
.post(
42-
url.href,
43-
urlToAdd,
44-
{
45-
headers: {
46-
'Authorization': `Bearer ${token}`
47-
}
48-
});
49-
const data = response.data;
50-
status.responseData = data;
51-
} catch (err) {
42+
const res = await fetch(url.href, {
43+
method: 'POST',
44+
headers: {
45+
'Authorization': `Bearer ${token}`,
46+
'Content-Type': 'application/json'
47+
},
48+
body: JSON.stringify(urlToAdd)
49+
});
50+
if (res.ok) {
51+
const result = await res.json();
52+
status.responseData = result;
53+
} else {
54+
status.error = true;
55+
}
56+
} catch(err) {
5257
status.error = true;
5358
status.responseData = err;
5459
}
@@ -59,18 +64,21 @@ export async function deleteUrl(aliasIn, token) {
5964
let status = new ApiResponse();
6065
const alias = { 'alias': aliasIn };
6166
const url = new URL('/api/Cleezy/deleteUrl', BASE_API_URL);
62-
await axios
63-
.post(
64-
url.href,
65-
alias,
66-
{
67-
headers: {
68-
'Authorization': `Bearer ${token}`
69-
}
70-
})
71-
.catch(err => {
72-
status.responseData = err;
73-
status.error = true;
67+
try {
68+
const res = await fetch(url.href, {
69+
method: 'POST',
70+
headers: {
71+
'Authorization': `Bearer ${token}`,
72+
'Content-Type': 'application/json'
73+
},
74+
body: JSON.stringify(alias)
7475
});
76+
if (!res.ok) {
77+
status.error = true;
78+
}
79+
} catch(err) {
80+
status.error = true;
81+
status.responseData = err;
82+
}
7583
return status;
7684
}

0 commit comments

Comments
 (0)