feat: migrate to typescript
This commit is contained in:
parent
9dddd75b86
commit
f2d07bbca0
4 changed files with 64 additions and 25 deletions
|
@ -1,10 +0,0 @@
|
|||
// SPDX-FileCopyrightText: 2023 Aravinth Manivannan <realaravinth@batsense.net>
|
||||
//
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import Forgejo from './api';
|
||||
|
||||
test('foo', () => {
|
||||
const api = new Forgejo('https://git.batsense.net');
|
||||
api.markNotificationReadForRepo(12)
|
||||
});
|
11
src/api.test.ts
Normal file
11
src/api.test.ts
Normal file
|
@ -0,0 +1,11 @@
|
|||
import Forgejo from "./api";
|
||||
import authtoken from "../secrets/user1-accesstoken.json";
|
||||
|
||||
test("use authentication without setting it ", () => {
|
||||
const api = new Forgejo("http://localhost:3000", "owner_user");
|
||||
const token = authtoken["sha1"]
|
||||
|
||||
expect(() => api.getTokenAuth()).toThrow();
|
||||
api.setTokenAuth(token)
|
||||
expect(api.getTokenAuth().getToken()).toBe(token)
|
||||
});
|
|
@ -2,23 +2,49 @@
|
|||
//
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import Auth from "./auth";
|
||||
|
||||
class Forgejo {
|
||||
url: URL;
|
||||
username: string;
|
||||
token?: Auth;
|
||||
/**
|
||||
* Represents a Forgejo instance.
|
||||
* @constructor
|
||||
* @param {string} url - The URL of the Forgejo instance
|
||||
* @param {string} username - username of the logged in user
|
||||
*/
|
||||
constructor(url, username) {
|
||||
constructor(url: string, username: string) {
|
||||
this.url = new URL(url);
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
/**
|
||||
* Authenticate with token
|
||||
* @param {Auth} token - access token
|
||||
*/
|
||||
setTokenAuth(token: string) {
|
||||
this.token = new Auth(token);
|
||||
console.log(this.token.getToken())
|
||||
}
|
||||
|
||||
getTokenAuth(): Auth {
|
||||
if (this.token) {
|
||||
return this.token;
|
||||
} else {
|
||||
throw new Error("set authentication token before use");
|
||||
}
|
||||
}
|
||||
|
||||
getTokenAuthHeader() {
|
||||
return { Authentication: `Bearer ${this.getTokenAuth().getToken()}` };
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all notifications
|
||||
*/
|
||||
async getNotifications() {
|
||||
this.url.path = "/api/v1/notifications";
|
||||
this.url.pathname = "/api/v1/notifications";
|
||||
let res = await fetch(this.url);
|
||||
return await res.json();
|
||||
}
|
||||
|
@ -26,7 +52,7 @@ class Forgejo {
|
|||
* Get number of unread notifications
|
||||
*/
|
||||
async getNumUnreadNotifications() {
|
||||
this.url.path = "/api/v1/notifications/new";
|
||||
this.url.pathname = "/api/v1/notifications/new";
|
||||
let res = await fetch(this.url);
|
||||
return await res.json()["new"];
|
||||
}
|
||||
|
@ -35,8 +61,8 @@ class Forgejo {
|
|||
* Get Notification Thread
|
||||
* @param {number} id - The ID of a notification thread
|
||||
*/
|
||||
async getNotificationThread(id) {
|
||||
this.url.path = `/api/v1/notifications/threads/${id}`;
|
||||
async getNotificationThread(id: number) {
|
||||
this.url.pathname = `/api/v1/notifications/threads/${id}`;
|
||||
let res = await fetch(this.url);
|
||||
return await res.json();
|
||||
}
|
||||
|
@ -44,18 +70,18 @@ class Forgejo {
|
|||
/**
|
||||
* Get the time at which the issue was last read
|
||||
*/
|
||||
lastReadTime(issue) {
|
||||
lastReadTime(issue: number): number {
|
||||
return 10120123; // TODO: return last read UNIX epoch time from storage. If unread, return 0
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if logged in user is mentioned in the issue
|
||||
*/
|
||||
async findMentionsInIssue(id) {
|
||||
let issue = this.getIssue(id);
|
||||
async findMentionsInIssue(owner: string, repo: string, id: number) {
|
||||
let issue = this.getIssue(owner, repo, id);
|
||||
let unreadTime = this.lastReadTime(issue);
|
||||
|
||||
const mentionUtil = (str) => str.includes(this.username);
|
||||
const mentionUtil = (str: string): boolean => str.includes(this.username);
|
||||
|
||||
const items = [];
|
||||
|
||||
|
@ -89,8 +115,8 @@ class Forgejo {
|
|||
* Mark Notification Read
|
||||
* @param {number} id - The ID of a notification thread
|
||||
*/
|
||||
async markNotificationRead(id) {
|
||||
this.url.path = `/api/v1/notifications/threads/${id}`;
|
||||
async markNotificationRead(id: number) {
|
||||
this.url.pathname = `/api/v1/notifications/threads/${id}`;
|
||||
await fetch(this.url, { method: "PATCH" });
|
||||
}
|
||||
|
||||
|
@ -99,8 +125,8 @@ class Forgejo {
|
|||
* @param {string} owner - Name of the owner of the repository
|
||||
* @param {string} repo - Name of the repository
|
||||
*/
|
||||
async markNotificationReadForRepo(owner, repo) {
|
||||
this.url.path = `/api/v1/repos/${owner}/${repo}/notifications`;
|
||||
async markNotificationReadForRepo(owner: string, repo: string) {
|
||||
this.url.pathname = `/api/v1/repos/${owner}/${repo}/notifications`;
|
||||
await fetch(this.url, { method: "PUT" });
|
||||
}
|
||||
|
||||
|
@ -110,7 +136,7 @@ class Forgejo {
|
|||
* @param {str} repo - The name of the repository
|
||||
* @param {number} id - The ID of an issue
|
||||
*/
|
||||
async getIssue(owner, repo, id) {}
|
||||
async getIssue(owner: string, repo: string, id: number) {}
|
||||
}
|
||||
|
||||
export default Forgejo;
|
12
src/auth.ts
Normal file
12
src/auth.ts
Normal file
|
@ -0,0 +1,12 @@
|
|||
class Auth {
|
||||
token: string;
|
||||
constructor(token: string) {
|
||||
this.token = token;
|
||||
}
|
||||
|
||||
getToken(): string {
|
||||
return this.token;
|
||||
}
|
||||
}
|
||||
|
||||
export default Auth;
|
Loading…
Reference in a new issue