feat: api.php를 sammoAPI()로 통일

This commit is contained in:
2021-12-26 01:58:26 +09:00
parent 09caaee58d
commit 38e689ebb2
5 changed files with 61 additions and 139 deletions
+10 -3
View File
@@ -2,11 +2,15 @@ import axios from "axios";
import { isArray } from "lodash";
import { InvalidResponse } from '@/defs';
type ValidResponse = {
export type ValidResponse = {
result: true
}
export async function sammoAPI<ResultType extends ValidResponse>(path: string | string[], args?: Record<string, unknown>): Promise<ResultType> {
export async function sammoAPI<ResultType extends ValidResponse>(path: string | string[], args?: Record<string, unknown>): Promise<ResultType>;
export async function sammoAPI<ResultType extends ValidResponse>(path: string | string[], args: Record<string, unknown> | undefined, returnError: false): Promise<ResultType>;
export async function sammoAPI<ResultType extends ValidResponse, ErrorType extends InvalidResponse>(path: string | string[], args: Record<string, unknown> | undefined, returnError: true): Promise<ResultType | ErrorType>;
export async function sammoAPI<ResultType extends ValidResponse, ErrorType extends InvalidResponse>(path: string | string[], args?: Record<string, unknown>, returnError = false): Promise<ResultType | ErrorType> {
if (isArray(path)) {
path = path.join('/');
}
@@ -20,8 +24,11 @@ export async function sammoAPI<ResultType extends ValidResponse>(path: string |
args,
},
});
const result: InvalidResponse | ResultType = response.data;
const result: ErrorType | ResultType = response.data;
if (!result.result) {
if (returnError) {
return result;
}
throw result.reason;
}
return result;