Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions frontend/src/services/github.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,26 @@
const BASE = 'https://api.github.com'

export async function fetchCurrentUser(token) {
const res = await fetch(`${BASE}/user`, {
headers: { Authorization: `Bearer ${token}`, Accept: 'application/vnd.github+json' },
})
const headers = (token) => ({
Authorization: `Bearer ${token}`,
Accept: 'application/vnd.github+json',
})

async function apiFetch(url, token) {
const res = await fetch(url, { headers: headers(token) })
if (!res.ok) return { error: res.status, message: await res.text() }
return res.json()
}

export const getUser = (token) => apiFetch(`${BASE}/user`, token)

export const listRepos = (token, page = 1) =>
apiFetch(`${BASE}/user/repos?per_page=50&sort=updated&page=${page}`, token)

export const getBranches = (token, owner, repo) =>
apiFetch(`${BASE}/repos/${owner}/${repo}/branches`, token)

export const getTree = (token, owner, repo, sha) =>
apiFetch(`${BASE}/repos/${owner}/${repo}/git/trees/${sha}?recursive=1`, token)

// kept for backwards compatibility with auth store
export const fetchCurrentUser = (token) => getUser(token)
22 changes: 22 additions & 0 deletions frontend/src/services/gitlab.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,25 @@ export async function exchangeGitlabCode(code) {

return response.json();
}

const apiBase = (host) => `https://${host}/api/v4`

const glHeaders = (token) => ({ Authorization: `Bearer ${token}` })

async function glFetch(url, token) {
const res = await fetch(url, { headers: glHeaders(token) })
if (!res.ok) return { error: res.status, message: await res.text() }
return res.json()
}

export const getUser = (token, host = 'gitlab.com') =>
glFetch(`${apiBase(host)}/user`, token)

export const listProjects = (token, host = 'gitlab.com', page = 1) =>
glFetch(`${apiBase(host)}/projects?membership=true&per_page=50&page=${page}`, token)

export const getBranches = (token, host = 'gitlab.com', id) =>
glFetch(`${apiBase(host)}/projects/${id}/repository/branches`, token)

export const getTree = (token, host = 'gitlab.com', id, ref) =>
glFetch(`${apiBase(host)}/projects/${id}/repository/tree?recursive=true&pagination=none&ref=${ref}`, token)
Loading