Skip to content

Commit

Permalink
feat: adding the pinned servers data.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ximaz committed Oct 17, 2023
1 parent cf3c3d9 commit 93c214d
Show file tree
Hide file tree
Showing 20 changed files with 173 additions and 5 deletions.
1 change: 1 addition & 0 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from "./types";
export declare function fetchServer(id: string): Promise<import("./models/CfxCitizenServer").default | undefined>;
export declare function fetchStatus(): Promise<import("./models/CfxStatus").default | undefined>;
export declare function fetchUnresolvedIncidents(): Promise<import("./models/CfxUnresolvedIncidents").default | undefined>;
export declare function fetchPinnedServers(): Promise<import("./models/CfxPinnedServers").default | undefined>;
7 changes: 6 additions & 1 deletion dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
11 changes: 11 additions & 0 deletions dist/models/CfxPinnedServers.d.ts
Original file line number Diff line number Diff line change
@@ -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);
}
15 changes: 15 additions & 0 deletions dist/models/CfxPinnedServers.js
Original file line number Diff line number Diff line change
@@ -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;
6 changes: 6 additions & 0 deletions dist/modules/CfxPinnedServers.module.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import CfxPinnedServers from "../models/CfxPinnedServers";
export default class CfxPinnedServersModule {
static instance: CfxPinnedServersModule | null;
static get(): CfxPinnedServersModule;
retrieve(): Promise<CfxPinnedServers | undefined>;
}
28 changes: 28 additions & 0 deletions dist/modules/CfxPinnedServers.module.js
Original file line number Diff line number Diff line change
@@ -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;
1 change: 1 addition & 0 deletions dist/types/CfxApiRoutes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
3 changes: 2 additions & 1 deletion dist/types/CfxApiRoutes.js
Original file line number Diff line number Diff line change
@@ -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";
9 changes: 9 additions & 0 deletions dist/types/CfxPinnedServers.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export interface CfxPinnedServersResponse {
pinIfEmpty: boolean;
pinnedServers: string[];
noAdServerId: string;
noAdServer: {
title: string;
ids: string[];
};
}
2 changes: 2 additions & 0 deletions dist/types/CfxPinnedServers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
1 change: 1 addition & 0 deletions dist/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * from "./CfxElementClub";
export * from "./CfxStatus";
export * from "./CfxSummary";
export * from "./CfxUnresolvedIncidents";
export * from "./CfxPinnedServers";
1 change: 1 addition & 0 deletions dist/types/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ __exportStar(require("./CfxElementClub"), exports);
__exportStar(require("./CfxStatus"), exports);
__exportStar(require("./CfxSummary"), exports);
__exportStar(require("./CfxUnresolvedIncidents"), exports);
__exportStar(require("./CfxPinnedServers"), exports);
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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()
}
18 changes: 18 additions & 0 deletions src/models/CfxPinnedServers.ts
Original file line number Diff line number Diff line change
@@ -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
}
}
24 changes: 24 additions & 0 deletions src/modules/CfxPinnedServers.module.ts
Original file line number Diff line number Diff line change
@@ -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<CfxPinnedServers | undefined> {
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")
}
}
}
2 changes: 2 additions & 0 deletions src/types/CfxApiRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
9 changes: 9 additions & 0 deletions src/types/CfxPinnedServers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export interface CfxPinnedServersResponse {
pinIfEmpty: boolean
pinnedServers: string[]
noAdServerId: string
noAdServer: {
title: string,
ids: string[]
}
}
3 changes: 2 additions & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export * from "./CfxCitizenServer"
export * from "./CfxElementClub"
export * from "./CfxStatus"
export * from "./CfxSummary"
export * from "./CfxUnresolvedIncidents"
export * from "./CfxUnresolvedIncidents"
export * from "./CfxPinnedServers"
30 changes: 30 additions & 0 deletions tests/CfxPinnedServers.test.ts
Original file line number Diff line number Diff line change
@@ -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)
}
})
})
2 changes: 0 additions & 2 deletions tests/CfxStatus.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 93c214d

Please sign in to comment.