-
-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathapi.ts
More file actions
55 lines (42 loc) · 1.19 KB
/
Copy pathapi.ts
File metadata and controls
55 lines (42 loc) · 1.19 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
/// <reference path="../global.d.ts" />
import { Gitlab } from '@gitbeaker/rest'
import type { ProxyAgentConfigurationType } from 'global-agent'
import { bootstrap } from 'global-agent'
import { env } from './env.ts'
const PROXY_PROPS = ['http_proxy', 'https_proxy', 'no_proxy'] as const
let bootstrapped = false
const createGitlab = (token: string) => {
const host = env.GITLAB_HOST
if (env.GITLAB_TOKEN_TYPE === 'oauth') {
return new Gitlab({
host,
oauthToken: token,
})
}
return new Gitlab({
host,
token,
})
}
const apiCache = new Map<string, ReturnType<typeof createGitlab>>()
export const createApi = (gitlabToken?: string) => {
if (!bootstrapped) {
bootstrapped = true
bootstrap()
for (const prop of PROXY_PROPS) {
const uProp = prop.toUpperCase() as keyof ProxyAgentConfigurationType
const value = process.env[uProp] || process.env[prop]
if (value) {
GLOBAL_AGENT[uProp] = value
}
}
}
const token = gitlabToken || env.GITLAB_TOKEN
let api = apiCache.get(token)
if (!api) {
api = createGitlab(token)
apiCache.set(token, api)
}
return api
}
export type GitLabApi = ReturnType<typeof createApi>