import axios from "axios"; import { isArray } from "lodash"; import type { InvalidResponse } from '@/defs'; export type ValidResponse = { result: true } export type RawArgType = Record|Record[]; export interface CallbackT{ (args?: ArgType): Promise; (args: ArgType | undefined, returnError: false): Promise; (args: ArgType | undefined, returnError: true): Promise; } export async function callSammoAPI(path: string | string[], args?: Record | Record[]): Promise; export async function callSammoAPI(path: string | string[], args: Record | Record[] | undefined, returnError: false): Promise; export async function callSammoAPI(path: string | string[], args: Record | Record[] | undefined, returnError: true): Promise; export async function callSammoAPI(path: string | string[], args?: Record | Record[], returnError = false): Promise { if (isArray(path)) { path = path.join('/'); } const response = await axios({ url: `api.php?path=${path}`, method: "post", responseType: "json", data: args }); const result: ErrorType | ResultType = response.data; if (!result.result) { if (returnError) { return result; } throw result.reason; } return result; } export async function done(args?: RawArgType): Promise; export async function done(args: RawArgType | undefined, returnError: false): Promise; export async function done(args: RawArgType | undefined, returnError: true): Promise; export async function done(args?: RawArgType, returnError = false): Promise { console.error(`Can't directly call. ${args}, ${returnError}. Use auto-generated path API.`); return callSammoAPI([], args, true); }