refac, feat: API 호출 구조 재작성

- api.php에서 param path 강제
- api.php에서 GET param 허용
- SammoAPI 호출자를 axios에서 fetch 기반(ky)로 변경
- SammoAPI에서 단순 POST대신 REST에 따라 지정 가능하도록 재구성
- SammoAPI에서 NumVar, StrVar를 PathParam으로 변경하도록 변경
- API CallType들을 def/API로 분리 시작
  - 일부 API를 시험삼아 변경(login)
This commit is contained in:
2022-04-02 23:31:05 +09:00
parent ad1f070557
commit 5879661267
14 changed files with 282 additions and 172 deletions
+40 -34
View File
@@ -1,65 +1,71 @@
import type { InvalidResponse, ReserveBulkCommandResponse } from "./defs";
import type { BettingDetailResponse, ReserveBulkCommandResponse } from "./defs";
import type { Args } from "./processing/args";
import { callSammoAPI, done, type CallbackT, type RawArgType, type ValidResponse } from "./util/callSammoAPI";
import { callSammoAPI, extractHttpMethod, GET, PATCH, POST, PUT, type APITail, type APICallT, type RawArgType, type ValidResponse, type InvalidResponse } from "./util/callSammoAPI";
export type { ValidResponse, InvalidResponse };
import { APIPathGen } from "./util/APIPathGen.js";
import { APIPathGen, NumVar } from "./util/APIPathGen.js";
const apiRealPath = {
Betting: {
Bet: done,
GetBettingDetail: done,
GetBettingList: done,
Bet: PUT,
GetBettingDetail: NumVar('betting_id',
GET as APICallT<undefined, BettingDetailResponse>
),
GetBettingList: GET,
},
Command: {
GetReservedCommand: done,
PushCommand: done,
RepeatCommand: done,
ReserveCommand: done,
ReserveBulkCommand: done as CallbackT<{
GetReservedCommand: GET as APICallT<undefined>,
PushCommand: PATCH,
RepeatCommand: PATCH,
ReserveCommand: PUT,
ReserveBulkCommand: PUT as APICallT<{
turnList: number[],
action: string,
arg: Args
}[], ReserveBulkCommandResponse>,
},
General: {
Join: done,
Join: POST,
},
InheritAction: {
BuyHiddenBuff: done,
BuyRandomUnique: done,
BuySpecificUnique: done,
ResetSpecialWar: done,
ResetTurnTime: done,
SetNextSpecialWar: done,
BuyHiddenBuff: PUT,
BuyRandomUnique: PUT,
BuySpecificUnique: PUT,
ResetSpecialWar: PUT,
ResetTurnTime: PUT,
SetNextSpecialWar: PUT,
},
Misc: { UploadImage: done },
Misc: { UploadImage: POST },
NationCommand: {
GetReservedCommand: done,
PushCommand: done,
RepeatCommand: done,
ReserveCommand: done,
ReserveBulkCommand: done as CallbackT<{
GetReservedCommand: GET,
PushCommand: PATCH,
RepeatCommand: PATCH,
ReserveCommand: PUT,
ReserveBulkCommand: PUT as APICallT<{
turnList: number[],
action: string,
arg: Args
}[], ReserveBulkCommandResponse>,
},
Nation: {
SetNotice: done,
SetScoutMsg: done,
SetBill: done,
SetRate: done,
SetSecretLimit: done,
SetBlockWar: done,
SetBlockScout: done,
SetNotice: PUT,
SetScoutMsg: PUT,
SetBill: PUT,
SetRate: PUT,
SetSecretLimit: PUT,
SetBlockWar: PUT,
SetBlockScout: PUT,
},
Test: NumVar('id', {
SetThis: PUT,
})
} as const;
export const SammoAPI = APIPathGen(apiRealPath, (path: string[]) => {
export const SammoAPI = APIPathGen(apiRealPath, (path: string[], tail: APITail, pathParam) => {
const method = extractHttpMethod(tail);
return (args?: RawArgType, returnError?: boolean) => {
if (returnError) {
return callSammoAPI(path.join('/'), args, true);
return callSammoAPI(method, path.join('/'), args, pathParam, true);
}
return callSammoAPI(path.join('/'), args);
return callSammoAPI(method, path.join('/'), args, pathParam);
};
});