mirror of
https://github.com/StepanovPlaton/torrent_frontend.git
synced 2026-04-03 20:30:48 +04:00
Work on game form
This commit is contained in:
16
src/entities/files/files.ts
Normal file
16
src/entities/files/files.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { HTTPService } from "@/shared/utils/http";
|
||||
import { coverNameSchema } from "./schemas/cover";
|
||||
|
||||
export abstract class FilesService {
|
||||
public static async UploadCover(cover: File) {
|
||||
const formData = new FormData();
|
||||
formData.append("cover", cover);
|
||||
return await HTTPService.post(
|
||||
`/files/cover`,
|
||||
coverNameSchema,
|
||||
formData,
|
||||
{},
|
||||
false
|
||||
);
|
||||
}
|
||||
}
|
||||
3
src/entities/files/index.ts
Normal file
3
src/entities/files/index.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
import { FilesService } from "./files";
|
||||
|
||||
export { FilesService };
|
||||
4
src/entities/files/schemas/cover.ts
Normal file
4
src/entities/files/schemas/cover.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
import { z } from "zod";
|
||||
|
||||
export const coverNameSchema = z.string().min(5);
|
||||
export type CoverNameType = z.infer<typeof coverNameSchema>;
|
||||
@@ -1,56 +1,56 @@
|
||||
import { z } from "zod";
|
||||
import { gameCardBaseSchema } from "./gameCard";
|
||||
|
||||
export const gameBaseSchema = gameCardBaseSchema.and(
|
||||
z.object({
|
||||
torrent_file: z.string().min(1),
|
||||
trailer: z.string().optional(),
|
||||
export const gameBaseSchema = gameCardBaseSchema.merge(
|
||||
z.object({
|
||||
torrent_file: z.string().min(1),
|
||||
trailer: z.string().optional(),
|
||||
|
||||
system: z.string().optional(),
|
||||
processor: z.string().optional(),
|
||||
memory: z.string().optional(),
|
||||
graphics: z.string().optional(),
|
||||
storage: z.string().optional(),
|
||||
system: z.string().optional(),
|
||||
processor: z.string().optional(),
|
||||
memory: z.string().optional(),
|
||||
graphics: z.string().optional(),
|
||||
storage: z.string().optional(),
|
||||
|
||||
developer: z.string().optional(),
|
||||
language: z.string().optional(),
|
||||
download_size: z.string().optional(),
|
||||
developer: z.string().optional(),
|
||||
language: z.string().optional(),
|
||||
download_size: z.string().optional(),
|
||||
|
||||
release_date: z
|
||||
.string()
|
||||
.min(1)
|
||||
.transform((d) => new Date(d)),
|
||||
})
|
||||
release_date: z
|
||||
.string()
|
||||
.min(1)
|
||||
.transform((d) => new Date(d)),
|
||||
})
|
||||
);
|
||||
|
||||
export const gameCreateSchema = gameBaseSchema.and(z.object({}));
|
||||
export const gameCreateSchema = gameBaseSchema.merge(z.object({}));
|
||||
export type GameCreateType = z.infer<typeof gameCreateSchema>;
|
||||
|
||||
export const gameSchema = gameBaseSchema.and(
|
||||
z.object({
|
||||
id: z.number().positive(),
|
||||
owner_id: z.number().positive(),
|
||||
update_date: z
|
||||
.string()
|
||||
.min(1)
|
||||
.transform((d) => new Date(d)),
|
||||
upload_date: z
|
||||
.string()
|
||||
.min(1)
|
||||
.transform((d) => new Date(d)),
|
||||
})
|
||||
export const gameSchema = gameBaseSchema.merge(
|
||||
z.object({
|
||||
id: z.number().positive(),
|
||||
owner_id: z.number().positive(),
|
||||
update_date: z
|
||||
.string()
|
||||
.min(1)
|
||||
.transform((d) => new Date(d)),
|
||||
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;
|
||||
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;
|
||||
const games: GameType[] = [];
|
||||
a.forEach((e) => {
|
||||
if (isGame(e)) games.push(gameSchema.parse(e));
|
||||
else console.error("Game parse error - ", e);
|
||||
});
|
||||
return games;
|
||||
});
|
||||
|
||||
@@ -1,40 +1,28 @@
|
||||
import { z } from "zod";
|
||||
|
||||
export const gameCardBaseSchema = z
|
||||
.object({
|
||||
title: z.string().min(3),
|
||||
cover: z.string().optional(),
|
||||
description: z.string().optional(),
|
||||
version: z.string().optional(),
|
||||
})
|
||||
.transform((card) => {
|
||||
return {
|
||||
...card,
|
||||
cover: card.cover
|
||||
? process.env.NEXT_PUBLIC_COVER_FULL_URL + "/" + card.cover
|
||||
: undefined,
|
||||
cover_preview: card.cover
|
||||
? process.env.NEXT_PUBLIC_COVER_PREVIEW_URL + "/" + card.cover
|
||||
: undefined,
|
||||
};
|
||||
});
|
||||
export const gameCardBaseSchema = z.object({
|
||||
title: z.string().min(3),
|
||||
cover: z.string().optional(),
|
||||
description: z.string().optional(),
|
||||
version: z.string().optional(),
|
||||
});
|
||||
|
||||
export const gameCardSchema = gameCardBaseSchema.and(
|
||||
z.object({
|
||||
id: z.number().positive(),
|
||||
})
|
||||
export const gameCardSchema = gameCardBaseSchema.merge(
|
||||
z.object({
|
||||
id: z.number().positive(),
|
||||
})
|
||||
);
|
||||
export type GameCardType = z.infer<typeof gameCardSchema>;
|
||||
|
||||
export const isGameCard = (a: any): a is GameCardType => {
|
||||
return gameCardSchema.safeParse(a).success;
|
||||
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;
|
||||
const cards: GameCardType[] = [];
|
||||
a.forEach((e) => {
|
||||
if (isGameCard(e)) cards.push(gameCardSchema.parse(e));
|
||||
else console.error("GameCard parse error - ", e);
|
||||
});
|
||||
return cards;
|
||||
});
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { z } from "zod";
|
||||
|
||||
export const userSchema = z.object({
|
||||
id: z.number().positive(),
|
||||
name: z.string().min(3),
|
||||
email: z.string().min(3),
|
||||
id: z.number().positive(),
|
||||
username: z.string().min(3),
|
||||
email: z.string().min(3),
|
||||
});
|
||||
export type User = z.infer<typeof userSchema>;
|
||||
|
||||
@@ -1,52 +1,53 @@
|
||||
import { HTTPService } from "@/shared/utils/http";
|
||||
import {
|
||||
LoginForm,
|
||||
TokenData,
|
||||
tokenDataSchema,
|
||||
TokenResponse,
|
||||
tokenResponseSchema,
|
||||
LoginForm,
|
||||
TokenData,
|
||||
tokenDataSchema,
|
||||
TokenResponse,
|
||||
tokenResponseSchema,
|
||||
} from "./schemas/auth";
|
||||
import { jwtDecode } from "jwt-decode";
|
||||
import Cookies from "js-cookie";
|
||||
|
||||
export abstract class UserService {
|
||||
public static async Login(loginForm: LoginForm) {
|
||||
const accessToken = await HTTPService.post(
|
||||
"/auth",
|
||||
tokenResponseSchema,
|
||||
new URLSearchParams(Object.entries(loginForm)),
|
||||
{
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
}
|
||||
);
|
||||
if (accessToken) {
|
||||
const tokenData = this.DecodeToken(accessToken);
|
||||
if (tokenData) {
|
||||
Cookies.set("access-token", accessToken, {
|
||||
secure: true,
|
||||
expires: tokenData.expire,
|
||||
});
|
||||
return tokenData;
|
||||
}
|
||||
}
|
||||
}
|
||||
public static async Login(loginForm: LoginForm) {
|
||||
const accessToken = await HTTPService.post(
|
||||
"/auth",
|
||||
tokenResponseSchema,
|
||||
new URLSearchParams(Object.entries(loginForm)),
|
||||
{
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
},
|
||||
false
|
||||
);
|
||||
if (accessToken) {
|
||||
const tokenData = this.DecodeToken(accessToken);
|
||||
if (tokenData) {
|
||||
Cookies.set("access-token", accessToken, {
|
||||
secure: true,
|
||||
expires: tokenData.expire,
|
||||
});
|
||||
return tokenData;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static GetToken(): string | undefined {
|
||||
return Cookies.get("access-token");
|
||||
}
|
||||
public static GetToken(): string | undefined {
|
||||
return Cookies.get("access-token");
|
||||
}
|
||||
|
||||
public static IdentifyYourself(): TokenData | undefined {
|
||||
const token = Cookies.get("access-token");
|
||||
if (token) {
|
||||
return this.DecodeToken(token);
|
||||
}
|
||||
}
|
||||
public static IdentifyYourself(): TokenData | undefined {
|
||||
const token = Cookies.get("access-token");
|
||||
if (token) {
|
||||
return this.DecodeToken(token);
|
||||
}
|
||||
}
|
||||
|
||||
public static DecodeToken(token: string): TokenData | undefined {
|
||||
const tokenPayload = jwtDecode(token);
|
||||
const parseResult = tokenDataSchema.safeParse(tokenPayload);
|
||||
if (parseResult.success) {
|
||||
return parseResult.data;
|
||||
} else console.error("JWT payload broken - " + parseResult.error);
|
||||
}
|
||||
public static DecodeToken(token: string): TokenData | undefined {
|
||||
const tokenPayload = jwtDecode(token);
|
||||
const parseResult = tokenDataSchema.safeParse(tokenPayload);
|
||||
if (parseResult.success) {
|
||||
return parseResult.data;
|
||||
} else console.error("JWT payload broken - " + parseResult.error);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user