Use nswag generator

This commit is contained in:
2024-10-02 21:03:18 -03:00
parent 332c0b2fa6
commit b5d1e086f3
25 changed files with 3063 additions and 1456 deletions

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
*/
protected 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 files from the Tribufu API.
* @param page
* @returns File[]
*/
public async getFiles(page: number = 1): Promise<any[]> {
const headers = this.getHeaders();
const responseBody = await this.http.get<any[]>(`/v1/files?page=${page}`, headers);
if (!responseBody) {
return [];
}
return responseBody;
}
/**
* Get a file by id from the Tribufu API.
* @param id
* @returns File | null
*/
public async getFileById(id: string): Promise<any> {
const headers = this.getHeaders()
const responseBody = await this.http.get<any>(`/v1/files/${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
View 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 (!process) {
return false;
}
return process.env.NODE_ENV === "development";
}
/**
* 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-Language"] = "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);
}
}

2233
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
View File

@ -0,0 +1 @@
import { TribufuApiBase } from "./api.base";

74
src/api/api.nswag Normal file
View 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": false,
"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": true,
"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"
}
}
}

107
src/api/index.ts Normal file
View File

@ -0,0 +1,107 @@
// 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.
* @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}_` : "";
if (!process) {
return null;
}
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();
}
/**
* 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 {
if (!process) {
return TRIBUFU_API_URL;
}
const baseUrl = process.env[`TRIBUFU_API_URL`] || null;
return TribufuApiBase.debugEnabled() && baseUrl
? baseUrl
: TRIBUFU_API_URL;
}
}

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,386 +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 current headers with the api key or access token.
* @returns HeaderMap
*/
protected override getHeaders(): HttpHeaders {
let headers = TribufuClient.defaultHeaders();
headers.set("Authorization", `Basic ${Buffer.from(`${this.clientId}:${this.clientSecret}`, "binary").toString("base64")}`);
if (this.options.accessToken) {
headers.set("Authorization", `Bearer ${this.options.accessToken}`);
return headers;
}
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;
}
}

View File

@ -1,26 +1,24 @@
// Copyright (c) Tribufu. All Rights Reserved.
// SPDX-License-Identifier: MIT
import packageJson from "../package.json";
/**
* The version of the Tribufu SDK.
* @type {string}
*/
export const TRIBUFU_VERSION = "0.1.13";
export const TRIBUFU_VERSION: string = packageJson.version;
/**
* The default Tribufu API URL.
* @type {string}
*/
export const TRIBUFU_API_URL = "https://api.tribufu.com";
export const TRIBUFU_API_URL: string = "https://api.tribufu.com";
/**
* 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 CDN URL.
* @type {string}
*/
export const TRIBUFU_CDN_URL = "https://cdn.tribufu.com";
export const TRIBUFU_CDN_URL: string = "https://cdn.tribufu.com";

11
src/http/headers.ts Normal file
View 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;
};

View File

@ -8,23 +8,9 @@ import {
TRIBUFU_WEB_URL,
} from "./constants";
export {
TRIBUFU_API_URL,
TRIBUFU_CDN_URL,
TRIBUFU_VERSION,
TRIBUFU_WEB_URL,
};
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,
};
export { TribufuApi, TribufuApiOptions };

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

@ -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>;
}

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;
}