diff --git a/src/api.test.js b/src/api.test.js deleted file mode 100644 index b658408..0000000 --- a/src/api.test.js +++ /dev/null @@ -1,10 +0,0 @@ -// SPDX-FileCopyrightText: 2023 Aravinth Manivannan -// -// 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) -}); diff --git a/src/api.test.ts b/src/api.test.ts new file mode 100644 index 0000000..81f1fbd --- /dev/null +++ b/src/api.test.ts @@ -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) +}); diff --git a/src/api.js b/src/api.ts similarity index 62% rename from src/api.js rename to src/api.ts index 239ce56..7242fd0 100644 --- a/src/api.js +++ b/src/api.ts @@ -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 + * 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; diff --git a/src/auth.ts b/src/auth.ts new file mode 100644 index 0000000..d05ff95 --- /dev/null +++ b/src/auth.ts @@ -0,0 +1,12 @@ +class Auth { + token: string; + constructor(token: string) { + this.token = token; + } + + getToken(): string { + return this.token; + } +} + +export default Auth;