feat: api.php를 sammoAPI()로 통일
This commit is contained in:
+10
-3
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user