From af4fc31ed371ae98b4fac18fce86fa6276ec272c Mon Sep 17 00:00:00 2001 From: Guilherme Werner Date: Wed, 3 Jan 2024 10:59:48 -0300 Subject: [PATCH] Add more functions --- src/api.ts | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++- src/client.ts | 8 +++++ 2 files changed, 101 insertions(+), 1 deletion(-) diff --git a/src/api.ts b/src/api.ts index 9ded640..6ce8ef0 100644 --- a/src/api.ts +++ b/src/api.ts @@ -234,7 +234,7 @@ export class TribufuApi { * @param id * @returns Game | null */ - public async getGameyId(id: string): Promise { + public async getGameById(id: string): Promise { const headers = this.getHeaders(); const responseBody = await this.http.get(`/v1/packages/${id}`, headers); @@ -305,4 +305,96 @@ export class TribufuApi { return responseBody; } + + /** + * Get a user by id from the Tribufu API. + * @param id + * @returns User | null + */ + public async getUserById(id: string): Promise { + const headers = this.getHeaders(); + const responseBody = await this.http.get(`/v1/users/${id}`, headers); + + if (!responseBody) { + return null; + } + + return responseBody; + } + + /** + * Get a user by uuid from the Tribufu API. + * @param uuid + * @returns User[] + */ + public async getUserByUuid(uuid: string): Promise { + return await this.getUserByKey("uuid", uuid); + } + + /** + * Get a user by name from the Tribufu API. + * @param uuid + * @returns User[] + */ + public async getUserByName(name: string): Promise { + return await this.getUserByKey("name", name); + } + + /** + * Get a user by email from the Tribufu API. + * @param uuid + * @returns User[] + */ + public async getUserByEmail(email: string): Promise { + return await this.getUserByKey("email", email); + } + + /** + * Get a user by custom key from the Tribufu API. + * @param key + * @param value + * @returns User[] + */ + private async getUserByKey(key: string, value: string): Promise { + const headers = this.getHeaders(); + const responseBody = await this.http.get(`/v1/users/?${key}=${value}`, headers); + + if (!responseBody) { + return []; + } + + return responseBody; + } + + /** + * Get all servers for a user from the Tribufu API. + * @param userId + * @returns Server[] + */ + public async getUserServers(userId: string): Promise { + const headers = this.getHeaders(); + const responseBody = await this.http.get(`/v1/users/${userId}/servers`, headers); + + if (!responseBody) { + return []; + } + + return responseBody; + } + + /** + * Get a oauth2 client by id from the Tribufu API. + * @param id + * @returns Client | null + */ + protected async getClientById(id: string): Promise { + const headers = this.getHeaders(); + const responseBody = await this.http.get(`/v1/oauth2/clients/${id}`, headers); + + if (!responseBody) { + return null; + } + + return responseBody; + } } diff --git a/src/client.ts b/src/client.ts index 9634cf3..1910675 100644 --- a/src/client.ts +++ b/src/client.ts @@ -333,6 +333,14 @@ export class TribufuClient extends TribufuApi { return responseBody; } + /** + * Get information about the current client. + * @returns Client | null + */ + public async getClientInfo(): Promise { + return this.getClientById(this.clientId); + } + /** * Get information about the current user. * @returns User | null