Generate api with NSwag

This commit is contained in:
2024-07-23 20:29:30 -03:00
parent 485d19b9f2
commit 0d24ecca3f
20 changed files with 1275 additions and 1243 deletions

View File

@ -1,15 +0,0 @@
// Copyright (c) Tribufu. All Rights Reserved.
// SPDX-License-Identifier: MIT
import dotenv from 'dotenv';
import { TribufuApi } from '../build/index.mjs';
dotenv.config();
async function main() {
const api = TribufuApi.fromEnv("TRIBUFU");
const games = await api.getGames();
console.log(games[0]);
}
main();

View File

@ -1,10 +0,0 @@
// Copyright (c) Tribufu. All Rights Reserved.
// SPDX-License-Identifier: MIT
import dotenv from 'dotenv';
import { TribufuBot } from '../build/index.mjs';
dotenv.config();
const bot = TribufuBot.fromEnv("TRIBUFU");
const botId = bot.getBotId();

View File

@ -1,18 +0,0 @@
// Copyright (c) Tribufu. All Rights Reserved.
// SPDX-License-Identifier: MIT
import dotenv from 'dotenv';
import { TribufuClient } from '../build/index.mjs';
dotenv.config();
async function main() {
const client = TribufuClient.fromEnv("TRIBUFU");
if (await client.passwordLogin("", "")) {
const userInfo = await client.getUserInfo();
console.log(userInfo);
}
}
main();

13
examples/openid.js Normal file
View File

@ -0,0 +1,13 @@
// Copyright (c) Tribufu. All Rights Reserved.
// SPDX-License-Identifier: MIT
import dotenv from 'dotenv';
import { TribufuClient } from '../build/index.mjs';
async function main() {
const client = new TribufuClient();
const config = await client.openidConfiguration();
console.log(config);
}
main();

View File

@ -1,11 +0,0 @@
// Copyright (c) Tribufu. All Rights Reserved.
// SPDX-License-Identifier: MIT
import dotenv from 'dotenv';
import { TribufuServer } from '../build/index.mjs';
dotenv.config();
const server = TribufuServer.fromEnv("TRIBUFU");
const serverId = server.getServerId();
const clientId = server.getClientId();

View File

