feat: rename http-common to fetcher (#2620)
Co-authored-by: Dominik Pschenitschni <mail@celement.de> Reviewed-on: https://kolaente.dev/vikunja/frontend/pulls/2620 Co-authored-by: Dominik Pschenitschni <dpschen@noreply.kolaente.de> Co-committed-by: Dominik Pschenitschni <dpschen@noreply.kolaente.de>
This commit is contained in:

committed by
konrad

parent
44e6981759
commit
096daad80a
@ -1,4 +1,4 @@
|
||||
import {AuthenticatedHTTPFactory} from '@/http-common'
|
||||
import {AuthenticatedHTTPFactory} from '@/helpers/fetcher'
|
||||
import type {AxiosResponse} from 'axios'
|
||||
|
||||
let savedToken: string | null = null
|
||||
|
36
src/helpers/fetcher.ts
Normal file
36
src/helpers/fetcher.ts
Normal file
@ -0,0 +1,36 @@
|
||||
import axios from 'axios'
|
||||
import {getToken} from '@/helpers/auth'
|
||||
|
||||
export function HTTPFactory() {
|
||||
const instance = axios.create({baseURL: window.API_URL})
|
||||
|
||||
instance.interceptors.request.use((config) => {
|
||||
// by setting the baseURL fresh for every request
|
||||
// we make sure that it is never outdated in case it is updated
|
||||
config.baseURL = window.API_URL
|
||||
|
||||
return config
|
||||
})
|
||||
|
||||
return instance
|
||||
}
|
||||
|
||||
export function AuthenticatedHTTPFactory() {
|
||||
const instance = HTTPFactory()
|
||||
|
||||
instance.interceptors.request.use((config) => {
|
||||
config.headers = {
|
||||
...config.headers,
|
||||
'Content-Type': 'application/json',
|
||||
}
|
||||
|
||||
// Set the default auth header if we have a token
|
||||
const token = getToken()
|
||||
if (token !== null) {
|
||||
config.headers['Authorization'] = `Bearer ${token}`
|
||||
}
|
||||
return config
|
||||
})
|
||||
|
||||
return instance
|
||||
}
|
Reference in New Issue
Block a user