/** * Gogs API Client * Provides methods to interact with a Gogs server REST API */ import axios, { AxiosInstance } from 'axios'; import type { GogsUser, GogsRepository, GogsSearchResponse, GogsFileContent, GogsBranch, GogsCommit, GogsConfig, } from './types.js'; export class GogsClient { private client: AxiosInstance; private serverUrl: string; constructor(config: GogsConfig) { this.serverUrl = config.serverUrl.replace(/\/$/, ''); this.client = axios.create({ baseURL: `${this.serverUrl}/api/v1`, headers: { 'Content-Type': 'application/json', ...(config.accessToken && { Authorization: `token ${config.accessToken}` }), }, }); } /** * Get information about the authenticated user */ async getCurrentUser(): Promise { const response = await this.client.get('/user'); return response.data; } /** * Get information about a specific user */ async getUser(username: string): Promise { const response = await this.client.get(`/users/${username}`); return response.data; } /** * Search for users */ async searchUsers(query: string, limit: number = 10): Promise { const response = await this.client.get>('/users/search', { params: { q: query, limit }, }); return response.data.data; } /** * List repositories for the authenticated user */ async listMyRepositories(): Promise { const response = await this.client.get('/user/repos'); return response.data; } /** * List repositories for a specific user */ async listUserRepositories(username: string): Promise { const response = await this.client.get(`/users/${username}/repos`); return response.data; } /** * Search for repositories */ async searchRepositories( query: string, options?: { uid?: number; limit?: number; page?: number } ): Promise { const response = await this.client.get>('/repos/search', { params: { q: query, uid: options?.uid || 0, limit: options?.limit || 10, page: options?.page || 1, }, }); return response.data.data; } /** * Get information about a specific repository */ async getRepository(owner: string, repo: string): Promise { const response = await this.client.get(`/repos/${owner}/${repo}`); return response.data; } /** * Create a new repository */ async createRepository(data: { name: string; description?: string; private?: boolean; auto_init?: boolean; gitignores?: string; license?: string; readme?: string; }): Promise { const response = await this.client.post('/user/repos', data); return response.data; } /** * Delete a repository */ async deleteRepository(owner: string, repo: string): Promise { await this.client.delete(`/repos/${owner}/${repo}`); } /** * Get file or directory contents */ async getContents( owner: string, repo: string, path: string, ref?: string ): Promise { const response = await this.client.get( `/repos/${owner}/${repo}/contents/${path}`, { params: ref ? { ref } : undefined, } ); return response.data; } /** * Get raw file content */ async getRawContent(owner: string, repo: string, ref: string, path: string): Promise { const response = await this.client.get( `/repos/${owner}/${repo}/raw/${ref}/${path}`, { responseType: 'text', } ); return response.data; } /** * List branches in a repository */ async listBranches(owner: string, repo: string): Promise { const response = await this.client.get(`/repos/${owner}/${repo}/branches`); return response.data; } /** * Get commits from a repository */ async getCommits( owner: string, repo: string, options?: { sha?: string; page?: number } ): Promise { const response = await this.client.get(`/repos/${owner}/${repo}/commits`, { params: { sha: options?.sha, page: options?.page || 1, }, }); return response.data; } }