feat: add RepositoryClient for workspace, repo, and branch browsing
This commit is contained in:
73
src/clients/repository-client.ts
Normal file
73
src/clients/repository-client.ts
Normal file
@@ -0,0 +1,73 @@
|
||||
import { BaseClient, ClientOptions } from './base-client.js';
|
||||
|
||||
export class RepositoryClient extends BaseClient {
|
||||
constructor(options: ClientOptions = {}) {
|
||||
super(options);
|
||||
}
|
||||
|
||||
async listWorkspaces(options?: { page?: number; pagelen?: number }): Promise<any> {
|
||||
await this.ensureInitialized();
|
||||
try {
|
||||
const params: Record<string, any> = {};
|
||||
if (options?.page) params.page = options.page;
|
||||
if (options?.pagelen) params.pagelen = options.pagelen;
|
||||
const response = await this.axiosInstance.get('/workspaces', { params });
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
throw new Error(`Failed to list workspaces: ${this.formatError(error)}`);
|
||||
}
|
||||
}
|
||||
|
||||
async listRepositories(
|
||||
workspace: string,
|
||||
options?: { role?: 'member' | 'contributor' | 'owner'; page?: number; pagelen?: number },
|
||||
): Promise<any> {
|
||||
await this.ensureInitialized();
|
||||
try {
|
||||
const params: Record<string, any> = {};
|
||||
if (options?.role) params.role = options.role;
|
||||
if (options?.page) params.page = options.page;
|
||||
if (options?.pagelen) params.pagelen = options.pagelen;
|
||||
const response = await this.axiosInstance.get(
|
||||
`/repositories/${workspace}`,
|
||||
{ params },
|
||||
);
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
throw new Error(`Failed to list repositories: ${this.formatError(error)}`);
|
||||
}
|
||||
}
|
||||
|
||||
async getRepository(workspace: string, repoSlug: string): Promise<any> {
|
||||
await this.ensureInitialized();
|
||||
try {
|
||||
const response = await this.axiosInstance.get(
|
||||
`/repositories/${workspace}/${repoSlug}`,
|
||||
);
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
throw new Error(`Failed to get repository: ${this.formatError(error)}`);
|
||||
}
|
||||
}
|
||||
|
||||
async listBranches(
|
||||
workspace: string,
|
||||
repoSlug: string,
|
||||
options?: { filter_by_name?: string; page?: number; pagelen?: number },
|
||||
): Promise<any> {
|
||||
await this.ensureInitialized();
|
||||
try {
|
||||
const params: Record<string, any> = {};
|
||||
if (options?.filter_by_name) params.q = `name~"${options.filter_by_name}"`;
|
||||
if (options?.page) params.page = options.page;
|
||||
if (options?.pagelen) params.pagelen = options.pagelen;
|
||||
const response = await this.axiosInstance.get(
|
||||
`/repositories/${workspace}/${repoSlug}/refs/branches`,
|
||||
{ params },
|
||||
);
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
throw new Error(`Failed to list branches: ${this.formatError(error)}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user