forked from ericc-ch/copilot-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi-instance.ts
More file actions
61 lines (52 loc) · 1.7 KB
/
api-instance.ts
File metadata and controls
61 lines (52 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import consola from "consola"
import { FetchError, ofetch } from "ofetch"
import {
COPILOT_API_CONFIG,
GITHUB_API_CONFIG,
GITHUB_WEB_API_CONFIG,
} from "~/lib/constants"
import { modelsCache } from "~/lib/models"
import { tokenService } from "~/lib/token"
export const copilot = ofetch.create({
baseURL: COPILOT_API_CONFIG.baseURL,
headers: COPILOT_API_CONFIG.headers,
onRequest({ options }) {
options.headers.set(
"authorization",
`Bearer ${tokenService.getCopilotToken()}`,
)
},
onRequestError({ error, options }) {
if (error instanceof FetchError) {
consola.error(
// eslint-disable-next-line @typescript-eslint/no-base-to-string, @typescript-eslint/restrict-template-expressions
`Request failed: ${options.body} \n ${error}`,
)
}
},
onResponse({ response }) {
if (response.url.endsWith("/models") && response._data) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
modelsCache.setModels(response._data)
}
},
onResponseError({ error, response, options }) {
if (error instanceof FetchError) {
consola.error(
// eslint-disable-next-line @typescript-eslint/no-base-to-string, @typescript-eslint/restrict-template-expressions
`Request failed: ${options.body} \n ${error} \n with response: ${JSON.stringify(response)}`,
)
}
},
})
export const github = ofetch.create({
baseURL: GITHUB_API_CONFIG.baseURL,
async onRequest({ options }) {
const token = await tokenService.getGithubToken()
options.headers.set("authorization", `token ${token}`)
},
})
// Only used for device flow auth
export const _github = ofetch.create({
baseURL: GITHUB_WEB_API_CONFIG.baseURL,
})