gogs-client.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /**
  2. * Gogs API Client
  3. * Provides methods to interact with a Gogs server REST API
  4. */
  5. import axios, { AxiosInstance } from 'axios';
  6. import type {
  7. GogsUser,
  8. GogsRepository,
  9. GogsSearchResponse,
  10. GogsFileContent,
  11. GogsBranch,
  12. GogsCommit,
  13. GogsConfig,
  14. } from './types.js';
  15. export class GogsClient {
  16. private client: AxiosInstance;
  17. private serverUrl: string;
  18. constructor(config: GogsConfig) {
  19. this.serverUrl = config.serverUrl.replace(/\/$/, '');
  20. this.client = axios.create({
  21. baseURL: `${this.serverUrl}/api/v1`,
  22. headers: {
  23. 'Content-Type': 'application/json',
  24. ...(config.accessToken && { Authorization: `token ${config.accessToken}` }),
  25. },
  26. });
  27. }
  28. /**
  29. * Get information about the authenticated user
  30. */
  31. async getCurrentUser(): Promise<GogsUser> {
  32. const response = await this.client.get<GogsUser>('/user');
  33. return response.data;
  34. }
  35. /**
  36. * Get information about a specific user
  37. */
  38. async getUser(username: string): Promise<GogsUser> {
  39. const response = await this.client.get<GogsUser>(`/users/${username}`);
  40. return response.data;
  41. }
  42. /**
  43. * Search for users
  44. */
  45. async searchUsers(query: string, limit: number = 10): Promise<GogsUser[]> {
  46. const response = await this.client.get<GogsSearchResponse<GogsUser>>('/users/search', {
  47. params: { q: query, limit },
  48. });
  49. return response.data.data;
  50. }
  51. /**
  52. * List repositories for the authenticated user
  53. */
  54. async listMyRepositories(): Promise<GogsRepository[]> {
  55. const response = await this.client.get<GogsRepository[]>('/user/repos');
  56. return response.data;
  57. }
  58. /**
  59. * List repositories for a specific user
  60. */
  61. async listUserRepositories(username: string): Promise<GogsRepository[]> {
  62. const response = await this.client.get<GogsRepository[]>(`/users/${username}/repos`);
  63. return response.data;
  64. }
  65. /**
  66. * Search for repositories
  67. */
  68. async searchRepositories(
  69. query: string,
  70. options?: { uid?: number; limit?: number; page?: number }
  71. ): Promise<GogsRepository[]> {
  72. const response = await this.client.get<GogsSearchResponse<GogsRepository>>('/repos/search', {
  73. params: {
  74. q: query,
  75. uid: options?.uid || 0,
  76. limit: options?.limit || 10,
  77. page: options?.page || 1,
  78. },
  79. });
  80. return response.data.data;
  81. }
  82. /**
  83. * Get information about a specific repository
  84. */
  85. async getRepository(owner: string, repo: string): Promise<GogsRepository> {
  86. const response = await this.client.get<GogsRepository>(`/repos/${owner}/${repo}`);
  87. return response.data;
  88. }
  89. /**
  90. * Create a new repository
  91. */
  92. async createRepository(data: {
  93. name: string;
  94. description?: string;
  95. private?: boolean;
  96. auto_init?: boolean;
  97. gitignores?: string;
  98. license?: string;
  99. readme?: string;
  100. }): Promise<GogsRepository> {
  101. const response = await this.client.post<GogsRepository>('/user/repos', data);
  102. return response.data;
  103. }
  104. /**
  105. * Delete a repository
  106. */
  107. async deleteRepository(owner: string, repo: string): Promise<void> {
  108. await this.client.delete(`/repos/${owner}/${repo}`);
  109. }
  110. /**
  111. * Get file or directory contents
  112. */
  113. async getContents(
  114. owner: string,
  115. repo: string,
  116. path: string,
  117. ref?: string
  118. ): Promise<GogsFileContent | GogsFileContent[]> {
  119. const response = await this.client.get<GogsFileContent | GogsFileContent[]>(
  120. `/repos/${owner}/${repo}/contents/${path}`,
  121. {
  122. params: ref ? { ref } : undefined,
  123. }
  124. );
  125. return response.data;
  126. }
  127. /**
  128. * Get raw file content
  129. */
  130. async getRawContent(owner: string, repo: string, ref: string, path: string): Promise<string> {
  131. const response = await this.client.get<string>(
  132. `/repos/${owner}/${repo}/raw/${ref}/${path}`,
  133. {
  134. responseType: 'text',
  135. }
  136. );
  137. return response.data;
  138. }
  139. /**
  140. * List branches in a repository
  141. */
  142. async listBranches(owner: string, repo: string): Promise<GogsBranch[]> {
  143. const response = await this.client.get<GogsBranch[]>(`/repos/${owner}/${repo}/branches`);
  144. return response.data;
  145. }
  146. /**
  147. * Get commits from a repository
  148. */
  149. async getCommits(
  150. owner: string,
  151. repo: string,
  152. options?: { sha?: string; page?: number }
  153. ): Promise<GogsCommit[]> {
  154. const response = await this.client.get<GogsCommit[]>(`/repos/${owner}/${repo}/commits`, {
  155. params: {
  156. sha: options?.sha,
  157. page: options?.page || 1,
  158. },
  159. });
  160. return response.data;
  161. }
  162. }