Skip to content

Commit

Permalink
Merge pull request #206 from Bux42/fix-calls-to-tm-io
Browse files Browse the repository at this point in the history
Fix calls to trackmania.io API
  • Loading branch information
Bux42 authored Jul 22, 2024
2 parents e8caeb3 + 3fb1b83 commit 5a2b90f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
4 changes: 1 addition & 3 deletions app/pages/maps/[mapUId]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ const Home = (): JSX.Element => {
isFetching: isFetchingReplays,
} = useMapReplays(mapUId);

const { user } = useContext(AuthContext);
const mapInfoQueryKey = user ? mapUId : undefined;
const { data: mapInfo } = useMapInfo(mapInfoQueryKey);
const { data: mapInfo } = useMapInfo(mapUId);

const selectedReplaysWithValidSectors = useMemo(
() => filterReplaysWithValidSectorTimes(selectedReplayData, mapReplaysResult?.replays || []),
Expand Down
28 changes: 16 additions & 12 deletions server/src/routes/maps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,24 @@ router.get('/:mapUID', async (req: Request, res: Response, next: Function) => {
router.get('/:mapUID/info', async (req: Request, res: Response) => {
let mapData = {};

// fetch tm.io data
try {
const tmxRes = await axios.get(`https://trackmania.io/api/map/${req.params.mapUID}`, {
withCredentials: true,
headers: { 'User-Agent': 'TMDojo API - https://github.com/Bux42/TMDojo' },
});
if (!req.user) {
// Only call tm.io if user is authenticated
res.status(401).send();
} else {
// fetch tm.io data
try {
const tmxRes = await axios.get(`https://trackmania.io/api/map/${req.params.mapUID}`, {
withCredentials: true,
headers: { 'User-Agent': 'TMDojo API - https://github.com/Bux42/TMDojo' },
});

const tmioData = tmxRes.data;
mapData = { ...mapData, ...tmioData };
} catch (error) {
req.log.error(`mapsRouter: tm.io request failed with error ${error.toString()}`);
const tmioData = tmxRes.data;
mapData = { ...mapData, ...tmioData };
} catch (error) {
req.log.error(`mapsRouter: tm.io request failed with error ${error.toString()}`);
}
res.send(mapData);
}

res.send(mapData);
});

/**
Expand Down

0 comments on commit 5a2b90f

Please sign in to comment.