refac: APICall에 타입 지정

- 비 undefined 타입에서 ?타입 호출 제거
- API 소속 type들을 defs/API/ 로 이동
- 모든 SammoAPI, SammoRootAPI 지정
This commit is contained in:
2022-04-03 18:16:53 +09:00
parent d0dc0f2bd1
commit 2d06f368f8
17 changed files with 176 additions and 115 deletions
+1 -1
View File
@@ -65,7 +65,6 @@
</template>
<script setup lang="ts">
import { getNpcColor } from "@/common_legacy";
import type { ChiefResponse } from "@/defs";
import { formatTime } from "@/util/formatTime";
import { mb_strwidth } from "@/util/mb_strwidth";
import { parseTime } from "@/util/parseTime";
@@ -77,6 +76,7 @@ import VueTypes from "vue-types";
import DragSelect from "@/components/DragSelect.vue";
import { BButton } from "bootstrap-vue-3";
import { QueryActionHelper } from "@/util/QueryActionHelper";
import type { ChiefResponse } from "@/defs/API/NationCommand";
const props = defineProps({
style: VueTypes.object.isRequired,
+2 -14
View File
@@ -249,6 +249,7 @@ import { abilityLeadint, abilityLeadpow, abilityPowint, abilityRand } from "@uti
import { clone, shuffle, sum } from "lodash";
import NumberInputWithInfo from "@/components/NumberInputWithInfo.vue";
import { SammoAPI } from "./SammoAPI";
import type { JoinArgs } from "./defs/API/General";
declare const nationList: {
nation: number;
@@ -306,19 +307,6 @@ declare module "@vue/runtime-core" {
}
}
type APIArgs = {
name: string;
leadership: number;
strength: number;
intel: number;
pic: boolean;
character: string;
inheritSpecial?: string;
inheritTurntime?: number;
inheritCity?: number;
inheritBonusStat?: [number, number, number];
};
export default defineComponent({
name: "PageJoin",
components: {
@@ -329,7 +317,7 @@ export default defineComponent({
const displayTable = JSON.parse(localStorage.getItem(`conf.${serverID}.join.displayTable`) ?? "true");
const displayInherit = JSON.parse(localStorage.getItem(`conf.${serverID}.join.displayInherit`) ?? "true");
const nationListShuffled = shuffle(nationList);
const args: APIArgs = {
const args: JoinArgs = {
name: member.name,
leadership: stats.total - 2 * Math.floor(stats.total / 3),
strength: Math.floor(stats.total / 3),
+1 -5
View File
@@ -244,7 +244,6 @@ import { type diplomacyState, diplomacyStateInfo, type NationStaticItem } from "
import { SammoAPI } from "./SammoAPI";
import { joinYearMonth } from "@/util/joinYearMonth";
import { parseYearMonth } from "@/util/parseYearMonth";
import type { ValidResponse } from "./util/callSammoAPI";
import { useToast, BContainer } from "bootstrap-vue-3";
import { unwrap } from "./util/unwrap";
@@ -256,9 +255,6 @@ type NationItem = NationStaticItem & {
};
};
type SetBlockWarResponse = ValidResponse & {
availableCnt: number;
};
declare const staticValues: {
editable: boolean;
nationMsg: string;
@@ -501,7 +497,7 @@ export default defineComponent({
async function setBlockWar() {
try {
const result = await SammoAPI.Nation.SetBlockWar<SetBlockWarResponse>({ value: self.policy.blockWar });
const result = await SammoAPI.Nation.SetBlockWar({ value: self.policy.blockWar });
self.warSettingCnt.remain = result.availableCnt;
toasts.info({
title: "변경",
+2 -2
View File
@@ -288,7 +288,7 @@ import { parseTime } from "@util/parseTime";
import { parseYearMonth } from "@util/parseYearMonth";
import DragSelect from "@/components/DragSelect.vue";
import { SammoAPI } from "./SammoAPI";
import type { CommandItem, ReserveCommandResponse } from "@/defs";
import type { CommandItem } from "@/defs";
import CommandSelectForm from "@/components/CommandSelectForm.vue";
import { BButton, BButtonGroup, BDropdownItem, BDropdown, BDropdownText, BDropdownDivider } from "bootstrap-vue-3";
import { StoredActionsHelper } from "./util/StoredActionsHelper";
@@ -536,7 +536,7 @@ async function reserveCommand() {
}
try {
const result = await SammoAPI.Command.ReserveCommand<ReserveCommandResponse>({
const result = await SammoAPI.Command.ReserveCommand({
turnList: reqTurnList,
action: commandName,
});
+76 -28
View File
@@ -1,16 +1,25 @@
import type { BettingDetailResponse, ReserveBulkCommandResponse } from "./defs";
import type { Args } from "./processing/args";
import { callSammoAPI, extractHttpMethod, GET, PATCH, POST, PUT, type APITail, type APICallT, type RawArgType, type ValidResponse, type InvalidResponse } 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, NumVar } from "./util/APIPathGen.js";
import type { BettingListResponse } from "./defs/API/Betting";
import type { ReservedCommandResponse } from "./defs/API/Command";
import type { BettingDetailResponse, BettingListResponse } from "./defs/API/Betting";
import type { ReserveBulkCommandResponse, ReserveCommandResponse, ReservedCommandResponse } from "./defs/API/Command";
import type { ChiefResponse } from "./defs/API/NationCommand";
import type { inheritBuffType } from "./defs/API/InheritAction";
import type { SetBlockWarResponse } from "./defs/API/Nation";
import type { UploadImageResponse } from "./defs/API/Misc";
import type { JoinArgs } from "./defs/API/General";
const apiRealPath = {
Betting: {
Bet: PUT,
Bet: PUT as APICallT<{
bettingID: number,
bettingType: number[],
amount: number,
}>,
GetBettingDetail: NumVar('betting_id',
GET as APICallT<undefined, BettingDetailResponse>
),
@@ -18,50 +27,89 @@ const apiRealPath = {
},
Command: {
GetReservedCommand: GET as APICallT<undefined, ReservedCommandResponse>,
PushCommand: PATCH as APICallT<{amount: number}>,
RepeatCommand: PATCH,
ReserveCommand: PUT,
PushCommand: PUT as APICallT<{
amount: number
}>,
RepeatCommand: PUT as APICallT<{
amount: number
}>,
ReserveCommand: PUT as APICallT<{
turnList: number[],
action: string,
arg?: Args
}, ReserveCommandResponse>,
ReserveBulkCommand: PUT as APICallT<{
turnList: number[],
action: string,
arg: Args
arg?: Args
}[], ReserveBulkCommandResponse>,
},
General: {
Join: POST,
Join: POST as APICallT<JoinArgs>,
},
InheritAction: {
BuyHiddenBuff: PUT as APICallT<{type: inheritBuffType, level: number}>,
BuyHiddenBuff: PUT as APICallT<{
type: inheritBuffType,
level: number
}>,
BuyRandomUnique: PUT as APICallT<undefined>,
BuySpecificUnique: PUT,
BuySpecificUnique: PUT as APICallT<{
item: string,
amount: number,
}>,
ResetSpecialWar: PUT as APICallT<undefined>,
ResetTurnTime: PUT as APICallT<undefined>,
SetNextSpecialWar: PUT,
SetNextSpecialWar: PUT as APICallT<{
type: string,
}>,
},
Misc: {
UploadImage: POST as APICallT<{
imageData: string,
}, UploadImageResponse>
},
Misc: { UploadImage: POST },
NationCommand: {
GetReservedCommand: GET as APICallT<undefined, ChiefResponse>,
PushCommand: PATCH as APICallT<{amount: number}>,
RepeatCommand: PATCH,
ReserveCommand: PUT,
PushCommand: PUT as APICallT<{
amount: number
}>,
RepeatCommand: PUT as APICallT<{
amount: number
}>,
ReserveCommand: PUT as APICallT<{
turnList: number[],
action: string,
arg?: Args
}, ReserveCommandResponse>,
ReserveBulkCommand: PUT as APICallT<{
turnList: number[],
action: string,
arg: Args
arg?: Args
}[], ReserveBulkCommandResponse>,
},
Nation: {
SetNotice: PUT,
SetScoutMsg: PUT,
SetBill: PUT,
SetRate: PUT,
SetSecretLimit: PUT,
SetBlockWar: PUT,
SetBlockScout: PUT,
SetNotice: PUT as APICallT<{
msg: string,
}>,
SetScoutMsg: PUT as APICallT<{
msg: string,
}>,
SetBill: PATCH as APICallT<{
amount: number,
}>,
SetRate: PATCH as APICallT<{
amount: number,
}>,
SetSecretLimit: PATCH as APICallT<{
amount: number,
}>,
SetBlockWar: PATCH as APICallT<{
value: boolean,
}, SetBlockWarResponse>,
SetBlockScout: PATCH as APICallT<{
value: boolean,
}>,
},
Test: NumVar('id', {
SetThis: PUT,
})
} as const;
export const SammoAPI = APIPathGen(apiRealPath, (path: string[], tail: APITail, pathParam) => {
+5 -2
View File
@@ -1,11 +1,14 @@
import type { AutoLoginFailed, AutoLoginNonceResponse, AutoLoginResponse } from "./defs/API/Login";
import type { AutoLoginFailed, AutoLoginNonceResponse, AutoLoginResponse, LoginFailed, LoginResponse } from "./defs/API/Login";
import { APIPathGen } from "./util/APIPathGen";
import { callSammoAPI, extractHttpMethod, GET, POST, type APICallT, type APITail, type InvalidResponse, type RawArgType, type ValidResponse } from "./util/callSammoAPI";
export type { ValidResponse, InvalidResponse };
const apiRealPath = {
Login: {
LoginByID: POST,
LoginByID: POST as APICallT<{
username: string,
password: string,
}, LoginResponse, LoginFailed>,
LoginByToken: POST as APICallT<{
hashedToken: string,
token_id: number,
+2 -1
View File
@@ -129,7 +129,8 @@
</template>
<script setup lang="ts">
import type { BettingDetailResponse, BettingInfo, ToastType } from "@/defs";
import type { ToastType } from "@/defs";
import type { BettingDetailResponse, BettingInfo } from "@/defs/API/Betting";
import { SammoAPI } from "@/SammoAPI";
import { joinYearMonth } from "@/util/joinYearMonth";
import { parseYearMonth } from "@/util/parseYearMonth";
+3 -2
View File
@@ -267,7 +267,7 @@ import VueTypes from "vue-types";
import DragSelect from "@/components/DragSelect.vue";
import { isString, range, trim } from "lodash";
import { SammoAPI } from "@/SammoAPI";
import type { ChiefResponse, CommandItem, ReserveCommandResponse, TurnObj } from "@/defs";
import type { CommandItem, TurnObj } from "@/defs";
import { QueryActionHelper } from "@/util/QueryActionHelper";
import type { Args } from "@/processing/args";
import type { StoredActionsHelper } from "@/util/StoredActionsHelper";
@@ -275,6 +275,7 @@ import { getNpcColor } from "@/common_legacy";
import { BButton, BDropdownItem, BDropdownText, BButtonGroup, BDropdownDivider, BDropdown } from "bootstrap-vue-3";
import CommandSelectForm from "@/components/CommandSelectForm.vue";
import SimpleClock from "@/components/SimpleClock.vue";
import type { ChiefResponse } from "@/defs/API/NationCommand";
type TurnObjWithTime = TurnObj & {
time: string;
@@ -539,7 +540,7 @@ async function reserveCommand() {
}
try {
const result = await SammoAPI.NationCommand.ReserveCommand<ReserveCommandResponse>({
const result = await SammoAPI.NationCommand.ReserveCommand({
turnList: reqTurnList,
action: commandName,
});
+36 -5
View File
@@ -1,7 +1,38 @@
import type { ValidResponse, BettingInfo } from "@/defs";
import type { ValidResponse } from "@/defs";
export type BettingListResponse = ValidResponse & {
bettingList: Record<number, Omit<BettingInfo & { totalAmount: number }, "candidates">>;
year: number;
month: number;
};
bettingList: Record<number, Omit<BettingInfo & { totalAmount: number }, "candidates">>;
year: number;
month: number;
};
export type BettingDetailResponse = ValidResponse & {
bettingInfo: BettingInfo;
bettingDetail: [string, number][];
myBetting: [string, number][];
remainPoint: number;
year: number;
month: number;
};
export type BettingInfo = {
id: number;
type: 'nationBetting',
name: string;
finished: boolean;
selectCnt: number;
isExclusive?: boolean;
reqInheritancePoint: boolean;
openYearMonth: number;
closeYearMonth: number;
candidates: SelectItem[];
winner?: number[];
}
export type SelectItem = {
title: string;
info?: string;
isHtml?: boolean;
aux?: Record<string, unknown>;
}
+19 -9
View File
@@ -1,12 +1,22 @@
import type { TurnObj } from "@/defs";
export type ReservedCommandResponse = {
result: true;
turnTime: string;
turnTerm: number;
year: number;
month: number;
date: string;
turn: TurnObj[];
autorun_limit: null | number;
};
result: true;
turnTime: string;
turnTerm: number;
year: number;
month: number;
date: string;
turn: TurnObj[];
autorun_limit: null | number;
};
export type ReserveCommandResponse = {
result: true,
brief: string,
}
export type ReserveBulkCommandResponse = {
result: true,
briefList: string[],
}
+12
View File
@@ -0,0 +1,12 @@
export type JoinArgs = {
name: string;
leadership: number;
strength: number;
intel: number;
pic: boolean;
character: string;
inheritSpecial?: string;
inheritTurntime?: number;
inheritCity?: number;
inheritBonusStat?: [number, number, number];
};
+5
View File
@@ -0,0 +1,5 @@
import type { ValidResponse } from "@/defs";
export type UploadImageResponse = ValidResponse & {
path: string;
}
+5
View File
@@ -0,0 +1,5 @@
import type { ValidResponse } from "@/defs";
export type SetBlockWarResponse = ValidResponse & {
availableCnt: number;
};
-42
View File
@@ -32,16 +32,6 @@ export type GeneralListResponse = {
token: Record<number, number>,
}
export type ReserveCommandResponse = {
result: true,
brief: string,
}
export type ReserveBulkCommandResponse = {
result: true,
briefList: string[],
}
export type NationLevel = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7;
export const NationLevelText: Record<NationLevel, string> = {
0: '방랑군',
@@ -211,35 +201,3 @@ export const diplomacyStateInfo: Record<diplomacyState, diplomacyInfo> = {
2: { name: '통상' },
7: { name: '불가침', color: 'green' },
}
export type SelectItem = {
title: string;
info?: string;
isHtml?: boolean;
aux?: Record<string, unknown>;
}
export type BettingInfo = {
id: number;
type: 'nationBetting',
name: string;
finished: boolean;
selectCnt: number;
isExclusive?: boolean;
reqInheritancePoint: boolean;
openYearMonth: number;
closeYearMonth: number;
candidates: SelectItem[];
winner?: number[];
}
export type BettingDetailResponse = ValidResponse & {
bettingInfo: BettingInfo;
bettingDetail: [string, number][];
myBetting: [string, number][];
remainPoint: number;
year: number;
month: number;
};
+1 -1
View File
@@ -273,7 +273,7 @@ $(async function ($) {
let result: LoginResponse | LoginFailed;
try {
result = await SammoRootAPI.Login.LoginByID<LoginResponse, LoginFailed>({
result = await SammoRootAPI.Login.LoginByID({
username: values.username,
password: hash_pw,
+3
View File
@@ -26,6 +26,9 @@ interface EmptyAPICallT<ResultType extends ValidResponse, ErrorType extends Inva
(args: undefined, returnError: true): Promise<ResultType | ErrorType>;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type ArgTypeOf<T> = T extends APICallT<infer A, any, any> ? A : never;
export type APICallT<
ArgType extends RawArgType,
ResultType extends ValidResponse = ValidResponse,
+3 -3
View File
@@ -11,7 +11,7 @@ import { isString } from 'lodash';
import { type Args, testSubmitArgs } from './processing/args';
import { SammoAPI } from './SammoAPI';
import { StoredActionsHelper } from './util/StoredActionsHelper';
import type { ReserveCommandResponse } from './defs';
import type { ReserveCommandResponse } from './defs/API/Command';
declare const staticValues: {
serverNick: string,
@@ -31,11 +31,11 @@ async function submitCommand<T extends ReserveCommandResponse>(isChiefTurn: bool
throw new TypeError(`Invalied Type ${testResult[0]}, ${testResult[2]} should be ${testResult[1]}`);
}
console.log('trySubmit', arg);
const responseP = targetAPI<T>({
const responseP = targetAPI({
action,
turnList,
arg,
});
}) as Promise<T>;
const storedActionsHelper = new StoredActionsHelper(staticValues.serverNick, isChiefTurn?'nation':'general', staticValues.mapName, staticValues.unitSet);