Skip to content

Commit cb2b18c

Browse files
committed
Refactor: Use native fetch instead of ofetch for GitHub API calls
1 parent 3ff3221 commit cb2b18c

File tree

7 files changed

+53
-96
lines changed

7 files changed

+53
-96
lines changed

src/lib/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ export const GITHUB_WEB_API_CONFIG = {
2929
baseURL: "https://github.com",
3030
} as const
3131

32+
export const GITHUB_API_BASE_URL = "https://api.github.com"
33+
3234
export const GITHUB_BASE_URL = "https://github.com"
3335
export const GITHUB_CLIENT_ID = "01ab8ac9400c4e429b23"
3436
export const GITHUB_APP_SCOPES = [

src/lib/token.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import fs from "node:fs/promises"
44
import { PATHS } from "~/lib/paths"
55
import { getCopilotToken } from "~/services/copilot/get-token/copilot-token"
66
import { getDeviceCode } from "~/services/github/get-device-code"
7-
import { getGitHubUser } from "~/services/github/get-user/service"
7+
import { getGitHubUser } from "~/services/github/get-user"
88
import { pollAccessToken } from "~/services/github/poll-access-token"
99

1010
import { state } from "./state"

src/services/api-instance.ts

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
import consola from "consola"
22
import { FetchError, ofetch } from "ofetch"
33

4-
import {
5-
COPILOT_API_CONFIG,
6-
GITHUB_API_CONFIG,
7-
GITHUB_WEB_API_CONFIG,
8-
} from "~/lib/constants"
4+
import { COPILOT_API_CONFIG } from "~/lib/constants"
95
import { modelsCache } from "~/lib/models"
106
import { tokenService } from "~/lib/token"
117

@@ -41,17 +37,3 @@ export const copilot = ofetch.create({
4137
}
4238
},
4339
})
44-
45-
export const github = ofetch.create({
46-
baseURL: GITHUB_API_CONFIG.baseURL,
47-
48-
async onRequest({ options }) {
49-
const token = await tokenService.getGithubToken()
50-
options.headers.set("authorization", `token ${token}`)
51-
},
52-
})
53-
54-
// Only used for device flow auth
55-
export const _github = ofetch.create({
56-
baseURL: GITHUB_WEB_API_CONFIG.baseURL,
57-
})
Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,28 @@
1-
import type { GetCopilotTokenResponse } from "./types"
1+
import { GITHUB_API_BASE_URL } from "~/lib/constants"
2+
import { state } from "~/lib/state"
23

3-
import { github } from "../../api-instance"
4+
export const getCopilotToken = async () => {
5+
const response = await fetch(
6+
`${GITHUB_API_BASE_URL}/copilot_internal/v2/token`,
7+
{
8+
headers: {
9+
authorization: `token ${state.githubToken}`,
10+
},
11+
},
12+
)
413

5-
export const getCopilotToken = async () =>
6-
github<GetCopilotTokenResponse>("/copilot_internal/v2/token", {
7-
method: "GET",
8-
})
14+
if (!response.ok) {
15+
throw new Error("Failed to get Copilot token", {
16+
cause: await response.json(),
17+
})
18+
}
19+
20+
return (await response.json()) as GetCopilotTokenResponse
21+
}
22+
23+
// Trimmed for the sake of simplicity
24+
interface GetCopilotTokenResponse {
25+
expires_at: number
26+
refresh_in: number
27+
token: string
28+
}

src/services/github/get-token/service.ts

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/services/github/get-user.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { GITHUB_API_BASE_URL } from "~/lib/constants"
2+
import { state } from "~/lib/state"
3+
4+
export async function getGitHubUser() {
5+
const response = await fetch(`${GITHUB_API_BASE_URL}/user`, {
6+
headers: {
7+
authorization: `token ${state.githubToken}`,
8+
},
9+
})
10+
11+
if (!response.ok) {
12+
throw new Error("Failed to get GitHub user", {
13+
cause: await response.json(),
14+
})
15+
}
16+
17+
return (await response.json()) as GithubUser
18+
}
19+
20+
// Trimmed for the sake of simplicity
21+
interface GithubUser {
22+
login: string
23+
}

src/services/github/get-user/service.ts

Lines changed: 0 additions & 56 deletions
This file was deleted.

0 commit comments

Comments
 (0)