feat: ts APICall을 강제화.

- 오버 엔지니어링의 정석
This commit is contained in:
2021-12-29 03:55:40 +09:00
parent 0a4d568b06
commit def230bf30
16 changed files with 166 additions and 125 deletions
+29 -19
View File
@@ -1,30 +1,40 @@
type SubValue = string | { [property: string]: SubValue };
import { InvalidResponse } from "@/defs";
import { callSammoAPI, ValidResponse } from "./callSammoAPI";
const hasKey = <T extends Record<string | symbol, unknown>>(obj: T, k: string | symbol | number): k is keyof T =>
k in obj;
export type CurryCall<ResultType extends ValidResponse, ErrorType extends InvalidResponse> =
((args?: Record<string, unknown>) => Promise<ResultType>)
| ((args: Record<string, unknown> | undefined, returnError: false) => Promise<ResultType>)
| ((args: Record<string, unknown> | undefined, returnError: true) => Promise<ResultType | ErrorType>);
export function APIPathGen<K extends string, V extends SubValue>(obj: Record<K, V>, path?: string[]): Record<K, V> {
type SubValue<ResultType extends ValidResponse, ErrorType extends InvalidResponse> = CurryCall<ResultType, ErrorType> | { [property: string]: SubValue<ResultType, ErrorType> };
export function APIPathGen<ResultType extends ValidResponse, ErrorType extends InvalidResponse, T extends { [property: string]: SubValue<ResultType, ErrorType> },>(obj: T, path?: string[]): T {
return new Proxy(obj, {
get(target, key: K) {
get(target, key: string) {
let nextPath: string[];
if (path === undefined) {
path = [key];
nextPath = [key];
}
else {
path.push(key);
else{
nextPath = path;
nextPath.push(key);
}
if (hasKey(target, key)) {
const next: V = target[key];
if (typeof (next) === 'string') {
return path.join('/');
}
if (typeof (next) === 'object') {
return APIPathGen(next, path);
}
throw 'unknown';
if (!(key in target)) {
throw `${nextPath.join('/')} is not exists`;
}
throw `${path.join('/')} is not exists`;
const next = target[key];
if (typeof (next) === 'function') {
const callAPI: CurryCall<ResultType, ErrorType> = (args: Record<string, unknown> | undefined, returnError?: boolean) => {
if (returnError) {
return callSammoAPI<ResultType, ErrorType>(nextPath.join('/'), args, returnError);
}
return callSammoAPI<ResultType>(nextPath.join('/'), args, false);
}
return callAPI;
}
return APIPathGen(next, nextPath);
}
})
}
+35
View File
@@ -0,0 +1,35 @@
import axios from "axios";
import { isArray } from "lodash";
import { InvalidResponse } from '@/defs';
export type ValidResponse = {
result: true
}
export async function callSammoAPI<ResultType extends ValidResponse>(path: string | string[], args?: Record<string, unknown>): Promise<ResultType>;
export async function callSammoAPI<ResultType extends ValidResponse>(path: string | string[], args: Record<string, unknown> | undefined, returnError: false): Promise<ResultType>;
export async function callSammoAPI<ResultType extends ValidResponse, ErrorType extends InvalidResponse>(path: string | string[], args: Record<string, unknown> | undefined, returnError: true): Promise<ResultType | ErrorType>;
export async function callSammoAPI<ResultType extends ValidResponse, ErrorType extends InvalidResponse>(path: string | string[], args?: Record<string, unknown>, returnError = false): Promise<ResultType | ErrorType> {
if (isArray(path)) {
path = path.join('/');
}
const response = await axios({
url: "api.php",
method: "post",
responseType: "json",
data: {
path,
args,
},
});
const result: ErrorType | ResultType = response.data;
if (!result.result) {
if (returnError) {
return result;
}
throw result.reason;
}
return result;
}
-35
View File
@@ -1,35 +0,0 @@
import axios from "axios";
import { isArray } from "lodash";
import { InvalidResponse } from '@/defs';
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> | 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('/');
}
const response = await axios({
url: "api.php",
method: "post",
responseType: "json",
data: {
path,
args,
},
});
const result: ErrorType | ResultType = response.data;
if (!result.result) {
if (returnError) {
return result;
}
throw result.reason;
}
return result;
}