Last active
March 25, 2024 18:05
-
-
Save cainthebest/4e308c4cf01078d96320ff6203fac968 to your computer and use it in GitHub Desktop.
BeamMP Master Server Info TS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface InitialServerInfo { | |
players: string; | |
playerslist: string; | |
maxplayers: string; | |
ip: string; | |
location: string; | |
port: string; | |
dport: string; | |
map: string; | |
sname: string; | |
version: string; | |
cversion: string; | |
official: boolean; | |
featured: boolean; | |
owner: string; | |
sdesc: string; | |
pps: string; | |
modlist: string; | |
modstotal: string; | |
modstotalsize: string; | |
} | |
interface ParsedServerInfo | |
extends Omit<InitialServerInfo, "playerslist" | "modlist"> { | |
playerslist: string[]; | |
modlist: string[]; | |
} | |
interface FetchResponse<T> { | |
data?: T; | |
error?: { code: number; status: string }; | |
} | |
async function fetchAndParseServerInfo(): Promise<FetchResponse<ParsedServerInfo[]>> { | |
try { | |
const response = await fetch("https://backend.beammp.com/servers-info"); | |
if (!response.ok) { | |
return { | |
error: { code: response.status, status: response.statusText }, | |
}; | |
} | |
const servers: InitialServerInfo[] = await response.json(); | |
const parsedData: ParsedServerInfo[] = servers.map(server => ({ | |
...server, | |
playerslist: server.playerslist.split(";").filter(player => player), | |
modlist: server.modlist.split(";").filter(mod => mod), | |
})); | |
return { data: parsedData }; | |
} catch (error) { | |
return { | |
error: { code: -1, status: `Fetch error: ${error}` }, | |
}; | |
} | |
} | |
fetchAndParseServerInfo().then(response => { | |
if (response.error) { | |
console.error(`Error: ${response.error.status} (Code: ${response.error.code})`); | |
} else { | |
console.log("Server info:", response.data); | |
} | |
}).catch(console.error); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment