Skip to content

Commit

Permalink
Merge pull request #70 from cohstats/improve-type-savety
Browse files Browse the repository at this point in the history
Add next js types to api handlers and fix all resulting type errors
  • Loading branch information
petrvecera committed Mar 4, 2023
2 parents 9599056 + cb7a507 commit 2efcf69
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 24 deletions.
3 changes: 2 additions & 1 deletion pages/api/appUpdateRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
* This route is used be the desktop app to determine if the auto updater should update the app
*/

import { NextApiRequest, NextApiResponse } from "next";
import { Octokit } from "octokit";

export default async function handler(req: any, res: any) {
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
const octokit = new Octokit();
const response = await octokit.request("GET /repos/{owner}/{repo}/releases/latest", {
owner: "cohstats",
Expand Down
5 changes: 3 additions & 2 deletions pages/api/onlineSteamPlayers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@

import { getNumberOfOnlinePlayersSteamUrl } from "../../src/steam-api";
import { logger } from "../../src/logger";
import { NextApiRequest, NextApiResponse } from "next";

export default async function handler(req: any, res: any) {
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
try {
const fetchResponse = await fetch(getNumberOfOnlinePlayersSteamUrl());
const { response } = await fetchResponse.json();
Expand All @@ -17,6 +18,6 @@ export default async function handler(req: any, res: any) {
.json({ playerCount: response.player_count, timeStampMs: new Date().valueOf() });
} catch (e) {
logger.error(e);
res.status(500).json();
res.status(500).json({});
}
}
23 changes: 17 additions & 6 deletions pages/api/playerExport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { getPlayerCardInfoUrl } from "../../src/coh3stats-api";
import { processPlayerInfoAPIResponse } from "../../src/players/standings";
import { leaderBoardType, PlayerCardDataType, raceType } from "../../src/coh3/coh3-types";
import { json2csvAsync } from "json-2-csv";
import { NextApiRequest, NextApiResponse } from "next";

const getPlayerCardInfo = async (profileID: string) => {
const PlayerCardRes = await fetch(getPlayerCardInfoUrl(profileID));
Expand Down Expand Up @@ -62,23 +63,33 @@ const generateCSVObject = (
};
};

export default async function handler(req: any, res: any) {
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
try {
const query = req.query;
const { profileIDs } = query;
const { types } = query;
const { profileIDs, types } = query;

if (!profileIDs) {
throw new Error("Invalid params");
return res.status(400).json({ error: "profile id param is missing" });
}

if (typeof profileIDs !== "string") {
return res.status(400).json({ error: "profile id contains invalid params" });
}
let parsedTypes;

if (types !== undefined && typeof types !== "string") {
return res.status(400).json({ error: "profile id contains invalid params" });
}
if (types !== undefined) {
parsedTypes = JSON.parse(types);
}

const arrayOfIds = JSON.parse(profileIDs);
logger.log(`Going to parse ${arrayOfIds.length} ids`);
logger.log(`List of IDs ${arrayOfIds}`);
if (arrayOfIds.length > 100) {
throw new Error("Too much records");
return res.status(500).json({ error: "Too many records requested" });
}
const parsedTypes = JSON.parse(types || null);

const finalArray = [];

Expand Down
18 changes: 13 additions & 5 deletions pages/leaderboards.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import Head from "next/head";
import { localizedGameTypes, localizedNames } from "../src/coh3/coh3-data";
import { raceType, leaderBoardType } from "../src/coh3/coh3-types";
import FactionIcon from "../components/faction-icon";
import { GetServerSideProps } from "next";

/**
* Timeago is causing issues with SSR, move to clinet side
Expand Down Expand Up @@ -229,13 +230,20 @@ const sortById = {
elo: 1,
};

export async function getServerSideProps({ query }: any) {
export const getServerSideProps: GetServerSideProps = async ({ query }) => {
const { race, type, sortBy, start } = query;

const raceToFetch = race || "american";
const typeToFetch = type || "1v1";
const raceToFetch = (race as raceType) || "american";
const typeToFetch = (type as leaderBoardType) || "1v1";
const sortByToFetch = sortById[sortBy as "wins" | "elo"] || 1;
const startToFetch = start || 1;
let startNumber: number | undefined;
if (start) {
const number = Number(start);
if (!isNaN(number)) {
startNumber = number;
}
}
const startToFetch = startNumber || 1;

let leaderBoardData = null;
let error = null;
Expand Down Expand Up @@ -267,6 +275,6 @@ export async function getServerSideProps({ query }: any) {
typeToFetch,
}, // will be passed to the page component as props
};
}
};

export default Leaderboards;
12 changes: 8 additions & 4 deletions pages/players/[playerID].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import Head from "next/head";
import React from "react";
import { PlayerCardDataType } from "../../src/coh3/coh3-types";
import { getPlayerCardInfoUrl } from "../../src/coh3stats-api";
import { GetServerSideProps } from "next";

/**
*
Expand Down Expand Up @@ -142,9 +143,12 @@ const PlayerCard = ({
// THIS code is super dirty to get the things done, needs to be fixed
// I am not sure if it's a good idea to have it as query parameter
// I am counting with the old url setup as we had on coh2stats https://coh2stats.com/players/76561198051168720-F3riG?view=recentMatches
// @ts-ignore
export async function getServerSideProps({ params, query }) {
const { playerID } = params;
export const getServerSideProps: GetServerSideProps<any, { playerID: string }> = async ({
params,
query,
res,
}) => {
const { playerID } = params!;
const { view } = query;

let playerData = null;
Expand Down Expand Up @@ -198,6 +202,6 @@ export async function getServerSideProps({ params, query }) {
return {
props: { playerID, playerData, error, playerMatchesData }, // will be passed to the page component as props
};
}
};

export default PlayerCard;
11 changes: 5 additions & 6 deletions pages/search.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { GetServerSideProps, NextPage } from "next";
import config from "../config";

/**
Expand All @@ -11,8 +12,7 @@ import config from "../config";
* @constructor
*/

// @ts-ignore
function Search({ searchQuery, data, error }) {
const Search: NextPage<any> = ({ searchQuery, data, error }) => {
// Render data...

return (
Expand All @@ -22,10 +22,9 @@ function Search({ searchQuery, data, error }) {
<div>error {JSON.stringify(error)}</div>
</>
);
}
};

// @ts-ignore
export async function getServerSideProps({ query }) {
export const getServerSideProps: GetServerSideProps = async ({ query }) => {
const { q } = query;

console.log("SEARCH", q);
Expand All @@ -49,6 +48,6 @@ export async function getServerSideProps({ query }) {
return {
props: { searchQuery: q, data, error }, // will be passed to the page component as props
};
}
};

export default Search;

0 comments on commit 2efcf69

Please sign in to comment.