feat: migrate to typescript

This commit is contained in:
Aravinth Manivannan 2023-09-18 17:56:40 +05:30
parent 9dddd75b86
commit f2d07bbca0
Signed by: realaravinth
GPG key ID: F8F50389936984FF
4 changed files with 64 additions and 25 deletions

View file

@ -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
View 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)
});

View file

@ -2,23 +2,49 @@
// //
// SPDX-License-Identifier: AGPL-3.0-or-later // SPDX-License-Identifier: AGPL-3.0-or-later
import Auth from "./auth";
class Forgejo { class Forgejo {
url: URL;
username: string;
token?: Auth;
/** /**
* Represents a Forgejo instance. * Represents a Forgejo instance.
* @constructor * @constructor
* @param {string} url - The URL of the Forgejo instance * @param {string} url - The URL of the Forgejo instance
* @param {string} username - username of the logged in user * @param {string} username - username of the logged in user
*/ */
constructor(url, username) { constructor(url: string, username: string) {
this.url = new URL(url); this.url = new URL(url);
this.username = username; 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 * Get all notifications
*/ */
async getNotifications() { async getNotifications() {
this.url.path = "/api/v1/notifications"; this.url.pathname = "/api/v1/notifications";
let res = await fetch(this.url); let res = await fetch(this.url);
return await res.json(); return await res.json();
} }
@ -26,7 +52,7 @@ class Forgejo {
* Get number of unread notifications * Get number of unread notifications
*/ */
async getNumUnreadNotifications() { async getNumUnreadNotifications() {
this.url.path = "/api/v1/notifications/new"; this.url.pathname = "/api/v1/notifications/new";
let res = await fetch(this.url); let res = await fetch(this.url);
return await res.json()["new"]; return await res.json()["new"];
} }
@ -35,8 +61,8 @@ class Forgejo {
* Get Notification Thread * Get Notification Thread
* @param {number} id - The ID of a notification thread * @param {number} id - The ID of a notification thread
*/ */
async getNotificationThread(id) { async getNotificationThread(id: number) {
this.url.path = `/api/v1/notifications/threads/${id}`; this.url.pathname = `/api/v1/notifications/threads/${id}`;
let res = await fetch(this.url); let res = await fetch(this.url);
return await res.json(); return await res.json();
} }
@ -44,18 +70,18 @@ class Forgejo {
/** /**
* Get the time at which the issue was last read * 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 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 * Check if logged in user is mentioned in the issue
*/ */
async findMentionsInIssue(id) { async findMentionsInIssue(owner: string, repo: string, id: number) {
let issue = this.getIssue(id); let issue = this.getIssue(owner, repo, id);
let unreadTime = this.lastReadTime(issue); let unreadTime = this.lastReadTime(issue);
const mentionUtil = (str) => str.includes(this.username); const mentionUtil = (str: string): boolean => str.includes(this.username);
const items = []; const items = [];
@ -89,8 +115,8 @@ class Forgejo {
* Mark Notification Read * Mark Notification Read
* @param {number} id - The ID of a notification thread * @param {number} id - The ID of a notification thread
*/ */
async markNotificationRead(id) { async markNotificationRead(id: number) {
this.url.path = `/api/v1/notifications/threads/${id}`; this.url.pathname = `/api/v1/notifications/threads/${id}`;
await fetch(this.url, { method: "PATCH" }); await fetch(this.url, { method: "PATCH" });
} }
@ -99,8 +125,8 @@ class Forgejo {
* @param {string} owner - Name of the owner of the repository * @param {string} owner - Name of the owner of the repository
* @param {string} repo - Name of the repository * @param {string} repo - Name of the repository
*/ */
async markNotificationReadForRepo(owner, repo) { async markNotificationReadForRepo(owner: string, repo: string) {
this.url.path = `/api/v1/repos/${owner}/${repo}/notifications`; this.url.pathname = `/api/v1/repos/${owner}/${repo}/notifications`;
await fetch(this.url, { method: "PUT" }); await fetch(this.url, { method: "PUT" });
} }
@ -110,7 +136,7 @@ class Forgejo {
* @param {str} repo - The name of the repository * @param {str} repo - The name of the repository
* @param {number} id - The ID of an issue * @param {number} id - The ID of an issue
*/ */
async getIssue(owner, repo, id) {} async getIssue(owner: string, repo: string, id: number) {}
} }
export default Forgejo; export default Forgejo;

12
src/auth.ts Normal file
View file

@ -0,0 +1,12 @@
class Auth {
token: string;
constructor(token: string) {
this.token = token;
}
getToken(): string {
return this.token;
}
}
export default Auth;