init
This commit is contained in:
65
src/lib/api.ts
Normal file
65
src/lib/api.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
import useSocketStore from "@/stores/socketSettingStore";
|
||||
import { AvatarHakushiType, AvatarType } from "@/types/avatar";
|
||||
|
||||
|
||||
export async function checkConnectTcpApi(): Promise<boolean> {
|
||||
const { host, port, connectionType } = useSocketStore.getState()
|
||||
let url = `${host}:${port}/check-tcp`
|
||||
if (connectionType === "FireflyPSLocal") {
|
||||
url = "http://localhost:21000/check-tcp"
|
||||
}
|
||||
const response = await fetch(url, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
const data = await response.json();
|
||||
return data.status
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
export async function getCharacterListApi(): Promise<AvatarType[]> {
|
||||
const res = await fetch('/api/hakushin', {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
console.log(`Error ${res.status}: ${res.statusText}`);
|
||||
return [];
|
||||
}
|
||||
|
||||
const data: Map<string, AvatarHakushiType> = new Map(Object.entries(await res.json()));
|
||||
|
||||
|
||||
return Array.from(data.entries()).map(([id, it]) => convertAvatar(id, it));
|
||||
}
|
||||
|
||||
|
||||
function convertAvatar(id: string, item: AvatarHakushiType): AvatarType {
|
||||
const lang = new Map<string, string>([
|
||||
['en', item.en],
|
||||
['kr', item.kr],
|
||||
['cn', item.cn],
|
||||
['jp', item.jp]
|
||||
]);
|
||||
|
||||
const result: AvatarType = {
|
||||
release: item.release,
|
||||
icon: item.icon,
|
||||
rank: item.rank,
|
||||
baseType: item.baseType,
|
||||
damageType: item.damageType,
|
||||
desc: item.desc,
|
||||
lang: lang,
|
||||
id: id
|
||||
};
|
||||
|
||||
return result;
|
||||
}
|
||||
Reference in New Issue
Block a user