16 Commits

Author SHA1 Message Date
485d19b9f2 Bump version to 0.1.17 2024-05-07 20:46:22 -03:00
51addfcd53 Fix getServerByAddress returning array 2024-05-07 20:46:16 -03:00
725b5f1a4c Update package.json 2024-05-07 20:24:36 -03:00
a1021ecd86 Fix bug in TribufuApi.getServer() 2024-05-07 20:21:24 -03:00
52dca6a519 Bump version to 0.1.14 2024-05-07 20:01:43 -03:00
23dac26596 Update TribufuClient.fromEnv()
Also read apiKey from env
2024-05-07 20:01:20 -03:00
db6a2cd5da Add setApiKey 2024-05-07 20:00:43 -03:00
804a116b7b Update api.ts 2024-03-28 20:30:09 -03:00
ab304a8b08 Update .env.example 2024-03-28 20:30:01 -03:00
23c6f5b30a Revert "Merge mintaka lib"
This reverts commit c6136b6e21.
2024-03-21 12:28:47 -03:00
c6136b6e21 Merge mintaka lib 2024-03-06 19:39:31 -03:00
b4d074420a Delete user.ts 2024-02-01 17:12:33 -03:00
a83926229a Create profile.ts 2024-01-31 12:06:13 -03:00
7fa3386cba Create cluster.ts 2024-01-30 16:10:28 -03:00
7c22788be4 Update client.ts 2024-01-30 16:10:17 -03:00
86abc9877e Update mintaka to 0.1.8 2024-01-29 09:02:47 -03:00
8 changed files with 198 additions and 63 deletions

View File

@ -1,3 +1,4 @@
NODE_ENV=development
TRIBUFU_API_KEY=
TRIBUFU_API_URL=
TRIBUFU_BOT_TOKEN=

View File

