From 1e0ccec03e1ef0b70fa40d729ad6efc6544f4411 Mon Sep 17 00:00:00 2001 From: Philipp Kunz Date: Sat, 14 Dec 2024 22:53:42 +0100 Subject: [PATCH] fix(core): Refactor fetch logic to use a unified fetchFunction for API calls --- changelog.md | 5 +++++ ts/00_commitinfo_data.ts | 2 +- ts/index.ts | 41 ++++++++++++++++++++-------------------- 3 files changed, 26 insertions(+), 22 deletions(-) diff --git a/changelog.md b/changelog.md index cbfc356..e2edd60 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,10 @@ # Changelog +## 2024-12-14 - 1.6.4 - fix(core) +Refactor fetch logic to use a unified fetchFunction for API calls + +- Consolidated API request logic in the CodeFeed class to use fetchFunction for improved maintainability. + ## 2024-12-14 - 1.6.3 - fix(codefeed) Refactor and fix formatting issues in the CodeFeed module diff --git a/ts/00_commitinfo_data.ts b/ts/00_commitinfo_data.ts index 4a53713..2d8c105 100644 --- a/ts/00_commitinfo_data.ts +++ b/ts/00_commitinfo_data.ts @@ -3,6 +3,6 @@ */ export const commitinfo = { name: '@foss.global/codefeed', - version: '1.6.3', + version: '1.6.4', description: 'The @foss.global/codefeed module is designed for generating feeds from Gitea repositories, enhancing development workflows by processing commit data and repository activities.' } diff --git a/ts/index.ts b/ts/index.ts index dd1e2df..0a330f5 100644 --- a/ts/index.ts +++ b/ts/index.ts @@ -20,13 +20,13 @@ export class CodeFeed { * Load the changelog directly from the Gitea repository. */ private async loadChangelogFromRepo(owner: string, repo: string): Promise { - const url = `${this.baseUrl}/api/v1/repos/${owner}/${repo}/contents/changelog.md`; + const url = `/api/v1/repos/${owner}/${repo}/contents/changelog.md`; const headers: Record = {}; if (this.token) { headers['Authorization'] = `token ${this.token}`; } - const response = await fetch(url, { headers }); + const response = await this.fetchFunction(url, { headers }); if (!response.ok) { console.error( @@ -80,8 +80,8 @@ export class CodeFeed { } private async fetchAllOrganizations(): Promise { - const url = `${this.baseUrl}/api/v1/orgs`; - const response = await fetch(url, { + const url = `/api/v1/orgs`; + const response = await this.fetchFunction(url, { headers: this.token ? { Authorization: `token ${this.token}` } : {}, }); @@ -99,14 +99,14 @@ export class CodeFeed { }): Promise { let rssUrl: string; if (optionsArg.orgName && !optionsArg.repoName) { - rssUrl = `${this.baseUrl}/${optionsArg.orgName}.atom`; + rssUrl = `/${optionsArg.orgName}.atom`; } else if (optionsArg.orgName && optionsArg.repoName) { - rssUrl = `${this.baseUrl}/${optionsArg.orgName}/${optionsArg.repoName}.atom`; + rssUrl = `/${optionsArg.orgName}/${optionsArg.repoName}.atom`; } else { throw new Error('Invalid arguments provided to fetchOrgRssFeed.'); } - const response = await fetch(rssUrl); + const response = await this.fetchFunction(rssUrl, {}); if (!response.ok) { throw new Error( `Failed to fetch RSS feed for organization ${optionsArg.orgName}/${optionsArg.repoName}: ${response.statusText}` @@ -135,11 +135,9 @@ export class CodeFeed { const allRepos: plugins.interfaces.IRepository[] = []; while (true) { - const url = new URL(`${this.baseUrl}/api/v1/repos/search`); - url.searchParams.set('limit', '50'); - url.searchParams.set('page', page.toString()); + const url = `/api/v1/repos/search?limit=50&page=${page.toString()}`; - const resp = await fetch(url.href, { + const resp = await this.fetchFunction(url, { headers: this.token ? { Authorization: `token ${this.token}` } : {}, }); @@ -164,17 +162,15 @@ export class CodeFeed { const tags: plugins.interfaces.ITag[] = []; while (true) { - const url = new URL(`${this.baseUrl}/api/v1/repos/${owner}/${repo}/tags`); - url.searchParams.set('limit', '50'); - url.searchParams.set('page', page.toString()); + const url = `/api/v1/repos/${owner}/${repo}/tags?limit=50&page=${page.toString()}`; - const resp = await fetch(url.href, { + const resp = await this.fetchFunction(url, { headers: this.token ? { Authorization: `token ${this.token}` } : {}, }); if (!resp.ok) { console.error( - `Failed to fetch tags for ${owner}/${repo}: ${resp.status} ${resp.statusText} at ${url.href}` + `Failed to fetch tags for ${owner}/${repo}: ${resp.status} ${resp.statusText} at ${url}` ); throw new Error(`Failed to fetch tags for ${owner}/${repo}: ${resp.statusText}`); } @@ -207,17 +203,15 @@ export class CodeFeed { const recentCommits: plugins.interfaces.ICommit[] = []; while (true) { - const url = new URL(`${this.baseUrl}/api/v1/repos/${owner}/${repo}/commits`); - url.searchParams.set('limit', '50'); - url.searchParams.set('page', page.toString()); + const url = `/api/v1/repos/${owner}/${repo}/commits?limit=50&page=${page.toString()}`; - const resp = await fetch(url.href, { + const resp = await this.fetchFunction(url, { headers: this.token ? { Authorization: `token ${this.token}` } : {}, }); if (!resp.ok) { console.error( - `Failed to fetch commits for ${owner}/${repo}: ${resp.status} ${resp.statusText} at ${url.href}` + `Failed to fetch commits for ${owner}/${repo}: ${resp.status} ${resp.statusText} at ${url}` ); throw new Error(`Failed to fetch commits for ${owner}/${repo}: ${resp.statusText}`); } @@ -369,4 +363,9 @@ export class CodeFeed { return allCommits; } + + public async fetchFunction(urlArg: string, optionsArg: RequestInit): Promise { + const response = await fetch(`${this.baseUrl}${urlArg}`, optionsArg); + return response; + } }