refac: SammoAPI Path 정의에 NumVar, StrVar 추가

This commit is contained in:
2022-03-25 23:29:10 +09:00
parent 11e28b7a87
commit 00ceb3f842
5 changed files with 51 additions and 33 deletions
+4 -22
View File
@@ -1,27 +1,9 @@
import type { InvalidResponse, ReserveBulkCommandResponse } from "./defs";
import type { Args } from "./processing/args";
import { callSammoAPI, type ValidResponse } from "./util/callSammoAPI";
import { callSammoAPI, done, type CallbackT, type RawArgType, type ValidResponse } from "./util/callSammoAPI";
export type { ValidResponse, InvalidResponse };
import { APIPathGen } from "./util/APIPathGen.js";
type RawArgType = Record<string, unknown>|Record<string, unknown>[];
interface CallbackT<ResultType extends ValidResponse, ErrorType extends InvalidResponse, ArgType extends RawArgType>{
(args?: ArgType): Promise<ResultType>;
(args: ArgType | undefined, returnError: false): Promise<ResultType>;
(args: ArgType | undefined, returnError: true): Promise<ResultType | ErrorType>;
}
async function done<ResultType extends ValidResponse>(args?: RawArgType): Promise<ResultType>;
async function done<ResultType extends ValidResponse>(args: RawArgType | undefined, returnError: false): Promise<ResultType>;
async function done<ResultType extends ValidResponse, ErrorType extends InvalidResponse>(args: RawArgType | undefined, returnError: true): Promise<ResultType | ErrorType>;
async function done<ResultType extends ValidResponse, ErrorType extends InvalidResponse>(args?: RawArgType, returnError = false): Promise<ResultType | ErrorType> {
console.error(`Can't directly call. ${args}, ${returnError}. Use auto-generated path API.`);
return callSammoAPI<ResultType, ErrorType>([], args, true);
}
const apiRealPath = {
Betting: {
Bet: done,
@@ -33,11 +15,11 @@ const apiRealPath = {
PushCommand: done,
RepeatCommand: done,
ReserveCommand: done,
ReserveBulkCommand: done as CallbackT<ReserveBulkCommandResponse, InvalidResponse, {
ReserveBulkCommand: done as CallbackT<{
turnList: number[],
action: string,
arg: Args
}[]>,
}[], ReserveBulkCommandResponse>,
},
General: {
Join: done,
@@ -75,4 +57,4 @@ export const SammoAPI = APIPathGen(apiRealPath, (path: string[]) => {
}
return callSammoAPI(path.join('/'), args);
};
}) as typeof apiRealPath;
});
+1 -10
View File
@@ -1,17 +1,8 @@
import type { InvalidResponse } from "./defs";
import { APIPathGen } from "./util/APIPathGen";
import { callSammoAPI, type ValidResponse } from "./util/callSammoAPI";
import { callSammoAPI, done, type ValidResponse } from "./util/callSammoAPI";
export type { ValidResponse, InvalidResponse };
async function done<ResultType extends ValidResponse>(args?: Record<string, unknown>): Promise<ResultType>;
async function done<ResultType extends ValidResponse>(args: Record<string, unknown> | undefined, returnError: false): Promise<ResultType>;
async function done<ResultType extends ValidResponse, ErrorType extends InvalidResponse>(args: Record<string, unknown> | undefined, returnError: true): Promise<ResultType | ErrorType>;
async function done<ResultType extends ValidResponse, ErrorType extends InvalidResponse>(args?: Record<string, unknown>, returnError = false): Promise<ResultType | ErrorType>{
console.error(`Can't directly call. ${args}, ${returnError}. Use auto-generated path API.`);
return callSammoAPI<ResultType, ErrorType>([], args, true);
}
const apiRealPath = {
Login: {
LoginByID: done,
+19 -1
View File
@@ -1 +1,19 @@
export function APIPathGen<T>(obj: T, callback: (path: string[])=>unknown): T;
export function APIPathGen<T>(obj: T, callback: (path: string[])=>unknown): T;
export function StrVar<PathType extends string>(): <NextCall>(next: NextCall)=>{
[v in PathType]: NextCall
};
export function NumVar<NextCall>(next: NextCall):{
[v: number]: NextCall
};
/*
const apiPath = {
SomePath: someFunc,
User: StrVar<'a'|'b'>()({
Update: someFunc,
Delete: someFunc,
})
}
*/
+10
View File
@@ -1,6 +1,12 @@
export function APIPathGen(obj, callback, path) {
return new Proxy(obj, {
get(target, key) {
if(typeof key === 'number'){
key = key.toString();
}
else if(typeof key !== 'string'){
throw `${key} is not string`;
}
let nextPath;
if (path === undefined) {
nextPath = [key];
@@ -21,4 +27,8 @@ export function APIPathGen(obj, callback, path) {
return APIPathGen(next, callback, nextPath);
}
})
}
export function StrVar(){
return (next)=>next;
}
+17
View File
@@ -6,6 +6,14 @@ export type ValidResponse = {
result: true
}
export type RawArgType = Record<string, unknown>|Record<string, unknown>[];
export interface CallbackT<ArgType extends RawArgType, ResultType extends ValidResponse = ValidResponse, ErrorType extends InvalidResponse = InvalidResponse>{
(args?: ArgType): Promise<ResultType>;
(args: ArgType | undefined, returnError: false): Promise<ResultType>;
(args: ArgType | undefined, returnError: true): Promise<ResultType | ErrorType>;
}
export async function callSammoAPI<ResultType extends ValidResponse>(path: string | string[], args?: Record<string, unknown> | Record<string, unknown>[]): Promise<ResultType>;
export async function callSammoAPI<ResultType extends ValidResponse>(path: string | string[], args: Record<string, unknown> | 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> | Record<string, unknown>[] | undefined, returnError: true): Promise<ResultType | ErrorType>;
@@ -29,4 +37,13 @@ export async function callSammoAPI<ResultType extends ValidResponse, ErrorType e
throw result.reason;
}
return result;
}
export async function done<ResultType extends ValidResponse>(args?: RawArgType): Promise<ResultType>;
export async function done<ResultType extends ValidResponse>(args: RawArgType | undefined, returnError: false): Promise<ResultType>;
export async function done<ResultType extends ValidResponse, ErrorType extends InvalidResponse>(args: RawArgType | undefined, returnError: true): Promise<ResultType | ErrorType>;
export async function done<ResultType extends ValidResponse, ErrorType extends InvalidResponse>(args?: RawArgType, returnError = false): Promise<ResultType | ErrorType> {
console.error(`Can't directly call. ${args}, ${returnError}. Use auto-generated path API.`);
return callSammoAPI<ResultType, ErrorType>([], args, true);
}