| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- /**
- * 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<GogsUser> {
- const response = await this.client.get<GogsUser>('/user');
- return response.data;
- }
- /**
- * Get information about a specific user
- */
- async getUser(username: string): Promise<GogsUser> {
- const response = await this.client.get<GogsUser>(`/users/${username}`);
- return response.data;
- }
- /**
- * Search for users
- */
- async searchUsers(query: string, limit: number = 10): Promise<GogsUser[]> {
- const response = await this.client.get<GogsSearchResponse<GogsUser>>('/users/search', {
- params: { q: query, limit },
- });
- return response.data.data;
- }
- /**
- * List repositories for the authenticated user
- */
- async listMyRepositories(): Promise<GogsRepository[]> {
- const response = await this.client.get<GogsRepository[]>('/user/repos');
- return response.data;
- }
- /**
- * List repositories for a specific user
- */
- async listUserRepositories(username: string): Promise<GogsRepository[]> {
- const response = await this.client.get<GogsRepository[]>(`/users/${username}/repos`);
- return response.data;
- }
- /**
- * Search for repositories
- */
- async searchRepositories(
- query: string,
- options?: { uid?: number; limit?: number; page?: number }
- ): Promise<GogsRepository[]> {
- const response = await this.client.get<GogsSearchResponse<GogsRepository>>('/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<GogsRepository> {
- const response = await this.client.get<GogsRepository>(`/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<GogsRepository> {
- const response = await this.client.post<GogsRepository>('/user/repos', data);
- return response.data;
- }
- /**
- * Delete a repository
- */
- async deleteRepository(owner: string, repo: string): Promise<void> {
- await this.client.delete(`/repos/${owner}/${repo}`);
- }
- /**
- * Get file or directory contents
- */
- async getContents(
- owner: string,
- repo: string,
- path: string,
- ref?: string
- ): Promise<GogsFileContent | GogsFileContent[]> {
- const response = await this.client.get<GogsFileContent | GogsFileContent[]>(
- `/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<string> {
- const response = await this.client.get<string>(
- `/repos/${owner}/${repo}/raw/${ref}/${path}`,
- {
- responseType: 'text',
- }
- );
- return response.data;
- }
- /**
- * List branches in a repository
- */
- async listBranches(owner: string, repo: string): Promise<GogsBranch[]> {
- const response = await this.client.get<GogsBranch[]>(`/repos/${owner}/${repo}/branches`);
- return response.data;
- }
- /**
- * Get commits from a repository
- */
- async getCommits(
- owner: string,
- repo: string,
- options?: { sha?: string; page?: number }
- ): Promise<GogsCommit[]> {
- const response = await this.client.get<GogsCommit[]>(`/repos/${owner}/${repo}/commits`, {
- params: {
- sha: options?.sha,
- page: options?.page || 1,
- },
- });
- return response.data;
- }
- }
|