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