77 lines
1.8 KiB
TypeScript
77 lines
1.8 KiB
TypeScript
import axios from 'axios';
|
|
|
|
// Gitea API configuration
|
|
|
|
const createGiteaApi = (baseUrl, token = null) => {
|
|
const headers = {
|
|
'Content-Type': 'application/json',
|
|
};
|
|
|
|
if (token) {
|
|
headers.Authorization = `token ${token}`;
|
|
}
|
|
|
|
const api = axios.create({
|
|
baseURL: `${baseUrl}/api/v1`,
|
|
headers,
|
|
});
|
|
|
|
return {
|
|
// Fetch commits for a repository
|
|
fetchCommits: async (owner, repo, limit = 10, branch = 'main') => {
|
|
try {
|
|
const response = await api.get(
|
|
`/repos/${owner}/${repo}/commits`,
|
|
{
|
|
params: {
|
|
limit,
|
|
page: 1,
|
|
sha: branch, // Optional branch parameter
|
|
}
|
|
}
|
|
);
|
|
return response.data;
|
|
} catch (error) {
|
|
console.error('Error fetching Gitea commits:', error);
|
|
throw error;
|
|
}
|
|
},
|
|
|
|
// Fetch user repositories
|
|
fetchUserRepos: async (username) => {
|
|
try {
|
|
const response = await api.get(`/users/${username}/repos`);
|
|
return response.data;
|
|
} catch (error) {
|
|
console.error('Error fetching user repos:', error);
|
|
throw error;
|
|
}
|
|
},
|
|
|
|
// Fetch repository information
|
|
fetchRepoInfo: async (owner, repo) => {
|
|
try {
|
|
const response = await api.get(`/repos/${owner}/${repo}`);
|
|
return response.data;
|
|
} catch (error) {
|
|
console.error('Error fetching repo info:', error);
|
|
throw error;
|
|
}
|
|
},
|
|
|
|
// Search repositories
|
|
searchRepos: async (query) => {
|
|
try {
|
|
const response = await api.get('/repos/search', {
|
|
params: { q: query, limit: 10 }
|
|
});
|
|
return response.data.data;
|
|
} catch (error) {
|
|
console.error('Error searching repos:', error);
|
|
throw error;
|
|
}
|
|
}
|
|
};
|
|
};
|
|
|
|
export default createGiteaApi; |