1 Commits

Author SHA1 Message Date
4d657a4617 Add translate api client 2025-05-06 14:50:12 -03:00
12 changed files with 248 additions and 98 deletions

View File

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

View File

@ -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>",

View File

@ -1,3 +1,4 @@
#!/usr/bin/env sh
nswag run ./src/api/api.nswag
nswag run ./src/translate/translate.nswag

View File

@ -4,7 +4,7 @@
"documentGenerator": {
"fromDocument": {
"json": "",
"url": "https://api.tribufu.com/v1/openapi.json",
"url": "http://localhost:5000/openapi.json",
"output": null,
"newLineBehavior": "Auto"
}
@ -48,7 +48,7 @@
"useLeafType": false,
"classTypes": [],
"extendedClasses": [],
"extensionCode": "include.ts",
"extensionCode": "../core/include.ts",
"generateDefaultValues": true,
"excludedTypeNames": [],
"excludedParameterNames": [],

View File

@ -8,7 +8,7 @@
/* eslint-disable */
// ReSharper disable InconsistentNaming
import { TribufuApiBase } from "./base";
import { TribufuApiBase } from "../core/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;

View File

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

View File

@ -2,7 +2,7 @@
// SPDX-License-Identifier: MIT
import { TRIBUFU_API_URL } from "..";
import { TribufuApiBase } from "./base";
import { TribufuApiBase } from "../core/base";
import { TribufuApiGenerated } from "./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;
}
}

View File

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

1
src/core/include.ts Normal file
View File

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

View File

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

View File

@ -0,0 +1,84 @@
//----------------------
// <auto-generated>
// Generated using the NSwag toolchain v14.1.0.0 (NJsonSchema v11.0.2.0 (Newtonsoft.Json v13.0.0.0)) (http://NSwag.org)
// </auto-generated>
//----------------------
/* tslint:disable */
/* eslint-disable */
// ReSharper disable InconsistentNaming
import { TribufuApiBase } from "../core/base";
export class TribufuTranslateGenerated extends TribufuApiBase {
private http: { fetch(url: RequestInfo, init?: RequestInit): Promise<Response> };
private baseUrl: string;
protected jsonParseReviver: ((key: string, value: any) => any) | undefined = undefined;
constructor(baseUrl?: string, http?: { fetch(url: RequestInfo, init?: RequestInit): Promise<Response> }) {
super();
this.http = http ? http : window as any;
this.baseUrl = baseUrl ?? "";
}
/**
* Translate a given text.
*/
translate(): Promise<void> {
let url_ = this.baseUrl + "/translate";
url_ = url_.replace(/[?&]$/, "");
let options_: RequestInit = {
method: "POST",
headers: {
}
};
return this.transformOptions(options_).then(transformedOptions_ => {
return this.http.fetch(url_, transformedOptions_);
}).then((_response: Response) => {
return this.processTranslate(_response);
});
}
protected processTranslate(response: Response): Promise<void> {
const status = response.status;
let _headers: any = {}; if (response.headers && response.headers.forEach) { response.headers.forEach((v: any, k: any) => _headers[k] = v); };
{
return response.text().then((_responseText) => {
return;
});
}
}
}
export class TribufuTranslateError extends Error {
override message: string;
status: number;
response: string;
headers: { [key: string]: any; };
result: any;
constructor(message: string, status: number, response: string, headers: { [key: string]: any; }, result: any) {
super();
this.message = message;
this.status = status;
this.response = response;
this.headers = headers;
this.result = result;
}
protected isTribufuTranslateError = true;
static isTribufuTranslateError(obj: any): obj is TribufuTranslateError {
return obj.isTribufuTranslateError === true;
}
}
function throwException(message: string, status: number, response: string, headers: { [key: string]: any; }, result?: any): any {
if (result !== null && result !== undefined)
throw result;
else
throw new TribufuTranslateError(message, status, response, headers, null);
}

View File

@ -0,0 +1,74 @@
{
"runtime": "Default",
"defaultVariables": null,
"documentGenerator": {
"fromDocument": {
"json": "",
"url": "http://localhost:5578/openapi.json",
"output": null,
"newLineBehavior": "Auto"
}
},
"codeGenerators": {
"openApiToTypeScriptClient": {
"className": "TribufuTranslateGenerated",
"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": "TribufuTranslateError",
"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": "../core/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": "generated.ts",
"newLineBehavior": "LF"
}
}
}