51 lines
1.1 KiB
TypeScript
51 lines
1.1 KiB
TypeScript
import Axios from "axios";
|
|
import store from "@/state/store"
|
|
import { AppState } from "@/state/reducer";
|
|
import { UserState } from "@/state/reducer/user";
|
|
|
|
import * as user from "./user";
|
|
import * as edition from "./edition";
|
|
import * as page from "./page";
|
|
import * as student from "./student";
|
|
import * as type from "./type";
|
|
import * as companies from "./companies";
|
|
import * as internship from "./internship";
|
|
import * as upload from "./upload";
|
|
import * as report from "./report";
|
|
|
|
export const axios = Axios.create({
|
|
baseURL: process.env.API_BASE_URL || `${window.location.protocol}//${window.location.hostname}/api/`,
|
|
})
|
|
|
|
axios.interceptors.request.use(config => {
|
|
const state = store.getState() as AppState;
|
|
const user = state.user as UserState;
|
|
|
|
if (!user.loggedIn) {
|
|
return config;
|
|
}
|
|
|
|
return {
|
|
...config,
|
|
headers: {
|
|
...config.headers,
|
|
Authorization: `Bearer ${ user.token }`
|
|
}
|
|
}
|
|
})
|
|
|
|
const api = {
|
|
user,
|
|
edition,
|
|
page,
|
|
student,
|
|
type,
|
|
companies,
|
|
internship,
|
|
upload,
|
|
report,
|
|
}
|
|
|
|
export default api;
|
|
|