Files
core/hwe/ts/util/callSammoAPI.ts
T
Hide_D def230bf30 feat: ts APICall을 강제화.
- 오버 엔지니어링의 정석
2021-12-29 03:55:40 +09:00

35 lines
1.3 KiB
TypeScript

import axios from "axios";
import { isArray } from "lodash";
import { InvalidResponse } from '@/defs';
export type ValidResponse = {
result: true
}
export async function callSammoAPI<ResultType extends ValidResponse>(path: string | string[], args?: Record<string, unknown>): Promise<ResultType>;
export async function callSammoAPI<ResultType extends ValidResponse>(path: string | string[], args: Record<string, unknown> | undefined, returnError: false): Promise<ResultType>;
export async function callSammoAPI<ResultType extends ValidResponse, ErrorType extends InvalidResponse>(path: string | string[], args: Record<string, unknown> | undefined, returnError: true): Promise<ResultType | ErrorType>;
export async function callSammoAPI<ResultType extends ValidResponse, ErrorType extends InvalidResponse>(path: string | string[], args?: Record<string, unknown>, returnError = false): Promise<ResultType | ErrorType> {
if (isArray(path)) {
path = path.join('/');
}
const response = await axios({
url: "api.php",
method: "post",
responseType: "json",
data: {
path,
args,
},
});
const result: ErrorType | ResultType = response.data;
if (!result.result) {
if (returnError) {
return result;
}
throw result.reason;
}
return result;
}