Basic functionality

This commit is contained in:
2025-05-26 04:22:15 +02:00
parent 2f523a9219
commit 521e53fadd
6 changed files with 193 additions and 4 deletions

View File

@ -0,0 +1,8 @@
import {createContext} from "react";
import type IGame from "../types/IGame.ts";
export const GamesContext = createContext<{games: IGame[]}>(
{
games: []
}
);

View File

@ -0,0 +1,8 @@
import {createContext} from "react";
import type IPlayer from "../types/IPlayer.ts";
export const PlayersContext = createContext<{players: IPlayer[]}>(
{
players: []
}
);

View File

@ -70,8 +70,7 @@ function makeRequest(method: string, uri: string, content?: object | string | nu
}else
throw new Error(response.statusText);
}
let json = response.json();
return json.then((json) => json).catch(() => null);
return response.text().then(text => JSON.parseBigInt(text) as object);
})
.catch(function(err : Error){
console.error(`Error ${method}ing Data ${uri}\n${err}`);
@ -79,6 +78,39 @@ function makeRequest(method: string, uri: string, content?: object | string | nu
}).finally(() => currentlyRequestedEndpoints.splice(currentlyRequestedEndpoints.indexOf(id), 1));
}
declare global {
interface JSON {
parseBigInt: (jsonStr: string, options?: { minDigits?: number; fallbackToString?: boolean; }) => unknown;
}
}
function quoteLargeNumbers(jsonStr: string, minDigits: number): string {
const regex = new RegExp(`(-?\\d{${minDigits},})(?=\\s*[,}\\]])`, 'g');
return jsonStr.replace(regex, '"$1"');
}
JSON.parseBigInt = function (jsonStr: string, options?: { minDigits?: number; fallbackToString?: boolean;}): unknown {
const minDigits = options?.minDigits ?? 15;
const fallbackToString = options?.fallbackToString ?? true;
const safeStr = quoteLargeNumbers(jsonStr, minDigits);
return JSON.parse(safeStr, (_key, value) => {
if (typeof value === 'string') {
const bigintRegex = new RegExp(`^-?\\d{${minDigits},}$`);
if (bigintRegex.test(value)) {
try {
if (typeof BigInt !== 'undefined') return BigInt(value);
else if (fallbackToString) return value;
} catch {
return value;
}
}
}
return value;
});
};
export function isValidUri(uri: string) : boolean{
try {
new URL(uri);