2019-02-19 04:41:58 +00:00
|
|
|
import axios, {AxiosInstance, AxiosRequestConfig} from 'axios';
|
|
|
|
import {ServerApplicationCredentials} from "@/store/types";
|
2018-09-23 23:06:23 +00:00
|
|
|
|
|
|
|
// This token is set in the bootstrap.js file at the beginning of the request
|
|
|
|
// and is carried through from there.
|
|
|
|
// const token: string = '';
|
|
|
|
|
2018-12-29 23:51:13 +00:00
|
|
|
const http: AxiosInstance = axios.create({
|
|
|
|
headers: {
|
|
|
|
'X-Requested-With': 'XMLHttpRequest',
|
|
|
|
'Accept': 'application/json',
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
2018-09-23 23:06:23 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// If we have a phpdebugbar instance registered at this point in time go
|
|
|
|
// ahead and route the response data through to it so things show up.
|
2018-12-29 23:51:13 +00:00
|
|
|
// @ts-ignore
|
2018-09-23 23:06:23 +00:00
|
|
|
if (typeof window.phpdebugbar !== 'undefined') {
|
|
|
|
http.interceptors.response.use(response => {
|
2018-12-29 23:51:13 +00:00
|
|
|
// @ts-ignore
|
2018-09-23 23:06:23 +00:00
|
|
|
window.phpdebugbar.ajaxHandler.handle(response.request);
|
|
|
|
|
|
|
|
return response;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export default http;
|
2019-02-19 04:41:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a request object for the node that uses the server UUID and connection
|
|
|
|
* credentials. Basically just a tiny wrapper to set this quickly.
|
|
|
|
*/
|
|
|
|
export function withCredentials(server: string, credentials: ServerApplicationCredentials): AxiosInstance {
|
|
|
|
http.defaults.baseURL = credentials.node;
|
|
|
|
http.defaults.headers['X-Access-Server'] = server;
|
|
|
|
http.defaults.headers['X-Access-Token'] = credentials.key;
|
|
|
|
|
|
|
|
return http;
|
|
|
|
}
|