2019-06-10 02:26:20 +00:00
|
|
|
import axios, { AxiosInstance } from 'axios';
|
2020-04-10 19:41:08 +00:00
|
|
|
import { store } from '@/state';
|
2019-06-10 02:26:20 +00:00
|
|
|
|
|
|
|
const http: AxiosInstance = axios.create({
|
2020-04-10 19:41:08 +00:00
|
|
|
timeout: 20000,
|
2019-06-10 02:26:20 +00:00
|
|
|
headers: {
|
|
|
|
'X-Requested-With': 'XMLHttpRequest',
|
|
|
|
'Accept': 'application/json',
|
|
|
|
'Content-Type': 'application/json',
|
2019-06-12 05:02:18 +00:00
|
|
|
'X-CSRF-Token': (window as any).X_CSRF_TOKEN as string || '',
|
2019-06-10 02:26:20 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2020-04-10 19:41:08 +00:00
|
|
|
http.interceptors.request.use(req => {
|
|
|
|
store.getActions().progress.startContinuous();
|
|
|
|
|
|
|
|
return req;
|
|
|
|
});
|
|
|
|
|
|
|
|
http.interceptors.response.use(resp => {
|
|
|
|
store.getActions().progress.setComplete();
|
|
|
|
|
|
|
|
return resp;
|
|
|
|
});
|
|
|
|
|
2019-06-10 02:26:20 +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.
|
|
|
|
// @ts-ignore
|
|
|
|
if (typeof window.phpdebugbar !== 'undefined') {
|
|
|
|
http.interceptors.response.use(response => {
|
|
|
|
// @ts-ignore
|
|
|
|
window.phpdebugbar.ajaxHandler.handle(response.request);
|
|
|
|
|
|
|
|
return response;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export default http;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts an error into a human readable response. Mostly just a generic helper to
|
|
|
|
* make sure we display the message from the server back to the user if we can.
|
|
|
|
*/
|
|
|
|
export function httpErrorToHuman (error: any): string {
|
|
|
|
if (error.response && error.response.data) {
|
2020-04-07 04:59:14 +00:00
|
|
|
let { data } = error.response;
|
|
|
|
|
|
|
|
// Some non-JSON requests can still return the error as a JSON block. In those cases, attempt
|
|
|
|
// to parse it into JSON so we can display an actual error.
|
|
|
|
if (typeof data === 'string') {
|
|
|
|
try {
|
|
|
|
data = JSON.parse(data);
|
|
|
|
} catch (e) {
|
|
|
|
// do nothing, bad json
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-10 02:26:20 +00:00
|
|
|
if (data.errors && data.errors[0] && data.errors[0].detail) {
|
|
|
|
return data.errors[0].detail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return error.message;
|
|
|
|
}
|
2019-08-17 23:03:10 +00:00
|
|
|
|
2019-11-03 20:20:11 +00:00
|
|
|
export interface FractalResponseData {
|
|
|
|
object: string;
|
|
|
|
attributes: {
|
|
|
|
[k: string]: any;
|
|
|
|
relationships?: {
|
|
|
|
[k: string]: FractalResponseData;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-08-17 23:03:10 +00:00
|
|
|
export interface PaginatedResult<T> {
|
|
|
|
items: T[];
|
|
|
|
pagination: PaginationDataSet;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface PaginationDataSet {
|
|
|
|
total: number;
|
|
|
|
count: number;
|
|
|
|
perPage: number;
|
|
|
|
currentPage: number;
|
|
|
|
totalPages: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getPaginationSet (data: any): PaginationDataSet {
|
|
|
|
return {
|
|
|
|
total: data.total,
|
|
|
|
count: data.count,
|
|
|
|
perPage: data.per_page,
|
|
|
|
currentPage: data.current_page,
|
|
|
|
totalPages: data.total_pages,
|
|
|
|
};
|
|
|
|
}
|