export class GiteaUnauthorizedError extends Error { constructor(...args: any[]) { super(...args) Object.setPrototypeOf(this, GiteaUnauthorizedError.prototype); } } 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(page = 1) { return fetch(`/gitea/api/v1/user/repos?page=${page}`) .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 ? [labelID] : undefined, }), }) .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();