// 與 Gitea API 溝通的 HTTP 客戶端,封裝認證標頭、分頁讀取與刪除請求。 // 對應原本 entrypoint.sh 的 fetch_all_pages 與 curl DELETE 呼叫。 export class GiteaClient { /** * @param {{ token?: string | null }} options 認證設定;有 token 時帶上 Authorization 標頭 */ constructor({ token = null } = {}) { this.headers = {} if (token) { this.headers.Authorization = `token ${token}` } } /** * 逐頁讀取分頁式清單 API,直到回傳空陣列為止,合併成單一陣列。 * @param {string} baseUrl 不含 query string 的 API 位址 * @returns {Promise} 所有頁面合併後的項目 */ async fetchAllPages(baseUrl) { const all = [] let page = 1 while (true) { const url = `${baseUrl}?page=${page}` const res = await fetch(url, { headers: this.headers }) if (!res.ok) { throw new Error(`GET ${url} failed: HTTP ${res.status}`) } const items = await res.json() if (!Array.isArray(items) || items.length === 0) { break } all.push(...items) page += 1 } return all } /** * 對指定資源發出 DELETE 請求。 * @param {string} url 目標資源位址 * @returns {Promise} HTTP 狀態碼 */ async deleteResource(url) { const res = await fetch(url, { method: 'DELETE', headers: this.headers }) return res.status } }