Add game cards and dev reverse proxy

This commit is contained in:
2024-05-11 13:50:52 +04:00
parent 4f8301d1e8
commit a2ea5fc409
13 changed files with 202 additions and 12 deletions

11
src/entities/game/game.ts Normal file
View File

@@ -0,0 +1,11 @@
import { HTTPService } from "@/shared/http/httpService";
import { gameCardsSchema, GameCardType } from "./schemas/gameCard";
export abstract class GameService {
public static async getGameCards() {
return await HTTPService.get<GameCardType[]>(
"/games/cards",
gameCardsSchema
);
}
}

View File

@@ -0,0 +1,5 @@
import { GameCardType } from "./schemas/gameCard";
import { GameType } from "./schemas/game";
import { GameService } from "./game";
export { GameService, type GameType, type GameCardType };

View File

@@ -0,0 +1,35 @@
import { z } from "zod";
import { gameCardSchema } from "./gameCard";
export const gameSchema = z.union([
gameCardSchema,
z.object({
torrent_file: z.string().min(1),
language: z.string().optional(),
version: z.string().optional(),
download_size: z.string().optional(),
system: z.string().optional(),
processor: z.string().optional(),
memory: z.string().optional(),
graphics: z.string().optional(),
storage: z.string().optional(),
upload_date: z
.string()
.min(1)
.transform((d) => new Date(d)),
}),
]);
export type GameType = z.infer<typeof gameSchema>;
export const isGame = (a: any): a is GameType => {
return gameSchema.safeParse(a).success;
};
export const gamesSchema = z.array(z.any()).transform((a) => {
const games: GameType[] = [];
a.forEach((e) => {
if (isGame(e)) games.push(gameSchema.parse(e));
else console.error("Game parse error - ", e);
});
return games;
});

View File

@@ -0,0 +1,31 @@
import { z } from "zod";
export const gameCardSchema = z.object({
id: z.number(),
title: z.string().min(3),
cover: z
.string()
.optional()
.transform((u) => {
if (!!u) return process.env.NEXT_PUBLIC_COVER_FULL_URL + "/" + u;
}),
description: z.string().optional(),
release_date: z
.string()
.min(1)
.transform((d) => new Date(d)),
});
export type GameCardType = z.infer<typeof gameCardSchema>;
export const isGameCard = (a: any): a is GameCardType => {
return gameCardSchema.safeParse(a).success;
};
export const gameCardsSchema = z.array(z.any()).transform((a) => {
const cards: GameCardType[] = [];
a.forEach((e) => {
if (isGameCard(e)) cards.push(gameCardSchema.parse(e));
else console.error("GameCard parse error - ", e);
});
return cards;
});