This commit is contained in:
2024-07-09 12:10:18 +04:00
parent e42777db8d
commit 1577eabcde
55 changed files with 1779 additions and 139 deletions

View File

@@ -0,0 +1,19 @@
import {
combinationSchema,
combinationsSchema,
pageOfCombinationsSchema,
thisItemIsCombination,
type CombinationType,
type PageOfCombinationsType,
} from "./schema";
export {
combinationSchema,
combinationsSchema,
pageOfCombinationsSchema,
thisItemIsCombination,
type CombinationType,
type PageOfCombinationsType,
};
import { CombinationsService } from "./service";
export { CombinationsService };

View File

@@ -0,0 +1,42 @@
import { z } from "zod";
import { ItemType, TypesOfItems } from "../types";
export const combinationSchema = z.object({
combinationId: z.number(),
name: z.string(),
tag: z.array(z.string()),
// Показывает, что этот item - combination
type: z
.any()
.optional()
.transform(() => TypesOfItems.combination),
});
export type CombinationType = z.infer<typeof combinationSchema>;
export const isCombination = (a: any): a is CombinationType => {
return combinationSchema.safeParse(a).success;
};
export const combinationsSchema = z.array(z.any()).transform((a) => {
const combinations: CombinationType[] = [];
a.forEach((e) => {
if (isCombination(e)) combinations.push(combinationSchema.parse(e));
else console.error("Combination parse error - ", e);
});
return combinations;
});
export const pageOfCombinationsSchema = z.object({
totalCount: z.number(),
pageSize: z.number(),
currentPage: z.number(),
totalPages: z.number(),
items: combinationsSchema,
});
export type PageOfCombinationsType = z.infer<typeof pageOfCombinationsSchema>;
export const thisItemIsCombination = (i: ItemType): i is CombinationType => {
return (i as CombinationType).type === TypesOfItems.combination;
};

View File

@@ -0,0 +1,29 @@
import { HTTPService } from "@/shared/utils/http";
import { IItemService, staticImplements } from "../types";
import { combinationSchema, pageOfCombinationsSchema } from "./schema";
@staticImplements<IItemService>()
export abstract class CombinationsService {
public static urlPrefix = "combinations";
public static cacheOptions = {
next: {
revalidate: 60 * 5,
},
};
public static async Get(id: number) {
return await HTTPService.get(
`/${this.urlPrefix}/${id}`,
combinationSchema,
this.cacheOptions
);
}
public static async GetPage(page: number, pageSize?: number) {
return await HTTPService.get(
`/${this.urlPrefix}?pageIndex=${page}&pageSize=${pageSize ?? 2 * 3 * 3}`,
pageOfCombinationsSchema,
this.cacheOptions
);
}
}