@ -16,7 +16,8 @@
"build": "npm run clean && tsc && node scripts/esbuild.js"
},
"dependencies": {
"@tribufu/mintaka": "0.1.8"
"@tribufu/mintaka": "0.1.8",
"axios": "^1.7.2"
},
"devDependencies": {
"@types/node": "^20.10.6",

15
pnpm-lock.yaml generated
View File

@ -8,6 +8,9 @@ dependencies:
'@tribufu/mintaka':
specifier: 0.1.8
version: 0.1.8
axios:
specifier: ^1.7.2
version: 1.7.2
devDependencies:
'@types/node':
@ -263,7 +266,7 @@ packages:
/@tribufu/mintaka@0.1.8:
resolution: {integrity: sha512-qUvReWlz8irSIbKCTfjFnUnUq7MIgLjnTBPeWv2ayyiwGkf8L3q7qi1Zxuqt3OduOugSOtxYQwXOaClldIMUTQ==}
dependencies:
axios: 1.6.4
axios: 1.7.2
camelcase-keys: 9.1.2
fp-ts: 2.16.2
json-bigint: 1.0.0
@ -308,10 +311,10 @@ packages:
resolution: {integrity: sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==}
dev: false
/axios@1.6.4:
resolution: {integrity: sha512-heJnIs6N4aa1eSthhN9M5ioILu8Wi8vmQW9iHQ9NUvfkJb0lEEDUiIdQNAuBtfUt3FxReaKdpQA5DbmMOqzF/A==}
/axios@1.7.2:
resolution: {integrity: sha512-2A8QhOMrbomlDuiLeK9XibIBzuHeRcqqNOHp0Cyp5EoJ1IFDh+XZH3A6BkXtv0K4gFGCI0Y4BM7B1wOEi0Rmgw==}
dependencies:
follow-redirects: 1.15.4
follow-redirects: 1.15.6
form-data: 4.0.0
proxy-from-env: 1.1.0
transitivePeerDependencies:
@ -471,8 +474,8 @@ packages:
path-exists: 4.0.0
dev: true
/follow-redirects@1.15.4:
resolution: {integrity: sha512-Cr4D/5wlrb0z9dgERpUL3LrmPKVDsETIJhaCMeDfuFYcqa5bldGV6wBsAN6X/vxlXQtFBMrXdXxdL8CbDTGniw==}
/follow-redirects@1.15.6:
resolution: {integrity: sha512-wWN62YITEaOpSK584EZXJafH1AGpO8RVgElfkuXbTOrPX4fIfOyEpW/CsiNd8JdYrAoOvafRTOEnvsO++qCqFA==}
engines: {node: '>=4.0'}
peerDependencies:
debug: '*'

1248
src/api.generated.ts Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,544 +0,0 @@
// Copyright (c) Tribufu. All Rights Reserved.
// SPDX-License-Identifier: MIT
import { HttpHeaders, HttpClient } from "@tribufu/mintaka";
import { JavaScriptRuntime } from "./node";
import { JsonCasing, JwtDecoder } from "@tribufu/mintaka";
import { TokenPayload } from "./token";
import { TRIBUFU_API_URL, TRIBUFU_VERSION } from ".";
import { TribufuApiOptions } from "./options";
/**
* **Tribufu API**
*
* Use this class to interact with the Tribufu API.
*
* *There are three ways to use the Tribufu API:*
* - A api key give you public read only access to the Tribufu API.
* - A bot give you read and write access to the Tribufu API as a bot account.
* - A client give you read and write access to the Tribufu API as a client application.
*/
export class TribufuApi {
protected readonly http: HttpClient;
protected readonly options: TribufuApiOptions;
constructor(options?: TribufuApiOptions | null) {
this.options = options || {};
this.http = new HttpClient({
baseUrl: TribufuApi.getBaseUrl(),
headers: TribufuApi.defaultHeaders(),
logEnabled: TribufuApi.debugEnabled(),
jsonRequestCasing: JsonCasing.SnakeCase,
jsonResponseCasing: JsonCasing.CamelCase,
});
}
/**
* Create a TribufuApi with the default options.
* @returns
*/
public static default(): TribufuApi {
return new TribufuApi();
}
/**
* Create a TribufuApi with the given api key.
*
* - A api key give you public read only access to the Tribufu API.
*
* @param apiKey
* @returns TribufuApi
*/
public static withApiKey(apiKey: string): TribufuApi {
return new TribufuApi({ apiKey });
}
/**
* Try to create a TribufuApi from environment variables.
*
* - This will only work if the environment variables are set.
*
* @param prefix A prefix for the environment variables.
* @returns TribufuApi | null
* @example
* ```ts
* // process.env.TRIBUFU_API_KEY
* const api = TribufuApi.fromEnv("TRIBUFU");
* ```
*/
public static fromEnv(prefix?: string | null): TribufuApi | null {
const envPrefix = prefix ? `${prefix}_` : "";
const apiKey = process.env[`${envPrefix}API_KEY`];
if (apiKey) {
return TribufuApi.withApiKey(apiKey);
}
return null;
}
/**
* Create a TribufuApi from environment variables or the default api.
*
* - This will fallback to the default api if the environment variables are not set.
*
* @param prefix A prefix for the environment variables.
* @returns TribufuApi | null
* @example
* ```ts
* // process.env.TRIBUFU_API_KEY = null
* const api = TribufuApi.fromEnvOrDefault("TRIBUFU_");
* ```
*/
public static fromEnvOrDefault(prefix: string = ""): 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.
*
* - Debug mode is enabled if the environment variable `NODE_ENV` is set to `development`.
* - Debug mode is disabled by default.
* - Debug mode is disabled in the browser.
*
* @returns boolean
*/
private static debugEnabled(): boolean {
return process.env.NODE_ENV ? process.env.NODE_ENV === "development" : false;
}
/**
* Get the base url for the Tribufu API.
*
* - The base url can be set using the environment variable `TRIBUFU_API_URL`.
* - The custom base url is only used if debug mode is enabled.
* - The default base url is `https://api.tribufu.com`.
*
* @returns string
*/
private static getBaseUrl(): string {
const baseUrl = process.env[`TRIBUFU_API_URL`] || null;
return TribufuApi.debugEnabled() && baseUrl ? baseUrl : TRIBUFU_API_URL;
}
/**
* Get the default headers for the Tribufu API.
* @returns HeaderMap
*/
private static defaultHeaders(): HttpHeaders {
const headers = new HttpHeaders();
headers.set("X-Tribufu-Language", "javascript");
headers.set("X-Tribufu-Version", TRIBUFU_VERSION);
return headers;
}
/**
* Detect the current JavaScript runtime.
*
* - This is used to determine if the code is running in a browser or in Node.js.
*
* @returns JavaScriptRuntime
*/
private static detectRuntime(): JavaScriptRuntime {
if (typeof window !== "undefined") {
return JavaScriptRuntime.Browser;
}
if (typeof process !== "undefined" && process?.versions?.node) {
return JavaScriptRuntime.Node;
}
return JavaScriptRuntime.Other;
}
/**
* Check if the current JavaScript runtime is a browser.
* @returns boolean
*/
public static isBrowser(): boolean {
return TribufuApi.detectRuntime() === JavaScriptRuntime.Browser;
}
/**
* Check if the current JavaScript runtime is Node.js.
* @returns boolean
*/
public static isNode(): boolean {
return TribufuApi.detectRuntime() === JavaScriptRuntime.Node;
}
/**
* Extract the payload from a Tribufu token.
* @param token
* @returns TokenPayload | null
*/
protected static parseToken(token: string): TokenPayload | null {
try {
const payload = JwtDecoder.decode(token);
if (!payload) {
return null;
}
return payload as TokenPayload;
}
catch (error) {
return null;
}
}
/**
* Get current headers with the api key or access token.
* @returns HeaderMap
*/
protected getHeaders(): HttpHeaders {
let headers = TribufuApi.defaultHeaders();
if (this.options.apiKey) {
headers.set("Authorization", `ApiKey ${this.options.apiKey}`);
return headers;
}
if (this.options.accessToken) {
headers.set("Authorization", `Bearer ${this.options.accessToken}`);
return headers;
}
return headers;
}
/**
* Get games from the Tribufu API.
* @param page
* @returns Game[]
*/
public async getGames(page: number = 1): Promise<any[]> {
const headers = this.getHeaders();
const responseBody = await this.http.get<any[]>(`/v1/games?page=${page}`, headers);
if (!responseBody) {
return [];
}
return responseBody;
}
/**
* Get a game from the Tribufu API.
* @param id
* @returns Game | null
*/
public async getGameById(id: string): Promise<any | null> {
const headers = this.getHeaders();
const responseBody = await this.http.get<any>(`/v1/games/${id}`, headers);
if (!responseBody) {
return null;
}
return responseBody;
}
/**
* Get servers from the Tribufu API.
* @param page
* @returns Server[]
*/
public async getServers(page: number = 1): Promise<any[]> {
const headers = this.getHeaders();
const responseBody = await this.http.get<any[]>(`/v1/servers?page=${page}`, headers);
if (!responseBody) {
return [];
}
return responseBody;
}
/**
* Get a server by id or address from the Tribufu API.
* @param idOrAddress
* @returns Server | null
*/
public async getServer(idOrAddress: string): Promise<any> {
if (/[.:]/.test(idOrAddress)) {
return await this.getServerByAddress(idOrAddress);
}
return await this.getServerById(idOrAddress);
}
/**
* Get a server by id from the Tribufu API.
* @param id
* @returns Server | null
*/
public async getServerById(id: string): Promise<any> {
const headers = this.getHeaders()
const responseBody = await this.http.get<any>(`/v1/servers/${id}`, headers);
if (!responseBody) {
return null;
}
return responseBody;
}
/**
* Get a server by address from the Tribufu API.
* @param address
* @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=${hostOrAddress}&query_port=${queryPort}`, headers);
if (!responseBody) {
return null;
}
return responseBody[0];
}
/**
* Get a user by id from the Tribufu API.
* @param id
* @returns User | null
*/
public async getUserById(id: string): Promise<any> {
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<any[]> {
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<any[]> {
return await this.getUserByKey("name", name);
}
/**
* Get a user by custom key from the Tribufu API.
* @param key
* @param value
* @returns User[]
*/
private async getUserByKey(key: string, value: string): Promise<any[]> {
const headers = this.getHeaders();
const responseBody = await this.http.get<any[]>(`/v1/users?${key}=${value}`, headers);
if (!responseBody) {
return [];
}
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
* @returns Server[]
*/
public async getUserServers(userId: string): Promise<any[]> {
const headers = this.getHeaders();
const responseBody = await this.http.get<any[]>(`/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<any> {
const headers = this.getHeaders();
const responseBody = await this.http.get(`/v1/oauth2/clients/${id}`, headers);
if (!responseBody) {
return null;
}
return responseBody;
}
/**
* Get packages from the Tribufu API.
* @param page
* @returns Package[]
*/
public async getPackages(page: number = 1): Promise<any[]> {
const headers = this.getHeaders();
const responseBody = await this.http.get<any[]>(`/v1/packages?page=${page}`, headers);
if (!responseBody) {
return [];
}
return responseBody;
}
/**
* Get a package by id from the Tribufu API.
* @param id
* @returns Package | null
*/
public async getPackageById(id: string): Promise<any> {
const headers = this.getHeaders()
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;
}
return responseBody;
}
}

View File

@ -1,68 +0,0 @@
// Copyright (c) Tribufu. All Rights Reserved.
// SPDX-License-Identifier: MIT
import { TribufuApi } from "./api";
/**
* **Tribufu Bot**
*
* To authenticate a bot you need to use the bot token obtained from the Tribufu Developer Portal.
*
* - A bot is a special type of user account.
* - A bot give you read and write access to the Tribufu API.
*/
export class TribufuBot extends TribufuApi {
private readonly botId: string;
constructor(token: string) {
const payload = TribufuApi.parseToken(token);
if (!payload) {
throw new Error("Invalid token");
}
if (payload.type !== "bot") {
throw new Error("Invalid token type");
}
if (!payload.bot_id) {
throw new Error("Invalid token payload");
}
super({ accessToken: token });
this.botId = payload.bot_id;
}
/**
* Try to create a TribufuBot from environment variables.
*
* - This will only work if the environment variables are set.
*
* @param prefix A prefix for the environment variables.
* @returns TribufuBot | null
* @example
* ```ts
* // process.env.TRIBUFU_BOT_TOKEN
* const bot = TribufuBot.fromEnv("TRIBUFU");
* ```
*/
public static override fromEnv(prefix?: string | null): TribufuBot | null {
const envPrefix = prefix ? `${prefix}_` : "";
const token = process.env[`${envPrefix}BOT_TOKEN`];
if (token) {
return new TribufuBot(token);
}
return null;
}
/**
* Get the bot id.
* @returns string
*/
public getBotId(): string {
return this.botId;
}
}

View File

@ -1,368 +1,10 @@
// Copyright (c) Tribufu. All Rights Reserved.
// SPDX-License-Identifier: MIT
import { HttpCookieMap, HttpHeaders } from "@tribufu/mintaka";
import { OAuth2GrantType, OAuth2IntrospectionRequest, OAuth2IntrospectionResponse, OAuth2TokenRequest, OAuth2TokenResponse } from "@tribufu/mintaka";
import { TribufuApi } from "./api";
import { TribufuApi } from "./api.generated";
/**
* **Tribufu Client**
*
* To authenticate a client you need to use the client id and client secret obtained from the Tribufu Developer Portal
*
* - A client is how external applications interact with the Tribufu API.
* - A client give you read and write access to the Tribufu API.
* - A client can be used to login users.
*/
export class TribufuClient extends TribufuApi {
private readonly clientId: string;
private readonly clientSecret: string;
constructor(clientId: string, clientSecret: string) {
super();
this.clientId = clientId;
this.clientSecret = clientSecret;
}
/**
* Try to create a TribufuClient from environment variables.
*
* - This will only work if the environment variables are set.
*
* @param prefix A prefix for the environment variables.
* @returns TribufuClient | null
* @example
* ```ts
* // process.env.TRIBUFU_CLIENT_ID
* // process.env.TRIBUFU_CLIENT_SECRET
* const client = TribufuClient.fromEnv("TRIBUFU");
* ```
*/
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 null;
}
const client = new TribufuClient(clientId, clientSecret);
if (apiKey) {
client.setApiKey(apiKey);
}
return client;
}
/**
* Try to create a TribufuClient from environment variables and cookies.
*
* - This will only work if the environment variables are set.
* - The cookies are used to get the access token and refresh token.
*
* @param cookies Cookies from the request.
* @param prefix A prefix for the environment variables.
* @returns TribufuClient | null
* @example
* ```ts
* // process.env.TRIBUFU_CLIENT_ID
* // process.env.TRIBUFU_CLIENT_SECRET
* const cookies = { "access_token": "...", "refresh_token": "..." };
* const client = TribufuClient.fromCookies(cookies, "TRIBUFU_");
* ```
*/
public static fromCookies(cookies: HttpCookieMap, prefix: string = ""): TribufuClient | null {
const client = TribufuClient.fromEnv(prefix);
const accessToken = cookies["access_token"] || null;
const refreshToken = cookies["refresh_token"] || null;
if (client && accessToken && refreshToken) {
client.setTokens(accessToken, refreshToken);
}
return client;
}
/**
* Set the tokens.
* @param accessToken
* @param refreshToken
* @param expiresIn
*/
protected setTokens(accessToken: string | null, refreshToken?: string | null, expiresIn?: number | null): void {
this.options.accessToken = accessToken;
this.options.refreshToken = refreshToken || null;
this.options.expiresIn = expiresIn || null;
}
/**
* Clear the tokens.
*/
protected clearTokens(): void {
this.setTokens(null, null, null);
}
/**
* Set the tokens from a oauth2 response.
* @param tokens
*/
private setTokensFromResponse(tokens: OAuth2TokenResponse): void {
this.setTokens(tokens.access_token, tokens.refresh_token || null, tokens.expires_in || null);
}
/**
* Get the headers for a oauth2 request.
* @returns HttpHeaders
*/
private getOAuthHeaders(): HttpHeaders {
let headers = this.getHeaders();
headers["Authorization"] = `Basic ${Buffer.from(`${this.clientId}:${this.clientSecret}`, "binary").toString("base64")}`;
headers["Content-Type"] = "application/x-www-form-urlencoded";
return headers;
}
/**
* Get the client id.
* @returns string
*/
public getClientId(): string {
return this.clientId;
}
/**
* Login using an authorization code.
* @param authorizationCode
* @returns boolean
*/
public async authorizationLogin(authorizationCode: string): Promise<boolean> {
const response = await this.getToken("authorization_code", authorizationCode, null, null);
if (!response) {
return false;
}
this.setTokensFromResponse(response);
return true;
}
/**
* Login using a device code.
* @param deviceCode
* @returns boolean
*/
public async deviceLogin(deviceCode: string): Promise<boolean> {
const response = await this.getToken("device_code", deviceCode, null, null);
if (!response) {
return false;
}
this.setTokensFromResponse(response);
return true;
}
/**
* Login using a username and password.
* @param username
* @param password
* @returns boolean
*/
public async passwordLogin(username: string, password: string): Promise<boolean> {
const response = await this.getToken("password", password, null, username);
if (!response) {
return false;
}
this.setTokensFromResponse(response);
return true;
}
/**
* Login using a passkey.
* @param username
* @param passkey
* @returns boolean
*/
public async passkeyLogin(username: string, passkey: string): Promise<boolean> {
const response = await this.getToken("passkey", passkey, null, username);
if (!response) {
return false;
}
this.setTokensFromResponse(response);
return true;
}
/**
* Get a token for a client application.
* @param subjectKey
* @param subjectValue
* @returns boolean
*/
public async clientLogin(subjectKey: string | null, subjectValue: string | null): Promise<boolean> {
const response = await this.getToken("client_credentials", null, subjectKey, subjectValue);
if (!response) {
return false;
}
this.setTokensFromResponse(response);
return true;
}
/**
* Get a new access token using a refresh token.
* @param refreshToken
* @returns boolean
*/
public async refreshToken(): Promise<boolean> {
if (!this.options.refreshToken) {
return false;
}
const response = await this.getToken("refresh_token", this.options.refreshToken, null, null);
if (!response) {
return false;
}
this.setTokensFromResponse(response);
return true;
}
/**
* Get information about a access token.
* @returns boolean
*/
public async instrospectToken(): Promise<OAuth2IntrospectionResponse | null> {
if (!this.options.accessToken) {
return null;
}
const requestBody: OAuth2IntrospectionRequest = {
token: this.options.accessToken,
token_type_hint: "access_token",
}
const url = `/v1/oauth2/introspect`;
const headers = this.getOAuthHeaders();
const responseBody = await this.http.post<OAuth2IntrospectionRequest, OAuth2IntrospectionResponse>(url, requestBody, headers);
if (!responseBody) {
return null;
}
return responseBody;
}
/**
* Revoke a refresh token.
* @returns boolean
*/
public async revokeToken(): Promise<boolean> {
if (!this.options.refreshToken) {
return false;
}
const requestBody: OAuth2IntrospectionRequest = {
token: this.options.refreshToken,
token_type_hint: "refresh_token",
}
const url = `/v1/oauth2/revoke`;
const headers = this.getOAuthHeaders();
const responseBody = await this.http.post<OAuth2IntrospectionRequest, OAuth2IntrospectionResponse>(url, requestBody, headers);
if (!responseBody) {
return false;
}
this.clearTokens();
return true;
}
/**
* Check if the current access token is valid.
* @returns boolean
*/
public async isTokenValid(): Promise<boolean> {
const response = await this.instrospectToken();
if (!response) {
return false;
}
return response.active;
}
/**
* Get a oauth2 token with the given grant type.
* @param grantType
* @param grantValue
* @param subjectKey
* @param subjectValue
* @returns OAuth2TokenResponse | null
*/
protected async getToken(grantType: OAuth2GrantType, grantValue: string | null, subjectKey: string | null, subjectValue: string | null): Promise<OAuth2TokenResponse | null> {
const requestBody: OAuth2TokenRequest = {
grant_type: grantType,
code: grantType === "authorization_code" ? grantValue : null,
refresh_token: grantType === "refresh_token" ? grantValue : null,
username: grantType === "password" || grantType === "passkey" ? subjectValue : null,
password: grantType === "password" ? grantValue : null,
passkey: grantType === "passkey" ? grantValue : null,
client_id: this.clientId,
client_secret: this.clientSecret,
}
const params = subjectKey && subjectValue ? `?${subjectKey}=${subjectValue}` : "";
const url = `/v1/oauth2/token${params}`;
const headers = this.getOAuthHeaders();
const responseBody = await this.http.post<OAuth2TokenRequest, OAuth2TokenResponse>(url, requestBody, headers);
if (!responseBody) {
return null;
}
return responseBody;
}
/**
* Get information about the current client.
* @returns Client | null
*/
public async getClientInfo(): Promise<any | null> {
return this.getClientById(this.clientId);
}
/**
* Get information about the current user.
* @returns User | null
*/
public async getUserInfo(): Promise<any | null> {
if (!this.options.accessToken) {
return null;
}
const headers = this.getHeaders();
const responseBody = await this.http.get<any>(`/v1/oauth2/userinfo`, headers);
if (!responseBody) {
return null;
}
return responseBody;
constructor() {
super("http://localhost:5000", { fetch });
}
}

View File

@ -1,26 +0,0 @@
// Copyright (c) Tribufu. All Rights Reserved.
// SPDX-License-Identifier: MIT
/**
* The version of the Tribufu SDK.
* @type {string}
*/
export const TRIBUFU_VERSION = "0.1.13";
/**
* The default Tribufu API URL.
* @type {string}
*/
export const TRIBUFU_API_URL = "https://api.tribufu.com";
/**
* The default Tribufu WEB URL.
* @type {string}
*/
export const TRIBUFU_WEB_URL = "https://www.tribufu.com";
/**
* The default Tribufu CDN URL.
* @type {string}
*/
export const TRIBUFU_CDN_URL = "https://cdn.tribufu.com";

View File

@ -1,30 +1,8 @@
// Copyright (c) Tribufu. All Rights Reserved.
// SPDX-License-Identifier: MIT
import {
TRIBUFU_API_URL,
TRIBUFU_CDN_URL,
TRIBUFU_VERSION,
TRIBUFU_WEB_URL,
} from "./constants";
export {
TRIBUFU_API_URL,
TRIBUFU_CDN_URL,
TRIBUFU_VERSION,
TRIBUFU_WEB_URL,
};
import { TribufuApi } from "./api";
import { TribufuApiOptions } from "./options";
import { TribufuBot } from "./bot";
import { TribufuClient } from "./client";
import { TribufuServer } from "./server";
export {
TribufuApi,
TribufuApiOptions,
TribufuBot,
TribufuClient,
TribufuServer,
};

View File

@ -1,22 +0,0 @@
// 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
}

View File

@ -1,16 +0,0 @@
// 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,8 +0,0 @@
// Copyright (c) Tribufu. All Rights Reserved.
// SPDX-License-Identifier: MIT
export enum JavaScriptRuntime {
Browser,
Node,
Other,
}

View File

@ -1,9 +0,0 @@
// Copyright (c) Tribufu. All Rights Reserved.
// SPDX-License-Identifier: MIT
export interface TribufuApiOptions {
apiKey?: string | null;
accessToken?: string | null;
refreshToken?: string | null;
expiresIn?: number | null;
}

View File

@ -1,74 +0,0 @@
// Copyright (c) Tribufu. All Rights Reserved.
// SPDX-License-Identifier: MIT
import { TribufuClient } from "./client";
/**
* **Tribufu Server**
*
* To authenticate a server you need to use the server id, client id and client secret obtained from your server subscription.
*
* - A server is a special type of client application.
* - A server is how game servers interact with the Tribufu API.
* - A server give you read and write access to the Tribufu API.
*/
export class TribufuServer extends TribufuClient {
private readonly serverId: string;
constructor(serverId: string, clientId: string, clientSecret: string) {
super(clientId, clientSecret);
this.serverId = serverId;
}
/**
* Try to create a TribufuServer from environment variables.
*
* - This will only work if the environment variables are set.
*
* @param prefix A prefix for the environment variables.
* @returns TribufuServer | null
* @example
* ```ts
* // process.env.TRIBUFU_SERVER_ID
* // process.env.TRIBUFU_CLIENT_ID
* // process.env.TRIBUFU_CLIENT_SECRET
* const server = TribufuServer.fromEnv("TRIBUFU");
* ```
*/
public static override fromEnv(prefix?: string | null): TribufuServer | null {
const envPrefix = prefix ? `${prefix}_` : "";
const serverId = process.env[`${envPrefix}SERVER_ID`];
const clientId = process.env[`${envPrefix}CLIENT_ID`];
const clientSecret = process.env[`${envPrefix}CLIENT_SECRET`];
if (serverId && clientId && clientSecret) {
return new TribufuServer(serverId, clientId, clientSecret);
}
return null;
}
/**
* Get the server id.
* @returns string
*/
public getServerId(): string {
return this.serverId;
}
/**
* Get information about the current server.
* @returns Server | null
*/
public async getServerInfo(): Promise<any | null> {
return this.getServerById(this.serverId);
}
/**
* Get a list of connected users.
* @returns
*/
public async getConnectedUsers(): Promise<any[]> {
return [];
}
}

View File

@ -1,12 +0,0 @@
// Copyright (c) Tribufu. All Rights Reserved.
// SPDX-License-Identifier: MIT
/**
* Tribufu Socket
*
* Helper class to connect to the Tribufu Socket API.
*/
export class TribufuSocket {
constructor() {
}
}

View File

@ -1,20 +0,0 @@
// Copyright (c) Tribufu. All Rights Reserved.
// SPDX-License-Identifier: MIT
export type TokenType = "user" | "bot" | "client" | "server";
export interface TokenPayload {
jti: string;
type: TokenType;
iss: string;
aud: string;
client_id: string;
scope: string;
user_id?: string;
bot_id?: string;
private_flags?: string;
public_flags?: string;
server_id?: string;
iat: number;
exp: number;
}