node.js이므로 generator 형식 변경

This commit is contained in:
2023-08-06 12:50:56 +00:00
parent cca7214446
commit 1d751ea045
2 changed files with 26 additions and 10 deletions
+22 -8
View File
@@ -3,16 +3,24 @@ import isEmpty from "lodash-es/isEmpty";
import ky from "ky"; import ky from "ky";
import type { HttpMethod, InvalidResponse, RawArgType, ValidResponse } from "../apiStructure/defs"; 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>( export async function callClientAPI<ResultType extends ValidResponse>(
method: HttpMethod, method: HttpMethod,
apiRoot: string,
path: string | string[], path: string | string[],
args: RawArgType, args: RawArgType,
paramArgs: Record<string, string | number> | undefined paramArgs: Record<string, string | number> | undefined
): Promise<ResultType>; ): Promise<ResultType>;
export async function callClientAPI<ResultType extends ValidResponse>( export async function callClientAPI<ResultType extends ValidResponse>(
method: HttpMethod, 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[], path: string | string[],
args: RawArgType, args: RawArgType,
paramArgs: Record<string, string | number> | undefined, paramArgs: Record<string, string | number> | undefined,
@@ -20,6 +28,7 @@ export async function callClientAPI<ResultType extends ValidResponse>(
): Promise<ResultType>; ): Promise<ResultType>;
export async function callClientAPI<ResultType extends ValidResponse, ErrorType extends InvalidResponse>( export async function callClientAPI<ResultType extends ValidResponse, ErrorType extends InvalidResponse>(
method: HttpMethod, method: HttpMethod,
apiRoot: string,
path: string | string[], path: string | string[],
args: RawArgType, args: RawArgType,
paramArgs: Record<string, string | number> | undefined, paramArgs: Record<string, string | number> | undefined,
@@ -27,13 +36,20 @@ export async function callClientAPI<ResultType extends ValidResponse, ErrorType
): Promise<ResultType | ErrorType>; ): Promise<ResultType | ErrorType>;
export async function callClientAPI<ResultType extends ValidResponse, ErrorType extends InvalidResponse>( export async function callClientAPI<ResultType extends ValidResponse, ErrorType extends InvalidResponse>(
method: HttpMethod, method: HttpMethod,
apiRoot: string,
path: string | string[], path: string | string[],
args: RawArgType, args: RawArgType,
paramArgs: Record<string, string | number> | undefined, paramArgs: Record<string, string | number> | undefined,
returnError = false returnError?: boolean
): Promise<ResultType | ErrorType> { ): Promise<ResultType | ErrorType> {
if (isArray(path)) { 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)) { if (args && isEmpty(args)) {
@@ -42,11 +58,10 @@ export async function callClientAPI<ResultType extends ValidResponse, ErrorType
const result = (await (() => { const result = (await (() => {
if (method == "get") { if (method == "get") {
return ky(apiPath, { return ky(path, {
searchParams: { searchParams: {
...paramArgs, ...paramArgs,
...(args as typeof paramArgs), ...(args as typeof paramArgs),
path,
}, },
method, method,
headers: { headers: {
@@ -56,10 +71,9 @@ export async function callClientAPI<ResultType extends ValidResponse, ErrorType
retry: 0, retry: 0,
}); });
} }
return ky(apiPath, { return ky(path, {
searchParams: { searchParams: {
...paramArgs, ...paramArgs,
path,
}, },
method, method,
json: args, json: args,
+4 -2
View File
@@ -3,12 +3,14 @@ import type { APITail, RawArgType } from "../apiStructure/defs";
import { structure } from "../apiStructure/sammoRootAPI"; import { structure } from "../apiStructure/sammoRootAPI";
import { callClientAPI } from "./generator"; 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) => { export const SammoRootAPI = APIPathGen(structure, (path: string[], tail: APITail, pathParam) => {
const method = tail.reqType; const method = tail.reqType;
return (args?: RawArgType, returnError?: boolean) => { return (args?: RawArgType, returnError?: boolean) => {
if (returnError) { 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);
}; };
}); });