diff --git a/spack.config.js b/spack.config.js deleted file mode 100644 index 78d7f5e..0000000 --- a/spack.config.js +++ /dev/null @@ -1,8 +0,0 @@ -module.exports = { - entry: { - web: __dirname + "/src/index.ts", - }, - output: { - path: __dirname + "/dist", - }, -}; diff --git a/src/index.ts b/src/index.ts index 9b6e8fd..79e6836 100644 --- a/src/index.ts +++ b/src/index.ts @@ -13,6 +13,7 @@ class Forgejo { url: URL; username: string; token?: Auth; + cors: boolean; /** * Represents a Forgejo instance. * @constructor @@ -20,6 +21,7 @@ class Forgejo { */ constructor(url: string) { this.url = new URL(url); + this.cors = false; } /** @@ -50,12 +52,21 @@ class Forgejo { return { Authorization: `token ${this.getTokenAuth().getToken()}` }; } + getUrl(): URL { + if (this.cors) { + let url = new URL("http://localhost:3030"); + url.pathname = this.url.toString(); + return url; + } + return this.url; + } + /** * Get logged in user */ async getUser(): Promise { this.url.pathname = "/api/v1/user"; - let res = await fetch(this.url, { + let res = await fetch(this.getUrl(), { method: "GET", credentials: "omit", headers: this.getTokenAuthHeader(), @@ -63,14 +74,17 @@ class Forgejo { return await res.json(); } + setCors() { + this.cors = true; + } + /** * Get all notifications */ async getNotifications(): Promise> { - this.url.pathname = "/api/v1/notifications"; this.url.pathname = "/api/v1/notifications"; console.log(this.url); - let res = await fetch(this.url, { + let res = await fetch(this.getUrl(), { method: "GET", credentials: "omit", headers: this.getTokenAuthHeader(), @@ -83,7 +97,7 @@ class Forgejo { */ async getNumUnreadNotifications(): Promise { this.url.pathname = "/api/v1/notifications/new"; - let res = await fetch(this.url, { + let res = await fetch(this.getUrl(), { method: "GET", credentials: "omit", headers: this.getTokenAuthHeader(), @@ -99,7 +113,7 @@ class Forgejo { */ async getNotificationThread(id: number): Promise { this.url.pathname = `/api/v1/notifications/threads/${id}`; - let res = await fetch(this.url, { + let res = await fetch(this.getUrl(), { method: "GET", credentials: "omit", headers: this.getTokenAuthHeader(), @@ -181,7 +195,7 @@ class Forgejo { */ async getIssue(owner: string, repo: string, id: number): Promise { this.url.pathname = `/api/v1/repos/${owner}/${repo}/issues/${id}`; - let res = await fetch(this.url, { + let res = await fetch(this.getUrl(), { method: "GET", credentials: "omit", headers: this.getTokenAuthHeader(), @@ -214,7 +228,7 @@ class Forgejo { // TODO: check if issue.number != issue.id causes problems. I'm assuming // Issue.number is the local repository issue ID and issue.id is DB ID this.url.pathname = `/api/v1/repos/${issue.repository.owner}/${issue.repository.name}/issues/${issue.number}/comments`; - let res = await fetch(this.url, { + let res = await fetch(this.getUrl(), { method: "GET", credentials: "omit", headers: this.getTokenAuthHeader(),