From 1d751ea045006a264d5113b8ce4f34f72dd25c2e Mon Sep 17 00:00:00 2001 From: Hide_D Date: Sun, 6 Aug 2023 12:50:56 +0000 Subject: [PATCH] =?UTF-8?q?node.js=EC=9D=B4=EB=AF=80=EB=A1=9C=20generator?= =?UTF-8?q?=20=ED=98=95=EC=8B=9D=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/clientAPI/generator.ts | 30 ++++++++++++++++++++++-------- server/clientAPI/sammoRootAPI.ts | 6 ++++-- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/server/clientAPI/generator.ts b/server/clientAPI/generator.ts index fc0547b..2d7ced4 100644 --- a/server/clientAPI/generator.ts +++ b/server/clientAPI/generator.ts @@ -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( method: HttpMethod, + apiRoot: string, path: string | string[], args: RawArgType, paramArgs: Record | undefined ): Promise; export async function callClientAPI( method: HttpMethod, + apiRoot: string, + path: string | string[], + args: RawArgType, + paramArgs: Record | undefined, + returnError: undefined +): Promise; +export async function callClientAPI( + method: HttpMethod, + apiRoot: string, path: string | string[], args: RawArgType, paramArgs: Record | undefined, @@ -20,6 +28,7 @@ export async function callClientAPI( ): Promise; export async function callClientAPI( method: HttpMethod, + apiRoot: string, path: string | string[], args: RawArgType, paramArgs: Record | undefined, @@ -27,13 +36,20 @@ export async function callClientAPI; export async function callClientAPI( method: HttpMethod, + apiRoot: string, path: string | string[], args: RawArgType, paramArgs: Record | undefined, - returnError = false + returnError?: boolean ): Promise { 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 { 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 { 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); }; }); \ No newline at end of file