forked from NdoleStudio/httpsms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaxios.ts
More file actions
38 lines (31 loc) · 804 Bytes
/
Copy pathaxios.ts
File metadata and controls
38 lines (31 loc) · 804 Bytes
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
import axios from 'axios'
type NuxtWindow = Window & {
__NUXT__?: {
config?: {
apiBaseUrl?: string
}
}
}
function resolveBaseURL() {
if (typeof window !== 'undefined') {
return (
(window as NuxtWindow).__NUXT__?.config?.apiBaseUrl ||
process.env.API_BASE_URL ||
'http://localhost:8000'
)
}
return process.env.API_BASE_URL || 'http://localhost:8000'
}
const client = axios.create({
baseURL: resolveBaseURL(),
headers: {
'X-Client-Version': process.env.GITHUB_SHA || 'dev',
},
})
export function setAuthHeader(token: string | null) {
client.defaults.headers.common.Authorization = 'Bearer ' + token
}
export function setApiKey(apiKey: string | null) {
client.defaults.headers.common['x-api-key'] = apiKey ?? ''
}
export default client