import { AxiosError, AxiosRequestConfig, AxiosResponse } from 'axios'; import { apiClient } from '../lib/apiClient'; type RequiredConfigParams = Required> & Pick, 'data'>; export type PageRequestConfig = RequiredConfigParams & Partial> & { locale?: string, token?: string }; export const apiRequest = async ( baseParams: PageRequestConfig, ): Promise => { try { const config = { url: baseParams.url, method: baseParams.method, data: baseParams?.data, headers: { 'X-User-Language': baseParams?.locale || 'en', 'X-Referrer-Channel': 'site', ...(baseParams?.token ? { Authorization: `Bearer ${baseParams.token}` } : {}), ...(baseParams.headers || {}) } }; const response: AxiosResponse = await apiClient.request, T>(config as AxiosRequestConfig); return response.data; } catch (err) { // const { // response: { // status: responseCode = null, // statusText = '', // data: { message = '', status: errorKey = '' } = {}, // } = {}, // code: statusCode = '', // } = err as AxiosError; // // throw new Error( // JSON.stringify({ // statusCode, // statusMessage: message || statusText, // responseCode, // errorKey, // }), // ); } };