2024-05-23 18:50:13 +09:00
|
|
|
import type { AxiosResponse, Method, RawAxiosRequestHeaders } from 'axios';
|
2023-09-11 23:09:22 +09:00
|
|
|
import axios from 'axios';
|
|
|
|
import LinkHeader from 'http-link-header';
|
|
|
|
|
2024-05-22 23:45:18 +09:00
|
|
|
import { getAccessToken } from './initial_state';
|
2023-09-11 23:09:22 +09: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 23:45:18 +09:00
|
|
|
const authorizationTokenFromInitialState = (): RawAxiosRequestHeaders => {
|
|
|
|
const accessToken = getAccessToken();
|
2024-03-27 21:47:09 +09:00
|
|
|
|
2024-05-22 23:45:18 +09:00
|
|
|
if (!accessToken) return {};
|
2023-09-11 23:09:22 +09:00
|
|
|
|
|
|
|
return {
|
|
|
|
Authorization: `Bearer ${accessToken}`,
|
2024-05-22 23:45:18 +09:00
|
|
|
};
|
2023-09-11 23:09:22 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
// eslint-disable-next-line import/no-default-export
|
2024-05-23 16:30:48 +09:00
|
|
|
export default function api(withAuthorization = true) {
|
2023-09-11 23:09:22 +09:00
|
|
|
return axios.create({
|
2024-09-18 17:43:24 +09:00
|
|
|
transitional: {
|
|
|
|
clarifyTimeoutError: true,
|
|
|
|
},
|
2023-09-11 23:09:22 +09:00
|
|
|
headers: {
|
|
|
|
...csrfHeader,
|
2024-05-23 16:30:48 +09:00
|
|
|
...(withAuthorization ? authorizationTokenFromInitialState() : {}),
|
2023-09-11 23:09:22 +09:00
|
|
|
},
|
|
|
|
|
|
|
|
transformResponse: [
|
|
|
|
function (data: unknown) {
|
|
|
|
try {
|
|
|
|
return JSON.parse(data as string) as unknown;
|
|
|
|
} catch {
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
}
|
2024-05-23 18:50:13 +09:00
|
|
|
|
2024-06-25 22:45:41 +09:00
|
|
|
type RequestParamsOrData = Record<string, unknown>;
|
|
|
|
|
2024-05-23 18:50:13 +09:00
|
|
|
export async function apiRequest<ApiResponse = unknown>(
|
|
|
|
method: Method,
|
|
|
|
url: string,
|
2024-06-25 22:45:41 +09:00
|
|
|
args: {
|
|
|
|
params?: RequestParamsOrData;
|
|
|
|
data?: RequestParamsOrData;
|
2024-09-26 03:13:36 +09:00
|
|
|
timeout?: number;
|
2024-06-25 22:45:41 +09:00
|
|
|
} = {},
|
2024-05-23 18:50:13 +09:00
|
|
|
) {
|
|
|
|
const { data } = await api().request<ApiResponse>({
|
|
|
|
method,
|
2024-05-24 03:22:42 +09:00
|
|
|
url: '/api/' + url,
|
2024-06-25 22:45:41 +09:00
|
|
|
...args,
|
2024-05-23 18:50:13 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
2024-06-25 22:45:41 +09: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 });
|
|
|
|
}
|