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({
|
2022-05-22 21:01:39 +00:00
|
|
|
withCredentials: true,
|
2020-04-10 19:41:08 +00:00
|
|
|
timeout: 20000,
|
2019-06-10 02:26:20 +00:00
|
|
|
headers: {
|
|
|
|
'X-Requested-With': 'XMLHttpRequest',
|
2020-07-05 01:30:50 +00:00
|
|
|
Accept: 'application/json',
|
2019-06-10 02:26:20 +00:00
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2020-04-10 19:41:08 +00:00
|
|
|
http.interceptors.request.use(req => {
|
2022-05-04 23:19:00 +00:00
|
|
|
if (!req.url?.endsWith('/resources')) {
|
2020-04-12 23:08:29 +00:00
|
|
|
store.getActions().progress.startContinuous();
|
|
|
|
}
|
2020-04-10 19:41:08 +00:00
|
|
|
|
|
|
|
return req;
|
|
|
|
});
|
|
|
|
|
|
|
|
http.interceptors.response.use(resp => {
|
2022-05-04 23:19:00 +00:00
|
|
|
if (!resp.request?.url?.endsWith('/resources')) {
|
2020-04-12 23:08:29 +00:00
|
|
|
store.getActions().progress.setComplete();
|
|
|
|
}
|
2020-04-10 19:41:08 +00:00
|
|
|
|
|
|
|
return resp;
|
2020-04-17 18:07:32 +00:00
|
|
|
}, error => {
|
2020-04-17 17:26:36 +00:00
|
|
|
store.getActions().progress.setComplete();
|
2020-04-17 18:07:32 +00:00
|
|
|
|
|
|
|
throw error;
|
2020-04-10 19:41:08 +00:00
|
|
|
});
|
|
|
|
|
2019-06-10 02:26:20 +00:00
|
|
|
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;
|
|
|
|
}
|
2020-08-23 05:35:53 +00:00
|
|
|
|
|
|
|
// Errors from wings directory, mostly just for file uploads.
|
|
|
|
if (data.error && typeof data.error === 'string') {
|
|
|
|
return data.error;
|
|
|
|
}
|
2019-06-10 02:26:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2022-05-30 00:34:48 +00:00
|
|
|
relationships?: Record<string, FractalResponseData | FractalResponseList | null | undefined>;
|
2019-11-03 20:20:11 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-07-10 02:56:46 +00:00
|
|
|
export interface FractalResponseList {
|
|
|
|
object: 'list';
|
|
|
|
data: FractalResponseData[];
|
|
|
|
}
|
|
|
|
|
2022-06-05 22:35:53 +00:00
|
|
|
export interface FractalPaginatedResponse extends FractalResponseList {
|
|
|
|
meta: {
|
|
|
|
pagination: {
|
|
|
|
total: number;
|
|
|
|
count: number;
|
|
|
|
/* eslint-disable camelcase */
|
|
|
|
per_page: number;
|
|
|
|
current_page: number;
|
|
|
|
total_pages: number;
|
|
|
|
/* eslint-enable camelcase */
|
|
|
|
};
|
2022-06-11 18:52:33 +00:00
|
|
|
};
|
2022-06-05 22:35:53 +00:00
|
|
|
}
|
|
|
|
|
2019-08-17 23:03:10 +00:00
|
|
|
export interface PaginatedResult<T> {
|
|
|
|
items: T[];
|
|
|
|
pagination: PaginationDataSet;
|
|
|
|
}
|
|
|
|
|
2022-06-05 22:35:53 +00:00
|
|
|
export interface PaginationDataSet {
|
2019-08-17 23:03:10 +00:00
|
|
|
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,
|
|
|
|
};
|
|
|
|
}
|
2022-06-05 22:35:53 +00:00
|
|
|
|
|
|
|
type QueryBuilderFilterValue = string | number | boolean | null;
|
|
|
|
|
|
|
|
export interface QueryBuilderParams<FilterKeys extends string = string, SortKeys extends string = string> {
|
2022-06-05 23:23:25 +00:00
|
|
|
page?: number;
|
2022-06-05 22:35:53 +00:00
|
|
|
filters?: {
|
|
|
|
[K in FilterKeys]?: QueryBuilderFilterValue | Readonly<QueryBuilderFilterValue[]>;
|
|
|
|
};
|
|
|
|
sorts?: {
|
|
|
|
[K in SortKeys]?: -1 | 0 | 1 | 'asc' | 'desc' | null;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function that parses a data object provided and builds query parameters
|
|
|
|
* for the Laravel Query Builder package automatically. This will apply sorts and
|
|
|
|
* filters deterministically based on the provided values.
|
|
|
|
*/
|
|
|
|
export const withQueryBuilderParams = (data?: QueryBuilderParams): Record<string, unknown> => {
|
|
|
|
if (!data) return {};
|
|
|
|
|
|
|
|
const filters = Object.keys(data.filters || {}).reduce((obj, key) => {
|
|
|
|
const value = data.filters?.[key];
|
|
|
|
|
|
|
|
return !value || value === '' ? obj : { ...obj, [`filter[${key}]`]: value };
|
|
|
|
}, {} as NonNullable<QueryBuilderParams['filters']>);
|
|
|
|
|
|
|
|
const sorts = Object.keys(data.sorts || {}).reduce((arr, key) => {
|
|
|
|
const value = data.sorts?.[key];
|
|
|
|
if (!value || ![ 'asc', 'desc', 1, -1 ].includes(value)) {
|
|
|
|
return arr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return [ ...arr, (value === -1 || value === 'desc' ? '-' : '') + key ];
|
|
|
|
}, [] as string[]);
|
|
|
|
|
|
|
|
return {
|
|
|
|
...filters,
|
2022-06-05 23:23:25 +00:00
|
|
|
sort: !sorts.length ? undefined : sorts.join(','),
|
|
|
|
page: data.page,
|
2022-06-05 22:35:53 +00:00
|
|
|
};
|
|
|
|
};
|