2024-05-23 09:50:13 +00:00
|
|
|
import type { AxiosResponse, Method, RawAxiosRequestHeaders } from 'axios';
|
2023-09-11 14:09:22 +00:00
|
|
|
import axios from 'axios';
|
|
|
|
import LinkHeader from 'http-link-header';
|
|
|
|
|
2024-05-22 14:45:18 +00:00
|
|
|
import { getAccessToken } from './initial_state';
|
2023-09-11 14:09:22 +00:00
|
|
|
import ready from './ready';
|
|
|
|
|
|
|
|
export const getLinks = (response: AxiosResponse) => {
|
|
|
|
const value = response.headers.link as string | undefined;
|
|
|
|
|
|
|
|
if (!value) {
|
|
|
|
return new LinkHeader();
|
|
|
|
}
|
|
|
|
|
|
|
|
return LinkHeader.parse(value);
|
|
|
|
};
|
|
|
|
|
|
|
|
const csrfHeader: RawAxiosRequestHeaders = {};
|
|
|
|
|
|
|
|
const setCSRFHeader = () => {
|
|
|
|
const csrfToken = document.querySelector<HTMLMetaElement>(
|
|
|
|
'meta[name=csrf-token]',
|
|
|
|
);
|
|
|
|
|
|
|
|
if (csrfToken) {
|
|
|
|
csrfHeader['X-CSRF-Token'] = csrfToken.content;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void ready(setCSRFHeader);
|
|
|
|
|
2024-05-22 14:45:18 +00:00
|
|
|
const authorizationTokenFromInitialState = (): RawAxiosRequestHeaders => {
|
|
|
|
const accessToken = getAccessToken();
|
2024-03-27 12:47:09 +00:00
|
|
|
|
2024-05-22 14:45:18 +00:00
|
|
|
if (!accessToken) return {};
|
2023-09-11 14:09:22 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
Authorization: `Bearer ${accessToken}`,
|
2024-05-22 14:45:18 +00:00
|
|
|
};
|
2023-09-11 14:09:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// eslint-disable-next-line import/no-default-export
|
2024-05-23 07:30:48 +00:00
|
|
|
export default function api(withAuthorization = true) {
|
2023-09-11 14:09:22 +00:00
|
|
|
return axios.create({
|
2024-09-18 08:43:24 +00:00
|
|
|
transitional: {
|
|
|
|
clarifyTimeoutError: true,
|
|
|
|
},
|
2023-09-11 14:09:22 +00:00
|
|
|
headers: {
|
|
|
|
...csrfHeader,
|
2024-05-23 07:30:48 +00:00
|
|
|
...(withAuthorization ? authorizationTokenFromInitialState() : {}),
|
2023-09-11 14:09:22 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
transformResponse: [
|
|
|
|
function (data: unknown) {
|
|
|
|
try {
|
|
|
|
return JSON.parse(data as string) as unknown;
|
|
|
|
} catch {
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
}
|
2024-05-23 09:50:13 +00:00
|
|
|
|
2024-06-25 13:45:41 +00:00
|
|
|
type RequestParamsOrData = Record<string, unknown>;
|
|
|
|
|
2024-05-23 09:50:13 +00:00
|
|
|
export async function apiRequest<ApiResponse = unknown>(
|
|
|
|
method: Method,
|
|
|
|
url: string,
|
2024-06-25 13:45:41 +00:00
|
|
|
args: {
|
|
|
|
params?: RequestParamsOrData;
|
|
|
|
data?: RequestParamsOrData;
|
2024-09-25 18:13:36 +00:00
|
|
|
timeout?: number;
|
2024-06-25 13:45:41 +00:00
|
|
|
} = {},
|
2024-05-23 09:50:13 +00:00
|
|
|
) {
|
|
|
|
const { data } = await api().request<ApiResponse>({
|
|
|
|
method,
|
2024-05-23 18:22:42 +00:00
|
|
|
url: '/api/' + url,
|
2024-06-25 13:45:41 +00:00
|
|
|
...args,
|
2024-05-23 09:50:13 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
2024-06-25 13:45:41 +00:00
|
|
|
|
|
|
|
export async function apiRequestGet<ApiResponse = unknown>(
|
|
|
|
url: string,
|
|
|
|
params?: RequestParamsOrData,
|
|
|
|
) {
|
|
|
|
return apiRequest<ApiResponse>('GET', url, { params });
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function apiRequestPost<ApiResponse = unknown>(
|
|
|
|
url: string,
|
|
|
|
data?: RequestParamsOrData,
|
|
|
|
) {
|
|
|
|
return apiRequest<ApiResponse>('POST', url, { data });
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function apiRequestPut<ApiResponse = unknown>(
|
|
|
|
url: string,
|
|
|
|
data?: RequestParamsOrData,
|
|
|
|
) {
|
|
|
|
return apiRequest<ApiResponse>('PUT', url, { data });
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function apiRequestDelete<ApiResponse = unknown>(
|
|
|
|
url: string,
|
|
|
|
params?: RequestParamsOrData,
|
|
|
|
) {
|
|
|
|
return apiRequest<ApiResponse>('DELETE', url, { params });
|
|
|
|
}
|