Skip to content

Commit

Permalink
implement some OAPI funcs using POST
Browse files Browse the repository at this point in the history
  • Loading branch information
Owen3H committed Jul 28, 2024
1 parent f5af1cf commit 362f5b4
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 17 deletions.
18 changes: 17 additions & 1 deletion src/OAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,24 @@ const parseNation = (nation: RawNation) => {
const ParamErr = () => new SyntaxError(`Parameter 'name' is invalid. Must be of type string!`)
const FetchErr = (type: string, name: string) => new FetchError(`Could not fetch ${type} '${name}'. Invalid response received!`)

type DiscordReqObject = {
type: 'minecraft' | 'discord'
target: string
}

type DiscordResObject = {
ID: string
UUID: string
}

export class OAPIV3 {
static serverInfo = async() => (await townyData('', 'v3')) as RawServerInfoV3
static serverInfo = async(): Promise<RawServerInfoV3> => await townyData('', 'v3')

static discord = async (...objs: DiscordReqObject[]): Promise<DiscordResObject[]> =>
await townyData('/discord', 'v3', { query: objs })

static players = async (...ids: string[]): Promise<OAPIResident> =>
await townyData('/players', 'v3', { query: ids })
}

export class OAPIV2 {
Expand Down
24 changes: 12 additions & 12 deletions src/types/oapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ export type OAPIResident = NestedOmit<RawResident,
townRanks?: string[]
nationRanks?: string[]
perms?: {
build: RawResidentPerms
destroy: RawResidentPerms
switch: RawResidentPerms
itemUse: RawResidentPerms
flags: RawFlagPerms
build: boolean[] //RawResidentPerms
destroy: boolean[] //RawResidentPerms
switch: boolean[] //RawResidentPerms
itemUse: boolean[] //RawResidentPerms
flags: RawFlagPerms //RawResidentPerms
}
}
//#endregion
Expand Down Expand Up @@ -83,12 +83,12 @@ export type RawEntityStats = Prettify<{
balance: number
}>

export type RawResidentPerms = Prettify<{
friend: boolean
town: boolean
ally: boolean
outsider: boolean
}>
// export type RawResidentPerms = Prettify<{
// friend: boolean
// town: boolean
// ally: boolean
// outsider: boolean
// }>

export type RawTownPerms = Prettify<{
resident: boolean
Expand Down Expand Up @@ -171,7 +171,7 @@ export type RawResident = Prettify<RawEntity & {
town?: string
nation?: string
timestamps?: Timestamps
perms: RawEntityPerms<RawResidentPerms>
perms: RawEntityPerms<boolean[]>
friends?: string[]
}>

Expand Down
8 changes: 4 additions & 4 deletions src/utils/endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import type { AnyMap } from "../types/index.js"

import { genRandomString } from './functions.js'

export type V3RequestBody = {
query: string
export type V3RequestBody<T> = {
query: T
[key: string]: any
}

Expand Down Expand Up @@ -68,7 +68,7 @@ const mapData = async <T>(mapName: AnyMap): Promise<T> => {
* By "towny" we are referring to the data that we receive (balance, registration date etc).
* @param endpoint The endpoint not including the domain, e.g: "lists/nations"
*/
const townyData = async (endpoint = '', version: EndpointVersion = 'v3', body?: V3RequestBody) => {
const townyData = async <T>(endpoint = '', version: EndpointVersion = 'v3', body?: V3RequestBody<T>) => {
if (endpoint.startsWith("/")) {
endpoint.replace("/", "")
}
Expand All @@ -77,7 +77,7 @@ const townyData = async (endpoint = '', version: EndpointVersion = 'v3', body?:
const url = get("towny", "v3/aurora")

return body ? asJSON(`${url}${endpoint}`, {
method: "GET",
method: "POST",
body: JSON.stringify(body)
}) : asJSON(`${url}${endpoint}`)
}
Expand Down

0 comments on commit 362f5b4

Please sign in to comment.