Basic functionality
This commit is contained in:
8
src/api/contexts/GamesContext.tsx
Normal file
8
src/api/contexts/GamesContext.tsx
Normal file
@ -0,0 +1,8 @@
|
||||
import {createContext} from "react";
|
||||
import type IGame from "../types/IGame.ts";
|
||||
|
||||
export const GamesContext = createContext<{games: IGame[]}>(
|
||||
{
|
||||
games: []
|
||||
}
|
||||
);
|
8
src/api/contexts/PlayersContext.tsx
Normal file
8
src/api/contexts/PlayersContext.tsx
Normal file
@ -0,0 +1,8 @@
|
||||
import {createContext} from "react";
|
||||
import type IPlayer from "../types/IPlayer.ts";
|
||||
|
||||
export const PlayersContext = createContext<{players: IPlayer[]}>(
|
||||
{
|
||||
players: []
|
||||
}
|
||||
);
|
@ -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);
|
||||
|
Reference in New Issue
Block a user