Skip to content

Commit

Permalink
continue parsed OAPI types
Browse files Browse the repository at this point in the history
  • Loading branch information
Owen3H committed Oct 2, 2023
1 parent c63b690 commit dbb0b26
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 89 deletions.
2 changes: 1 addition & 1 deletion src/classes/Nations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Nations implements Base {
const nations = await this.all()
if (!nations) throw new FetchError('Error fetching nations! Please try again.')

const existing = fn.getExisting(nations, nationList, 'name') as Nation | Nation[]
const existing = fn.getExisting(nations, nationList, 'name') as Nation[] | Nation
const isArr = existing instanceof Array

return isArr ?
Expand Down
40 changes: 18 additions & 22 deletions src/classes/OAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import {
RawTown,
RawNation,
RawResident,
ApiResident,
RawServerInfo,
OAPITown
OAPITown,
OAPIResident,
OAPINation
} from '../types.js'

import { townyData } from '../utils/endpoint.js'
Expand All @@ -20,10 +21,12 @@ class OfficialAPI {
const res = await townyData(`/residents/${name}`) as RawResident
if (!res) throw new FetchError(`Could not fetch resident '${name}'. Received invalid response.`)

const obj: any = {
online: res.status?.isOnline ?? false,
balance: res.stats?.balance ?? 0
}
const obj: any = {}
if (res.status?.isOnline)
obj.online = res.status.isOnline

if (res.stats?.balance)
obj.balance = res.stats.balance

if (res.timestamps) obj.timestamps = res.timestamps
if (res.strings?.username) obj.name = res.strings.username
Expand All @@ -50,41 +53,34 @@ class OfficialAPI {
}
}

obj.friends = res.friends || []
return obj as ApiResident
return obj as OAPIResident
}

static town = async (name: string) => {
if (!name) return

const town = await townyData(`/towns/${name}`) as RawTown

// TODO: Implement a proper error
if (!town) return
if (!town) return // TODO: Implement a proper error

return {
name: town.strings.town,
founder: town.strings.founder,
created: town.timestamps?.registered,
joinedNation: town.timestamps?.joinedNationAt
joinedNation: town.timestamps?.joinedNationAt,
...town
} as OAPITown
}

static nation = async (name: string) => {
if (!name) return

const nation = await townyData(`/nations/${name}`) as RawNation
const obj: any = {}

if (nation) {
if (nation.stats) obj.stats = nation.stats
if (nation.ranks) obj.ranks = nation.ranks

if (nation.timestamps?.registered)
obj.created = nation.timestamps.registered
}
if (!nation) return // TODO: Implement a proper error

return obj
return {
name: nation.strings.nation,
created: nation.timestamps.registered
} as OAPINation
}
}

Expand Down
12 changes: 7 additions & 5 deletions src/types/nation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ export type Nation = {
residents: string[]
area: number
wiki?: string
capital: {
name: string
x: number
z: number
}
capital: NationCapital
stats: unknown
}

export type NationCapital = {
name: string
x: number
z: number
}
83 changes: 56 additions & 27 deletions src/types/oapi_v1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,55 @@ type NestedOmit<T, K extends PropertyKey> = {
NestedOmit<T[P], K extends `${Exclude<P, symbol>}.${infer R}` ? R : never>
} extends infer O ? { [P in keyof O]: O[P] } : never;

//#region Parsed
export type OAPITown = NestedOmit<RawTown,
"strings.town" |
"strings.founder" |
"timestamps.registered" |
"timestamps"
> & {
name: string
founder: string
created: number
joinedNation: number
}

export type OAPINation = NestedOmit<RawNation,
"strings.nation" |
"timestamps"
> & {
name: string
created: number
}

export type OAPIResident = NestedOmit<RawResident,
"strings" |
"affiliation" |
"ranks" |
"perms" |
"status" |
"stats"
> & {
name: string
title?: string
surname?: string
town?: string
nation?: string
townRanks: string[]
nationRanks: string[]
timestamps: Timestamps
perms?: {
build: RawResidentPerms
destroy: RawResidentPerms
switch: RawResidentPerms
itemUse: RawResidentPerms
flags: RawFlagPerms
}
online: boolean
balance: number
}
//#endregion

//#region Raw, unparsed types
export type RawEntity = {
status: RawEntityStatus
Expand Down Expand Up @@ -99,6 +148,12 @@ export type RawNation = RawEntity & {
enemies: string[]
}

export type Timestamps = {
joinedTownAt?: number
registered: number
lastOnline?: number
}

export type RawResident = RawEntity & {
strings: {
title: string
Expand All @@ -109,11 +164,7 @@ export type RawResident = RawEntity & {
town: string
nation: string
}>
timestamps?: {
joinedTownAt?: number
registered: number
lastOnline: number
}
timestamps?: Timestamps
perms: RawEntityPerms<RawResidentPerms>
friends: string[]
}
Expand All @@ -138,26 +189,4 @@ export type RawServerInfo = {
numTownBlocks: number
}
}
//#endregion

//#region Parsed
export type OAPITown = NestedOmit<RawTown,
"strings.town" |
"strings.founder" |
"timestamps.registered" |
"timestamps.joinedNationAt"
> & {
name: string
founder: string
created: number
joinedNation: number
}

export type OAPINation = Partial<RawEntity> & {
s
}

export type OAPIResident = Partial<RawEntity> & {
s
}
//#endregion
31 changes: 0 additions & 31 deletions src/types/resident.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,6 @@
import { RawFlagPerms, RawResidentPerms } from '../types.js'

export type Resident = {
name: string
town: string
nation?: string
rank: string
}

export type ApiResident = {
name: string
title: string
surname: string
town?: string
nation?: string
balance: number
online: boolean
timestamps?: {
joinedTownAt?: number
registered: number
lastOnline: number
}
townRanks: ResidentRanks
nationRanks: ResidentRanks
perms?: {
build: RawResidentPerms
destroy: RawResidentPerms
switch: RawResidentPerms
itemUse: RawResidentPerms
flags: RawFlagPerms
}
friends?: string[]
}

type ResidentRanks = {
ranks?: { [key: string]: string[] }
}
17 changes: 14 additions & 3 deletions tests/oapi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import {
} from 'vitest'

import { OfficialAPI } from '../src/main'
import { ApiResident, ServerInfoRaw } from '../src/types'
import { OAPINation, OAPIResident, RawServerInfo } from '../src/types'

describe('OfficialAPI', async () => {
it('can get valid towny/server info (v2)', async () => {
const info = await OfficialAPI.serverInfo()

expect(info).toBeDefined()
assertType<ServerInfoRaw>(info)
assertType<RawServerInfo>(info)

expect(info.world).toBeDefined()
expect(info.players).toBeDefined()
Expand All @@ -26,8 +26,19 @@ describe('OfficialAPI', async () => {
const res = await OfficialAPI.resident('owen3h')

expect(res).toBeDefined()
assertType<ApiResident>(res)
assertType<OAPIResident>(res)

expect(res.name).toBe("Owen3H")
//console.log(res)
})

it('can get valid nation (v1)', async () => {
const nation = await OfficialAPI.nation('venice')

expect(nation).toBeDefined()
assertType<OAPINation>(nation)

expect(nation.name).toBe("Venice")
console.log(nation)
})
})

0 comments on commit dbb0b26

Please sign in to comment.