feat: api.php를 sammoAPI()로 통일

This commit is contained in:
2021-12-26 01:58:26 +09:00
parent 09caaee58d
commit 38e689ebb2
5 changed files with 61 additions and 139 deletions
+10 -61
View File
@@ -265,6 +265,7 @@ import _ from "lodash";
import { InvalidResponse } from "@/defs";
import axios from "axios";
import NumberInputWithInfo from "@/components/NumberInputWithInfo.vue";
import { sammoAPI } from "./util/sammoAPI";
type InheritanceType =
| "previous"
@@ -505,24 +506,11 @@ export default defineComponent({
return;
}
let result: InvalidResponse;
try {
const response = await axios({
url: "api.php",
method: "post",
responseType: "json",
data: {
path: "InheritAction/BuyHiddenBuff",
args: {
type: buffKey,
level,
},
},
await sammoAPI("InheritAction/BuyHiddenBuff", {
type: buffKey,
level,
});
result = response.data;
if (!result.result) {
throw result.reason;
}
} catch (e) {
console.error(e);
alert(`실패했습니다: ${e}`);
@@ -561,21 +549,8 @@ export default defineComponent({
return;
}
let result: InvalidResponse;
try {
const response = await axios({
url: "api.php",
method: "post",
responseType: "json",
data: {
path: `InheritAction/${type}`,
args: {},
},
});
result = response.data;
if (!result.result) {
throw result.reason;
}
await sammoAPI(`InheritAction/${type}`, {});
} catch (e) {
console.error(e);
alert(`실패했습니다: ${e}`);
@@ -608,23 +583,10 @@ export default defineComponent({
return;
}
let result: InvalidResponse;
try {
const response = await axios({
url: "api.php",
method: "post",
responseType: "json",
data: {
path: `InheritAction/SetNextSpecialWar`,
args: {
type: this.nextSpecialWar,
},
},
await sammoAPI(`InheritAction/SetNextSpecialWar`, {
type: this.nextSpecialWar,
});
result = response.data;
if (!result.result) {
throw result.reason;
}
} catch (e) {
console.error(e);
alert(`실패했습니다: ${e}`);
@@ -653,24 +615,11 @@ export default defineComponent({
return;
}
let result: InvalidResponse;
try {
const response = await axios({
url: "api.php",
method: "post",
responseType: "json",
data: {
path: `InheritAction/BuySpecificUnique`,
args: {
item: this.specificUnique,
amount,
},
},
await sammoAPI(`InheritAction/BuySpecificUnique`, {
item: this.specificUnique,
amount,
});
result = response.data;
if (!result.result) {
throw result.reason;
}
} catch (e) {
console.error(e);
alert(`실패했습니다: ${e}`);
+2 -14
View File
@@ -318,6 +318,7 @@ import { clone, shuffle, sum } from "lodash";
import axios from "axios";
import { InvalidResponse } from "@/defs";
import NumberInputWithInfo from "@/components/NumberInputWithInfo.vue";
import { sammoAPI } from "./util/sammoAPI";
declare const nationList: {
nation: number;
@@ -534,21 +535,8 @@ export default defineComponent({
async submitForm() {
//검증은 언제 되어야 하는가?
const args = clone(this.args);
let result: InvalidResponse;
try {
const response = await axios({
url: "api.php",
method: "post",
responseType: "json",
data: {
path: ["General", "Join"],
args,
},
});
result = response.data;
if (!result.result) {
throw result.reason;
}
await sammoAPI(["General", "Join"], args);
} catch (e) {
console.error(e);
alert(`실패했습니다: ${e}`);
+29 -35
View File
@@ -12,16 +12,22 @@ import { InvalidResponse } from '@/defs';
import { delay } from '@util/delay';
import { Modal } from 'bootstrap';
import '@/gateway/common';
import { sammoAPI } from '@/util/sammoAPI';
import { isString } from 'lodash';
type LoginResponse = {
result: true,
nextToken: [number, string] | undefined,
} | {
}
type LoginFailed = {
result: false,
reqOTP: boolean,
reason: string,
}
type LoginResponseWithKakao = LoginResponse | LoginFailed;
type OTPResponse = {
result: true,
validUntil: string,
@@ -31,15 +37,18 @@ type OTPResponse = {
reason: string,
}
type AutoLoginNonceResponse = {
result: true,
loginNonce: string,
} | InvalidResponse;
};
type AutoLoginResponse = {
result: true,
nextToken: [number, string] | undefined,
} | {
}
type AutoLoginFailed = {
result: false,
silent: boolean,
reason: string,
@@ -92,36 +101,26 @@ async function tryAutoLogin() {
const [tokenID, token] = tokenInfo;
const result = await axios.post<AutoLoginNonceResponse>('api.php', {
path: ["Login", "ReqNonce"]
});
const result = await sammoAPI<AutoLoginNonceResponse, AutoLoginFailed>(["Login", "ReqNonce"], {}, true);
if (!result) {
//api 에러.
return;
}
if (!result.data.result) {
if (!result.result) {
resetToken();
return;
}
const nonce = result.data.loginNonce;
const nonce = result.loginNonce;
const hashedToken = sha512(token + nonce);
const _loginResult = await axios.post<AutoLoginResponse>('api.php', {
path: ["Login", "LoginByToken"],
args: {
'hashedToken': hashedToken,
'token_id': tokenID,
}
});
const loginResult = await sammoAPI<AutoLoginResponse, AutoLoginFailed>(["Login", "LoginByToken"], {
'hashedToken': hashedToken,
'token_id': tokenID,
}, true);
if (!_loginResult) {
return;
}
const loginResult = _loginResult.data;
if (!loginResult.result) {
if (!loginResult.silent) {
alert(loginResult.reason);
@@ -137,6 +136,9 @@ async function tryAutoLogin() {
}
catch (e) {
if (isString(e)) {
alert(e);
}
console.error(e);
return;
}
@@ -201,7 +203,7 @@ async function sendTempPasswordToKakaoTalk(): Promise<void> {
}
async function doLoginUsingOAuth() {
let result: LoginResponse;
let result: LoginResponseWithKakao;
try {
const response = await axios({
@@ -307,22 +309,14 @@ $(async function ($) {
const salt = unwrap_any<string>($('#global_salt').val());
const hash_pw = sha512(salt + raw_password + salt);
let result: LoginResponse;
let result: LoginResponse | LoginFailed;
try {
const response = await axios({
url: 'api.php',
responseType: 'json',
method: 'post',
data: {
path: ["Login", "LoginByID"],
args: {
username: values.username,
password: hash_pw,
}
}
});
result = response.data;
result = await sammoAPI<LoginResponse, LoginFailed>(["Login", "LoginByID"], {
username: values.username,
password: hash_pw,
}, true);
}
catch (e) {
console.error(e);
+10 -3
View File
@@ -2,11 +2,15 @@ import axios from "axios";
import { isArray } from "lodash";
import { InvalidResponse } from '@/defs';
type ValidResponse = {
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>): 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('/');
}
@@ -20,8 +24,11 @@ export async function sammoAPI<ResultType extends ValidResponse>(path: string |
args,
},
});
const result: InvalidResponse | ResultType = response.data;
const result: ErrorType | ResultType = response.data;
if (!result.result) {
if (returnError) {
return result;
}
throw result.reason;
}
return result;
+10 -26
View File
@@ -1,9 +1,5 @@
import '@scss/processing.scss';
import axios from 'axios';
import { setAxiosXMLHttpRequest } from '@util/setAxiosXMLHttpRequest';
import { convertFormData } from '@util/convertFormData';
import { InvalidResponse } from '@/defs';
import { unwrap } from "@util/unwrap";
import BootstrapVue3 from 'bootstrap-vue-3'
import Multiselect from 'vue-multiselect';
@@ -13,36 +9,24 @@ import { App, createApp } from 'vue';
import { auto500px } from './util/auto500px';
import { isString } from 'lodash';
import { Args, testSubmitArgs } from './processing/args';
import { sammoAPI, ValidResponse } from './util/sammoAPI';
declare const turnList: number[];
setAxiosXMLHttpRequest();
async function submitCommand<T>(isChiefTurn: boolean, turnList: number[], command: string, args: Args): Promise<T> {
const target = isChiefTurn ? 'j_set_chief_command.php' : 'j_set_general_command.php';
async function submitCommand<T extends ValidResponse>(isChiefTurn: boolean, turnList: number[], action: string, arg: Args): Promise<T> {
const target = isChiefTurn ? 'Command/ReserveCommand' : 'NationCommand/ReserveCommand';
try {
const testResult = testSubmitArgs(args);
const testResult = testSubmitArgs(arg);
if (testResult !== true) {
throw new TypeError(`Invalied Type ${testResult[0]}, ${testResult[2]} should be ${testResult[1]}`);
}
console.log('trySubmit', args);
const response = await axios({
url: target,
responseType: 'json',
method: 'post',
data: convertFormData({
action: command,
turnList: turnList,
arg: JSON.stringify(args)
})
console.log('trySubmit', arg);
const response = await sammoAPI(target, {
action,
turnList,
arg,
});
const data = response.data as InvalidResponse;
if (!data.result) {
throw data.reason;
}
if (!isChiefTurn) {
window.location.href = './';
@@ -50,7 +34,7 @@ async function submitCommand<T>(isChiefTurn: boolean, turnList: number[], comman
window.location.href = 'v_chiefCenter.php';
}
return data as unknown as T;
return response as T;
}
catch (e) {
console.error(e);