diff --git a/src/classes/OAPI.ts b/src/classes/OAPI.ts index 63fb1e0..dda9455 100644 --- a/src/classes/OAPI.ts +++ b/src/classes/OAPI.ts @@ -1,22 +1,23 @@ import { - ApiTownRaw, - ApiNationRaw, - ApiResidentRaw, + RawTown, + RawNation, + RawResident, ApiResident, - ServerInfoRaw + RawServerInfo, + OAPITown } from '../types.js' import { townyData } from '../utils/endpoint.js' import { FetchError } from '../utils/errors.js' class OfficialAPI { - static serverInfo = async () => await townyData('', 'v2') as ServerInfoRaw + static serverInfo = async () => await townyData('', 'v2') as RawServerInfo static resident = async (name: string) => { // TODO: Properly handle this case and implement an error. if (!name) return - const res = await townyData(`/residents/${name}`) as ApiResidentRaw + const res = await townyData(`/residents/${name}`) as RawResident if (!res) throw new FetchError(`Could not fetch resident '${name}'. Received invalid response.`) const obj: any = { @@ -56,28 +57,23 @@ class OfficialAPI { static town = async (name: string) => { if (!name) return - const town = await townyData(`/towns/${name}`) as ApiTownRaw - const obj: any = {} + const town = await townyData(`/towns/${name}`) as RawTown - if (town) { - if (town.strings.founder) obj.founder = town.strings.founder - if (town.stats) obj.stats = town.stats - if (town.ranks) obj.ranks = town.ranks - - if (town.timestamps?.registered) - obj.created = town.timestamps.registered - - if (town.timestamps?.joinedNationAt) - obj.joinedNation = town.timestamps.joinedNationAt - } + // TODO: Implement a proper error + if (!town) return - return obj + return { + name: town.strings.town, + founder: town.strings.founder, + created: town.timestamps?.registered, + joinedNation: town.timestamps?.joinedNationAt + } as OAPITown } static nation = async (name: string) => { if (!name) return - const nation = await townyData(`/nations/${name}`) as ApiNationRaw + const nation = await townyData(`/nations/${name}`) as RawNation const obj: any = {} if (nation) { diff --git a/src/types/oapi_v1.ts b/src/types/oapi_v1.ts index bdc4a87..35d45b4 100644 --- a/src/types/oapi_v1.ts +++ b/src/types/oapi_v1.ts @@ -1,13 +1,19 @@ import { Location } from '../types.js' -export type ApiEntityRaw = { - status: EntityStatusRaw - stats: EntityStatsRaw +type NestedOmit = { + [P in keyof T as P extends K ? never : P]: + NestedOmit}.${infer R}` ? R : never> +} extends infer O ? { [P in keyof O]: O[P] } : never; + +//#region Raw, unparsed types +export type RawEntity = { + status: RawEntityStatus + stats: RawEntityStats ranks?: { [key: string]: string[] } spawn?: Location } -export type EntityStatusRaw = { +export type RawEntityStatus = { isPublic: boolean isOpen: boolean isNeutral: boolean @@ -17,7 +23,7 @@ export type EntityStatusRaw = { isOnline?: boolean } -export type EntityStatsRaw = { +export type RawEntityStats = { maxTownBlocks?: number numTownBlocks?: number numResidents?: number @@ -25,29 +31,29 @@ export type EntityStatsRaw = { balance: number } -export type ResidentPermsRaw = { +export type RawResidentPerms = { friend: boolean town: boolean ally: boolean outsider: boolean } -export type TownPermsRaw = { +export type RawTownPerms = { resident: boolean nation: boolean ally: boolean outsider: boolean } -export type FlagPermsRaw = { +export type RawFlagPerms = { pvp: boolean explosion: boolean fire: boolean mobs: boolean } -export type EntityPermsRaw = { - flagPerms: FlagPermsRaw +export type RawEntityPerms = { + flagPerms: RawFlagPerms rnaoPerms: { buildPerms: PermsType destroyPerms: PermsType @@ -56,7 +62,7 @@ export type EntityPermsRaw = { } } -export type ApiTownRaw = ApiEntityRaw & { +export type RawTown = RawEntity & { strings: { town: string board: string @@ -73,10 +79,10 @@ export type ApiTownRaw = ApiEntityRaw & { } home: Location residents: string[] - perms: EntityPermsRaw + perms: RawEntityPerms } -export type ApiNationRaw = ApiEntityRaw & { +export type RawNation = RawEntity & { strings: { nation: string board: string @@ -93,26 +99,26 @@ export type ApiNationRaw = ApiEntityRaw & { enemies: string[] } -export type ApiResidentRaw = ApiEntityRaw & { +export type RawResident = RawEntity & { strings: { title: string username: string surname: string } - affiliation?: { - town?: string - nation?: string - } + affiliation?: Partial<{ + town: string + nation: string + }> timestamps?: { joinedTownAt?: number registered: number lastOnline: number } - perms: EntityPermsRaw + perms: RawEntityPerms friends: string[] } -export type ServerInfoRaw = { +export type RawServerInfo = { world: { hasStorm: boolean isThundering: boolean @@ -131,4 +137,27 @@ export type ServerInfoRaw = { numNations: number numTownBlocks: number } -} \ No newline at end of file +} +//#endregion + +//#region Parsed +export type OAPITown = NestedOmit & { + name: string + founder: string + created: number + joinedNation: number +} + +export type OAPINation = Partial & { + s +} + +export type OAPIResident = Partial & { + s +} +//#endregion \ No newline at end of file diff --git a/src/types/resident.ts b/src/types/resident.ts index 4017f76..1c477e8 100644 --- a/src/types/resident.ts +++ b/src/types/resident.ts @@ -1,4 +1,4 @@ -import { FlagPermsRaw, ResidentPermsRaw } from '../types.js' +import { RawFlagPerms, RawResidentPerms } from '../types.js' export type Resident = { name: string @@ -23,11 +23,11 @@ export type ApiResident = { townRanks: ResidentRanks nationRanks: ResidentRanks perms?: { - build: ResidentPermsRaw - destroy: ResidentPermsRaw - switch: ResidentPermsRaw - itemUse: ResidentPermsRaw - flags: FlagPermsRaw + build: RawResidentPerms + destroy: RawResidentPerms + switch: RawResidentPerms + itemUse: RawResidentPerms + flags: RawFlagPerms } friends?: string[] }