2026-10-08 18:49:15 -04:00
|
|
|
import type { AxiosInstance, AxiosResponse } from 'axios';
|
2026-10-05 18:10:18 -04:00
|
|
|
import axios from 'axios';
|
2026-11-23 14:51:03 +00:00
|
|
|
import i18next from 'i18next';
|
2026-10-05 18:10:18 -04:00
|
|
|
import { completeHttpProgress, startHttpProgress } from '@/state/httpProgress';
|
|
|
|
|
import { isObject, isString } from '@/lib/objects';
|
2019-06-09 19:26:20 -07:00
|
|
|
|
|
|
|
|
const http: AxiosInstance = axios.create({
|
2022-05-22 17:01:39 -04:00
|
|
|
withCredentials: true,
|
2020-04-10 12:41:08 -07:00
|
|
|
timeout: 20000,
|
2019-06-09 19:26:20 -07:00
|
|
|
headers: {
|
|
|
|
|
'X-Requested-With': 'XMLHttpRequest',
|
2020-07-04 18:30:50 -07:00
|
|
|
Accept: 'application/json',
|
2019-06-09 19:26:20 -07:00
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
const shouldTrackHttpProgress = (url?: string): boolean => !url?.endsWith('/resources');
|
|
|
|
|
|
2022-06-26 15:13:52 -04:00
|
|
|
http.interceptors.request.use((req) => {
|
2026-10-05 18:10:18 -04:00
|
|
|
if (req.data instanceof FormData) {
|
|
|
|
|
req.headers.delete('Content-Type');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (shouldTrackHttpProgress(req.url)) {
|
|
|
|
|
startHttpProgress();
|
2020-04-12 16:08:29 -07:00
|
|
|
}
|
2020-04-10 12:41:08 -07:00
|
|
|
|
|
|
|
|
return req;
|
|
|
|
|
});
|
|
|
|
|
|
2022-06-26 15:13:52 -04:00
|
|
|
http.interceptors.response.use(
|
|
|
|
|
(resp) => {
|
2026-10-05 18:10:18 -04:00
|
|
|
if (shouldTrackHttpProgress(resp.config.url)) {
|
|
|
|
|
completeHttpProgress();
|
2022-06-26 15:13:52 -04:00
|
|
|
}
|
2020-04-10 12:41:08 -07:00
|
|
|
|
2022-06-26 15:13:52 -04:00
|
|
|
return resp;
|
|
|
|
|
},
|
|
|
|
|
(error) => {
|
2026-10-05 18:10:18 -04:00
|
|
|
if (shouldTrackHttpProgress(axios.isAxiosError(error) ? error.config?.url : undefined)) {
|
|
|
|
|
completeHttpProgress();
|
|
|
|
|
}
|
2020-04-17 11:07:32 -07:00
|
|
|
|
2022-06-26 15:13:52 -04:00
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
);
|
2020-04-10 12:41:08 -07:00
|
|
|
|
2019-06-09 19:26:20 -07:00
|
|
|
export default http;
|
|
|
|
|
|
2026-10-08 18:49:15 -04:00
|
|
|
function parseJson(text: string): unknown {
|
|
|
|
|
try {
|
|
|
|
|
return JSON.parse(text);
|
|
|
|
|
} catch {
|
|
|
|
|
return text;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** The message in a JSON:API error body, or in a Wings error body (mostly from file uploads). */
|
|
|
|
|
function responseErrorMessage(response: AxiosResponse): string | undefined {
|
|
|
|
|
// Non-JSON responses can still carry a JSON error body.
|
|
|
|
|
const data: unknown = isString(response.data) ? parseJson(response.data) : response.data;
|
|
|
|
|
|
|
|
|
|
if (!isObject(data)) {
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
2020-04-06 21:59:14 -07:00
|
|
|
|
2026-10-08 18:49:15 -04:00
|
|
|
if ('errors' in data && Array.isArray(data.errors)) {
|
|
|
|
|
const firstError: unknown = data.errors[0];
|
2026-10-08 16:53:20 -04:00
|
|
|
|
2026-10-08 18:49:15 -04:00
|
|
|
if (isObject(firstError) && 'detail' in firstError && isString(firstError.detail)) {
|
|
|
|
|
return firstError.detail;
|
2019-06-09 19:26:20 -07:00
|
|
|
}
|
2026-10-08 18:49:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ('error' in data && isString(data.error)) {
|
|
|
|
|
return data.error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function httpErrorToHuman(cause: unknown): string {
|
|
|
|
|
if (axios.isAxiosError(cause) && cause.response?.data) {
|
|
|
|
|
const message = responseErrorMessage(cause.response);
|
2020-08-22 22:35:53 -07:00
|
|
|
|
2026-10-08 18:49:15 -04:00
|
|
|
if (message !== undefined) {
|
|
|
|
|
return message;
|
2020-08-22 22:35:53 -07:00
|
|
|
}
|
2019-06-09 19:26:20 -07:00
|
|
|
}
|
|
|
|
|
|
2026-11-23 14:51:03 +00:00
|
|
|
return cause instanceof Error ? cause.message : i18next.t('An unexpected error occurred.');
|
2019-11-03 12:20:11 -08:00
|
|
|
}
|
2026-10-09 06:26:45 +01:00
|
|
|
|
|
|
|
|
/** Validation messages keyed by the request path of the field they are about. */
|
|
|
|
|
export type ValidationErrors = Record<string, string>;
|
|
|
|
|
|
2026-11-23 14:51:03 +00:00
|
|
|
type ValidationFailure = {
|
|
|
|
|
field: string;
|
|
|
|
|
rule: string;
|
|
|
|
|
detail: string;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function responseErrors(cause: unknown): unknown[] {
|
2026-10-09 06:26:45 +01:00
|
|
|
if (!axios.isAxiosError(cause) || cause.response?.status !== 422) {
|
2026-11-23 14:51:03 +00:00
|
|
|
return [];
|
2026-10-09 06:26:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const data: unknown = cause.response.data;
|
|
|
|
|
|
2026-11-23 14:51:03 +00:00
|
|
|
return isObject(data) && 'errors' in data && Array.isArray(data.errors) ? data.errors : [];
|
|
|
|
|
}
|
2026-10-09 06:26:45 +01:00
|
|
|
|
2026-11-23 14:51:03 +00:00
|
|
|
function validationFailures(cause: unknown): ValidationFailure[] {
|
|
|
|
|
const failures: ValidationFailure[] = [];
|
2026-10-09 06:26:45 +01:00
|
|
|
|
2026-11-23 14:51:03 +00:00
|
|
|
for (const error of responseErrors(cause)) {
|
2026-10-09 06:26:45 +01:00
|
|
|
if (!isObject(error) || !('detail' in error) || !isString(error.detail)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-23 14:51:03 +00:00
|
|
|
const meta = 'meta' in error && isObject(error.meta) ? error.meta : {};
|
|
|
|
|
const field = 'source_field' in meta ? meta.source_field : undefined;
|
2026-10-09 06:26:45 +01:00
|
|
|
|
2026-11-23 14:51:03 +00:00
|
|
|
if (isString(field)) {
|
|
|
|
|
failures.push({
|
|
|
|
|
field,
|
|
|
|
|
rule: 'rule' in meta && isString(meta.rule) ? meta.rule : '',
|
|
|
|
|
detail: error.detail,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return failures;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** The first message for each field a validation (422) response rejected. */
|
|
|
|
|
export function httpValidationErrors(cause: unknown): ValidationErrors {
|
|
|
|
|
const errors: ValidationErrors = {};
|
|
|
|
|
|
|
|
|
|
for (const { field, detail } of validationFailures(cause)) {
|
|
|
|
|
if (!(field in errors)) {
|
|
|
|
|
errors[field] = detail;
|
2026-10-09 06:26:45 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return errors;
|
|
|
|
|
}
|
2026-11-23 14:51:03 +00:00
|
|
|
|
|
|
|
|
export function httpValidationFailure(cause: unknown, field: string, rule: string): string | null {
|
|
|
|
|
return (
|
|
|
|
|
validationFailures(cause).find((failure) => failure.field === field && failure.rule === rule)?.detail ?? null
|
|
|
|
|
);
|
|
|
|
|
}
|