import axios from 'axios' import toCamel from 'camelcase-keys' import { API_BASE_URL } from '@/config' type Opt = { params?: Record headers?: Record } const client = axios.create ({ baseURL: API_BASE_URL }) const withUserCode = (opt?: Opt): Opt => ({ ...opt, headers: { 'X-Transfer-Code': localStorage.getItem ('user_code') ?? '', ...(opt?.headers ?? { }) } }) const apiP = async ( method: 'post' | 'put' | 'patch', path: string, body?: unknown, opt?: Opt, ): Promise => { const res = await client[method] (path, body ?? { }, withUserCode (opt)) return toCamel (res.data as any, { deep: true }) as T } export const apiGet = async ( path: string, opt?: Opt, ): Promise => { const res = await client.get (path, withUserCode (opt)) return toCamel (res.data as any, { deep: true }) as T } export const apiPost = async ( path: string, body?: unknown, opt?: Opt, ): Promise => apiP ('post', path, body, opt) export const apiPut = async ( path: string, body?: unknown, opt?: Opt, ): Promise => apiP ('put', path, body, opt) export const apiPatch = async ( path: string, body?: unknown, opt?: Opt, ): Promise => apiP ('patch', path, body, opt) export const apiDelete = async ( path: string, opt?: Opt, ): Promise => { await client.delete (path, withUserCode (opt)) }