feat: update API endpoint for enterprise

This commit is contained in:
EnixCoda 2020-05-01 18:54:43 +08:00
parent 6f83adf934
commit 749af84cbb
No known key found for this signature in database
GPG key ID: 0C1A07377913A1DD
6 changed files with 20 additions and 5 deletions

View file

@ -1,4 +1,5 @@
import { errors } from 'platforms'
import { isEnterprise } from '.'
function apiRateLimitExceeded(content: any /* safe any */) {
return content?.['documentation_url'] === 'https://developer.github.com/v3/#rate-limiting'
@ -55,12 +56,14 @@ async function request(
}
}
const API_ENDPOINT = isEnterprise() ? `${window.location.host}/api/v3` : 'api.github.com'
export async function getRepoMeta(
userName: string,
repoName: string,
accessToken?: string,
): Promise<GitHubAPI.MetaData> {
const url = `https://api.github.com/repos/${userName}/${repoName}`
const url = `https://${API_ENDPOINT}/repos/${userName}/${repoName}`
return await request(url, { accessToken })
}
@ -70,7 +73,7 @@ export async function getTreeData(
branchName: string,
accessToken?: string,
): Promise<GitHubAPI.TreeData> {
const url = `https://api.github.com/repos/${userName}/${repoName}/git/trees/${branchName}?recursive=1`
const url = `https://${API_ENDPOINT}/repos/${userName}/${repoName}/git/trees/${branchName}?recursive=1`
return await request(url, { accessToken })
}
@ -80,7 +83,7 @@ export async function getBlobData(
sha: string,
accessToken?: string,
): Promise<GitHubAPI.BlobData> {
const url = `https://api.github.com/repos/${userName}/${repoName}/git/blobs/${sha}`
const url = `https://${API_ENDPOINT}/repos/${userName}/${repoName}/git/blobs/${sha}`
return await request(url, { accessToken })
}

View file

@ -76,10 +76,15 @@ function getUrlForRedirect(
type = 'blob',
path = '',
) {
return `https://github.com/${userName}/${repoName}/${type}/${branchName}/${path}`
return `https://${window.location.host}/${userName}/${repoName}/${type}/${branchName}/${path}`
}
export function isEnterprise() {
return !window.location.host.endsWith('github.com')
}
export const GitHub: Platform = {
isEnterprise,
resolveMeta() {
if (!URLHelper.isInRepoPage()) {
return null

View file

@ -78,6 +78,9 @@ function getUrlForRedirect(
}
export const Gitee: Platform = {
isEnterprise() {
return !window.location.host.endsWith('gitee.com')
},
resolveMeta() {
if (!DOMHelper.isInRepoPage()) {
return null

View file

@ -1,4 +1,7 @@
export const dummyPlatformForTypeSafety: Platform = {
isEnterprise() {
return false
},
resolveMeta() {
return null
},

View file

@ -1,4 +1,5 @@
type Platform = {
isEnterprise(): boolean
resolveMeta(): MetaData | null
getMetaData(
metaData: Pick<MetaData, 'userName' | 'repoName'>,

View file

@ -11,7 +11,7 @@ function transformModuleGitURL(node: TreeNode, URL: string) {
const matched = URL.match(subModuleURLRegex.git)
if (!matched) return
const [_, userName, repoName] = matched
return appendCommitPath(`https://github.com/${userName}/${repoName}`, node)
return appendCommitPath(`https://${window.location.host}/${userName}/${repoName}`, node)
}
function cutDotGit(URL: string) {