Migration du client sur Typescript
This commit is contained in:
84
client/src/util/gitea.ts
Normal file
84
client/src/util/gitea.ts
Normal file
@ -0,0 +1,84 @@
|
||||
|
||||
export class GiteaUnauthorizedError extends Error {
|
||||
constructor(...args: any[]) {
|
||||
super(...args)
|
||||
}
|
||||
}
|
||||
|
||||
export class GiteaClient {
|
||||
|
||||
fetchIssues(project: any, page = 1) {
|
||||
return fetch(`/gitea/api/v1/repos/${project}/issues?page=${page}`)
|
||||
.then(this.assertAuthorization)
|
||||
.then(this.assertOk)
|
||||
.then(res => res.json())
|
||||
;
|
||||
}
|
||||
|
||||
fetchUserProjects() {
|
||||
return fetch(`/gitea/api/v1/user/repos`)
|
||||
.then(this.assertAuthorization)
|
||||
.then(this.assertOk)
|
||||
.then(res => res.json())
|
||||
;
|
||||
}
|
||||
|
||||
addIssueLabel(project: any, issueNumber: any, labelID: any) {
|
||||
return fetch(`/gitea/api/v1/repos/${project}/issues/${issueNumber}/labels`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({ labels: [labelID] }),
|
||||
})
|
||||
.then(this.assertAuthorization)
|
||||
.then(this.assertOk)
|
||||
.then(res => res.json())
|
||||
}
|
||||
|
||||
fetchProjectLabels(project: any) {
|
||||
return fetch(`/gitea/api/v1/repos/${project}/labels`)
|
||||
.then(this.assertAuthorization)
|
||||
.then(this.assertOk)
|
||||
.then(res => res.json())
|
||||
;
|
||||
}
|
||||
|
||||
removeIssueLabel(project: any, issueNumber: any, labelID: any) {
|
||||
return fetch(`/gitea/api/v1/repos/${project}/issues/${issueNumber}/labels/${labelID}`, {
|
||||
method: 'DELETE'
|
||||
})
|
||||
.then(this.assertAuthorization)
|
||||
.then(this.assertOk)
|
||||
}
|
||||
|
||||
createIssue(project: any, title: any, body: any, labelID: any) {
|
||||
return fetch(`/gitea/api/v1/repos/${project}/issues`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
title,
|
||||
body,
|
||||
labels: [labelID],
|
||||
}),
|
||||
})
|
||||
.then(this.assertAuthorization)
|
||||
.then(this.assertOk)
|
||||
.then(res => res.json())
|
||||
}
|
||||
|
||||
assertOk(res: any) {
|
||||
if (!res.ok) return Promise.reject(new Error('Request failed'));
|
||||
return res;
|
||||
}
|
||||
|
||||
assertAuthorization(res: any) {
|
||||
if (res.status === 401 || res.status === 404) return Promise.reject(new GiteaUnauthorizedError());
|
||||
return res;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export const gitea = new GiteaClient();
|
Reference in New Issue
Block a user