mirror of
https://github.com/tribufu/sdk-js
synced 2025-06-16 10:44:17 +00:00
Compare commits
1 Commits
Author | SHA1 | Date | |
---|---|---|---|
d4e9f48751 |
@ -1,17 +1,15 @@
|
||||
root = true
|
||||
|
||||
[*]
|
||||
charset = utf-8
|
||||
end_of_line = lf
|
||||
indent_size = 4
|
||||
indent_style = space
|
||||
insert_final_newline = true
|
||||
max_line_length = 120
|
||||
indent_size = 4
|
||||
charset = utf-8
|
||||
trim_trailing_whitespace = true
|
||||
insert_final_newline = true
|
||||
|
||||
[*.md]
|
||||
indent_size = 1
|
||||
trim_trailing_whitespace = false
|
||||
|
||||
[.env*]
|
||||
[*.env*]
|
||||
insert_final_newline = false
|
||||
|
14
examples/client.js
Normal file
14
examples/client.js
Normal file
@ -0,0 +1,14 @@
|
||||
// Copyright (c) Tribufu. All Rights Reserved.
|
||||
// SPDX-License-Identifier: MIT AND Apache-2.0
|
||||
|
||||
import dotenv from "dotenv";
|
||||
import { TribufuClient } from "../build/index.mjs";
|
||||
|
||||
dotenv.config();
|
||||
|
||||
async function main() {
|
||||
const tribufu = TribufuClient.fromEnv();
|
||||
console.log(await tribufu.getClientInfo());
|
||||
}
|
||||
|
||||
main();
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "tribufu",
|
||||
"version": "1.0.1",
|
||||
"version": "1.0.0",
|
||||
"description": "Tribufu JS SDK",
|
||||
"repository": "https://github.com/Tribufu/TribufuJs",
|
||||
"author": "Tribufu <contact@Tribufu.com>",
|
||||
|
@ -92,10 +92,12 @@ export abstract class TribufuApiBase {
|
||||
* @returns
|
||||
*/
|
||||
protected transformOptions(options: RequestInit) {
|
||||
options.headers = {
|
||||
...options.headers,
|
||||
...this.getHeaders(),
|
||||
};
|
||||
if (this.apiKey) {
|
||||
options.headers = {
|
||||
...options.headers,
|
||||
...this.getHeaders(),
|
||||
};
|
||||
}
|
||||
|
||||
return Promise.resolve(options);
|
||||
}
|
@ -8,7 +8,7 @@
|
||||
/* eslint-disable */
|
||||
// ReSharper disable InconsistentNaming
|
||||
|
||||
import { TribufuApiBase } from "./base";
|
||||
import { TribufuApiBase } from "./api.base";
|
||||
|
||||
export class TribufuApiGenerated extends TribufuApiBase {
|
||||
private http: { fetch(url: RequestInfo, init?: RequestInit): Promise<Response> };
|
||||
@ -447,7 +447,7 @@ export class TribufuApiGenerated extends TribufuApiBase {
|
||||
* Get a list of games.
|
||||
* @return OK
|
||||
*/
|
||||
getGames(): Promise<Game[]> {
|
||||
getGames(): Promise<Application[]> {
|
||||
let url_ = this.baseUrl + "/v1/games";
|
||||
url_ = url_.replace(/[?&]$/, "");
|
||||
|
||||
@ -465,13 +465,13 @@ export class TribufuApiGenerated extends TribufuApiBase {
|
||||
});
|
||||
}
|
||||
|
||||
protected processGetGames(response: Response): Promise<Game[]> {
|
||||
protected processGetGames(response: Response): Promise<Application[]> {
|
||||
const status = response.status;
|
||||
let _headers: any = {}; if (response.headers && response.headers.forEach) { response.headers.forEach((v: any, k: any) => _headers[k] = v); };
|
||||
if (status === 200) {
|
||||
return response.text().then((_responseText) => {
|
||||
let result200: any = null;
|
||||
result200 = _responseText === "" ? null : JSON.parse(_responseText, this.jsonParseReviver) as Game[];
|
||||
result200 = _responseText === "" ? null : JSON.parse(_responseText, this.jsonParseReviver) as Application[];
|
||||
return result200;
|
||||
});
|
||||
} else if (status !== 200 && status !== 204) {
|
||||
@ -479,14 +479,14 @@ export class TribufuApiGenerated extends TribufuApiBase {
|
||||
return throwException("An unexpected server error occurred.", status, _responseText, _headers);
|
||||
});
|
||||
}
|
||||
return Promise.resolve<Game[]>(null as any);
|
||||
return Promise.resolve<Application[]>(null as any);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a game by id.
|
||||
* @return OK
|
||||
*/
|
||||
getGameById(id: string): Promise<Game> {
|
||||
getGameById(id: string): Promise<Application> {
|
||||
let url_ = this.baseUrl + "/v1/games/{id}";
|
||||
if (id === undefined || id === null)
|
||||
throw new Error("The parameter 'id' must be defined.");
|
||||
@ -507,13 +507,13 @@ export class TribufuApiGenerated extends TribufuApiBase {
|
||||
});
|
||||
}
|
||||
|
||||
protected processGetGameById(response: Response): Promise<Game> {
|
||||
protected processGetGameById(response: Response): Promise<Application> {
|
||||
const status = response.status;
|
||||
let _headers: any = {}; if (response.headers && response.headers.forEach) { response.headers.forEach((v: any, k: any) => _headers[k] = v); };
|
||||
if (status === 200) {
|
||||
return response.text().then((_responseText) => {
|
||||
let result200: any = null;
|
||||
result200 = _responseText === "" ? null : JSON.parse(_responseText, this.jsonParseReviver) as Game;
|
||||
result200 = _responseText === "" ? null : JSON.parse(_responseText, this.jsonParseReviver) as Application;
|
||||
return result200;
|
||||
});
|
||||
} else if (status !== 200 && status !== 204) {
|
||||
@ -521,7 +521,7 @@ export class TribufuApiGenerated extends TribufuApiBase {
|
||||
return throwException("An unexpected server error occurred.", status, _responseText, _headers);
|
||||
});
|
||||
}
|
||||
return Promise.resolve<Game>(null as any);
|
||||
return Promise.resolve<Application>(null as any);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -2713,28 +2713,7 @@ export interface Account {
|
||||
updated: string | null;
|
||||
}
|
||||
|
||||
export enum ApplicationType {
|
||||
Application = "application",
|
||||
Game = "game",
|
||||
}
|
||||
|
||||
export interface CryptoViewModel {
|
||||
encoded: string | null;
|
||||
decoded: string | null;
|
||||
}
|
||||
|
||||
export interface Game {
|
||||
game_port: number | null;
|
||||
query_port: number | null;
|
||||
rcon_port: number | null;
|
||||
server_count: number;
|
||||
steam_app_id: number | null;
|
||||
steam_server_app_id: number | null;
|
||||
enable_servers: boolean;
|
||||
rust_gamedig_id: string | null;
|
||||
node_gamedig_id: string | null;
|
||||
server_connect_url: string | null;
|
||||
server_tags: string | null;
|
||||
export interface Application {
|
||||
id: string;
|
||||
name: string | null;
|
||||
description: string | null;
|
||||
@ -2757,6 +2736,16 @@ export interface Game {
|
||||
updated: string | null;
|
||||
}
|
||||
|
||||
export enum ApplicationType {
|
||||
Application = "application",
|
||||
Game = "game",
|
||||
}
|
||||
|
||||
export interface CryptoViewModel {
|
||||
encoded: string | null;
|
||||
decoded: string | null;
|
||||
}
|
||||
|
||||
export interface GameServer {
|
||||
id: string;
|
||||
name: string | null;
|
1
src/api/api.include.ts
Normal file
1
src/api/api.include.ts
Normal file
@ -0,0 +1 @@
|
||||
import { TribufuApiBase } from "./api.base";
|
@ -4,7 +4,7 @@
|
||||
"documentGenerator": {
|
||||
"fromDocument": {
|
||||
"json": "",
|
||||
"url": "https://api.tribufu.com/v1/openapi.json",
|
||||
"url": "http://localhost:5000/v1/openapi.json",
|
||||
"output": null,
|
||||
"newLineBehavior": "Auto"
|
||||
}
|
||||
@ -48,7 +48,7 @@
|
||||
"useLeafType": false,
|
||||
"classTypes": [],
|
||||
"extendedClasses": [],
|
||||
"extensionCode": "include.ts",
|
||||
"extensionCode": "api.include.ts",
|
||||
"generateDefaultValues": true,
|
||||
"excludedTypeNames": [],
|
||||
"excludedParameterNames": [],
|
||||
@ -67,7 +67,7 @@
|
||||
"templateDirectory": null,
|
||||
"serviceHost": null,
|
||||
"serviceSchemes": null,
|
||||
"output": "generated.ts",
|
||||
"output": "api.generated.ts",
|
||||
"newLineBehavior": "LF"
|
||||
}
|
||||
}
|
||||
|
133
src/api/client.ts
Normal file
133
src/api/client.ts
Normal file
@ -0,0 +1,133 @@
|
||||
// Copyright (c) Tribufu. All Rights Reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import { TribufuApi } from ".";
|
||||
import { GrantType, TokenHintType } from "./generated";
|
||||
|
||||
/**
|
||||
* **Tribufu Client**
|
||||
*
|
||||
* Use this class to interact with Tribufu OAuth service.
|
||||
*/
|
||||
export class TribufuClient extends TribufuApi {
|
||||
private clientId: string | null = null;
|
||||
private clientSecret: string | null = null;
|
||||
|
||||
private accessToken: string | null = null;
|
||||
private refreshToken: string | null = null;
|
||||
private expiresIn: number = 0;
|
||||
|
||||
constructor(clientId: string, clientSecret: string) {
|
||||
if (!clientId || !clientSecret) {
|
||||
throw new Error("ClientId and ClientSecret are required");
|
||||
}
|
||||
|
||||
super({ credentials: `Basic ${Buffer.from(`${clientId}:${clientSecret}`).toString("base64")}` });
|
||||
|
||||
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. Default is `TRIBUFU`.
|
||||
* @returns TribufuClient | null
|
||||
* @example
|
||||
* ```ts
|
||||
* // process.env.TRIBUFU_CLIENT_ID
|
||||
* // process.env.TRIBUFU_CLIENT_SECRET
|
||||
* const client = TribufuClient.fromEnv();
|
||||
* ```
|
||||
*/
|
||||
public static fromEnv(prefix?: string | null): TribufuApi | null {
|
||||
if (typeof process === "undefined") {
|
||||
return null;
|
||||
}
|
||||
|
||||
const clientId = process.env[`${prefix || "TRIBUFU"}_CLIENT_ID`];
|
||||
const clientSecret = process.env[`${prefix || "TRIBUFU"}_CLIENT_SECRET`];
|
||||
if (clientId && clientSecret) {
|
||||
return new TribufuClient(clientId, clientSecret);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private useClientCredentials(): void {
|
||||
if (this.clientId && this.clientSecret) {
|
||||
this.credentials = `Basic ${Buffer.from(`${this.clientId}:${this.clientSecret}`).toString("base64")}`;
|
||||
}
|
||||
}
|
||||
|
||||
private useBearerCredentials(): void {
|
||||
if (this.accessToken) {
|
||||
this.credentials = `Bearer ${this.accessToken}`;
|
||||
}
|
||||
}
|
||||
|
||||
public async login(username: string, password: string): Promise<boolean> {
|
||||
this.useClientCredentials();
|
||||
|
||||
const response = await this.createToken({
|
||||
grant_type: GrantType.Password,
|
||||
code: null,
|
||||
username,
|
||||
password,
|
||||
refresh_token: null,
|
||||
client_id: this.clientId,
|
||||
redirect_uri: null,
|
||||
code_verifier: null,
|
||||
});
|
||||
|
||||
if (response) {
|
||||
this.accessToken = response.access_token;
|
||||
this.refreshToken = response.refresh_token;
|
||||
this.expiresIn = response.expires_in;
|
||||
this.useBearerCredentials();
|
||||
}
|
||||
|
||||
return !!response;
|
||||
}
|
||||
|
||||
public async refresh(): Promise<boolean> {
|
||||
this.useClientCredentials();
|
||||
|
||||
const response = await this.createToken({
|
||||
grant_type: GrantType.Refresh_token,
|
||||
code: null,
|
||||
username: null,
|
||||
password: null,
|
||||
refresh_token: this.refreshToken,
|
||||
client_id: this.clientId,
|
||||
redirect_uri: null,
|
||||
code_verifier: null,
|
||||
});
|
||||
|
||||
if (response) {
|
||||
this.accessToken = response.access_token;
|
||||
this.refreshToken = response.refresh_token;
|
||||
this.expiresIn = response.expires_in;
|
||||
this.useBearerCredentials();
|
||||
}
|
||||
|
||||
return !!response;
|
||||
}
|
||||
|
||||
public logout(): Promise<any> {
|
||||
this.useClientCredentials();
|
||||
|
||||
const response = this.revokeToken({
|
||||
token: this.refreshToken,
|
||||
token_type_hint: TokenHintType.Refresh_token,
|
||||
});
|
||||
|
||||
this.accessToken = null;
|
||||
this.refreshToken = null;
|
||||
this.expiresIn = 0;
|
||||
|
||||
return response;
|
||||
}
|
||||
}
|
@ -1 +0,0 @@
|
||||
import { TribufuApiBase } from "./base";
|
@ -2,8 +2,8 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import { TRIBUFU_API_URL } from "..";
|
||||
import { TribufuApiBase } from "./base";
|
||||
import { TribufuApiGenerated } from "./generated";
|
||||
import { TribufuApiBase } from "./api.base";
|
||||
import { TribufuApiGenerated } from "./api.generated";
|
||||
import { TribufuApiOptions } from "../options";
|
||||
|
||||
/**
|
||||
@ -98,6 +98,8 @@ export class TribufuApi extends TribufuApiGenerated {
|
||||
}
|
||||
|
||||
const baseUrl = process.env[`TRIBUFU_API_URL`] || null;
|
||||
return TribufuApiBase.debugEnabled() && baseUrl ? baseUrl : TRIBUFU_API_URL;
|
||||
return TribufuApiBase.debugEnabled() && baseUrl
|
||||
? baseUrl
|
||||
: TRIBUFU_API_URL;
|
||||
}
|
||||
}
|
||||
|
@ -22,9 +22,9 @@ export {
|
||||
TribufuApiSingleton,
|
||||
};
|
||||
|
||||
export * from "./api/base";
|
||||
export * from "./api/generated";
|
||||
export * from "./api/include";
|
||||
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";
|
||||
|
Reference in New Issue
Block a user