Use of Server Components for easier Data fetching + start use of v2 API
This commit is contained in:
parent
49ceda5575
commit
f23c9dc0f1
6 changed files with 195 additions and 5 deletions
components/server
27
components/server/fetchers.ts
Normal file
27
components/server/fetchers.ts
Normal file
|
@ -0,0 +1,27 @@
|
|||
import { Game } from "@/types/stellaris"
|
||||
|
||||
type Parameter = {
|
||||
key: string,
|
||||
val: string | number
|
||||
}
|
||||
|
||||
const baseUrl = 'http://stellaris.neshweb.net/api/v2'
|
||||
|
||||
export const generateUrl = (url: string, params?: Parameter[]) => {
|
||||
let query: string = baseUrl + url;
|
||||
if (params && params.length > 0) {
|
||||
query = query + '?';
|
||||
params.forEach((param, idx) => {
|
||||
query = query + param.key + '=' + param.val;
|
||||
if (idx + 1 != params.length) {
|
||||
query = query + '&';
|
||||
}
|
||||
});
|
||||
}
|
||||
return query;
|
||||
}
|
||||
|
||||
export const fetchGameGroups = async (game: Game) => {
|
||||
const gameGroups = await fetch(generateUrl('/game_groups', [{key: "game_id", val: game.id}])).then((res) => res.json())
|
||||
return gameGroups
|
||||
}
|
Reference in a new issue