import urljoin from 'url-join'; import axios from '~/utils/axios'; const apiv1Root = '/_api'; class Apiv1ErrorHandler extends Error { code; data; constructor(message = '', code = '', data = '') { super(); this.message = message; this.code = code; this.data = data; } } export async function apiRequest(method: string, path: string, params: unknown): Promise { const res = await axios[method](urljoin(apiv1Root, path), params); if (res.data.ok) { return res.data; } // Return error code if code is exist if (res.data.code != null) { const error = new Apiv1ErrorHandler(res.data.error, res.data.code, res.data.data); throw error; } throw new Error(res.data.error); } export async function apiGet(path: string, params: unknown = {}): Promise { return apiRequest('get', path, { params }); } export async function apiPost(path: string, params: unknown = {}): Promise { return apiRequest('post', path, params); } export async function apiPostForm(path: string, formData: FormData): Promise { return apiRequest('postForm', path, formData); } export async function apiDelete(path: string, params: unknown = {}): Promise { return apiRequest('delete', path, { data: params }); }