mirror of
https://github.com/tribufu/sdk-js
synced 2025-06-16 10:44:17 +00:00
Compare commits
33 Commits
Author | SHA1 | Date | |
---|---|---|---|
ea9d87f5ab | |||
f8d2153123 | |||
76cd15fc4c | |||
9c54143d2e | |||
10a0619f80 | |||
5081c11ad5 | |||
3bc568dcb4 | |||
864b46c4a2 | |||
1ab428cfd7 | |||
f02a66c09c | |||
1cf38a35de | |||
be08efe49f | |||
009dfc04ab | |||
962a3de36a | |||
55b4e64594 | |||
b0788d5f72 | |||
14572419cf | |||
db828be54a | |||
b48ee646e1 | |||
80ec992b89 | |||
a2fc56a98f | |||
10cd001665 | |||
93d89995b5 | |||
b5d1e086f3 | |||
332c0b2fa6 | |||
e04da0a06c | |||
a3dfa0d4f4 | |||
6a8fe8d988 | |||
744c6771b3 | |||
c441ff7ff5 | |||
2a3c9854e6 | |||
6d631ffea8 | |||
4186350f7e |
1
.prettierignore
Normal file
1
.prettierignore
Normal file
@ -0,0 +1 @@
|
||||
src/api.generated.ts
|
@ -1,15 +1,16 @@
|
||||
// Copyright (c) Tribufu. All Rights Reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
// SPDX-License-Identifier: MIT AND Apache-2.0
|
||||
|
||||
import dotenv from 'dotenv';
|
||||
import { TribufuApi } from '../build/index.mjs';
|
||||
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]);
|
||||
const tribufu = TribufuApi.fromEnv();
|
||||
console.log(
|
||||
await tribufu.getServerByAddressAndQueryPort("mine.tribufu.com", 25565),
|
||||
);
|
||||
}
|
||||
|
||||
main();
|
||||
|
@ -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();
|
@ -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();
|
@ -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();
|
19
package.json
19
package.json
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "tribufu",
|
||||
"version": "0.1.17",
|
||||
"version": "1.0.0",
|
||||
"description": "Tribufu JS SDK",
|
||||
"repository": "https://github.com/Tribufu/TribufuJs",
|
||||
"author": "Tribufu <contact@Tribufu.com>",
|
||||
@ -9,21 +9,28 @@
|
||||
"types": "./build/index.d.ts",
|
||||
"exports": {
|
||||
"import": "./build/index.mjs",
|
||||
"require": "./build/index.cjs"
|
||||
"require": "./build/index.cjs",
|
||||
"types": "./build/index.d.ts"
|
||||
},
|
||||
"typesVersions": {
|
||||
"*": {
|
||||
"*": [
|
||||
"./build/*"
|
||||
]
|
||||
}
|
||||
},
|
||||
"scripts": {
|
||||
"clean": "rimraf build",
|
||||
"build": "npm run clean && tsc && node scripts/esbuild.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"@tribufu/mintaka": "0.1.8"
|
||||
},
|
||||
"dependencies": {},
|
||||
"devDependencies": {
|
||||
"@types/node": "^20.10.6",
|
||||
"cross-env": "^7.0.3",
|
||||
"dotenv": "^16.3.1",
|
||||
"esbuild": "^0.19.10",
|
||||
"esbuild-node-externals": "^1.12.0",
|
||||
"eslint": "9.11.1",
|
||||
"prettier": "3.3.3",
|
||||
"rimraf": "^5.0.5",
|
||||
"typescript": "^5.3.3"
|
||||
}
|
||||
|
1454
pnpm-lock.yaml
generated
1454
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
@ -22,12 +22,26 @@ const moduleConfig = {
|
||||
format: "esm",
|
||||
};
|
||||
|
||||
const moduleMinConfig = {
|
||||
...moduleConfig,
|
||||
outfile: "build/index.min.mjs",
|
||||
minify: true,
|
||||
sourcemap: true,
|
||||
};
|
||||
|
||||
const legacyConfig = {
|
||||
...baseConfig,
|
||||
outfile: "build/index.cjs",
|
||||
format: "cjs",
|
||||
};
|
||||
|
||||
const legacyMinConfig = {
|
||||
...legacyConfig,
|
||||
outfile: "build/index.min.cjs",
|
||||
minify: true,
|
||||
sourcemap: true,
|
||||
};
|
||||
|
||||
async function addCopyrightHeader(filename) {
|
||||
const header = `// Copyright (c) Tribufu. All Rights Reserved.\n// SPDX-License-Identifier: MIT\n\n`;
|
||||
const content = await fs.readFile(filename, 'utf-8');
|
||||
@ -44,5 +58,8 @@ async function buildAndAddHeader(config) {
|
||||
}
|
||||
};
|
||||
|
||||
await buildAndAddHeader(moduleConfig);
|
||||
await buildAndAddHeader(legacyConfig);
|
||||
await buildAndAddHeader(moduleConfig);
|
||||
|
||||
//await buildAndAddHeader(legacyMinConfig);
|
||||
//await buildAndAddHeader(moduleMinConfig);
|
||||
|
3
scripts/nswag.ps1
Normal file
3
scripts/nswag.ps1
Normal file
@ -0,0 +1,3 @@
|
||||
#!/usr/bin/env sh
|
||||
|
||||
nswag run ./src/api/api.nswag
|
544
src/api.ts
544
src/api.ts
@ -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;
|
||||
}
|
||||
}
|
104
src/api/api.base.ts
Normal file
104
src/api/api.base.ts
Normal file
@ -0,0 +1,104 @@
|
||||
// Copyright (c) Tribufu. All Rights Reserved.
|
||||
// SPDX-License-Identifier: UNLICENSED
|
||||
|
||||
import { HttpHeaders } from "../http/headers";
|
||||
import { JavaScriptRuntime } from "../node";
|
||||
import { TRIBUFU_VERSION } from "..";
|
||||
|
||||
export abstract class TribufuApiBase {
|
||||
protected apiKey: string | null = null;
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
public static debugEnabled(): boolean {
|
||||
if (typeof process !== "undefined") {
|
||||
return process.env.NODE_ENV === "development";
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Detect the current JavaScript runtime.
|
||||
*
|
||||
* - This is used to determine if the code is running in a browser or in Node.js.
|
||||
*
|
||||
* @returns JavaScriptRuntime
|
||||
*/
|
||||
public 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 TribufuApiBase.detectRuntime() === JavaScriptRuntime.Browser;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the current JavaScript runtime is Node.js.
|
||||
* @returns boolean
|
||||
*/
|
||||
public static isNode(): boolean {
|
||||
return TribufuApiBase.detectRuntime() === JavaScriptRuntime.Node;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the default headers for the Tribufu API.
|
||||
* @returns HeaderMap
|
||||
*/
|
||||
protected static defaultHeaders(): HttpHeaders {
|
||||
const headers = {};
|
||||
headers["X-Tribufu-Library"] = "javascript";
|
||||
headers["X-Tribufu-Version"] = TRIBUFU_VERSION;
|
||||
return headers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get current headers with the api key or access token.
|
||||
* @returns HeaderMap
|
||||
*/
|
||||
protected getHeaders(): HttpHeaders {
|
||||
let headers = TribufuApiBase.defaultHeaders();
|
||||
|
||||
if (this.apiKey) {
|
||||
headers["Authorization"] = `ApiKey ${this.apiKey}`;
|
||||
return headers;
|
||||
}
|
||||
|
||||
return headers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transform the options before sending the request.
|
||||
* @param options
|
||||
* @returns
|
||||
*/
|
||||
protected transformOptions(options: RequestInit) {
|
||||
if (this.apiKey) {
|
||||
options.headers = {
|
||||
...options.headers,
|
||||
...this.getHeaders(),
|
||||
};
|
||||
}
|
||||
|
||||
return Promise.resolve(options);
|
||||
}
|
||||
}
|
3064
src/api/api.generated.ts
Normal file
3064
src/api/api.generated.ts
Normal file
File diff suppressed because it is too large
Load Diff
1
src/api/api.include.ts
Normal file
1
src/api/api.include.ts
Normal file
@ -0,0 +1 @@
|
||||
import { TribufuApiBase } from "./api.base";
|
74
src/api/api.nswag
Normal file
74
src/api/api.nswag
Normal file
@ -0,0 +1,74 @@
|
||||
{
|
||||
"runtime": "Default",
|
||||
"defaultVariables": null,
|
||||
"documentGenerator": {
|
||||
"fromDocument": {
|
||||
"json": "",
|
||||
"url": "http://localhost:5000/v1/openapi.json",
|
||||
"output": null,
|
||||
"newLineBehavior": "Auto"
|
||||
}
|
||||
},
|
||||
"codeGenerators": {
|
||||
"openApiToTypeScriptClient": {
|
||||
"className": "TribufuApiGenerated",
|
||||
"moduleName": "",
|
||||
"namespace": "",
|
||||
"typeScriptVersion": 4.3,
|
||||
"template": "Fetch",
|
||||
"promiseType": "Promise",
|
||||
"httpClass": "HttpClient",
|
||||
"withCredentials": false,
|
||||
"useSingletonProvider": false,
|
||||
"injectionTokenType": "OpaqueToken",
|
||||
"rxJsVersion": 6,
|
||||
"dateTimeType": "String",
|
||||
"nullValue": "Null",
|
||||
"generateClientClasses": true,
|
||||
"generateClientInterfaces": false,
|
||||
"generateOptionalParameters": true,
|
||||
"exportTypes": true,
|
||||
"wrapDtoExceptions": false,
|
||||
"exceptionClass": "TribufuApiError",
|
||||
"clientBaseClass": "TribufuApiBase",
|
||||
"wrapResponses": false,
|
||||
"wrapResponseMethods": [],
|
||||
"generateResponseClasses": true,
|
||||
"responseClass": "SwaggerResponse",
|
||||
"protectedMethods": [],
|
||||
"configurationClass": null,
|
||||
"useTransformOptionsMethod": true,
|
||||
"useTransformResultMethod": false,
|
||||
"generateDtoTypes": true,
|
||||
"operationGenerationMode": "SingleClientFromOperationId",
|
||||
"markOptionalProperties": false,
|
||||
"generateCloneMethod": false,
|
||||
"typeStyle": "Interface",
|
||||
"enumStyle": "Enum",
|
||||
"useLeafType": false,
|
||||
"classTypes": [],
|
||||
"extendedClasses": [],
|
||||
"extensionCode": "api.include.ts",
|
||||
"generateDefaultValues": true,
|
||||
"excludedTypeNames": [],
|
||||
"excludedParameterNames": [],
|
||||
"handleReferences": false,
|
||||
"generateTypeCheckFunctions": false,
|
||||
"generateConstructorInterface": true,
|
||||
"convertConstructorInterfaceData": false,
|
||||
"importRequiredTypes": true,
|
||||
"useGetBaseUrlMethod": false,
|
||||
"baseUrlTokenName": "API_BASE_URL",
|
||||
"queryNullValue": "",
|
||||
"useAbortSignal": false,
|
||||
"inlineNamedDictionaries": false,
|
||||
"inlineNamedAny": false,
|
||||
"includeHttpContext": false,
|
||||
"templateDirectory": null,
|
||||
"serviceHost": null,
|
||||
"serviceSchemes": null,
|
||||
"output": "api.generated.ts",
|
||||
"newLineBehavior": "LF"
|
||||
}
|
||||
}
|
||||
}
|
105
src/api/index.ts
Normal file
105
src/api/index.ts
Normal file
@ -0,0 +1,105 @@
|
||||
// Copyright (c) Tribufu. All Rights Reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import { TRIBUFU_API_URL } from "..";
|
||||
import { TribufuApiBase } from "./api.base";
|
||||
import { TribufuApiGenerated } from "./api.generated";
|
||||
import { TribufuApiOptions } from "../options";
|
||||
|
||||
/**
|
||||
* **Tribufu API**
|
||||
*
|
||||
* Use this class to interact with the Tribufu API.
|
||||
*/
|
||||
export class TribufuApi extends TribufuApiGenerated {
|
||||
constructor(options?: TribufuApiOptions | null) {
|
||||
const baseUrl = options?.baseUrl || TribufuApi.getBaseUrl();
|
||||
const http = options?.fetch ? { fetch: options.fetch } : { fetch };
|
||||
super(baseUrl, http);
|
||||
this.apiKey = options?.apiKey || null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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. Default is `TRIBUFU`.
|
||||
* @returns TribufuApi | null
|
||||
* @example
|
||||
* ```ts
|
||||
* // process.env.TRIBUFU_API_KEY
|
||||
* const api = TribufuApi.fromEnv();
|
||||
* ```
|
||||
*/
|
||||
public static fromEnv(prefix?: string | null): TribufuApi | null {
|
||||
if (typeof process === "undefined") {
|
||||
return null;
|
||||
}
|
||||
|
||||
const apiKey = process.env[`${prefix || "TRIBUFU"}_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. Default is `TRIBUFU`.
|
||||
* @returns TribufuApi | null
|
||||
* @example
|
||||
* ```ts
|
||||
* // process.env.TRIBUFU_API_KEY = null
|
||||
* const api = TribufuApi.fromEnvOrDefault();
|
||||
* ```
|
||||
*/
|
||||
public static fromEnvOrDefault(prefix: string = ""): TribufuApi {
|
||||
return TribufuApi.fromEnv(prefix) || TribufuApi.default();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
protected static getBaseUrl(): string {
|
||||
if (typeof process === "undefined") {
|
||||
return TRIBUFU_API_URL;
|
||||
}
|
||||
|
||||
const baseUrl = process.env[`TRIBUFU_API_URL`] || null;
|
||||
return TribufuApiBase.debugEnabled() && baseUrl
|
||||
? baseUrl
|
||||
: TRIBUFU_API_URL;
|
||||
}
|
||||
}
|
23
src/api/singletion.ts
Normal file
23
src/api/singletion.ts
Normal file
@ -0,0 +1,23 @@
|
||||
// Copyright (c) Tribufu. All Rights Reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import { TribufuApi } from "./index";
|
||||
|
||||
/**
|
||||
* **Tribufu API**
|
||||
*
|
||||
* Helper class to get a singleton instance of the Tribufu API.
|
||||
*/
|
||||
export class TribufuApiSingleton {
|
||||
private static INSTANCE: TribufuApi | null = null;
|
||||
|
||||
private constructor() {}
|
||||
|
||||
public static getInstance(): TribufuApi {
|
||||
if (!TribufuApiSingleton.INSTANCE) {
|
||||
TribufuApiSingleton.INSTANCE = TribufuApi.fromEnvOrDefault();
|
||||
}
|
||||
|
||||
return TribufuApiSingleton.INSTANCE;
|
||||
}
|
||||
}
|
68
src/bot.ts
68
src/bot.ts
@ -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;
|
||||
}
|
||||
}
|
368
src/client.ts
368
src/client.ts
@ -1,368 +0,0 @@
|
||||
// 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";
|
||||
|
||||
/**
|
||||
* **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;
|
||||
}
|
||||
}
|
@ -1,26 +1,29 @@
|
||||
// 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";
|
||||
import packageJson from "../package.json";
|
||||
|
||||
/**
|
||||
* The default Tribufu API URL.
|
||||
* @type {string}
|
||||
* The version of the Tribufu SDK.
|
||||
*/
|
||||
export const TRIBUFU_API_URL = "https://api.tribufu.com";
|
||||
export const TRIBUFU_VERSION: string = packageJson.version;
|
||||
|
||||
/**
|
||||
* The default Tribufu WEB URL.
|
||||
* @type {string}
|
||||
*/
|
||||
export const TRIBUFU_WEB_URL = "https://www.tribufu.com";
|
||||
export const TRIBUFU_WEB_URL: string = "https://www.tribufu.com";
|
||||
|
||||
/**
|
||||
* The default Tribufu API URL.
|
||||
*/
|
||||
export const TRIBUFU_API_URL: string = "https://api.tribufu.com";
|
||||
|
||||
/**
|
||||
* The default Tribufu CDN URL.
|
||||
* @type {string}
|
||||
*/
|
||||
export const TRIBUFU_CDN_URL = "https://cdn.tribufu.com";
|
||||
export const TRIBUFU_CDN_URL: string = "https://cdn.tribufu.com";
|
||||
|
||||
/**
|
||||
* Tribufu copyright notice.
|
||||
*/
|
||||
export const TRIBUFU_COPYRIGHT = `© ${new Date().getFullYear()} Tribufu.`;
|
||||
|
11
src/http/headers.ts
Normal file
11
src/http/headers.ts
Normal file
@ -0,0 +1,11 @@
|
||||
// Copyright (c) Tribufu. All Rights Reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
/**
|
||||
* Http Headers
|
||||
*
|
||||
* Helper type to represent HTTP headers.
|
||||
*/
|
||||
export type HttpHeaders = {
|
||||
[key: string]: string;
|
||||
};
|
25
src/index.ts
25
src/index.ts
@ -8,23 +8,24 @@ import {
|
||||
TRIBUFU_WEB_URL,
|
||||
} from "./constants";
|
||||
|
||||
import { TribufuApi } from "./api";
|
||||
import { TribufuApiOptions } from "./options";
|
||||
import { TribufuApiSingleton } from "./api/singletion";
|
||||
|
||||
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,
|
||||
TribufuApiSingleton,
|
||||
};
|
||||
|
||||
export * from "./api/api.base";
|
||||
export * from "./api/api.generated";
|
||||
export * from "./api/api.include";
|
||||
export * from "./api/index";
|
||||
export * from "./api/singletion";
|
||||
export * from "./http/headers";
|
||||
export * from "./node";
|
||||
|
@ -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
|
||||
}
|
@ -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
|
||||
}
|
@ -2,8 +2,7 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
export interface TribufuApiOptions {
|
||||
apiKey?: string | null;
|
||||
accessToken?: string | null;
|
||||
refreshToken?: string | null;
|
||||
expiresIn?: number | null;
|
||||
baseUrl?: string;
|
||||
apiKey?: string;
|
||||
fetch?: (url: RequestInfo, init?: RequestInit) => Promise<Response>;
|
||||
}
|
||||
|
@ -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 [];
|
||||
}
|
||||
}
|
@ -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() {
|
||||
}
|
||||
}
|
20
src/token.ts
20
src/token.ts
@ -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;
|
||||
}
|
@ -1,12 +1,13 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"strict": true,
|
||||
"target": "ESNext",
|
||||
"target": "esnext",
|
||||
"declaration": true,
|
||||
"emitDeclarationOnly": true,
|
||||
"noImplicitAny": false,
|
||||
"moduleResolution": "Node",
|
||||
"esModuleInterop": true,
|
||||
"module": "esnext",
|
||||
"allowImportingTsExtensions": true,
|
||||
"resolveJsonModule": true,
|
||||
"rootDir": "src",
|
||||
|
Reference in New Issue
Block a user