From 0d24ecca3fe29e1887c1ebc0bca2bbaa617c198b Mon Sep 17 00:00:00 2001 From: Guilherme Werner Date: Tue, 23 Jul 2024 20:29:30 -0300 Subject: [PATCH] Generate api with NSwag --- examples/api.js | 15 - examples/bot.js | 10 - examples/client.js | 18 - examples/openid.js | 13 + examples/server.js | 11 - package.json | 3 +- pnpm-lock.yaml | 15 +- src/api.generated.ts | 1248 +++++++++++++++++++++++++++++++++++++++++ src/api.ts | 544 ------------------ src/bot.ts | 68 --- src/client.ts | 364 +----------- src/constants.ts | 26 - src/index.ts | 22 - src/models/cluster.ts | 22 - src/models/profile.ts | 16 - src/node.ts | 8 - src/options.ts | 9 - src/server.ts | 74 --- src/socket/index.ts | 12 - src/token.ts | 20 - 20 files changed, 1275 insertions(+), 1243 deletions(-) delete mode 100644 examples/api.js delete mode 100644 examples/bot.js delete mode 100644 examples/client.js create mode 100644 examples/openid.js delete mode 100644 examples/server.js create mode 100644 src/api.generated.ts delete mode 100644 src/api.ts delete mode 100644 src/bot.ts delete mode 100644 src/constants.ts delete mode 100644 src/models/cluster.ts delete mode 100644 src/models/profile.ts delete mode 100644 src/node.ts delete mode 100644 src/options.ts delete mode 100644 src/server.ts delete mode 100644 src/socket/index.ts delete mode 100644 src/token.ts diff --git a/examples/api.js b/examples/api.js deleted file mode 100644 index 34d9d16..0000000 --- a/examples/api.js +++ /dev/null @@ -1,15 +0,0 @@ -// Copyright (c) Tribufu. All Rights Reserved. -// SPDX-License-Identifier: MIT - -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]); -} - -main(); diff --git a/examples/bot.js b/examples/bot.js deleted file mode 100644 index 347605f..0000000 --- a/examples/bot.js +++ /dev/null @@ -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(); diff --git a/examples/client.js b/examples/client.js deleted file mode 100644 index e2a428a..0000000 --- a/examples/client.js +++ /dev/null @@ -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(); diff --git a/examples/openid.js b/examples/openid.js new file mode 100644 index 0000000..a3a03fd --- /dev/null +++ b/examples/openid.js @@ -0,0 +1,13 @@ +// Copyright (c) Tribufu. All Rights Reserved. +// SPDX-License-Identifier: MIT + +import dotenv from 'dotenv'; +import { TribufuClient } from '../build/index.mjs'; + +async function main() { + const client = new TribufuClient(); + const config = await client.openidConfiguration(); + console.log(config); +} + +main(); diff --git a/examples/server.js b/examples/server.js deleted file mode 100644 index 78e41e1..0000000 --- a/examples/server.js +++ /dev/null @@ -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(); diff --git a/package.json b/package.json index a156510..d279902 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,8 @@ "build": "npm run clean && tsc && node scripts/esbuild.js" }, "dependencies": { - "@tribufu/mintaka": "0.1.8" + "@tribufu/mintaka": "0.1.8", + "axios": "^1.7.2" }, "devDependencies": { "@types/node": "^20.10.6", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 953fa75..6e241c1 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -8,6 +8,9 @@ dependencies: '@tribufu/mintaka': specifier: 0.1.8 version: 0.1.8 + axios: + specifier: ^1.7.2 + version: 1.7.2 devDependencies: '@types/node': @@ -263,7 +266,7 @@ packages: /@tribufu/mintaka@0.1.8: resolution: {integrity: sha512-qUvReWlz8irSIbKCTfjFnUnUq7MIgLjnTBPeWv2ayyiwGkf8L3q7qi1Zxuqt3OduOugSOtxYQwXOaClldIMUTQ==} dependencies: - axios: 1.6.4 + axios: 1.7.2 camelcase-keys: 9.1.2 fp-ts: 2.16.2 json-bigint: 1.0.0 @@ -308,10 +311,10 @@ packages: resolution: {integrity: sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==} dev: false - /axios@1.6.4: - resolution: {integrity: sha512-heJnIs6N4aa1eSthhN9M5ioILu8Wi8vmQW9iHQ9NUvfkJb0lEEDUiIdQNAuBtfUt3FxReaKdpQA5DbmMOqzF/A==} + /axios@1.7.2: + resolution: {integrity: sha512-2A8QhOMrbomlDuiLeK9XibIBzuHeRcqqNOHp0Cyp5EoJ1IFDh+XZH3A6BkXtv0K4gFGCI0Y4BM7B1wOEi0Rmgw==} dependencies: - follow-redirects: 1.15.4 + follow-redirects: 1.15.6 form-data: 4.0.0 proxy-from-env: 1.1.0 transitivePeerDependencies: @@ -471,8 +474,8 @@ packages: path-exists: 4.0.0 dev: true - /follow-redirects@1.15.4: - resolution: {integrity: sha512-Cr4D/5wlrb0z9dgERpUL3LrmPKVDsETIJhaCMeDfuFYcqa5bldGV6wBsAN6X/vxlXQtFBMrXdXxdL8CbDTGniw==} + /follow-redirects@1.15.6: + resolution: {integrity: sha512-wWN62YITEaOpSK584EZXJafH1AGpO8RVgElfkuXbTOrPX4fIfOyEpW/CsiNd8JdYrAoOvafRTOEnvsO++qCqFA==} engines: {node: '>=4.0'} peerDependencies: debug: '*' diff --git a/src/api.generated.ts b/src/api.generated.ts new file mode 100644 index 0000000..093ffe4 --- /dev/null +++ b/src/api.generated.ts @@ -0,0 +1,1248 @@ +//---------------------- +// +// 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 + +export class TribufuApi { + 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 }) { + this.http = http ? http : window as any; + this.baseUrl = baseUrl ?? ""; + } + + /** + * @param page (optional) + * @return Success + */ + clustersAll(page: number | undefined): Promise { + let url_ = this.baseUrl + "/v1/games/clusters?"; + if (page === null) + throw new Error("The parameter 'page' cannot be null."); + else if (page !== undefined) + url_ += "page=" + encodeURIComponent("" + page) + "&"; + url_ = url_.replace(/[?&]$/, ""); + + let options_: RequestInit = { + method: "GET", + headers: { + "Accept": "text/plain" + } + }; + + return this.http.fetch(url_, options_).then((_response: Response) => { + return this.processClustersAll(_response); + }); + } + + protected processClustersAll(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); }; + if (status === 200) { + return response.text().then((_responseText) => { + let result200: any = null; + result200 = _responseText === "" ? null : JSON.parse(_responseText, this.jsonParseReviver) as any[]; + return result200; + }); + } else if (status !== 200 && status !== 204) { + return response.text().then((_responseText) => { + return throwException("An unexpected server error occurred.", status, _responseText, _headers); + }); + } + return Promise.resolve(null as any); + } + + /** + * @return Success + */ + clusters(id: number): Promise { + let url_ = this.baseUrl + "/v1/games/clusters/{id}"; + if (id === undefined || id === null) + throw new Error("The parameter 'id' must be defined."); + url_ = url_.replace("{id}", encodeURIComponent("" + id)); + url_ = url_.replace(/[?&]$/, ""); + + let options_: RequestInit = { + method: "GET", + headers: { + } + }; + + return this.http.fetch(url_, options_).then((_response: Response) => { + return this.processClusters(_response); + }); + } + + protected processClusters(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); }; + if (status === 200) { + return response.text().then((_responseText) => { + return; + }); + } else if (status !== 200 && status !== 204) { + return response.text().then((_responseText) => { + return throwException("An unexpected server error occurred.", status, _responseText, _headers); + }); + } + return Promise.resolve(null as any); + } + + /** + * @param page (optional) + * @return Success + */ + gamesAll(page: number | undefined): Promise { + let url_ = this.baseUrl + "/v1/games?"; + if (page === null) + throw new Error("The parameter 'page' cannot be null."); + else if (page !== undefined) + url_ += "page=" + encodeURIComponent("" + page) + "&"; + url_ = url_.replace(/[?&]$/, ""); + + let options_: RequestInit = { + method: "GET", + headers: { + "Accept": "text/plain" + } + }; + + return this.http.fetch(url_, options_).then((_response: Response) => { + return this.processGamesAll(_response); + }); + } + + protected processGamesAll(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); }; + if (status === 200) { + return response.text().then((_responseText) => { + let result200: any = null; + result200 = _responseText === "" ? null : JSON.parse(_responseText, this.jsonParseReviver) as any[]; + return result200; + }); + } else if (status !== 200 && status !== 204) { + return response.text().then((_responseText) => { + return throwException("An unexpected server error occurred.", status, _responseText, _headers); + }); + } + return Promise.resolve(null as any); + } + + /** + * @return Success + */ + games(id: number): Promise { + let url_ = this.baseUrl + "/v1/games/{id}"; + if (id === undefined || id === null) + throw new Error("The parameter 'id' must be defined."); + url_ = url_.replace("{id}", encodeURIComponent("" + id)); + url_ = url_.replace(/[?&]$/, ""); + + let options_: RequestInit = { + method: "GET", + headers: { + } + }; + + return this.http.fetch(url_, options_).then((_response: Response) => { + return this.processGames(_response); + }); + } + + protected processGames(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); }; + if (status === 200) { + return response.text().then((_responseText) => { + return; + }); + } else if (status !== 200 && status !== 204) { + return response.text().then((_responseText) => { + return throwException("An unexpected server error occurred.", status, _responseText, _headers); + }); + } + return Promise.resolve(null as any); + } + + /** + * @return Success + */ + serversAll(id: number): Promise { + let url_ = this.baseUrl + "/v1/games/{id}/servers"; + if (id === undefined || id === null) + throw new Error("The parameter 'id' must be defined."); + url_ = url_.replace("{id}", encodeURIComponent("" + id)); + url_ = url_.replace(/[?&]$/, ""); + + let options_: RequestInit = { + method: "GET", + headers: { + "Accept": "text/plain" + } + }; + + return this.http.fetch(url_, options_).then((_response: Response) => { + return this.processServersAll(_response); + }); + } + + protected processServersAll(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); }; + if (status === 200) { + return response.text().then((_responseText) => { + let result200: any = null; + result200 = _responseText === "" ? null : JSON.parse(_responseText, this.jsonParseReviver) as any[]; + return result200; + }); + } else if (status !== 200 && status !== 204) { + return response.text().then((_responseText) => { + return throwException("An unexpected server error occurred.", status, _responseText, _headers); + }); + } + return Promise.resolve(null as any); + } + + /** + * @return Success + */ + clustersAll2(id: number): Promise { + let url_ = this.baseUrl + "/v1/games/{id}/clusters"; + if (id === undefined || id === null) + throw new Error("The parameter 'id' must be defined."); + url_ = url_.replace("{id}", encodeURIComponent("" + id)); + url_ = url_.replace(/[?&]$/, ""); + + let options_: RequestInit = { + method: "GET", + headers: { + "Accept": "text/plain" + } + }; + + return this.http.fetch(url_, options_).then((_response: Response) => { + return this.processClustersAll2(_response); + }); + } + + protected processClustersAll2(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); }; + if (status === 200) { + return response.text().then((_responseText) => { + let result200: any = null; + result200 = _responseText === "" ? null : JSON.parse(_responseText, this.jsonParseReviver) as any[]; + return result200; + }); + } else if (status !== 200 && status !== 204) { + return response.text().then((_responseText) => { + return throwException("An unexpected server error occurred.", status, _responseText, _headers); + }); + } + return Promise.resolve(null as any); + } + + /** + * @param page (optional) + * @return Success + */ + groupsAll(page: number | undefined): Promise { + let url_ = this.baseUrl + "/v1/groups?"; + if (page === null) + throw new Error("The parameter 'page' cannot be null."); + else if (page !== undefined) + url_ += "page=" + encodeURIComponent("" + page) + "&"; + url_ = url_.replace(/[?&]$/, ""); + + let options_: RequestInit = { + method: "GET", + headers: { + "Accept": "text/plain" + } + }; + + return this.http.fetch(url_, options_).then((_response: Response) => { + return this.processGroupsAll(_response); + }); + } + + protected processGroupsAll(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); }; + if (status === 200) { + return response.text().then((_responseText) => { + let result200: any = null; + result200 = _responseText === "" ? null : JSON.parse(_responseText, this.jsonParseReviver) as any[]; + return result200; + }); + } else if (status !== 200 && status !== 204) { + return response.text().then((_responseText) => { + return throwException("An unexpected server error occurred.", status, _responseText, _headers); + }); + } + return Promise.resolve(null as any); + } + + /** + * @return Success + */ + groups(id: number): Promise { + let url_ = this.baseUrl + "/v1/groups/{id}"; + if (id === undefined || id === null) + throw new Error("The parameter 'id' must be defined."); + url_ = url_.replace("{id}", encodeURIComponent("" + id)); + url_ = url_.replace(/[?&]$/, ""); + + let options_: RequestInit = { + method: "GET", + headers: { + } + }; + + return this.http.fetch(url_, options_).then((_response: Response) => { + return this.processGroups(_response); + }); + } + + protected processGroups(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); }; + if (status === 200) { + return response.text().then((_responseText) => { + return; + }); + } else if (status !== 200 && status !== 204) { + return response.text().then((_responseText) => { + return throwException("An unexpected server error occurred.", status, _responseText, _headers); + }); + } + return Promise.resolve(null as any); + } + + /** + * @return Success + */ + members(id: number): Promise { + let url_ = this.baseUrl + "/v1/groups/{id}/members"; + if (id === undefined || id === null) + throw new Error("The parameter 'id' must be defined."); + url_ = url_.replace("{id}", encodeURIComponent("" + id)); + url_ = url_.replace(/[?&]$/, ""); + + let options_: RequestInit = { + method: "GET", + headers: { + "Accept": "text/plain" + } + }; + + return this.http.fetch(url_, options_).then((_response: Response) => { + return this.processMembers(_response); + }); + } + + protected processMembers(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); }; + if (status === 200) { + return response.text().then((_responseText) => { + let result200: any = null; + result200 = _responseText === "" ? null : JSON.parse(_responseText, this.jsonParseReviver) as any[]; + return result200; + }); + } else if (status !== 200 && status !== 204) { + return response.text().then((_responseText) => { + return throwException("An unexpected server error occurred.", status, _responseText, _headers); + }); + } + return Promise.resolve(null as any); + } + + /** + * @return Success + */ + gamesAll2(id: number): Promise { + let url_ = this.baseUrl + "/v1/groups/{id}/games"; + if (id === undefined || id === null) + throw new Error("The parameter 'id' must be defined."); + url_ = url_.replace("{id}", encodeURIComponent("" + id)); + url_ = url_.replace(/[?&]$/, ""); + + let options_: RequestInit = { + method: "GET", + headers: { + "Accept": "text/plain" + } + }; + + return this.http.fetch(url_, options_).then((_response: Response) => { + return this.processGamesAll2(_response); + }); + } + + protected processGamesAll2(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); }; + if (status === 200) { + return response.text().then((_responseText) => { + let result200: any = null; + result200 = _responseText === "" ? null : JSON.parse(_responseText, this.jsonParseReviver) as any[]; + return result200; + }); + } else if (status !== 200 && status !== 204) { + return response.text().then((_responseText) => { + return throwException("An unexpected server error occurred.", status, _responseText, _headers); + }); + } + return Promise.resolve(null as any); + } + + /** + * @param page (optional) + * @return Success + */ + leaderboard(page: number | undefined): Promise { + let url_ = this.baseUrl + "/v1/leaderboard?"; + if (page === null) + throw new Error("The parameter 'page' cannot be null."); + else if (page !== undefined) + url_ += "page=" + encodeURIComponent("" + page) + "&"; + url_ = url_.replace(/[?&]$/, ""); + + let options_: RequestInit = { + method: "GET", + headers: { + "Accept": "text/plain" + } + }; + + return this.http.fetch(url_, options_).then((_response: Response) => { + return this.processLeaderboard(_response); + }); + } + + protected processLeaderboard(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); }; + if (status === 200) { + return response.text().then((_responseText) => { + let result200: any = null; + result200 = _responseText === "" ? null : JSON.parse(_responseText, this.jsonParseReviver) as any[]; + return result200; + }); + } else if (status !== 200 && status !== 204) { + return response.text().then((_responseText) => { + return throwException("An unexpected server error occurred.", status, _responseText, _headers); + }); + } + return Promise.resolve(null as any); + } + + /** + * @return Success + */ + userinfo(): Promise { + let url_ = this.baseUrl + "/v1/oauth2/userinfo"; + url_ = url_.replace(/[?&]$/, ""); + + let options_: RequestInit = { + method: "GET", + headers: { + } + }; + + return this.http.fetch(url_, options_).then((_response: Response) => { + return this.processUserinfo(_response); + }); + } + + protected processUserinfo(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); }; + if (status === 200) { + return response.text().then((_responseText) => { + return; + }); + } else if (status !== 200 && status !== 204) { + return response.text().then((_responseText) => { + return throwException("An unexpected server error occurred.", status, _responseText, _headers); + }); + } + return Promise.resolve(null as any); + } + + /** + * @return Success + */ + jwks(): Promise { + let url_ = this.baseUrl + "/v1/oauth2/jwks"; + url_ = url_.replace(/[?&]$/, ""); + + let options_: RequestInit = { + method: "GET", + headers: { + } + }; + + return this.http.fetch(url_, options_).then((_response: Response) => { + return this.processJwks(_response); + }); + } + + protected processJwks(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); }; + if (status === 200) { + return response.text().then((_responseText) => { + return; + }); + } else if (status !== 200 && status !== 204) { + return response.text().then((_responseText) => { + return throwException("An unexpected server error occurred.", status, _responseText, _headers); + }); + } + return Promise.resolve(null as any); + } + + /** + * @param body (optional) + * @return Success + */ + authorize(body: OAuth2AuthorizeRequest | undefined): Promise { + let url_ = this.baseUrl + "/v1/oauth2/authorize"; + url_ = url_.replace(/[?&]$/, ""); + + const content_ = JSON.stringify(body); + + let options_: RequestInit = { + body: content_, + method: "POST", + headers: { + "Content-Type": "application/json", + } + }; + + return this.http.fetch(url_, options_).then((_response: Response) => { + return this.processAuthorize(_response); + }); + } + + protected processAuthorize(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); }; + if (status === 200) { + return response.text().then((_responseText) => { + return; + }); + } else if (status !== 200 && status !== 204) { + return response.text().then((_responseText) => { + return throwException("An unexpected server error occurred.", status, _responseText, _headers); + }); + } + return Promise.resolve(null as any); + } + + /** + * @param body (optional) + * @return Success + */ + token(body: OAuth2TokenRequest | undefined): Promise { + let url_ = this.baseUrl + "/v1/oauth2/token"; + url_ = url_.replace(/[?&]$/, ""); + + const content_ = JSON.stringify(body); + + let options_: RequestInit = { + body: content_, + method: "POST", + headers: { + "Content-Type": "application/json", + "Accept": "text/plain" + } + }; + + return this.http.fetch(url_, options_).then((_response: Response) => { + return this.processToken(_response); + }); + } + + protected processToken(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); }; + if (status === 200) { + return response.text().then((_responseText) => { + let result200: any = null; + result200 = _responseText === "" ? null : JSON.parse(_responseText, this.jsonParseReviver) as OAuth2TokenResponse; + return result200; + }); + } else if (status !== 200 && status !== 204) { + return response.text().then((_responseText) => { + return throwException("An unexpected server error occurred.", status, _responseText, _headers); + }); + } + return Promise.resolve(null as any); + } + + /** + * @param body (optional) + * @return Success + */ + revoke(body: OAuth2RevokeRequest | undefined): Promise { + let url_ = this.baseUrl + "/v1/oauth2/revoke"; + url_ = url_.replace(/[?&]$/, ""); + + const content_ = JSON.stringify(body); + + let options_: RequestInit = { + body: content_, + method: "POST", + headers: { + "Content-Type": "application/json", + } + }; + + return this.http.fetch(url_, options_).then((_response: Response) => { + return this.processRevoke(_response); + }); + } + + protected processRevoke(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); }; + if (status === 200) { + return response.text().then((_responseText) => { + return; + }); + } else if (status !== 200 && status !== 204) { + return response.text().then((_responseText) => { + return throwException("An unexpected server error occurred.", status, _responseText, _headers); + }); + } + return Promise.resolve(null as any); + } + + /** + * @param body (optional) + * @return Success + */ + introspect(body: OAuth2IntrospectRequest | undefined): Promise { + let url_ = this.baseUrl + "/v1/oauth2/introspect"; + url_ = url_.replace(/[?&]$/, ""); + + const content_ = JSON.stringify(body); + + let options_: RequestInit = { + body: content_, + method: "POST", + headers: { + "Content-Type": "application/json", + } + }; + + return this.http.fetch(url_, options_).then((_response: Response) => { + return this.processIntrospect(_response); + }); + } + + protected processIntrospect(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); }; + if (status === 200) { + return response.text().then((_responseText) => { + return; + }); + } else if (status !== 200 && status !== 204) { + return response.text().then((_responseText) => { + return throwException("An unexpected server error occurred.", status, _responseText, _headers); + }); + } + return Promise.resolve(null as any); + } + + /** + * @return Success + */ + openidConfiguration(): Promise { + let url_ = this.baseUrl + "/v1/.well-known/openid-configuration"; + url_ = url_.replace(/[?&]$/, ""); + + let options_: RequestInit = { + method: "GET", + headers: { + "Accept": "text/plain" + } + }; + + return this.http.fetch(url_, options_).then((_response: Response) => { + return this.processOpenidConfiguration(_response); + }); + } + + protected processOpenidConfiguration(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); }; + if (status === 200) { + return response.text().then((_responseText) => { + let result200: any = null; + result200 = _responseText === "" ? null : JSON.parse(_responseText, this.jsonParseReviver) as OpenIDConfigurationResponse; + return result200; + }); + } else if (status !== 200 && status !== 204) { + return response.text().then((_responseText) => { + return throwException("An unexpected server error occurred.", status, _responseText, _headers); + }); + } + return Promise.resolve(null as any); + } + + /** + * @param page (optional) + * @return Success + */ + packagesAll(page: number | undefined): Promise { + let url_ = this.baseUrl + "/v1/packages?"; + if (page === null) + throw new Error("The parameter 'page' cannot be null."); + else if (page !== undefined) + url_ += "page=" + encodeURIComponent("" + page) + "&"; + url_ = url_.replace(/[?&]$/, ""); + + let options_: RequestInit = { + method: "GET", + headers: { + "Accept": "text/plain" + } + }; + + return this.http.fetch(url_, options_).then((_response: Response) => { + return this.processPackagesAll(_response); + }); + } + + protected processPackagesAll(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); }; + if (status === 200) { + return response.text().then((_responseText) => { + let result200: any = null; + result200 = _responseText === "" ? null : JSON.parse(_responseText, this.jsonParseReviver) as any[]; + return result200; + }); + } else if (status !== 200 && status !== 204) { + return response.text().then((_responseText) => { + return throwException("An unexpected server error occurred.", status, _responseText, _headers); + }); + } + return Promise.resolve(null as any); + } + + /** + * @return Success + */ + packages(id: number): Promise { + let url_ = this.baseUrl + "/v1/packages/{id}"; + if (id === undefined || id === null) + throw new Error("The parameter 'id' must be defined."); + url_ = url_.replace("{id}", encodeURIComponent("" + id)); + url_ = url_.replace(/[?&]$/, ""); + + let options_: RequestInit = { + method: "GET", + headers: { + } + }; + + return this.http.fetch(url_, options_).then((_response: Response) => { + return this.processPackages(_response); + }); + } + + protected processPackages(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); }; + if (status === 200) { + return response.text().then((_responseText) => { + return; + }); + } else if (status !== 200 && status !== 204) { + return response.text().then((_responseText) => { + return throwException("An unexpected server error occurred.", status, _responseText, _headers); + }); + } + return Promise.resolve(null as any); + } + + /** + * @return Success + */ + profiles(name: string): Promise { + let url_ = this.baseUrl + "/v1/profiles/{name}"; + if (name === undefined || name === null) + throw new Error("The parameter 'name' must be defined."); + url_ = url_.replace("{name}", encodeURIComponent("" + name)); + url_ = url_.replace(/[?&]$/, ""); + + let options_: RequestInit = { + method: "GET", + headers: { + "Accept": "text/plain" + } + }; + + return this.http.fetch(url_, options_).then((_response: Response) => { + return this.processProfiles(_response); + }); + } + + protected processProfiles(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); }; + if (status === 200) { + return response.text().then((_responseText) => { + let result200: any = null; + result200 = _responseText === "" ? null : JSON.parse(_responseText, this.jsonParseReviver) as Profile; + return result200; + }); + } else if (status !== 200 && status !== 204) { + return response.text().then((_responseText) => { + return throwException("An unexpected server error occurred.", status, _responseText, _headers); + }); + } + return Promise.resolve(null as any); + } + + /** + * @return Success + */ + groupsAll2(name: string): Promise { + let url_ = this.baseUrl + "/v1/profiles/{name}/groups"; + if (name === undefined || name === null) + throw new Error("The parameter 'name' must be defined."); + url_ = url_.replace("{name}", encodeURIComponent("" + name)); + url_ = url_.replace(/[?&]$/, ""); + + let options_: RequestInit = { + method: "GET", + headers: { + "Accept": "text/plain" + } + }; + + return this.http.fetch(url_, options_).then((_response: Response) => { + return this.processGroupsAll2(_response); + }); + } + + protected processGroupsAll2(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); }; + if (status === 200) { + return response.text().then((_responseText) => { + let result200: any = null; + result200 = _responseText === "" ? null : JSON.parse(_responseText, this.jsonParseReviver) as any[]; + return result200; + }); + } else if (status !== 200 && status !== 204) { + return response.text().then((_responseText) => { + return throwException("An unexpected server error occurred.", status, _responseText, _headers); + }); + } + return Promise.resolve(null as any); + } + + /** + * @return Success + */ + gamesAll3(name: string): Promise { + let url_ = this.baseUrl + "/v1/profiles/{name}/games"; + if (name === undefined || name === null) + throw new Error("The parameter 'name' must be defined."); + url_ = url_.replace("{name}", encodeURIComponent("" + name)); + url_ = url_.replace(/[?&]$/, ""); + + let options_: RequestInit = { + method: "GET", + headers: { + "Accept": "text/plain" + } + }; + + return this.http.fetch(url_, options_).then((_response: Response) => { + return this.processGamesAll3(_response); + }); + } + + protected processGamesAll3(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); }; + if (status === 200) { + return response.text().then((_responseText) => { + let result200: any = null; + result200 = _responseText === "" ? null : JSON.parse(_responseText, this.jsonParseReviver) as any[]; + return result200; + }); + } else if (status !== 200 && status !== 204) { + return response.text().then((_responseText) => { + return throwException("An unexpected server error occurred.", status, _responseText, _headers); + }); + } + return Promise.resolve(null as any); + } + + /** + * @return Success + */ + friends(name: string): Promise { + let url_ = this.baseUrl + "/v1/profiles/{name}/friends"; + if (name === undefined || name === null) + throw new Error("The parameter 'name' must be defined."); + url_ = url_.replace("{name}", encodeURIComponent("" + name)); + url_ = url_.replace(/[?&]$/, ""); + + let options_: RequestInit = { + method: "GET", + headers: { + "Accept": "text/plain" + } + }; + + return this.http.fetch(url_, options_).then((_response: Response) => { + return this.processFriends(_response); + }); + } + + protected processFriends(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); }; + if (status === 200) { + return response.text().then((_responseText) => { + let result200: any = null; + result200 = _responseText === "" ? null : JSON.parse(_responseText, this.jsonParseReviver) as any[]; + return result200; + }); + } else if (status !== 200 && status !== 204) { + return response.text().then((_responseText) => { + return throwException("An unexpected server error occurred.", status, _responseText, _headers); + }); + } + return Promise.resolve(null as any); + } + + /** + * @param page (optional) + * @return Success + */ + serversAll2(page: number | undefined): Promise { + let url_ = this.baseUrl + "/v1/games/servers?"; + if (page === null) + throw new Error("The parameter 'page' cannot be null."); + else if (page !== undefined) + url_ += "page=" + encodeURIComponent("" + page) + "&"; + url_ = url_.replace(/[?&]$/, ""); + + let options_: RequestInit = { + method: "GET", + headers: { + "Accept": "text/plain" + } + }; + + return this.http.fetch(url_, options_).then((_response: Response) => { + return this.processServersAll2(_response); + }); + } + + protected processServersAll2(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); }; + if (status === 200) { + return response.text().then((_responseText) => { + let result200: any = null; + result200 = _responseText === "" ? null : JSON.parse(_responseText, this.jsonParseReviver) as any[]; + return result200; + }); + } else if (status !== 200 && status !== 204) { + return response.text().then((_responseText) => { + return throwException("An unexpected server error occurred.", status, _responseText, _headers); + }); + } + return Promise.resolve(null as any); + } + + /** + * @return Success + */ + servers(id: number): Promise { + let url_ = this.baseUrl + "/v1/games/servers/{id}"; + if (id === undefined || id === null) + throw new Error("The parameter 'id' must be defined."); + url_ = url_.replace("{id}", encodeURIComponent("" + id)); + url_ = url_.replace(/[?&]$/, ""); + + let options_: RequestInit = { + method: "GET", + headers: { + } + }; + + return this.http.fetch(url_, options_).then((_response: Response) => { + return this.processServers(_response); + }); + } + + protected processServers(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); }; + if (status === 200) { + return response.text().then((_responseText) => { + return; + }); + } else if (status !== 200 && status !== 204) { + return response.text().then((_responseText) => { + return throwException("An unexpected server error occurred.", status, _responseText, _headers); + }); + } + return Promise.resolve(null as any); + } + + /** + * @param page (optional) + * @return Success + */ + subscriptionsAll(page: number | undefined): Promise { + let url_ = this.baseUrl + "/v1/subscriptions?"; + if (page === null) + throw new Error("The parameter 'page' cannot be null."); + else if (page !== undefined) + url_ += "page=" + encodeURIComponent("" + page) + "&"; + url_ = url_.replace(/[?&]$/, ""); + + let options_: RequestInit = { + method: "GET", + headers: { + "Accept": "text/plain" + } + }; + + return this.http.fetch(url_, options_).then((_response: Response) => { + return this.processSubscriptionsAll(_response); + }); + } + + protected processSubscriptionsAll(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); }; + if (status === 200) { + return response.text().then((_responseText) => { + let result200: any = null; + result200 = _responseText === "" ? null : JSON.parse(_responseText, this.jsonParseReviver) as any[]; + return result200; + }); + } else if (status !== 200 && status !== 204) { + return response.text().then((_responseText) => { + return throwException("An unexpected server error occurred.", status, _responseText, _headers); + }); + } + return Promise.resolve(null as any); + } + + /** + * @return Success + */ + subscriptions(id: number): Promise { + let url_ = this.baseUrl + "/v1/subscriptions/{id}"; + if (id === undefined || id === null) + throw new Error("The parameter 'id' must be defined."); + url_ = url_.replace("{id}", encodeURIComponent("" + id)); + url_ = url_.replace(/[?&]$/, ""); + + let options_: RequestInit = { + method: "GET", + headers: { + } + }; + + return this.http.fetch(url_, options_).then((_response: Response) => { + return this.processSubscriptions(_response); + }); + } + + protected processSubscriptions(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); }; + if (status === 200) { + return response.text().then((_responseText) => { + return; + }); + } else if (status !== 200 && status !== 204) { + return response.text().then((_responseText) => { + return throwException("An unexpected server error occurred.", status, _responseText, _headers); + }); + } + return Promise.resolve(null as any); + } +} + +export interface DateOnly { + year?: number; + month?: number; + day?: number; + day_of_week?: DayOfWeek; + readonly day_of_year?: number; + readonly day_number?: number; +} + +export enum DayOfWeek { + Sunday = "sunday", + Monday = "monday", + Tuesday = "tuesday", + Wednesday = "wednesday", + Thursday = "thursday", + Friday = "friday", + Saturday = "saturday", +} + +export interface OAuth2AuthorizeRequest { + response_type?: OAuth2ResponseType; + client_id?: string | null; + scope?: string | null; + redirect_uri?: string | null; + state?: string | null; +} + +export enum OAuth2GrantType { + Authorization_code = "authorization_code", + Client_credentials = "client_credentials", + Passkey = "passkey", + Password = "password", + Refresh_token = "refresh_token", +} + +export interface OAuth2IntrospectRequest { + token?: string | null; + token_type_hint?: OAuth2TokenHintType; +} + +export enum OAuth2ResponseType { + Code = "code", + Token = "token", +} + +export interface OAuth2RevokeRequest { + token?: string | null; + token_type_hint?: OAuth2TokenHintType; +} + +export enum OAuth2TokenHintType { + Refresh_token = "refresh_token", + Access_token = "access_token", +} + +export interface OAuth2TokenRequest { + grant_type?: OAuth2GrantType; + code?: string | null; + refresh_token?: string | null; + username?: string | null; + password?: string | null; + client_id?: string | null; + client_secret?: string | null; + redirect_uri?: string | null; + server_id?: string | null; +} + +export interface OAuth2TokenResponse { + token_type?: OAuth2TokenType; + access_token?: string | null; + refresh_token?: string | null; + scope?: string | null; + state?: string | null; + expires_in?: number; +} + +export enum OAuth2TokenType { + Bearer = "bearer", +} + +export interface OpenIDConfigurationResponse { + issuer?: string | null; + authorization_endpoint?: string | null; + token_endpoint?: string | null; + userinfo_endpoint?: string | null; + revocation_endpoint?: string | null; + introspection_endpoint?: string | null; + user_info_endpoint?: string | null; + check_session_iframe?: string | null; + end_session_endpoint?: string | null; + jwks_uri?: string | null; + scopes_supported?: string[] | null; + response_types_supported?: string[] | null; + grant_types_supported?: string[] | null; + subject_types_supported?: string[] | null; + claim_types_supported?: string[] | null; + claims_supported?: string[] | null; + id_token_signing_alg_values_supported?: string[] | null; + token_endpoint_auth_methods_supported?: string[] | null; + token_endpoint_auth_signing_alg_values_supported?: string[] | null; + claims_parameter_supported?: boolean; +} + +export interface Profile { + id?: number; + uuid?: string; + name?: string | null; + display_name?: string | null; + public_flags?: number; + verified?: boolean; + level?: number; + experience?: number; + public_birthday?: boolean; + birthday?: DateOnly; + points?: number; + location?: string | null; + photo_url?: string | null; + banner_url?: string | null; + last_online?: string | null; + biography?: string | null; + view_count?: number; + created?: string; + updated?: string | null; +} + +export class TribufuError 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 isTribufuError = true; + + static isTribufuError(obj: any): obj is TribufuError { + return obj.isTribufuError === 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 TribufuError(message, status, response, headers, null); +} diff --git a/src/api.ts b/src/api.ts deleted file mode 100644 index dca2615..0000000 --- a/src/api.ts +++ /dev/null @@ -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 { - const headers = this.getHeaders(); - const responseBody = await this.http.get(`/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 { - const headers = this.getHeaders(); - const responseBody = await this.http.get(`/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 { - const headers = this.getHeaders(); - const responseBody = await this.http.get(`/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 { - 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 { - const headers = this.getHeaders() - const responseBody = await this.http.get(`/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 { - 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 { - 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 { - 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 { - 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 { - const headers = this.getHeaders(); - const responseBody = await this.http.get(`/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 { - const headers = this.getHeaders(); - const responseBody = await this.http.get(`/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 { - const headers = this.getHeaders(); - const responseBody = await this.http.get(`/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 { - const headers = this.getHeaders(); - const responseBody = await this.http.get(`/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 { - const headers = this.getHeaders(); - const responseBody = await this.http.get(`/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 { - 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 { - const headers = this.getHeaders(); - const responseBody = await this.http.get(`/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 { - const headers = this.getHeaders() - const responseBody = await this.http.get(`/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 { - const headers = this.getHeaders(); - const responseBody = await this.http.get(`/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 { - const headers = this.getHeaders() - const responseBody = await this.http.get(`/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 { - const headers = this.getHeaders(); - const responseBody = await this.http.get(`/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 { - const headers = this.getHeaders() - const responseBody = await this.http.get(`/v1/subscriptions/${id}`, headers); - - if (!responseBody) { - return null; - } - - return responseBody; - } -} diff --git a/src/bot.ts b/src/bot.ts deleted file mode 100644 index 4b4b63c..0000000 --- a/src/bot.ts +++ /dev/null @@ -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; - } -} diff --git a/src/client.ts b/src/client.ts index 049fe41..8324f1b 100644 --- a/src/client.ts +++ b/src/client.ts @@ -1,368 +1,10 @@ // 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"; +import { TribufuApi } from "./api.generated"; -/** - * **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 { - 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 { - 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 { - 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 { - 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 { - 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 { - 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 { - 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(url, requestBody, headers); - - if (!responseBody) { - return null; - } - - return responseBody; - } - - /** - * Revoke a refresh token. - * @returns boolean - */ - public async revokeToken(): Promise { - 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(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 { - 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 { - 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(url, requestBody, headers); - - if (!responseBody) { - return null; - } - - return responseBody; - } - - /** - * Get information about the current client. - * @returns Client | null - */ - public async getClientInfo(): Promise { - return this.getClientById(this.clientId); - } - - /** - * Get information about the current user. - * @returns User | null - */ - public async getUserInfo(): Promise { - if (!this.options.accessToken) { - return null; - } - - const headers = this.getHeaders(); - const responseBody = await this.http.get(`/v1/oauth2/userinfo`, headers); - - if (!responseBody) { - return null; - } - - return responseBody; + constructor() { + super("http://localhost:5000", { fetch }); } } diff --git a/src/constants.ts b/src/constants.ts deleted file mode 100644 index d791f8d..0000000 --- a/src/constants.ts +++ /dev/null @@ -1,26 +0,0 @@ -// 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"; - -/** - * The default Tribufu API URL. - * @type {string} - */ -export const TRIBUFU_API_URL = "https://api.tribufu.com"; - -/** - * The default Tribufu WEB URL. - * @type {string} - */ -export const TRIBUFU_WEB_URL = "https://www.tribufu.com"; - -/** - * The default Tribufu CDN URL. - * @type {string} - */ -export const TRIBUFU_CDN_URL = "https://cdn.tribufu.com"; diff --git a/src/index.ts b/src/index.ts index 845cf6d..f67884c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,30 +1,8 @@ // Copyright (c) Tribufu. All Rights Reserved. // SPDX-License-Identifier: MIT -import { - TRIBUFU_API_URL, - TRIBUFU_CDN_URL, - TRIBUFU_VERSION, - TRIBUFU_WEB_URL, -} from "./constants"; - -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, }; diff --git a/src/models/cluster.ts b/src/models/cluster.ts deleted file mode 100644 index 490bd32..0000000 --- a/src/models/cluster.ts +++ /dev/null @@ -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 -} diff --git a/src/models/profile.ts b/src/models/profile.ts deleted file mode 100644 index d07cd28..0000000 --- a/src/models/profile.ts +++ /dev/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 -} diff --git a/src/node.ts b/src/node.ts deleted file mode 100644 index a945919..0000000 --- a/src/node.ts +++ /dev/null @@ -1,8 +0,0 @@ -// Copyright (c) Tribufu. All Rights Reserved. -// SPDX-License-Identifier: MIT - -export enum JavaScriptRuntime { - Browser, - Node, - Other, -} diff --git a/src/options.ts b/src/options.ts deleted file mode 100644 index ff95462..0000000 --- a/src/options.ts +++ /dev/null @@ -1,9 +0,0 @@ -// Copyright (c) Tribufu. All Rights Reserved. -// SPDX-License-Identifier: MIT - -export interface TribufuApiOptions { - apiKey?: string | null; - accessToken?: string | null; - refreshToken?: string | null; - expiresIn?: number | null; -} diff --git a/src/server.ts b/src/server.ts deleted file mode 100644 index 33bda1a..0000000 --- a/src/server.ts +++ /dev/null @@ -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 { - return this.getServerById(this.serverId); - } - - /** - * Get a list of connected users. - * @returns - */ - public async getConnectedUsers(): Promise { - return []; - } -} diff --git a/src/socket/index.ts b/src/socket/index.ts deleted file mode 100644 index da87559..0000000 --- a/src/socket/index.ts +++ /dev/null @@ -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() { - } -} diff --git a/src/token.ts b/src/token.ts deleted file mode 100644 index b1f38f8..0000000 --- a/src/token.ts +++ /dev/null @@ -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; -}