@ -1,6 +1,6 @@
{
"name": "tribufu",
"version": "0.1.13",
"version": "0.1.17",
"description": "Tribufu JS SDK",
"repository": "https://github.com/Tribufu/TribufuJs",
"author": "Tribufu <contact@Tribufu.com>",
@ -16,7 +16,7 @@
"build": "npm run clean && tsc && node scripts/esbuild.js"
},
"dependencies": {
"@tribufu/mintaka": "0.1.7"
"@tribufu/mintaka": "0.1.8"
},
"devDependencies": {
"@types/node": "^20.10.6",

8
pnpm-lock.yaml generated
View File

@ -6,8 +6,8 @@ settings:
dependencies:
'@tribufu/mintaka':
specifier: 0.1.7
version: 0.1.7
specifier: 0.1.8
version: 0.1.8
devDependencies:
'@types/node':
@ -260,8 +260,8 @@ packages:
dev: true
optional: true
/@tribufu/mintaka@0.1.7:
resolution: {integrity: sha512-9GZb4WzChV8NCWV9Z3Q2n5TSg7XLZKjv6H2+spCaIhqdolhak/V/IT/3hNoMHmK1Oygm4MXrn6p9lA/DfcUq/w==}
/@tribufu/mintaka@0.1.8:
resolution: {integrity: sha512-qUvReWlz8irSIbKCTfjFnUnUq7MIgLjnTBPeWv2ayyiwGkf8L3q7qi1Zxuqt3OduOugSOtxYQwXOaClldIMUTQ==}
dependencies:
axios: 1.6.4
camelcase-keys: 9.1.2

View File

@ -28,7 +28,7 @@ export class TribufuApi {
this.http = new HttpClient({
baseUrl: TribufuApi.getBaseUrl(),
headers: TribufuApi.defaultHeaders(),
enableLog: TribufuApi.debugEnabled(),
logEnabled: TribufuApi.debugEnabled(),
jsonRequestCasing: JsonCasing.SnakeCase,
jsonResponseCasing: JsonCasing.CamelCase,
});
@ -95,6 +95,16 @@ export class TribufuApi {
return TribufuApi.fromEnv(prefix) || TribufuApi.default();
}
/**
* Set the tokens.
* @param accessToken
* @param refreshToken
* @param expiresIn
*/
protected setApiKey(apiKey: string | null): void {
this.options.apiKey = apiKey;
}
/**
* Check if debug mode is enabled.
*
@ -215,7 +225,7 @@ export class TribufuApi {
*/
public async getGames(page: number = 1): Promise<any[]> {
const headers = this.getHeaders();
const responseBody = await this.http.get<any[]>(`/v1/packages?page=${page}`, headers);
const responseBody = await this.http.get<any[]>(`/v1/games?page=${page}`, headers);
if (!responseBody) {
return [];
@ -231,7 +241,7 @@ export class TribufuApi {
*/
public async getGameById(id: string): Promise<any | null> {
const headers = this.getHeaders();
const responseBody = await this.http.get<any>(`/v1/packages/${id}`, headers);
const responseBody = await this.http.get<any>(`/v1/games/${id}`, headers);
if (!responseBody) {
return null;
@ -291,14 +301,18 @@ export class TribufuApi {
* @returns Server | null
*/
public async getServerByAddress(address: string): Promise<any> {
const parts = address.split(":");
const hostOrAddress = parts[0];
const queryPort = parts[1];
const headers = this.getHeaders();
const responseBody = await this.http.get(`/v1/servers/address/${address}`, headers);
const responseBody = await this.http.get(`/v1/servers?address=${hostOrAddress}&query_port=${queryPort}`, headers);
if (!responseBody) {
return null;
}
return responseBody;
return responseBody[0];
}
/**
@ -335,15 +349,6 @@ export class TribufuApi {
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<any[]> {
return await this.getUserByKey("email", email);
}
/**
* Get a user by custom key from the Tribufu API.
* @param key
@ -361,6 +366,54 @@ export class TribufuApi {
return responseBody;
}
/**
* Get all groups for a user from the Tribufu API.
* @param userId
* @returns Group[]
*/
public async getUserGroups(userId: string): Promise<any[]> {
const headers = this.getHeaders();
const responseBody = await this.http.get<any[]>(`/v1/users/${userId}/groups`, headers);
if (!responseBody) {
return [];
}
return responseBody;
}
/**
* Get all games for a user from the Tribufu API.
* @param userId
* @returns Game[]
*/
public async getUserGames(userId: string): Promise<any[]> {
const headers = this.getHeaders();
const responseBody = await this.http.get<any[]>(`/v1/users/${userId}/games`, headers);
if (!responseBody) {
return [];
}
return responseBody;
}
/**
* Get all punishments for a user from the Tribufu API.
* @param userId
* @returns Punishment[]
*/
public async getUserPunishments(userId: string): Promise<any[]> {
const headers = this.getHeaders();
const responseBody = await this.http.get<any[]>(`/v1/users/${userId}/punishments`, headers);
if (!responseBody) {
return [];
}
return responseBody;
}
/**
* Get all servers for a user from the Tribufu API.
* @param userId
@ -394,13 +447,13 @@ export class TribufuApi {
}
/**
* Get files from the Tribufu API.
* Get packages from the Tribufu API.
* @param page
* @returns File[]
* @returns Package[]
*/
public async getFiles(page: number = 1): Promise<any[]> {
public async getPackages(page: number = 1): Promise<any[]> {
const headers = this.getHeaders();
const responseBody = await this.http.get<any[]>(`/v1/files?page=${page}`, headers);
const responseBody = await this.http.get<any[]>(`/v1/packages?page=${page}`, headers);
if (!responseBody) {
return [];
@ -410,13 +463,77 @@ export class TribufuApi {
}
/**
* Get a file by id from the Tribufu API.
* Get a package by id from the Tribufu API.
* @param id
* @returns File | null
* @returns Package | null
*/
public async getFileById(id: string): Promise<any> {
public async getPackageById(id: string): Promise<any> {
const headers = this.getHeaders()
const responseBody = await this.http.get<any>(`/v1/files/${id}`, headers);
const responseBody = await this.http.get<any>(`/v1/packages/${id}`, headers);
if (!responseBody) {
return null;
}
return responseBody;
}
/**
* Get clusters from the Tribufu API.
* @param page
* @returns Cluster[]
*/
public async getClusters(page: number = 1): Promise<any[]> {
const headers = this.getHeaders();
const responseBody = await this.http.get<any[]>(`/v1/clusters?page=${page}`, headers);
if (!responseBody) {
return [];
}
return responseBody;
}
/**
* Get a cluster by id from the Tribufu API.
* @param id
* @returns Cluster | null
*/
public async getClusterById(id: string): Promise<any> {
const headers = this.getHeaders()
const responseBody = await this.http.get<any>(`/v1/clusters/${id}`, headers);
if (!responseBody) {
return null;
}
return responseBody;
}
/**
* Get subscriptions from the Tribufu API.
* @param page
* @returns Subscription[]
*/
public async getSubscriptions(page: number = 1): Promise<any[]> {
const headers = this.getHeaders();
const responseBody = await this.http.get<any[]>(`/v1/subscriptions?page=${page}`, headers);
if (!responseBody) {
return [];
}
return responseBody;
}
/**
* Get a subscription by id from the Tribufu API.
* @param id
* @returns Subscription | null
*/
public async getSubscriptionById(id: string): Promise<any> {
const headers = this.getHeaders()
const responseBody = await this.http.get<any>(`/v1/subscriptions/${id}`, headers);
if (!responseBody) {
return null;

View File

@ -4,7 +4,6 @@
import { HttpCookieMap, HttpHeaders } from "@tribufu/mintaka";
import { OAuth2GrantType, OAuth2IntrospectionRequest, OAuth2IntrospectionResponse, OAuth2TokenRequest, OAuth2TokenResponse } from "@tribufu/mintaka";
import { TribufuApi } from "./api";
import { User } from "./models/user";
/**
* **Tribufu Client**
@ -41,14 +40,21 @@ export class TribufuClient extends TribufuApi {
*/
public static override fromEnv(prefix?: string | null): TribufuClient | null {
const envPrefix = prefix ? `${prefix}_` : "";
const apiKey = process.env[`${envPrefix}API_KEY`];
const clientId = process.env[`${envPrefix}CLIENT_ID`];
const clientSecret = process.env[`${envPrefix}CLIENT_SECRET`];
if (clientId && clientSecret) {
return new TribufuClient(clientId, clientSecret);
if (!clientId || !clientSecret) {
return null;
}
return null;
const client = new TribufuClient(clientId, clientSecret);
if (apiKey) {
client.setApiKey(apiKey);
}
return client;
}
/**
@ -73,7 +79,7 @@ export class TribufuClient extends TribufuApi {
const accessToken = cookies["access_token"] || null;
const refreshToken = cookies["refresh_token"] || null;
if (client) {
if (client && accessToken && refreshToken) {
client.setTokens(accessToken, refreshToken);
}
@ -337,7 +343,7 @@ export class TribufuClient extends TribufuApi {
* Get information about the current client.
* @returns Client | null
*/
public async getClientInfo(): Promise<User | null> {
public async getClientInfo(): Promise<any | null> {
return this.getClientById(this.clientId);
}
@ -345,13 +351,13 @@ export class TribufuClient extends TribufuApi {
* Get information about the current user.
* @returns User | null
*/
public async getUserInfo(): Promise<User | null> {
public async getUserInfo(): Promise<any | null> {
if (!this.options.accessToken) {
return null;
}
const headers = this.getHeaders();
const responseBody = await this.http.get<User>(`/v1/oauth2/userinfo`, headers);
const responseBody = await this.http.get<any>(`/v1/oauth2/userinfo`, headers);
if (!responseBody) {
return null;

22
src/models/cluster.ts Normal file
View File

@ -0,0 +1,22 @@
// Copyright (c) Tribufu. All Rights Reserved.
// SPDX-License-Identifier: MIT
import { MiniProfile } from "./profile";
export interface Cluster {
id: string,
name: string,
description: string | null,
packageId: string,
websiteUrl: string | null,
bannerUrl: string | null,
ownerId: string | null,
owner: MiniProfile | null,
discordServerId: string | null,
youtubeVideoUrl: string | null,
tags: string | null,
commentCount: number,
serverCount: number,
created: string,
updated: string | null
}

16
src/models/profile.ts Normal file
View File

@ -0,0 +1,16 @@
// Copyright (c) Tribufu. All Rights Reserved.
// SPDX-License-Identifier: MIT
export type ProfileType = "user" | "bot" | "org";
export type Profile = any;
export interface MiniProfile {
id: string,
uuid: string,
name: string,
displayName: string,
type: ProfileType,
verified: boolean,
photoUrl: string | null
}

View File

@ -1,27 +0,0 @@
// Copyright (c) Tribufu. All Rights Reserved.
// SPDX-License-Identifier: MIT
export type AccountType = "user" | "bot" | "org";
export interface User {
id: number,
uuid: string,
name: string,
displayName: string,
kind: AccountType,
publicFlags: number,
verified: boolean,
level: number,
experience: number,
publicBirthday: boolean,
birthday?: string | null,
points: number,
location?: string | null,
photoUrl?: string | null,
bannerUrl?: string | null,
lastOnline?: string | null,
biography?: string | null,
viewCount: number,
created: string,
updated?: string | null,
}