From 362f5b45fe0ca198f27c7a811b62128604cbefa1 Mon Sep 17 00:00:00 2001 From: Owen Date: Sun, 28 Jul 2024 18:48:16 +0100 Subject: [PATCH] implement some OAPI funcs using POST --- src/OAPI.ts | 18 +++++++++++++++++- src/types/oapi.ts | 24 ++++++++++++------------ src/utils/endpoint.ts | 8 ++++---- 3 files changed, 33 insertions(+), 17 deletions(-) diff --git a/src/OAPI.ts b/src/OAPI.ts index 0b93dbe..d3276f3 100644 --- a/src/OAPI.ts +++ b/src/OAPI.ts @@ -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 => await townyData('', 'v3') + + static discord = async (...objs: DiscordReqObject[]): Promise => + await townyData('/discord', 'v3', { query: objs }) + + static players = async (...ids: string[]): Promise => + await townyData('/players', 'v3', { query: ids }) } export class OAPIV2 { diff --git a/src/types/oapi.ts b/src/types/oapi.ts index 73ca353..9c8f856 100644 --- a/src/types/oapi.ts +++ b/src/types/oapi.ts @@ -47,11 +47,11 @@ export type OAPIResident = NestedOmit -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 @@ -171,7 +171,7 @@ export type RawResident = Prettify + perms: RawEntityPerms friends?: string[] }> diff --git a/src/utils/endpoint.ts b/src/utils/endpoint.ts index 873e9e4..f1465ab 100644 --- a/src/utils/endpoint.ts +++ b/src/utils/endpoint.ts @@ -5,8 +5,8 @@ import type { AnyMap } from "../types/index.js" import { genRandomString } from './functions.js' -export type V3RequestBody = { - query: string +export type V3RequestBody = { + query: T [key: string]: any } @@ -68,7 +68,7 @@ const mapData = async (mapName: AnyMap): Promise => { * 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 (endpoint = '', version: EndpointVersion = 'v3', body?: V3RequestBody) => { if (endpoint.startsWith("/")) { endpoint.replace("/", "") } @@ -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}`) }