From 4d657a46178a8f7f2347f2b6636ba40c12783b9a Mon Sep 17 00:00:00 2001 From: Guilherme Werner Date: Tue, 6 May 2025 14:50:12 -0300 Subject: [PATCH] Add translate api client --- scripts/nswag.ps1 | 1 + src/api/api.include.ts | 1 - src/api/api.nswag | 6 +- src/api/{api.generated.ts => generated.ts} | 98 +++++++++++----------- src/api/index.ts | 4 +- src/{api/api.base.ts => core/base.ts} | 0 src/core/include.ts | 1 + src/translate/generated.ts | 84 +++++++++++++++++++ src/translate/translate.nswag | 74 ++++++++++++++++ 9 files changed, 214 insertions(+), 55 deletions(-) delete mode 100644 src/api/api.include.ts rename src/api/{api.generated.ts => generated.ts} (98%) rename src/{api/api.base.ts => core/base.ts} (100%) create mode 100644 src/core/include.ts create mode 100644 src/translate/generated.ts create mode 100644 src/translate/translate.nswag diff --git a/scripts/nswag.ps1 b/scripts/nswag.ps1 index ad9ee6b..2939bc6 100644 --- a/scripts/nswag.ps1 +++ b/scripts/nswag.ps1 @@ -1,3 +1,4 @@ #!/usr/bin/env sh nswag run ./src/api/api.nswag +nswag run ./src/translate/translate.nswag diff --git a/src/api/api.include.ts b/src/api/api.include.ts deleted file mode 100644 index e6021f9..0000000 --- a/src/api/api.include.ts +++ /dev/null @@ -1 +0,0 @@ -import { TribufuApiBase } from "./api.base"; diff --git a/src/api/api.nswag b/src/api/api.nswag index 25e9158..e95c2e4 100644 --- a/src/api/api.nswag +++ b/src/api/api.nswag @@ -4,7 +4,7 @@ "documentGenerator": { "fromDocument": { "json": "", - "url": "http://localhost:5000/v1/openapi.json", + "url": "http://localhost:5000/openapi.json", "output": null, "newLineBehavior": "Auto" } @@ -48,7 +48,7 @@ "useLeafType": false, "classTypes": [], "extendedClasses": [], - "extensionCode": "api.include.ts", + "extensionCode": "../core/include.ts", "generateDefaultValues": true, "excludedTypeNames": [], "excludedParameterNames": [], @@ -67,7 +67,7 @@ "templateDirectory": null, "serviceHost": null, "serviceSchemes": null, - "output": "api.generated.ts", + "output": "generated.ts", "newLineBehavior": "LF" } } diff --git a/src/api/api.generated.ts b/src/api/generated.ts similarity index 98% rename from src/api/api.generated.ts rename to src/api/generated.ts index 1317b22..48ee8c4 100644 --- a/src/api/api.generated.ts +++ b/src/api/generated.ts @@ -8,7 +8,7 @@ /* eslint-disable */ // ReSharper disable InconsistentNaming -import { TribufuApiBase } from "./api.base"; +import { TribufuApiBase } from "../core/base"; export class TribufuApiGenerated extends TribufuApiBase { private http: { fetch(url: RequestInfo, init?: RequestInit): Promise }; @@ -62,7 +62,7 @@ export class TribufuApiGenerated extends TribufuApiBase { /** * Login with name or email and password. - * @param body (optional) + * @param body (optional) * @return OK */ login(body?: LoginRequest | undefined): Promise { @@ -106,7 +106,7 @@ export class TribufuApiGenerated extends TribufuApiBase { /** * Create a new user. - * @param body (optional) + * @param body (optional) * @return OK */ register(body?: RegisterRequest | undefined): Promise { @@ -150,7 +150,7 @@ export class TribufuApiGenerated extends TribufuApiBase { /** * Refresh credentials. - * @param body (optional) + * @param body (optional) * @return OK */ refresh(body?: RefreshRequest | undefined): Promise { @@ -230,8 +230,8 @@ export class TribufuApiGenerated extends TribufuApiBase { /** * Get a list of game server clusters. - * @param page (optional) - * @param limit (optional) + * @param page (optional) + * @param limit (optional) * @return OK */ getClusters(page?: number | undefined, limit?: number | undefined): Promise { @@ -279,7 +279,7 @@ export class TribufuApiGenerated extends TribufuApiBase { /** * Create a new game server cluster. - * @param body (optional) + * @param body (optional) * @return OK */ createCluster(body?: any | undefined): Promise { @@ -362,7 +362,7 @@ export class TribufuApiGenerated extends TribufuApiBase { /** * Update a game server cluster. - * @param body (optional) + * @param body (optional) * @return OK */ updateCluster(id: string, body?: any | undefined): Promise { @@ -526,8 +526,8 @@ export class TribufuApiGenerated extends TribufuApiBase { /** * Get a list of game servers of a game. - * @param page (optional) - * @param limit (optional) + * @param page (optional) + * @param limit (optional) * @return OK */ getGameServers(id: string, page?: number | undefined, limit?: number | undefined): Promise { @@ -578,8 +578,8 @@ export class TribufuApiGenerated extends TribufuApiBase { /** * Get a list of game server clusters of a game. - * @param page (optional) - * @param limit (optional) + * @param page (optional) + * @param limit (optional) * @return OK */ getGameClusters(id: string, page?: number | undefined, limit?: number | undefined): Promise { @@ -630,8 +630,8 @@ export class TribufuApiGenerated extends TribufuApiBase { /** * Get a list of game items. - * @param page (optional) - * @param limit (optional) + * @param page (optional) + * @param limit (optional) * @return OK */ getGameItems(id: string, page?: number | undefined, limit?: number | undefined): Promise { @@ -682,8 +682,8 @@ export class TribufuApiGenerated extends TribufuApiBase { /** * Get a list of groups. - * @param page (optional) - * @param limit (optional) + * @param page (optional) + * @param limit (optional) * @return OK */ getGroups(page?: number | undefined, limit?: number | undefined): Promise { @@ -731,7 +731,7 @@ export class TribufuApiGenerated extends TribufuApiBase { /** * Create a new group. - * @param body (optional) + * @param body (optional) * @return OK */ createGroup(body?: any | undefined): Promise { @@ -814,7 +814,7 @@ export class TribufuApiGenerated extends TribufuApiBase { /** * Update a group. - * @param body (optional) + * @param body (optional) * @return OK */ updateGroup(id: string, body?: any | undefined): Promise { @@ -1065,7 +1065,7 @@ export class TribufuApiGenerated extends TribufuApiBase { /** * Get the top 20 leaderboard users. - * @param order (optional) + * @param order (optional) * @return OK */ getLeaderboard(order?: LeaderboardOrder | undefined): Promise { @@ -1109,8 +1109,8 @@ export class TribufuApiGenerated extends TribufuApiBase { /** * Get a list of packages. - * @param page (optional) - * @param limit (optional) + * @param page (optional) + * @param limit (optional) * @return OK */ getPackages(page?: number | undefined, limit?: number | undefined): Promise { @@ -1200,7 +1200,7 @@ export class TribufuApiGenerated extends TribufuApiBase { /** * Advanced search for servers or players. - * @param body (optional) + * @param body (optional) * @return OK */ search(body?: SearchRequest | undefined): Promise { @@ -1244,8 +1244,8 @@ export class TribufuApiGenerated extends TribufuApiBase { /** * Get a list of game servers. - * @param page (optional) - * @param limit (optional) + * @param page (optional) + * @param limit (optional) * @return OK */ getServers(page?: number | undefined, limit?: number | undefined): Promise { @@ -1293,7 +1293,7 @@ export class TribufuApiGenerated extends TribufuApiBase { /** * Create a new game server. - * @param body (optional) + * @param body (optional) * @return OK */ createServer(body?: any | undefined): Promise { @@ -1376,7 +1376,7 @@ export class TribufuApiGenerated extends TribufuApiBase { /** * Update a game server. - * @param body (optional) + * @param body (optional) * @return OK */ updateServer(id: string, body?: any | undefined): Promise { @@ -1504,8 +1504,8 @@ export class TribufuApiGenerated extends TribufuApiBase { /** * Get a list of game servers from a country. - * @param page (optional) - * @param limit (optional) + * @param page (optional) + * @param limit (optional) * @return OK */ getServersByCountry(country: string, page?: number | undefined, limit?: number | undefined): Promise { @@ -1556,7 +1556,7 @@ export class TribufuApiGenerated extends TribufuApiBase { /** * Claim a game server. - * @param body (optional) + * @param body (optional) * @return OK */ claimServer(id: string, body?: any | undefined): Promise { @@ -1678,8 +1678,8 @@ export class TribufuApiGenerated extends TribufuApiBase { /** * Get a list of subscriptions. - * @param page (optional) - * @param limit (optional) + * @param page (optional) + * @param limit (optional) * @return OK */ getSubscriptions(page?: number | undefined, limit?: number | undefined): Promise { @@ -1769,8 +1769,8 @@ export class TribufuApiGenerated extends TribufuApiBase { /** * Get a list of user profiles. - * @param page (optional) - * @param limit (optional) + * @param page (optional) + * @param limit (optional) * @return OK */ getUsers(page?: number | undefined, limit?: number | undefined): Promise { @@ -2070,8 +2070,8 @@ export class TribufuApiGenerated extends TribufuApiBase { /** * Get a list of servers the user is owner of. - * @param page (optional) - * @param limit (optional) + * @param page (optional) + * @param limit (optional) * @return OK */ getUserServers(id: string, page?: number | undefined, limit?: number | undefined): Promise { @@ -2206,7 +2206,7 @@ export class TribufuApiGenerated extends TribufuApiBase { /** * Update a user profile. - * @param body (optional) + * @param body (optional) * @return OK */ updateUserProfile(id: string, body?: UpdateProfile | undefined): Promise { @@ -2253,7 +2253,7 @@ export class TribufuApiGenerated extends TribufuApiBase { /** * Change the email of a user. - * @param body (optional) + * @param body (optional) * @return OK */ changeEmail(id: string, body?: any | undefined): Promise { @@ -2297,7 +2297,7 @@ export class TribufuApiGenerated extends TribufuApiBase { /** * Change the password of a user. - * @param body (optional) + * @param body (optional) * @return OK */ changePassword(id: string, body?: any | undefined): Promise { @@ -2341,8 +2341,8 @@ export class TribufuApiGenerated extends TribufuApiBase { /** * Generate a random password. - * @param length (optional) - * @param symbols (optional) + * @param length (optional) + * @param symbols (optional) * @return OK */ generatePassword(length?: number | undefined, symbols?: boolean | undefined): Promise { @@ -2390,7 +2390,7 @@ export class TribufuApiGenerated extends TribufuApiBase { /** * Generate one or more flake ids. - * @param amount (optional) + * @param amount (optional) * @return OK */ generateFlakeId(amount?: number | undefined): Promise { @@ -2434,8 +2434,8 @@ export class TribufuApiGenerated extends TribufuApiBase { /** * Generate one or more uuids with a specific version. - * @param version (optional) - * @param amount (optional) + * @param version (optional) + * @param amount (optional) * @return OK */ generateUuid(version?: number | undefined, amount?: number | undefined): Promise { @@ -2483,7 +2483,7 @@ export class TribufuApiGenerated extends TribufuApiBase { /** * Convert a string to base64 or vice versa. - * @param body (optional) + * @param body (optional) * @return OK */ convertBase64(body?: CryptoViewModel | undefined): Promise { @@ -2527,7 +2527,7 @@ export class TribufuApiGenerated extends TribufuApiBase { /** * Hash a string using md5. - * @param body (optional) + * @param body (optional) * @return OK */ hashMd5(body?: HashViewModel | undefined): Promise { @@ -2571,7 +2571,7 @@ export class TribufuApiGenerated extends TribufuApiBase { /** * Hash a string using sha256. - * @param body (optional) + * @param body (optional) * @return OK */ hashSha256(body?: HashViewModel | undefined): Promise { @@ -2615,7 +2615,7 @@ export class TribufuApiGenerated extends TribufuApiBase { /** * Hash a string using bcrypt. - * @param body (optional) + * @param body (optional) * @return OK */ hashBcrypt(body?: HashViewModel | undefined): Promise { @@ -2659,7 +2659,7 @@ export class TribufuApiGenerated extends TribufuApiBase { /** * Hash a string using argon2. - * @param body (optional) + * @param body (optional) * @return OK */ hashArgon2(body?: HashViewModel | undefined): Promise { @@ -3061,4 +3061,4 @@ function throwException(message: string, status: number, response: string, heade throw result; else throw new TribufuApiError(message, status, response, headers, null); -} \ No newline at end of file +} diff --git a/src/api/index.ts b/src/api/index.ts index c18a582..f22494c 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -2,8 +2,8 @@ // SPDX-License-Identifier: MIT import { TRIBUFU_API_URL } from ".."; -import { TribufuApiBase } from "./api.base"; -import { TribufuApiGenerated } from "./api.generated"; +import { TribufuApiBase } from "../core/base"; +import { TribufuApiGenerated } from "./generated"; import { TribufuApiOptions } from "../options"; /** diff --git a/src/api/api.base.ts b/src/core/base.ts similarity index 100% rename from src/api/api.base.ts rename to src/core/base.ts diff --git a/src/core/include.ts b/src/core/include.ts new file mode 100644 index 0000000..4866b90 --- /dev/null +++ b/src/core/include.ts @@ -0,0 +1 @@ +import { TribufuApiBase } from "../core/base"; diff --git a/src/translate/generated.ts b/src/translate/generated.ts new file mode 100644 index 0000000..0c6fc61 --- /dev/null +++ b/src/translate/generated.ts @@ -0,0 +1,84 @@ +//---------------------- +// +// Generated using the NSwag toolchain v14.1.0.0 (NJsonSchema v11.0.2.0 (Newtonsoft.Json v13.0.0.0)) (http://NSwag.org) +// +//---------------------- + +/* 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 }; + private baseUrl: string; + protected jsonParseReviver: ((key: string, value: any) => any) | undefined = undefined; + + constructor(baseUrl?: string, http?: { fetch(url: RequestInfo, init?: RequestInit): Promise }) { + super(); + this.http = http ? http : window as any; + this.baseUrl = baseUrl ?? ""; + } + + /** + * Translate a given text. + */ + translate(): Promise { + 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 { + 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); +} \ No newline at end of file diff --git a/src/translate/translate.nswag b/src/translate/translate.nswag new file mode 100644 index 0000000..81f51b4 --- /dev/null +++ b/src/translate/translate.nswag @@ -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" + } + } +}