From 93c214d86a15e0c3be88374ee789277dcce65e54 Mon Sep 17 00:00:00 2001 From: Ximaz Date: Tue, 17 Oct 2023 13:46:08 +0200 Subject: [PATCH] feat: adding the pinned servers data. --- dist/index.d.ts | 1 + dist/index.js | 7 +++++- dist/models/CfxPinnedServers.d.ts | 11 +++++++++ dist/models/CfxPinnedServers.js | 15 ++++++++++++ dist/modules/CfxPinnedServers.module.d.ts | 6 +++++ dist/modules/CfxPinnedServers.module.js | 28 +++++++++++++++++++++ dist/types/CfxApiRoutes.d.ts | 1 + dist/types/CfxApiRoutes.js | 3 ++- dist/types/CfxPinnedServers.d.ts | 9 +++++++ dist/types/CfxPinnedServers.js | 2 ++ dist/types/index.d.ts | 1 + dist/types/index.js | 1 + src/index.ts | 5 ++++ src/models/CfxPinnedServers.ts | 18 ++++++++++++++ src/modules/CfxPinnedServers.module.ts | 24 ++++++++++++++++++ src/types/CfxApiRoutes.ts | 2 ++ src/types/CfxPinnedServers.ts | 9 +++++++ src/types/index.ts | 3 ++- tests/CfxPinnedServers.test.ts | 30 +++++++++++++++++++++++ tests/CfxStatus.test.ts | 2 -- 20 files changed, 173 insertions(+), 5 deletions(-) create mode 100644 dist/models/CfxPinnedServers.d.ts create mode 100644 dist/models/CfxPinnedServers.js create mode 100644 dist/modules/CfxPinnedServers.module.d.ts create mode 100644 dist/modules/CfxPinnedServers.module.js create mode 100644 dist/types/CfxPinnedServers.d.ts create mode 100644 dist/types/CfxPinnedServers.js create mode 100644 src/models/CfxPinnedServers.ts create mode 100644 src/modules/CfxPinnedServers.module.ts create mode 100644 src/types/CfxPinnedServers.ts create mode 100644 tests/CfxPinnedServers.test.ts diff --git a/dist/index.d.ts b/dist/index.d.ts index 56aa518..8f78962 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -2,3 +2,4 @@ export * from "./types"; export declare function fetchServer(id: string): Promise; export declare function fetchStatus(): Promise; export declare function fetchUnresolvedIncidents(): Promise; +export declare function fetchPinnedServers(): Promise; diff --git a/dist/index.js b/dist/index.js index aeb53c4..08a5cee 100644 --- a/dist/index.js +++ b/dist/index.js @@ -17,10 +17,11 @@ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); -exports.fetchUnresolvedIncidents = exports.fetchStatus = exports.fetchServer = void 0; +exports.fetchPinnedServers = exports.fetchUnresolvedIncidents = exports.fetchStatus = exports.fetchServer = void 0; const CfxStatus_module_1 = __importDefault(require("./modules/CfxStatus.module")); const CfxCitizenServer_module_1 = __importDefault(require("./modules/CfxCitizenServer.module")); const CfxUnresolvedIncidents_module_1 = __importDefault(require("./modules/CfxUnresolvedIncidents.module")); +const CfxPinnedServers_module_1 = __importDefault(require("./modules/CfxPinnedServers.module")); __exportStar(require("./types"), exports); async function fetchServer(id) { return await CfxCitizenServer_module_1.default.get().retrieve(id); @@ -34,3 +35,7 @@ async function fetchUnresolvedIncidents() { return await CfxUnresolvedIncidents_module_1.default.get().retrieve(); } exports.fetchUnresolvedIncidents = fetchUnresolvedIncidents; +async function fetchPinnedServers() { + return await CfxPinnedServers_module_1.default.get().retrieve(); +} +exports.fetchPinnedServers = fetchPinnedServers; diff --git a/dist/models/CfxPinnedServers.d.ts b/dist/models/CfxPinnedServers.d.ts new file mode 100644 index 0000000..55b65c7 --- /dev/null +++ b/dist/models/CfxPinnedServers.d.ts @@ -0,0 +1,11 @@ +import { CfxPinnedServersResponse } from "src/types/CfxPinnedServers"; +export default class CfxPinnedServers { + pinIfEmpty: boolean; + pinnedServers: string[]; + noAdServerId: string; + noAdServer: { + title: string; + ids: string[]; + }; + constructor(summary: CfxPinnedServersResponse); +} diff --git a/dist/models/CfxPinnedServers.js b/dist/models/CfxPinnedServers.js new file mode 100644 index 0000000..c9d7adc --- /dev/null +++ b/dist/models/CfxPinnedServers.js @@ -0,0 +1,15 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +class CfxPinnedServers { + pinIfEmpty; + pinnedServers; + noAdServerId; + noAdServer; + constructor(summary) { + this.pinIfEmpty = summary.pinIfEmpty; + this.pinnedServers = summary.pinnedServers; + this.noAdServerId = summary.noAdServerId; + this.noAdServer = summary.noAdServer; + } +} +exports.default = CfxPinnedServers; diff --git a/dist/modules/CfxPinnedServers.module.d.ts b/dist/modules/CfxPinnedServers.module.d.ts new file mode 100644 index 0000000..1230747 --- /dev/null +++ b/dist/modules/CfxPinnedServers.module.d.ts @@ -0,0 +1,6 @@ +import CfxPinnedServers from "../models/CfxPinnedServers"; +export default class CfxPinnedServersModule { + static instance: CfxPinnedServersModule | null; + static get(): CfxPinnedServersModule; + retrieve(): Promise; +} diff --git a/dist/modules/CfxPinnedServers.module.js b/dist/modules/CfxPinnedServers.module.js new file mode 100644 index 0000000..2ad6518 --- /dev/null +++ b/dist/modules/CfxPinnedServers.module.js @@ -0,0 +1,28 @@ +"use strict"; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +const types_1 = require("../types"); +const CfxPinnedServers_1 = __importDefault(require("../models/CfxPinnedServers")); +const axios_1 = __importDefault(require("axios")); +class CfxPinnedServersModule { + static instance = null; + static get() { + if (this.instance === null) + this.instance = new CfxPinnedServersModule(); + return this.instance; + } + async retrieve() { + try { + const response = await axios_1.default.get(types_1.CFX_PINNED_SERVERS, { + headers: { "User-Agent": "cfx" }, + }); + return new CfxPinnedServers_1.default(response.data); + } + catch (error) { + throw new Error("Internal error occurred"); + } + } +} +exports.default = CfxPinnedServersModule; diff --git a/dist/types/CfxApiRoutes.d.ts b/dist/types/CfxApiRoutes.d.ts index 33db816..444c7e2 100644 --- a/dist/types/CfxApiRoutes.d.ts +++ b/dist/types/CfxApiRoutes.d.ts @@ -2,3 +2,4 @@ export declare const CFX_MASTERLIST = "https://servers-frontend.fivem.net/api/se export declare const CFX_STATUS_SUMMARY = "https://status.cfx.re/api/v2/status.json"; export declare const CFX_STATUS_COMPONENTS = "https://status.cfx.re/api/v2/components.json"; export declare const CFX_STATUS_UNRESOLVED = "https://status.cfx.re/api/v2/incidents/unresolved.json"; +export declare const CFX_PINNED_SERVERS = "https://runtime.fivem.net/pins.json"; diff --git a/dist/types/CfxApiRoutes.js b/dist/types/CfxApiRoutes.js index 949b7c6..239351d 100644 --- a/dist/types/CfxApiRoutes.js +++ b/dist/types/CfxApiRoutes.js @@ -1,7 +1,8 @@ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); -exports.CFX_STATUS_UNRESOLVED = exports.CFX_STATUS_COMPONENTS = exports.CFX_STATUS_SUMMARY = exports.CFX_MASTERLIST = void 0; +exports.CFX_PINNED_SERVERS = exports.CFX_STATUS_UNRESOLVED = exports.CFX_STATUS_COMPONENTS = exports.CFX_STATUS_SUMMARY = exports.CFX_MASTERLIST = void 0; exports.CFX_MASTERLIST = "https://servers-frontend.fivem.net/api/servers/single/"; exports.CFX_STATUS_SUMMARY = "https://status.cfx.re/api/v2/status.json"; exports.CFX_STATUS_COMPONENTS = "https://status.cfx.re/api/v2/components.json"; exports.CFX_STATUS_UNRESOLVED = "https://status.cfx.re/api/v2/incidents/unresolved.json"; +exports.CFX_PINNED_SERVERS = "https://runtime.fivem.net/pins.json"; diff --git a/dist/types/CfxPinnedServers.d.ts b/dist/types/CfxPinnedServers.d.ts new file mode 100644 index 0000000..f6ac621 --- /dev/null +++ b/dist/types/CfxPinnedServers.d.ts @@ -0,0 +1,9 @@ +export interface CfxPinnedServersResponse { + pinIfEmpty: boolean; + pinnedServers: string[]; + noAdServerId: string; + noAdServer: { + title: string; + ids: string[]; + }; +} diff --git a/dist/types/CfxPinnedServers.js b/dist/types/CfxPinnedServers.js new file mode 100644 index 0000000..c8ad2e5 --- /dev/null +++ b/dist/types/CfxPinnedServers.js @@ -0,0 +1,2 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); diff --git a/dist/types/index.d.ts b/dist/types/index.d.ts index e2ad994..b13eab9 100644 --- a/dist/types/index.d.ts +++ b/dist/types/index.d.ts @@ -5,3 +5,4 @@ export * from "./CfxElementClub"; export * from "./CfxStatus"; export * from "./CfxSummary"; export * from "./CfxUnresolvedIncidents"; +export * from "./CfxPinnedServers"; diff --git a/dist/types/index.js b/dist/types/index.js index 3a93554..1ee2287 100644 --- a/dist/types/index.js +++ b/dist/types/index.js @@ -21,3 +21,4 @@ __exportStar(require("./CfxElementClub"), exports); __exportStar(require("./CfxStatus"), exports); __exportStar(require("./CfxSummary"), exports); __exportStar(require("./CfxUnresolvedIncidents"), exports); +__exportStar(require("./CfxPinnedServers"), exports); diff --git a/src/index.ts b/src/index.ts index 3b06ad9..f3e9e7c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,7 @@ import CfxStatusModule from "./modules/CfxStatus.module" import CfxCitizenServerModule from "./modules/CfxCitizenServer.module" import CfxUnresolvedIncidentsModule from "./modules/CfxUnresolvedIncidents.module" +import CfxPinnedServersModule from "./modules/CfxPinnedServers.module" export * from "./types" export async function fetchServer(id: string) { @@ -14,3 +15,7 @@ export async function fetchStatus() { export async function fetchUnresolvedIncidents() { return await CfxUnresolvedIncidentsModule.get().retrieve() } + +export async function fetchPinnedServers() { + return await CfxPinnedServersModule.get().retrieve() +} \ No newline at end of file diff --git a/src/models/CfxPinnedServers.ts b/src/models/CfxPinnedServers.ts new file mode 100644 index 0000000..66f4777 --- /dev/null +++ b/src/models/CfxPinnedServers.ts @@ -0,0 +1,18 @@ +import { CfxPinnedServersResponse } from "src/types/CfxPinnedServers" + +export default class CfxPinnedServers { + pinIfEmpty: boolean + pinnedServers: string[] + noAdServerId: string + noAdServer: { + title: string, + ids: string[] + } + + constructor(summary: CfxPinnedServersResponse) { + this.pinIfEmpty = summary.pinIfEmpty + this.pinnedServers = summary.pinnedServers + this.noAdServerId = summary.noAdServerId + this.noAdServer = summary.noAdServer + } +} diff --git a/src/modules/CfxPinnedServers.module.ts b/src/modules/CfxPinnedServers.module.ts new file mode 100644 index 0000000..32baca2 --- /dev/null +++ b/src/modules/CfxPinnedServers.module.ts @@ -0,0 +1,24 @@ +import { CFX_PINNED_SERVERS } from "../types" +import CfxPinnedServers from "../models/CfxPinnedServers" +import axios from "axios" + +export default class CfxPinnedServersModule { + static instance: CfxPinnedServersModule | null = null + + static get() { + if (this.instance === null) this.instance = new CfxPinnedServersModule() + return this.instance + } + + async retrieve(): Promise { + try { + const response = await axios.get(CFX_PINNED_SERVERS, { + headers: { "User-Agent": "cfx" }, + }) + + return new CfxPinnedServers(response.data) + } catch (error) { + throw new Error("Internal error occurred") + } + } +} diff --git a/src/types/CfxApiRoutes.ts b/src/types/CfxApiRoutes.ts index 2d21a05..faa1706 100644 --- a/src/types/CfxApiRoutes.ts +++ b/src/types/CfxApiRoutes.ts @@ -8,3 +8,5 @@ export const CFX_STATUS_COMPONENTS = export const CFX_STATUS_UNRESOLVED = "https://status.cfx.re/api/v2/incidents/unresolved.json" + +export const CFX_PINNED_SERVERS = "https://runtime.fivem.net/pins.json" \ No newline at end of file diff --git a/src/types/CfxPinnedServers.ts b/src/types/CfxPinnedServers.ts new file mode 100644 index 0000000..09e4fde --- /dev/null +++ b/src/types/CfxPinnedServers.ts @@ -0,0 +1,9 @@ +export interface CfxPinnedServersResponse { + pinIfEmpty: boolean + pinnedServers: string[] + noAdServerId: string + noAdServer: { + title: string, + ids: string[] + } +} diff --git a/src/types/index.ts b/src/types/index.ts index 4326fab..13e7322 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -4,4 +4,5 @@ export * from "./CfxCitizenServer" export * from "./CfxElementClub" export * from "./CfxStatus" export * from "./CfxSummary" -export * from "./CfxUnresolvedIncidents" \ No newline at end of file +export * from "./CfxUnresolvedIncidents" +export * from "./CfxPinnedServers" \ No newline at end of file diff --git a/tests/CfxPinnedServers.test.ts b/tests/CfxPinnedServers.test.ts new file mode 100644 index 0000000..571f9d6 --- /dev/null +++ b/tests/CfxPinnedServers.test.ts @@ -0,0 +1,30 @@ +import CfxPinnedServersModule from "../src/modules/CfxPinnedServers.module" +import CfxPinnedServers from "../src/models/CfxPinnedServers" +import { fetchPinnedServers } from "../src" + +describe("Tests for CfxPinnedServers module", function () { + + test("Default behaviour of the CfxPinnedServers.retrieve function", async function () { + + try { + const instance = new CfxPinnedServersModule(), + pinnedServers = await instance.retrieve() + expect(pinnedServers).toBeInstanceOf(CfxPinnedServers) + } catch (error) { + // If there is a catch, it's mostly due to Cfx.re server + // because the route won't take any argument. + expect(error).toBeInstanceOf(Error) + } + }) + + test("Default behaviour of the fetchPinnedServers function", async function () { + try { + const status = await fetchPinnedServers() + expect(status).toBeInstanceOf(CfxPinnedServers) + } catch (error) { + // If there is a catch, it's mostly due to Cfx.re server + // because the route won't take any argument. + expect(error).toBeInstanceOf(Error) + } + }) +}) \ No newline at end of file diff --git a/tests/CfxStatus.test.ts b/tests/CfxStatus.test.ts index 22c7e14..0c34631 100644 --- a/tests/CfxStatus.test.ts +++ b/tests/CfxStatus.test.ts @@ -18,8 +18,6 @@ describe("Tests for CfxStatus module", function () { }) test("Default behaviour of the fetchStatus function", async function () { - const instance = new CfxStatusModule() - try { const status = await fetchStatus() expect(status).toBeInstanceOf(CfxStatus)