node.js이므로 generator 형식 변경
This commit is contained in:
@@ -3,16 +3,24 @@ import isEmpty from "lodash-es/isEmpty";
|
||||
import ky from "ky";
|
||||
import type { HttpMethod, InvalidResponse, RawArgType, ValidResponse } from "../apiStructure/defs";
|
||||
|
||||
const apiPath = process.env.API_ROOT_PATH ?? process.env.VITE_API_ROOT_PATH ?? '/api';
|
||||
|
||||
export async function callClientAPI<ResultType extends ValidResponse>(
|
||||
method: HttpMethod,
|
||||
apiRoot: string,
|
||||
path: string | string[],
|
||||
args: RawArgType,
|
||||
paramArgs: Record<string, string | number> | undefined
|
||||
): Promise<ResultType>;
|
||||
export async function callClientAPI<ResultType extends ValidResponse>(
|
||||
method: HttpMethod,
|
||||
apiRoot: string,
|
||||
path: string | string[],
|
||||
args: RawArgType,
|
||||
paramArgs: Record<string, string | number> | undefined,
|
||||
returnError: undefined
|
||||
): Promise<ResultType>;
|
||||
export async function callClientAPI<ResultType extends ValidResponse>(
|
||||
method: HttpMethod,
|
||||
apiRoot: string,
|
||||
path: string | string[],
|
||||
args: RawArgType,
|
||||
paramArgs: Record<string, string | number> | undefined,
|
||||
@@ -20,6 +28,7 @@ export async function callClientAPI<ResultType extends ValidResponse>(
|
||||
): Promise<ResultType>;
|
||||
export async function callClientAPI<ResultType extends ValidResponse, ErrorType extends InvalidResponse>(
|
||||
method: HttpMethod,
|
||||
apiRoot: string,
|
||||
path: string | string[],
|
||||
args: RawArgType,
|
||||
paramArgs: Record<string, string | number> | undefined,
|
||||
@@ -27,13 +36,20 @@ export async function callClientAPI<ResultType extends ValidResponse, ErrorType
|
||||
): Promise<ResultType | ErrorType>;
|
||||
export async function callClientAPI<ResultType extends ValidResponse, ErrorType extends InvalidResponse>(
|
||||
method: HttpMethod,
|
||||
apiRoot: string,
|
||||
path: string | string[],
|
||||
args: RawArgType,
|
||||
paramArgs: Record<string, string | number> | undefined,
|
||||
returnError = false
|
||||
returnError?: boolean
|
||||
): Promise<ResultType | ErrorType> {
|
||||
if (isArray(path)) {
|
||||
path = path.join("/");
|
||||
path = [apiRoot, ...path].join("/");
|
||||
}
|
||||
else if (path.startsWith("/")) {
|
||||
path = `${apiRoot}${path}`;
|
||||
}
|
||||
else {
|
||||
path = `${apiRoot}/${path}`;
|
||||
}
|
||||
|
||||
if (args && isEmpty(args)) {
|
||||
@@ -42,11 +58,10 @@ export async function callClientAPI<ResultType extends ValidResponse, ErrorType
|
||||
|
||||
const result = (await (() => {
|
||||
if (method == "get") {
|
||||
return ky(apiPath, {
|
||||
return ky(path, {
|
||||
searchParams: {
|
||||
...paramArgs,
|
||||
...(args as typeof paramArgs),
|
||||
path,
|
||||
},
|
||||
method,
|
||||
headers: {
|
||||
@@ -56,10 +71,9 @@ export async function callClientAPI<ResultType extends ValidResponse, ErrorType
|
||||
retry: 0,
|
||||
});
|
||||
}
|
||||
return ky(apiPath, {
|
||||
return ky(path, {
|
||||
searchParams: {
|
||||
...paramArgs,
|
||||
path,
|
||||
},
|
||||
method,
|
||||
json: args,
|
||||
|
||||
@@ -3,12 +3,14 @@ import type { APITail, RawArgType } from "../apiStructure/defs";
|
||||
import { structure } from "../apiStructure/sammoRootAPI";
|
||||
import { callClientAPI } from "./generator";
|
||||
|
||||
const apiRoot = process.env.API_ROOT_PATH ?? process.env.VITE_API_ROOT_PATH ?? '/api';
|
||||
|
||||
export const SammoRootAPI = APIPathGen(structure, (path: string[], tail: APITail, pathParam) => {
|
||||
const method = tail.reqType;
|
||||
return (args?: RawArgType, returnError?: boolean) => {
|
||||
if (returnError) {
|
||||
return callClientAPI(method, path.join('/'), args, pathParam, true);
|
||||
return callClientAPI(method, apiRoot, path.join('/'), args, pathParam, returnError);
|
||||
}
|
||||
return callClientAPI(method, path.join('/'), args, pathParam);
|
||||
return callClientAPI(method, apiRoot, path.join('/'), args, pathParam);
|
||||
};
|
||||
});
|
||||
Reference in New Issue
Block a user