Skip to content

Commit 8606a21

Browse files
committed
refactor(ui): replace promise chaining with async/await in repo service
1 parent cd90d1b commit 8606a21

File tree

1 file changed

+24
-20
lines changed

1 file changed

+24
-20
lines changed

src/ui/services/repo.ts

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,18 @@ import { ServiceResult, getServiceError, errorResult, successResult } from './er
88
const canAddUser = async (repoId: string, user: string, action: string) => {
99
const apiV1Base = await getApiV1BaseUrl();
1010
const url = new URL(`${apiV1Base}/repo/${repoId}`);
11-
return axios
12-
.get<Repo>(url.toString(), getAxiosConfig())
13-
.then((response) => {
14-
const repo = response.data;
15-
if (action === 'authorise') {
16-
return !repo.users.canAuthorise.includes(user);
17-
} else {
18-
return !repo.users.canPush.includes(user);
19-
}
20-
})
21-
.catch((error: any) => {
22-
const { message } = getServiceError(error, 'Failed to validate repo permissions');
23-
throw new Error(message);
24-
});
11+
try {
12+
const response = await axios.get<Repo>(url.toString(), getAxiosConfig());
13+
const repo = response.data;
14+
if (action === 'authorise') {
15+
return !repo.users.canAuthorise.includes(user);
16+
} else {
17+
return !repo.users.canPush.includes(user);
18+
}
19+
} catch (error: any) {
20+
const { message } = getServiceError(error, 'Failed to validate repo permissions');
21+
throw new Error(message);
22+
}
2523
};
2624

2725
class DupUserValidationError extends Error {
@@ -79,11 +77,13 @@ const addUser = async (repoId: string, user: string, action: string): Promise<vo
7977
const apiV1Base = await getApiV1BaseUrl();
8078
const url = new URL(`${apiV1Base}/repo/${repoId}/user/${action}`);
8179
const data = { username: user };
82-
await axios.patch(url.toString(), data, getAxiosConfig()).catch((error: any) => {
80+
try {
81+
await axios.patch(url.toString(), data, getAxiosConfig());
82+
} catch (error: any) {
8383
const { message } = getServiceError(error, 'Failed to add user');
8484
console.log(message);
8585
throw new Error(message);
86-
});
86+
}
8787
} else {
8888
console.log('Duplicate user can not be added');
8989
throw new DupUserValidationError('Duplicate user can not be added');
@@ -94,22 +94,26 @@ const deleteUser = async (user: string, repoId: string, action: string): Promise
9494
const apiV1Base = await getApiV1BaseUrl();
9595
const url = new URL(`${apiV1Base}/repo/${repoId}/user/${action}/${user}`);
9696

97-
await axios.delete(url.toString(), getAxiosConfig()).catch((error: any) => {
97+
try {
98+
await axios.delete(url.toString(), getAxiosConfig());
99+
} catch (error: any) {
98100
const { message } = getServiceError(error, 'Failed to remove user');
99101
console.log(message);
100102
throw new Error(message);
101-
});
103+
}
102104
};
103105

104106
const deleteRepo = async (repoId: string): Promise<void> => {
105107
const apiV1Base = await getApiV1BaseUrl();
106108
const url = new URL(`${apiV1Base}/repo/${repoId}/delete`);
107109

108-
await axios.delete(url.toString(), getAxiosConfig()).catch((error: any) => {
110+
try {
111+
await axios.delete(url.toString(), getAxiosConfig());
112+
} catch (error: any) {
109113
const { message } = getServiceError(error, 'Failed to delete repository');
110114
console.log(message);
111115
throw new Error(message);
112-
});
116+
}
113117
};
114118

115119
export { addUser, deleteUser, getRepos, getRepo, addRepo, deleteRepo };

0 commit comments

Comments
 (0)